001: package org.drools.decisiontable.parser;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.Properties;
022:
023: import junit.framework.TestCase;
024:
025: import org.drools.decisiontable.model.Condition;
026: import org.drools.decisiontable.model.Consequence;
027: import org.drools.decisiontable.model.Import;
028: import org.drools.decisiontable.model.Package;
029: import org.drools.decisiontable.model.Rule;
030:
031: /**
032: * @author Shaun Addison, Michael Neale
033: *
034: * Test an excel file.
035: *
036: * Assumes it has a sheet called "Decision Tables" with a rule table identified
037: * by a "RuleTable" cell
038: */
039: public class RuleWorksheetParseTest extends TestCase {
040:
041: public void testBasicWorkbookProperties() throws Exception {
042:
043: final InputStream stream = RuleWorksheetParseTest.class
044: .getResourceAsStream("/data/BasicWorkbook.xls");
045:
046: final RuleSheetListener listener = getRuleSheetListener(stream);
047:
048: final Properties props = listener.getProperties();
049: assertNotNull(props);
050: assertEquals("myruleset", props.getProperty("RuleSet"));
051: assertEquals("someMisc", props.getProperty("misc"));
052: /*
053: * System.out.println("Here are the global properties...");
054: * listener.getProperties().list(System.out);
055: */
056: }
057:
058: public void testComplexWorkbookProperties() throws Exception {
059:
060: final InputStream stream = RuleWorksheetParseTest.class
061: .getResourceAsStream("/data/ComplexWorkbook.xls");
062: final RuleSheetListener listener = getRuleSheetListener(stream);
063:
064: final Properties props = listener.getProperties();
065: assertNotNull(props);
066: final String ruleSetName = props.getProperty("RuleSet");
067: assertEquals("ruleSetName", ruleSetName);
068:
069: }
070:
071: public void testWorkbookParse() throws Exception {
072: final InputStream stream = RuleWorksheetParseTest.class
073: .getResourceAsStream("/data/BasicWorkbook.xls");
074: final RuleSheetListener listener = getRuleSheetListener(stream);
075:
076: final Package ruleset = listener.getRuleSet();
077: assertNotNull(ruleset);
078:
079: final Rule firstRule = (Rule) ruleset.getRules().get(0);
080: assertNotNull(firstRule.getSalience());
081: assertTrue(firstRule.getSalience().intValue() > 0);
082:
083: // System.out.println(ruleset.toXML());
084:
085: assertEquals("myruleset", ruleset.getName());
086: assertEquals(2, ruleset.getImports().size());
087: assertEquals(6, ruleset.getRules().size());
088:
089: // check imports
090: Import imp = (Import) ruleset.getImports().get(0);
091: assertEquals("blah.class1", imp.getClassName());
092: imp = (Import) ruleset.getImports().get(1);
093: assertEquals("blah.class2", imp.getClassName());
094:
095: // check rules
096: Rule rule = (Rule) ruleset.getRules().get(0);
097: Condition cond = (Condition) rule.getConditions().get(0);
098: assertEquals(
099: "Foo(myObject.getColour().equals(red), myObject.size () > 1)",
100: cond.getSnippet());
101:
102: Consequence cons = (Consequence) rule.getConsequences().get(0);
103: assertNotNull(cons);
104: assertEquals("myObject.setIsValid(Y);", cons.getSnippet());
105:
106: rule = (Rule) ruleset.getRules().get(5);
107: cond = (Condition) rule.getConditions().get(1);
108: assertEquals("myObject.size () > 7", cond.getSnippet());
109: cons = (Consequence) rule.getConsequences().get(0);
110: assertEquals("myObject.setIsValid(10-Jul-1974)", cons
111: .getSnippet());
112:
113: }
114:
115: /**
116: * See if it can cope with odd shaped rule table, including missing
117: * conditions. Also is not "sequential".
118: */
119: // TIRELLI: test commented out while we decide what to do in order to solve i18n issues
120: // public void testComplexWorksheetMissingConditions() throws Exception {
121: // final InputStream stream = RuleWorksheetParseTest.class.getResourceAsStream( "/data/ComplexWorkbook.xls" );
122: // final RuleSheetListener listener = getRuleSheetListener( stream );
123: //
124: // final Package ruleset = listener.getRuleSet();
125: // assertEquals( 6,
126: // ruleset.getRules().size() );
127: // assertEquals( 0,
128: // ruleset.getImports().size() );
129: //
130: // Rule rule = (Rule) ruleset.getRules().get( 0 );
131: // assertEquals( 3,
132: // rule.getConditions().size() );
133: // assertEquals( 2,
134: // rule.getConsequences().size() );
135: // final Consequence cons = (Consequence) rule.getConsequences().get( 1 );
136: // assertEquals( "myObject.setIsValid(1, 2)",
137: // cons.getSnippet() );
138: // final Condition con = (Condition) rule.getConditions().get( 2 );
139: // assertEquals( "myObject.size() < $3.00",
140: // con.getSnippet() );
141: //
142: // rule = (Rule) ruleset.getRules().get( 4 );
143: //
144: // // this should have less conditions
145: // assertEquals( 1,
146: // rule.getConditions().size() );
147: //
148: // rule = (Rule) ruleset.getRules().get( 5 );
149: // assertEquals( 2,
150: // rule.getConditions().size() );
151: // assertEquals( 1,
152: // rule.getConsequences().size() );
153: //
154: // }
155: /**
156: * Utility method showing how to get a rule sheet listener from a stream.
157: */
158: public static RuleSheetListener getRuleSheetListener(
159: final InputStream stream) throws IOException {
160: return RulesheetUtil.getRuleSheetListener(stream);
161: }
162:
163: }
|