[jboss-svn-commits] JBL Code SVN: r16643 - in labs/jbossrules/trunk/drools-decisiontables/src: main/java/org/drools/decisiontable/parser and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Nov 16 08:18:58 EST 2007
Author: stevearoonie
Date: 2007-11-16 08:18:58 -0500 (Fri, 16 Nov 2007)
New Revision: 16643
Added:
labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/DataProvider.java
labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/DataProviderCompiler.java
labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/DataProviderCompilerIntegrationTest.java
labs/jbossrules/trunk/drools-decisiontables/src/test/resources/templates/rule_template_1.drl
Modified:
labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/parser/ExternalSheetListener.java
Log:
Add DataProvider interface and compiler for generating rules from non-spreadsheet data
Added: labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/DataProvider.java
===================================================================
--- labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/DataProvider.java (rev 0)
+++ labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/DataProvider.java 2007-11-16 13:18:58 UTC (rev 16643)
@@ -0,0 +1,9 @@
+package org.drools.decisiontable;
+
+public interface DataProvider {
+
+ boolean hasNext();
+
+ String[] next();
+
+}
Added: labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/DataProviderCompiler.java
===================================================================
--- labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/DataProviderCompiler.java (rev 0)
+++ labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/DataProviderCompiler.java 2007-11-16 13:18:58 UTC (rev 16643)
@@ -0,0 +1,127 @@
+package org.drools.decisiontable;
+
+/*
+ * Copyright 2005 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.drools.decisiontable.parser.DefaultTemplateContainer;
+import org.drools.decisiontable.parser.ExternalSheetListener;
+import org.drools.decisiontable.parser.SheetListener;
+import org.drools.decisiontable.parser.TemplateContainer;
+
+public class DataProviderCompiler {
+
+ /**
+ * Generates DRL from the input stream containing the spreadsheet.
+ *
+ * @param xlsStream
+ * The stream to the spreadsheet. Uses the first worksheet found
+ * for the decision tables, ignores others.
+ * @param type
+ * The type of the file - InputType.CSV or InputType.XLS
+ * @param listener
+ * @return DRL xml, ready for use in drools.
+ * @throws IOException
+ */
+ public String compile(final DataProvider dataProvider,
+ final String template) {
+ final InputStream templateStream = this.getClass().getResourceAsStream( template );
+ return compile( dataProvider,
+ templateStream );
+ }
+
+ public String compile(final DataProvider dataProvider,
+ final InputStream templateStream) {
+ TemplateContainer tc = new DefaultTemplateContainer( templateStream );
+ closeStream( templateStream );
+ return compile( dataProvider,
+ new ExternalSheetListener( tc ) );
+ }
+
+ public String compile(final DataProvider dataProvider,
+ final ExternalSheetListener listener) {
+ List listeners = new ArrayList();
+ listeners.add( listener );
+ processData( dataProvider,
+ listeners );
+ return listener.renderDRL();
+ }
+
+ private void processData(final DataProvider dataProvider,
+ List listeners) {
+ for ( int i = 0; dataProvider.hasNext(); i++ ) {
+ String[] row = dataProvider.next();
+ newRow( listeners,
+ i,
+ row.length );
+ for ( int cellNum = 0; cellNum < row.length; cellNum++ ) {
+ String cell = row[cellNum];
+
+ newCell( listeners,
+ i,
+ cellNum,
+ cell,
+ SheetListener.NON_MERGED );
+ }
+ }
+ finishData( listeners );
+ }
+
+ private void finishData(List listeners) {
+ for ( Iterator it = listeners.iterator(); it.hasNext(); ) {
+ SheetListener listener = (SheetListener) it.next();
+ listener.finishSheet();
+ }
+ }
+
+ private void newRow(List listeners,
+ int row,
+ int cols) {
+ for ( Iterator it = listeners.iterator(); it.hasNext(); ) {
+ SheetListener listener = (SheetListener) it.next();
+ listener.newRow( row,
+ cols );
+ }
+ }
+
+ public void newCell(List listeners,
+ int row,
+ int column,
+ String value,
+ int mergedColStart) {
+ for ( Iterator it = listeners.iterator(); it.hasNext(); ) {
+ SheetListener listener = (SheetListener) it.next();
+ listener.newCell( row,
+ column,
+ value,
+ mergedColStart );
+ }
+ }
+
+ private void closeStream(final InputStream stream) {
+ try {
+ stream.close();
+ } catch ( final Exception e ) {
+ System.err.print( "WARNING: Wasn't able to correctly close stream for rule template. " + e.getMessage() );
+ }
+ }
+
+}
Modified: labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/parser/ExternalSheetListener.java
===================================================================
--- labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/parser/ExternalSheetListener.java 2007-11-16 13:12:34 UTC (rev 16642)
+++ labs/jbossrules/trunk/drools-decisiontables/src/main/java/org/drools/decisiontable/parser/ExternalSheetListener.java 2007-11-16 13:18:58 UTC (rev 16643)
@@ -2,13 +2,13 @@
/*
* Copyright 2005 JBoss Inc
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -16,18 +16,16 @@
* limitations under the License.
*/
-import java.util.Properties;
-
import org.drools.StatefulSession;
import org.drools.audit.WorkingMemoryFileLogger;
import org.drools.decisiontable.model.DRLOutput;
/**
* SheetListener for creating rules from a template
- *
+ *
* @author <a href="mailto:stevearoonie at gmail.com">Steven Williams</a>
*/
-public class ExternalSheetListener implements RuleSheetListener {
+public class ExternalSheetListener implements SheetListener {
private int startRow = -1;
@@ -45,14 +43,18 @@
private Generator generator;
- //private WorkingMemoryFileLogger logger;
+// private WorkingMemoryFileLogger logger;
- public ExternalSheetListener(final int startRow, final int startCol,
- final String template) {
- this(startRow, startCol, new DefaultTemplateContainer(template));
+ public ExternalSheetListener(final TemplateContainer tc) {
+ this(1, 1, tc);
}
public ExternalSheetListener(final int startRow, final int startCol,
+ final String template) {
+ this(startRow, startCol, new DefaultTemplateContainer(template));
+ }
+
+ public ExternalSheetListener(final int startRow, final int startCol,
final TemplateContainer tc) {
this(startRow, startCol, tc, new DefaultTemplateRuleBase(tc));
}
@@ -80,18 +82,10 @@
private void assertColumns() {
for (int i = 0; i < columns.length; i++) {
- session.insert(columns[i]);
+ session.insert(columns[i]);
}
}
- public Properties getProperties() {
- return null;
- }
-
- public org.drools.decisiontable.model.Package getRuleSet() {
- return null;
- }
-
public void finishSheet() {
if (currentRow != null) {
session.insert(currentRow);
Added: labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/DataProviderCompilerIntegrationTest.java
===================================================================
--- labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/DataProviderCompilerIntegrationTest.java (rev 0)
+++ labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/DataProviderCompilerIntegrationTest.java 2007-11-16 13:18:58 UTC (rev 16643)
@@ -0,0 +1,57 @@
+package org.drools.decisiontable;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+public class DataProviderCompilerIntegrationTest extends TestCase {
+
+ private class TestDataProvider implements DataProvider {
+
+ private Iterator iterator;
+
+ TestDataProvider(List rows) {
+ this.iterator = rows.iterator();
+ }
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ public String[] next() {
+ return (String[]) iterator.next();
+ }
+
+ }
+
+ public void testCompiler() throws Exception {
+ ArrayList rows = new ArrayList();
+ rows.add( createRow("1","STANDARD","FLAT",null,"SBLC","ISS","Commission",null,"USD",null,"750") );
+ rows.add( createRow("15","STANDARD","FLAT",null,"SBLC","ISS","Commission",null,"YEN",null,"1600") );
+ rows.add( createRow("12","STANDARD","FLAT",null,"SBLC","ISS","Postage",null,"YEN",null,"40") );
+ rows.add( createRow("62","STANDARD","FLAT",null,"SBLC","ISS","Telex",null,"YEN","< 30000","45") );
+ TestDataProvider tdp = new TestDataProvider(rows);
+ final DataProviderCompiler converter = new DataProviderCompiler();
+ final String drl = converter.compile(tdp, "/templates/rule_template_1.drl");
+ System.out.println(drl);
+
+ }
+
+ private String[] createRow(String cell1, String cell2, String cell3, String cell4, String cell5, String cell6, String cell7, String cell8, String cell9, String cell10, String cell11) {
+ String[] row = new String[11];
+ row[0] = cell1;
+ row[1] = cell2;
+ row[2] = cell3;
+ row[3] = cell4;
+ row[4] = cell5;
+ row[5] = cell6;
+ row[6] = cell7;
+ row[7] = cell8;
+ row[8] = cell9;
+ row[9] = cell10;
+ row[10] = cell11;
+ return row;
+ }
+
+}
Added: labs/jbossrules/trunk/drools-decisiontables/src/test/resources/templates/rule_template_1.drl
===================================================================
--- labs/jbossrules/trunk/drools-decisiontables/src/test/resources/templates/rule_template_1.drl (rev 0)
+++ labs/jbossrules/trunk/drools-decisiontables/src/test/resources/templates/rule_template_1.drl 2007-11-16 13:18:58 UTC (rev 16643)
@@ -0,0 +1,36 @@
+template header
+FEE_SCHEDULE_ID
+FEE_SCHEDULE_TYPE
+FEE_MODE_TYPE
+ENTITY_BRANCH
+PRODUCT_TYPE
+ACTIVITY_TYPE
+FEE_TYPE
+OWNING_PARTY
+CCY
+LC_AMOUNT
+dummy
+AMOUNT
+
+
+package org.drools.decisiontable;
+#generated from Decision Table
+
+global FeeResult result;
+
+template "Fee Schedule"
+rule "Fee Schedule_@{row.rowNumber}"
+ agenda-group "@{FEE_SCHEDULE_TYPE}"
+ when
+ FeeEvent(productType == "@{PRODUCT_TYPE}",
+ activityType == "@{ACTIVITY_TYPE}",
+ feeType == "@{FEE_TYPE}",
+ txParty == "@{OWNING_PARTY}",
+ entityBranch == "@{ENTITY_BRANCH}",
+ amount @{LC_AMOUNT},
+ ccy == "@{CCY}"
+ )
+ then
+ result.setSchedule(new FeeSchedule("@{FEE_SCHEDULE_ID}", "@{FEE_SCHEDULE_TYPE}", @{AMOUNT}));
+end
+end template
More information about the jboss-svn-commits
mailing list