[jboss-svn-commits] JBL Code SVN: r23897 - in labs/jbossrules/trunk/drools-decisiontables/src: main/java/org/drools/decisiontable/parser/xls and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Nov 17 16:11:56 EST 2008
Author: Rikkola
Date: 2008-11-17 16:11:56 -0500 (Mon, 17 Nov 2008)
New Revision: 23897
Modified:
labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/DecisionTableProviderImpl.java
labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/parser/xls/ExcelParser.java
labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/acme/insurance/launcher/PricingRuleLauncher.java
labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/SpreadsheetIntegrationTest.java
Log:
JBRULES-1734: Drools API
-Fixed broken decision table unit tests. They worked in Windows, but not in Linux, reason was that they used different default charsets. The charset can now be set.
Modified: labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/DecisionTableProviderImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/DecisionTableProviderImpl.java 2008-11-17 18:35:05 UTC (rev 23896)
+++ labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/DecisionTableProviderImpl.java 2008-11-17 21:11:56 UTC (rev 23897)
@@ -2,34 +2,46 @@
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.Reader;
import org.drools.builder.DecisionTableConfiguration;
import org.drools.compiler.DecisionTableProvider;
import org.drools.util.StringUtils;
-public class DecisionTableProviderImpl implements DecisionTableProvider {
+public class DecisionTableProviderImpl
+ implements
+ DecisionTableProvider {
public String loadFromReader(Reader reader,
DecisionTableConfiguration configuration) {
+ InputStream is;
+ if ( reader instanceof InputStreamReader ) {
+ is = new ReaderInputStream( reader,
+ ((InputStreamReader) reader).getEncoding() );
+ } else {
+ is = new ReaderInputStream( reader );
+ }
+
SpreadsheetCompiler compiler = new SpreadsheetCompiler();
- InputStream is = new ReaderInputStream( reader );
switch ( configuration.getInputType() ) {
case XLS : {
if ( StringUtils.isEmpty( configuration.getWorksheetName() ) ) {
- return compiler.compile( is, InputType.XLS );
+ return compiler.compile( is,
+ InputType.XLS );
} else {
- return compiler.compile( is, configuration.getWorksheetName() );
+ return compiler.compile( is,
+ configuration.getWorksheetName() );
}
}
case CSV : {
- return compiler.compile( is, InputType.CSV );
+ return compiler.compile( is,
+ InputType.CSV );
}
}
-
+
return null;
}
-
/**
* Adapts a <code>Reader</code> as an <code>InputStream</code>.
@@ -41,11 +53,11 @@
/** Source Reader */
private Reader in;
- private String encoding = System.getProperty("file.encoding");
+ private String encoding = System.getProperty( "file.encoding" );
private byte[] slack;
- private int begin;
+ private int begin;
/**
* Construct a <CODE>ReaderInputStream</CODE>
@@ -65,10 +77,11 @@
* @param reader non-null <CODE>Reader</CODE>.
* @param encoding non-null <CODE>String</CODE> encoding.
*/
- public ReaderInputStream(Reader reader, String encoding) {
- this(reader);
- if (encoding == null) {
- throw new IllegalArgumentException("encoding must not be null");
+ public ReaderInputStream(Reader reader,
+ String encoding) {
+ this( reader );
+ if ( encoding == null ) {
+ throw new IllegalArgumentException( "encoding must not be null" );
} else {
this.encoding = encoding;
}
@@ -82,25 +95,27 @@
* @exception IOException if the original <code>Reader</code> fails to be read
*/
public synchronized int read() throws IOException {
- if (in == null) {
- throw new IOException("Stream Closed");
+ if ( in == null ) {
+ throw new IOException( "Stream Closed" );
}
byte result;
- if (slack != null && begin < slack.length) {
+ if ( slack != null && begin < slack.length ) {
result = slack[begin];
- if (++begin == slack.length) {
+ if ( ++begin == slack.length ) {
slack = null;
}
} else {
byte[] buf = new byte[1];
- if (read(buf, 0, 1) <= 0) {
+ if ( read( buf,
+ 0,
+ 1 ) <= 0 ) {
result = -1;
}
result = buf[0];
}
- if (result < -1) {
+ if ( result < -1 ) {
result += 256;
}
@@ -117,31 +132,38 @@
* the end of the stream
* @exception IOException if an error occurs
*/
- public synchronized int read(byte[] b, int off, int len)
- throws IOException {
- if (in == null) {
- throw new IOException("Stream Closed");
+ public synchronized int read(byte[] b,
+ int off,
+ int len) throws IOException {
+ if ( in == null ) {
+ throw new IOException( "Stream Closed" );
}
- while (slack == null) {
+ while ( slack == null ) {
char[] buf = new char[len]; // might read too much
- int n = in.read(buf);
- if (n == -1) {
+ int n = in.read( buf );
+ if ( n == -1 ) {
return -1;
}
- if (n > 0) {
- slack = new String(buf, 0, n).getBytes(encoding);
+ if ( n > 0 ) {
+ slack = new String( buf,
+ 0,
+ n ).getBytes( encoding );
begin = 0;
}
}
- if (len > slack.length - begin) {
+ if ( len > slack.length - begin ) {
len = slack.length - begin;
}
- System.arraycopy(slack, begin, b, off, len);
+ System.arraycopy( slack,
+ begin,
+ b,
+ off,
+ len );
- if ((begin += len) >= slack.length) {
+ if ( (begin += len) >= slack.length ) {
slack = null;
}
@@ -156,25 +178,24 @@
*/
public synchronized void mark(final int limit) {
try {
- in.mark(limit);
- } catch (IOException ioe) {
- throw new RuntimeException(ioe.getMessage());
+ in.mark( limit );
+ } catch ( IOException ioe ) {
+ throw new RuntimeException( ioe.getMessage() );
}
}
-
/**
* @return the current number of bytes ready for reading
* @exception IOException if an error occurs
*/
public synchronized int available() throws IOException {
- if (in == null) {
- throw new IOException("Stream Closed");
+ if ( in == null ) {
+ throw new IOException( "Stream Closed" );
}
- if (slack != null) {
+ if ( slack != null ) {
return slack.length - begin;
}
- if (in.ready()) {
+ if ( in.ready() ) {
return 1;
} else {
return 0;
@@ -184,8 +205,8 @@
/**
* @return false - mark is not supported
*/
- public boolean markSupported () {
- return false; // would be imprecise
+ public boolean markSupported() {
+ return false; // would be imprecise
}
/**
@@ -194,8 +215,8 @@
* @exception IOException if the StringReader fails to be reset
*/
public synchronized void reset() throws IOException {
- if (in == null) {
- throw new IOException("Stream Closed");
+ if ( in == null ) {
+ throw new IOException( "Stream Closed" );
}
slack = null;
in.reset();
@@ -207,13 +228,12 @@
* @exception IOException if the original StringReader fails to be closed
*/
public synchronized void close() throws IOException {
- if (in != null) {
+ if ( in != null ) {
in.close();
slack = null;
in = null;
}
}
}
-
}
Modified: labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/parser/xls/ExcelParser.java
===================================================================
--- labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/parser/xls/ExcelParser.java 2008-11-17 18:35:05 UTC (rev 23896)
+++ labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/parser/xls/ExcelParser.java 2008-11-17 21:11:56 UTC (rev 23897)
@@ -41,9 +41,9 @@
implements
DecisionTableParser {
- public static final String DEFAULT_RULESHEET_NAME = "Decision Tables";
- private Map<String, List<DataListener>> _listners = new HashMap<String, List<DataListener>>();
- private boolean _useFirstSheet;
+ public static final String DEFAULT_RULESHEET_NAME = "Decision Tables";
+ private Map<String, List<DataListener>> _listners = new HashMap<String, List<DataListener>>();
+ private boolean _useFirstSheet;
/**
* Define a map of sheet name to listner handlers.
@@ -86,7 +86,7 @@
}
}
} catch ( BiffException e ) {
- throw new DecisionTableParseException( "An error occured opening the workbook. ",
+ throw new DecisionTableParseException( "An error occured opening the workbook. It is possible that the encoding of the document did not match the encoding of the reader.",
e );
} catch ( IOException e ) {
@@ -97,7 +97,7 @@
}
private void processSheet(Sheet sheet,
- List<? extends DataListener> listeners) {
+ List< ? extends DataListener> listeners) {
int maxRows = sheet.getRows();
Range[] mergedRanges = sheet.getMergedCells();
@@ -153,13 +153,13 @@
return stringVal;
}
- private void finishSheet(List<? extends DataListener> listeners) {
+ private void finishSheet(List< ? extends DataListener> listeners) {
for ( DataListener listener : listeners ) {
listener.finishSheet();
}
}
- private void newRow(List<? extends DataListener> listeners,
+ private void newRow(List< ? extends DataListener> listeners,
int row,
int cols) {
for ( DataListener listener : listeners ) {
@@ -168,7 +168,7 @@
}
}
- public void newCell(List<? extends DataListener> listeners,
+ public void newCell(List< ? extends DataListener> listeners,
int row,
int column,
String value,
Modified: labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/acme/insurance/launcher/PricingRuleLauncher.java
===================================================================
--- labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/acme/insurance/launcher/PricingRuleLauncher.java 2008-11-17 18:35:05 UTC (rev 23896)
+++ labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/acme/insurance/launcher/PricingRuleLauncher.java 2008-11-17 21:11:56 UTC (rev 23897)
@@ -28,21 +28,22 @@
*/
public class PricingRuleLauncher {
- public static final void main(String[] args) throws Exception {
- PricingRuleLauncher launcher = new PricingRuleLauncher();
- launcher.executeExample();
+ public static final void main(String[] args) throws Exception {
+ PricingRuleLauncher launcher = new PricingRuleLauncher();
+ launcher.executeExample();
}
-
+
public int executeExample() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
-
+
DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
dtconf.setInputType( DecisionTableInputType.XLS );
-
- kbuilder.addResource( new InputStreamReader( getClass().getResourceAsStream( "/data/ExamplePolicyPricing.xls" ) ),
+
+ kbuilder.addResource( new InputStreamReader( getClass().getResourceAsStream( "/data/ExamplePolicyPricing.xls" ),
+ "windows-1252" ),
KnowledgeType.DTABLE,
- dtconf );
-
+ dtconf );
+
if ( kbuilder.hasErrors() ) {
throw new RuntimeException( kbuilder.getErrors().toString() );
}
@@ -53,44 +54,39 @@
//NEW WORKING MEMORY
final StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
-
- //now create some test data
- Driver driver = new Driver();
- Policy policy = new Policy();
-
- session.insert(driver);
- session.insert(policy);
-
- session.fireAllRules();
-
- System.out.println("BASE PRICE IS: " + policy.getBasePrice());
- System.out.println("DISCOUNT IS: " + policy.getDiscountPercent());
-
+
+ //now create some test data
+ Driver driver = new Driver();
+ Policy policy = new Policy();
+
+ session.insert( driver );
+ session.insert( policy );
+
+ session.fireAllRules();
+
+ System.out.println( "BASE PRICE IS: " + policy.getBasePrice() );
+ System.out.println( "DISCOUNT IS: " + policy.getDiscountPercent() );
+
return policy.getBasePrice();
-
+
}
-
/** Build the rule base from the generated DRL */
- private RuleBase buildRuleBase(String drl) throws DroolsParserException, IOException, Exception {
- //now we build the rule package and rulebase, as if they are normal rules
- PackageBuilder builder = new PackageBuilder();
- builder.addPackageFromDrl( new StringReader(drl) );
-
- //add the package to a rulebase (deploy the rule package).
- RuleBase ruleBase = RuleBaseFactory.newRuleBase();
- ruleBase.addPackage( builder.getPackage() );
- return ruleBase;
- }
-
-
+ private RuleBase buildRuleBase(String drl) throws DroolsParserException,
+ IOException,
+ Exception {
+ //now we build the rule package and rulebase, as if they are normal rules
+ PackageBuilder builder = new PackageBuilder();
+ builder.addPackageFromDrl( new StringReader( drl ) );
+ //add the package to a rulebase (deploy the rule package).
+ RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ ruleBase.addPackage( builder.getPackage() );
+ return ruleBase;
+ }
private InputStream getSpreadsheetStream() {
- return this.getClass().getResourceAsStream("/data/ExamplePolicyPricing.xls");
- }
+ return this.getClass().getResourceAsStream( "/data/ExamplePolicyPricing.xls" );
+ }
-
-
-
}
Modified: labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/SpreadsheetIntegrationTest.java
===================================================================
--- labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/SpreadsheetIntegrationTest.java 2008-11-17 18:35:05 UTC (rev 23896)
+++ labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/SpreadsheetIntegrationTest.java 2008-11-17 21:11:56 UTC (rev 23897)
@@ -17,7 +17,6 @@
*/
import java.io.InputStreamReader;
-import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
@@ -26,30 +25,26 @@
import org.acme.insurance.launcher.PricingRuleLauncher;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
-import org.drools.RuleBase;
-import org.drools.RuleBaseFactory;
-import org.drools.WorkingMemory;
import org.drools.builder.DecisionTableConfiguration;
import org.drools.builder.DecisionTableInputType;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.KnowledgeType;
-import org.drools.compiler.PackageBuilder;
-import org.drools.rule.Package;
import org.drools.runtime.StatefulKnowledgeSession;
public class SpreadsheetIntegrationTest extends TestCase {
public void testExecute() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
-
+
DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
dtconf.setInputType( DecisionTableInputType.XLS );
-
- kbuilder.addResource( new InputStreamReader( getClass().getResourceAsStream( "/data/IntegrationExampleTest.xls" ) ),
+
+ kbuilder.addResource( new InputStreamReader( getClass().getResourceAsStream( "/data/IntegrationExampleTest.xls" ),
+ "windows-1252" ),
KnowledgeType.DTABLE,
- dtconf );
-
+ dtconf );
+
assertFalse( kbuilder.hasErrors() );
//BUILD RULEBASE
@@ -61,31 +56,32 @@
//ASSERT AND FIRE
session.insert( new Cheese( "stilton",
- 42 ) );
+ 42 ) );
session.insert( new Person( "michael",
- "stilton",
- 42 ) );
+ "stilton",
+ 42 ) );
final List<String> list = new ArrayList<String>();
session.setGlobal( "list",
- list );
+ list );
session.fireAllRules();
assertEquals( 1,
list.size() );
assertEquals( "Old man stilton",
list.get( 0 ) );
}
-
- public void testNamedWorksheet() {
+
+ public void testNamedWorksheet() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
-
+
DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
dtconf.setInputType( DecisionTableInputType.XLS );
dtconf.setWorksheetName( "Tables_2" );
-
- kbuilder.addResource( new InputStreamReader( getClass().getResourceAsStream( "/data/IntegrationExampleTest.xls" ) ),
+
+ kbuilder.addResource( new InputStreamReader( getClass().getResourceAsStream( "/data/IntegrationExampleTest.xls" ),
+ "windows-1252" ),
KnowledgeType.DTABLE,
- dtconf );
-
+ dtconf );
+
assertFalse( kbuilder.hasErrors() );
//BUILD RULEBASE
@@ -97,26 +93,27 @@
//ASSERT AND FIRE
session.insert( new Cheese( "cheddar",
- 42 ) );
+ 42 ) );
session.insert( new Person( "michael",
- "stilton",
- 25 ) );
+ "stilton",
+ 25 ) );
final List<String> list = new ArrayList<String>();
session.setGlobal( "list",
- list );
+ list );
session.fireAllRules();
assertEquals( 1,
list.size() );
assertEquals( "Young man cheddar",
- list.get( 0 ) );
+ list.get( 0 ) );
}
-
+
/**
* A smoke test mainly.
*/
public void testInsuranceExample() throws Exception {
PricingRuleLauncher launcher = new PricingRuleLauncher();
- assertEquals(120, launcher.executeExample());
+ assertEquals( 120,
+ launcher.executeExample() );
}
}
\ No newline at end of file
More information about the jboss-svn-commits
mailing list