Search in sources :

Example 6 with Package

use of org.drools.template.model.Package in project drools by kiegroup.

the class SpreadsheetCompiler method listenerToString.

public String listenerToString(RuleSheetListener listener) {
    final Package rulePackage = listener.getRuleSet();
    final DRLOutput out = new DRLOutput();
    rulePackage.renderDRL(out);
    return out.getDRL();
}
Also used : DRLOutput(org.drools.template.model.DRLOutput) Package(org.drools.template.model.Package)

Example 7 with Package

use of org.drools.template.model.Package in project drools by kiegroup.

the class RuleWorksheetParseLargeTest method testLargeWorkSheetParseToRuleset.

/**
 * Tests parsing a large spreadsheet into an in memory ruleset. This doesn't
 * really do anything much at present. Takes a shed-load of memory to dump
 * out this much XML as a string, so really should think of using a stream
 * in some cases... (tried StringWriter, but is still in memory, so doesn't
 * help).
 *
 * Stream to a temp file would work: return a stream from that file
 * (decorate FileInputStream such that when you close it, it deletes the
 * temp file).... must be other options.
 *
 * @throws Exception
 */
@Test
public void testLargeWorkSheetParseToRuleset() throws Exception {
    // Test removed until have streaming sorted in future. No one using Uber Tables just yet !
    InputStream stream = RuleWorksheetParseLargeTest.class.getResourceAsStream("/data/VeryLargeWorkbook.xls");
    startTimer();
    RuleSheetListener listener = RuleWorksheetParseTest.getRuleSheetListener(stream);
    stopTimer();
    System.out.println("Time to parse large table : " + getTime() + "ms");
    Package ruleset = listener.getRuleSet();
    assertNotNull(ruleset);
// startTimer();
// String xml = ...;   // toXml() not in Package any more.
// System.out.println( xml );
// stopTimer();
// System.out.println("Time taken for rendering: " + getTime() + "ms");
}
Also used : InputStream(java.io.InputStream) Package(org.drools.template.model.Package) Test(org.junit.Test)

Example 8 with Package

use of org.drools.template.model.Package in project drools by kiegroup.

the class RuleWorksheetParseTest method testWorkbookParse.

@Test
public void testWorkbookParse() throws Exception {
    final InputStream stream = RuleWorksheetParseTest.class.getResourceAsStream("/data/BasicWorkbook.xls");
    final RuleSheetListener listener = getRuleSheetListener(stream);
    final Package ruleset = listener.getRuleSet();
    assertNotNull(ruleset);
    final Rule firstRule = (Rule) ruleset.getRules().get(0);
    assertNotNull(firstRule.getSalience());
    assertTrue(Integer.parseInt(firstRule.getSalience()) > 0);
    // System.out.println(ruleset.toXML());
    assertEquals("myruleset", ruleset.getName());
    assertEquals(3, ruleset.getImports().size());
    assertEquals(6, ruleset.getRules().size());
    // check imports
    Import imp = (Import) ruleset.getImports().get(0);
    assertEquals("blah.class1", imp.getClassName());
    imp = (Import) ruleset.getImports().get(1);
    assertEquals("blah.class2", imp.getClassName());
    imp = (Import) ruleset.getImports().get(2);
    assertEquals("lah.di.dah", imp.getClassName());
    // check rules
    Rule rule = (Rule) ruleset.getRules().get(0);
    Condition cond = (Condition) rule.getConditions().get(0);
    assertEquals("Foo(myObject.getColour().equals(red), myObject.size () > 12\\\")", cond.getSnippet());
    Consequence cons = (Consequence) rule.getConsequences().get(0);
    assertNotNull(cons);
    assertEquals("myObject.setIsValid(Y);", cons.getSnippet());
    rule = (Rule) ruleset.getRules().get(5);
    cond = (Condition) rule.getConditions().get(1);
    assertEquals("myObject.size () > 7", cond.getSnippet());
    cons = (Consequence) rule.getConsequences().get(0);
    assertEquals("myObject.setIsValid(10-Jul-1974)", cons.getSnippet());
}
Also used : Condition(org.drools.template.model.Condition) Import(org.drools.template.model.Import) InputStream(java.io.InputStream) Consequence(org.drools.template.model.Consequence) Package(org.drools.template.model.Package) Rule(org.drools.template.model.Rule) Test(org.junit.Test)

Example 9 with Package

use of org.drools.template.model.Package in project drools by kiegroup.

the class RuleWorksheetParseTest method testNumericDisabled.

@Test
public void testNumericDisabled() throws Exception {
    // DROOLS-1378
    final InputStream stream = RuleWorksheetParseTest.class.getResourceAsStream("/data/NumericDisabled.xls");
    final RuleSheetListener listener = getRuleSheetListener(stream);
    final Package ruleset = listener.getRuleSet();
    assertNotNull(ruleset);
    DRLOutput dout = new DRLOutput();
    ruleset.renderDRL(dout);
    String drl = dout.getDRL();
    System.out.println(drl);
    // check rules
    Rule rule = (Rule) ruleset.getRules().get(0);
    Condition cond = (Condition) rule.getConditions().get(0);
    assertEquals("Cheese(price == 6600)", cond.getSnippet());
}
Also used : Condition(org.drools.template.model.Condition) DRLOutput(org.drools.template.model.DRLOutput) InputStream(java.io.InputStream) Package(org.drools.template.model.Package) Rule(org.drools.template.model.Rule) Test(org.junit.Test)

Example 10 with Package

use of org.drools.template.model.Package in project drools by kiegroup.

the class RuleWorksheetParseTest method doComplexWorksheetMissingConditions.

private void doComplexWorksheetMissingConditions() throws IOException {
    final InputStream stream = RuleWorksheetParseTest.class.getResourceAsStream("/data/ComplexWorkbook.xls");
    final RuleSheetListener listener = getRuleSheetListener(stream);
    final Package ruleset = listener.getRuleSet();
    assertEquals(6, ruleset.getRules().size());
    assertEquals(0, ruleset.getImports().size());
    Rule rule = (Rule) ruleset.getRules().get(0);
    assertEquals(3, rule.getConditions().size());
    assertEquals(2, rule.getConsequences().size());
    final Consequence cons = (Consequence) rule.getConsequences().get(1);
    assertEquals("myObject.setIsValid(1, 2)", cons.getSnippet());
    final Condition con = (Condition) rule.getConditions().get(2);
    assertEquals("myObject.size() < $3.00", con.getSnippet());
    rule = (Rule) ruleset.getRules().get(4);
    // this should have less conditions
    assertEquals(1, rule.getConditions().size());
    rule = (Rule) ruleset.getRules().get(5);
    assertEquals(2, rule.getConditions().size());
    assertEquals(1, rule.getConsequences().size());
}
Also used : Condition(org.drools.template.model.Condition) InputStream(java.io.InputStream) Consequence(org.drools.template.model.Consequence) Package(org.drools.template.model.Package) Rule(org.drools.template.model.Rule)

Aggregations

Package (org.drools.template.model.Package)14 Test (org.junit.Test)11 DRLOutput (org.drools.template.model.DRLOutput)10 InputStream (java.io.InputStream)7 Rule (org.drools.template.model.Rule)7 Condition (org.drools.template.model.Condition)5 DecisionTableParseException (org.drools.template.parser.DecisionTableParseException)3 Consequence (org.drools.template.model.Consequence)2 Import (org.drools.template.model.Import)2 Code (org.drools.decisiontable.parser.ActionType.Code)1 Global (org.drools.template.model.Global)1