[jboss-svn-commits] JBL Code SVN: r21563 - in labs/jbossrules/trunk: drools-decisiontables/src/main/java/org/drools/decisiontable and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Aug 15 03:54:36 EDT 2008
Author: Rikkola
Date: 2008-08-15 03:54:36 -0400 (Fri, 15 Aug 2008)
New Revision: 21563
Added:
labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/SourcePackageProvider.java
labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/SourcePackageProviderTest.java
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/FileScanner.java
Log:
JBRULES-1680: Support XLS source type in RuleAgent
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/FileScanner.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/FileScanner.java 2008-08-15 07:39:04 UTC (rev 21562)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/FileScanner.java 2008-08-15 07:54:36 UTC (rev 21563)
@@ -18,11 +18,10 @@
*
*/
public class FileScanner extends PackageProvider {
-
- File[] files;
- Map lastUpdated = new HashMap();
- Map<String,String> pathToPackage = null;
+ File[] files;
+ Map lastUpdated = new HashMap();
+ Map<String, String> pathToPackage = null;
/**
* This sets the list of files to be monitored.
@@ -53,38 +52,36 @@
* just do nothing (as there may be a temporary IO issue).
*/
PackageChangeInfo loadPackageChanges() {
- PackageChangeInfo changes = getChangeSet();
+ PackageChangeInfo changes = getChangeSet();
return changes;
}
-
-
-
/**
* Calculate a change set, based on last updated times.
* (keep a map of files).
*/
private PackageChangeInfo getChangeSet() {
- PackageChangeInfo info = new PackageChangeInfo();
+ PackageChangeInfo info = new PackageChangeInfo();
if ( this.files == null ) return info;
-
- if( pathToPackage == null ) pathToPackage = new HashMap<String,String>();
-
+
+ if ( pathToPackage == null ) pathToPackage = new HashMap<String, String>();
+
for ( int i = 0; i < files.length; i++ ) {
File f = files[i];
-
- if( !f.exists() ){
- String name = pathToPackage.get(f.getPath());
- if( name != null ){
- info.addRemovedPackage( name );
- }
+
+ if ( !f.exists() ) {
+ String name = pathToPackage.get( f.getPath() );
+ if ( name != null ) {
+ info.addRemovedPackage( name );
+ }
} else if ( hasChanged( f.getPath(),
- this.lastUpdated,
- f.lastModified() ) ) {
+ this.lastUpdated,
+ f.lastModified() ) ) {
Package p = readPackage( f );
if ( p != null ) {
- info.addPackage( p );
- pathToPackage.put(f.getPath(), p.getName());
+ info.addPackage( p );
+ pathToPackage.put( f.getPath(),
+ p.getName() );
}
}
}
@@ -97,36 +94,46 @@
*/
private Package readPackage(File pkgFile) {
- String name = pkgFile.getName();
- if (!(name.endsWith(".pkg") || name.endsWith(".drl"))) {
- return null;
- }
- //use reflection to load if its DRL, the provider lives in drools compiler.
- if (pkgFile.getName().endsWith(".drl")) {
- try {
- FileLoader fl = (FileLoader) Class.forName("org.drools.compiler.SourcePackageProvider").newInstance();
- return fl.loadPackage(pkgFile);
- } catch (Exception e) {
- this.listener.exception(e);
- return null;
- }
+ String name = pkgFile.getName();
+ if ( !(name.endsWith( ".pkg" ) || name.endsWith( ".drl" ) || name.endsWith( ".xls" )) ) {
+ return null;
+ }
+ //use reflection to load if its DRL, the provider lives in drools compiler.
+ if ( pkgFile.getName().endsWith( ".drl" ) ) {
+ try {
+ FileLoader fl = (FileLoader) Class.forName( "org.drools.compiler.SourcePackageProvider" ).newInstance();
+ return fl.loadPackage( pkgFile );
+ } catch ( Exception e ) {
+ this.listener.exception( e );
+ return null;
+ }
- } else {
+ //use reflection to load if its XLS, the provider lives in drools decision tables.
+ } else if ( pkgFile.getName().endsWith( ".xls" ) ) {
+ try {
+ FileLoader fl = (FileLoader) Class.forName( "org.drools.decisiontable.SourcePackageProvider" ).newInstance();
+ return fl.loadPackage( pkgFile );
+ } catch ( Exception e ) {
+ this.listener.exception( e );
+ return null;
+ }
- Package p1_ = null;
- try {
- p1_ = (Package) DroolsStreamUtils.streamIn( new FileInputStream( pkgFile ) );
- } catch ( FileNotFoundException e ) {
- this.listener.exception( e );
- this.listener.warning( "Was unable to find the file " + pkgFile.getPath() );
- } catch ( IOException e ) {
- this.listener.exception( e );
- } catch ( ClassNotFoundException e ) {
- this.listener.exception( e );
- this.listener.warning( "Was unable to load a class when loading a package. Perhaps it is missing from this application." );
- }
- return p1_;
- }
+ } else {
+
+ Package p1_ = null;
+ try {
+ p1_ = (Package) DroolsStreamUtils.streamIn( new FileInputStream( pkgFile ) );
+ } catch ( FileNotFoundException e ) {
+ this.listener.exception( e );
+ this.listener.warning( "Was unable to find the file " + pkgFile.getPath() );
+ } catch ( IOException e ) {
+ this.listener.exception( e );
+ } catch ( ClassNotFoundException e ) {
+ this.listener.exception( e );
+ this.listener.warning( "Was unable to load a class when loading a package. Perhaps it is missing from this application." );
+ }
+ return p1_;
+ }
}
boolean hasChanged(String path,
Added: labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/SourcePackageProvider.java
===================================================================
--- labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/SourcePackageProvider.java (rev 0)
+++ labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/SourcePackageProvider.java 2008-08-15 07:54:36 UTC (rev 21563)
@@ -0,0 +1,46 @@
+package org.drools.decisiontable;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.StringReader;
+
+import org.drools.RuntimeDroolsException;
+import org.drools.agent.FileLoader;
+import org.drools.compiler.DroolsParserException;
+import org.drools.compiler.PackageBuilder;
+import org.drools.rule.Package;
+
+/**
+ * This is used by the agent when a source file is encountered.
+ *
+ * @author Toni Rikkola
+ *
+ */
+public class SourcePackageProvider
+ implements
+ FileLoader {
+
+ public Package loadPackage(File rm) throws IOException {
+ final FileInputStream fin = new FileInputStream( rm );
+
+ final SpreadsheetCompiler converter = new SpreadsheetCompiler();
+ String drl = converter.compile( fin,
+ InputType.XLS );
+
+ PackageBuilder b = new PackageBuilder();
+ try {
+ b.addPackageFromDrl( new StringReader( drl ) );
+ if ( b.hasErrors() ) {
+ throw new RuntimeDroolsException( "Error building rules from source: " + b.getErrors() );
+ } else {
+ return b.getPackage();
+ }
+ } catch ( DroolsParserException e ) {
+ throw new RuntimeException( e );
+ }
+
+ }
+
+}
Added: labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/SourcePackageProviderTest.java
===================================================================
--- labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/SourcePackageProviderTest.java (rev 0)
+++ labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/SourcePackageProviderTest.java 2008-08-15 07:54:36 UTC (rev 21563)
@@ -0,0 +1,88 @@
+package org.drools.decisiontable;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+import org.drools.RuleBase;
+import org.drools.agent.RuleAgent;
+
+public class SourcePackageProviderTest extends TestCase {
+
+ public void testSourceProvider() throws Exception {
+ new SourcePackageProvider();
+
+ File dir = getTempDirectory();
+
+ InputStream in = this.getClass().getResourceAsStream( "/data/ExamplePolicyPricing.xls" );
+
+ File target = new File( dir,
+ "Something.xls" );
+
+ OutputStream out = new FileOutputStream( target );
+
+ byte[] buf = new byte[1024];
+ int len;
+ while ( (len = in.read( buf )) > 0 ) {
+ out.write( buf,
+ 0,
+ len );
+ }
+ in.close();
+ out.close();
+
+ Properties config = new Properties();
+ config.setProperty( RuleAgent.FILES,
+ target.getPath() );
+
+ RuleAgent ag = RuleAgent.newRuleAgent( config );
+
+ assertNotNull( ag );
+
+ RuleBase rb = ag.getRuleBase();
+ assertNotNull( rb );
+ }
+
+ public static File getTempDirectory() {
+ File f = tempDir();
+ if ( f.exists() ) {
+ if ( f.isFile() ) {
+ throw new IllegalStateException( "The temp directory exists as a file. Nuke it now !" );
+ }
+ deleteDir( f );
+ f.mkdir();
+ } else {
+ f.mkdir();
+ }
+ return f;
+ }
+
+ private static File tempDir() {
+ File tmp = new File( System.getProperty( "java.io.tmpdir" ) );
+
+ return new File( tmp,
+ "__temp_test_drools_packages" );
+ }
+
+ public static boolean deleteDir(File dir) {
+
+ if ( dir.isDirectory() ) {
+ String[] children = dir.list();
+ for ( int i = 0; i < children.length; i++ ) {
+ boolean success = deleteDir( new File( dir,
+ children[i] ) );
+ if ( !success ) {
+ //throw new RuntimeException("Unable to delete !");
+ return false;
+ }
+ }
+ }
+
+ // The directory is now empty so delete it
+ return dir.delete();
+ }
+}
More information about the jboss-svn-commits
mailing list