ExternalSpreadsheetCompiler { public String compile(final String xls, final String template, int startRow, int startCol) { return compile( xls, template, InputType.XLS, startRow, startCol ); }  String compile(final String xls,
                          final String template,
                          int startRow,
                          int startCol); String compile(final String xls,
                          final String template,
                          InputType type,
                          int startRow,
                          int startCol); String compile(final String xls,
                          final String worksheetName,
                          final String template,
                          int startRow,
                          int startCol); String compile(final InputStream xlsStream,
                          final InputStream templateStream,
                          int startRow,
                          int startCol); String compile(final InputStream xlsStream,
                          final InputStream templateStream,
                          InputType type,
                          int startRow,
                          int startCol); String compile(final InputStream xlsStream,
                          final String worksheetName,
                          final InputStream templateStream,
                          int startRow,
                          int startCol); void compile(final String xls,
                        InputType type,
                        final List<DataListener> listeners); void compile(final String xls,
                        final Map<String, List<DataListener>> listeners); void compile(final InputStream xlsStream,
                        InputType type,
                        final List<DataListener> listeners); void compile(final InputStream xlsStream,
                        final Map<String, List<DataListener>> listeners); String compile(final InputStream xlsStream,
                          final InputType type,
                          final TemplateDataListener listener); String compile(final InputStream xlsStream,
                          final String worksheetName,
                          final TemplateDataListener listener);  }
@Test public void testLoadFromClassPath() { final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler(); final String drl = converter.compile( "/data/MultiSheetDST.xls", "/templates/test_template1.drl", 11, 2 ); assertNotNull( drl ); assertTrue( drl.indexOf( "rule \"How cool is Shaun 12\"" ) > 0 ); assertTrue( drl.indexOf( "rule \"How cool is Kumar 11\"" ) > 0 ); assertTrue( drl.indexOf( "import example.model.User;" ) > -1 ); assertTrue( drl.indexOf( "import example.model.Car;" ) > -1 ); }
@Test public void testLoadSpecificWorksheet() { final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler(); final String drl = converter.compile( "/data/MultiSheetDST.xls", "Another Sheet", "/templates/test_template1.drl", 11, 2 ); assertNotNull( drl ); }
@Test public void testLoadCsv() { final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler(); final String drl = converter.compile( "/data/ComplexWorkbook.csv", "/templates/test_template2.drl", InputType.CSV, 10, 2 ); assertNotNull( drl ); assertTrue( drl.indexOf( "myObject.setIsValid(1, 2)" ) > 0 ); assertTrue( drl.indexOf( "myObject.size () > 2" ) > 0 ); assertTrue( drl.indexOf( "Foo(myObject.getColour().equals(red),\n myObject.size () > 1" ) > 0 ); }
@Test public void testLoadBasicWithMergedCells() { final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler(); final String drl = converter.compile( "/data/BasicWorkbook.xls", "/templates/test_template3.drl", InputType.XLS, 10, 2 ); final String drl1 = converter.compile( "/data/BasicWorkbook.xls", "/templates/test_template3.drl", InputType.XLS, 21, 2 ); assertNotNull( drl ); Pattern p = Pattern.compile( ".*setIsValid\\(Y\\).*setIsValid\\(Y\\).*setIsValid\\(Y\\).*", Pattern.DOTALL | Pattern.MULTILINE ); Matcher m = p.matcher( drl ); assertTrue( m.matches() ); assertTrue( drl.indexOf( "This is a function block" ) > -1 ); assertTrue( drl.indexOf( "global Class1 obj1;" ) > -1 ); assertTrue( drl1.indexOf( "myObject.setIsValid(10-Jul-1974)" ) > -1 ); assertTrue( drl.indexOf( "myObject.getColour().equals(blue)" ) > -1 ); assertTrue( drl.indexOf( "Foo(myObject.getColour().equals(red), myObject.size() > 12\")" ) > -1 ); assertTrue( drl.indexOf( "b: Bar()\n eval(myObject.size() < 3)" ) > -1 ); assertTrue( drl.indexOf( "b: Bar()\n eval(myObject.size() < 9)" ) > -1 ); assertTrue( drl.indexOf( "Foo(myObject.getColour().equals(red), myObject.size() > 1)" ) < drl.indexOf( "b: Bar()\n eval(myObject.size() < 3)" ) ); }
@Test public void testLoadBasicWithExtraCells() { final ExternalSpreadsheetCompiler compiler = new ExternalSpreadsheetCompiler(); final String drl = compiler.compile( "/data/BasicWorkbook.xls", "/templates/test_template4.drl", InputType.XLS, 10, 2 ); assertNotNull( drl ); assertTrue( drl.indexOf( "This is a function block" ) > -1 ); assertTrue( drl.indexOf( "global Class1 obj1;" ) > -1 ); assertTrue( drl.indexOf( "myObject.getColour().equals(blue)" ) > -1 ); assertTrue( drl.indexOf( "Foo(myObject.getColour().equals(red), myObject.size() > 12\")" ) > -1 ); assertTrue( drl.indexOf( "b: Bar()\n eval(myObject.size() < 3)" ) > -1 ); assertTrue( drl.indexOf( "b: Bar()\n eval(myObject.size() < 9)" ) > -1 ); assertTrue( drl.indexOf( "Foo(myObject.getColour().equals(red), myObject.size() > 1)" ) < drl.indexOf( "b: Bar()\n eval(myObject.size() < 3)" ) ); }
@Test public void testIntegration() throws Exception { final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler(); final String drl = converter.compile("/data/IntegrationExampleTest.xls", "/templates/test_integration.drl", 18, 3); KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL); assertFalse(kbuilder.hasErrors()); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages(kbuilder.getKnowledgePackages()); KieSession kSession = kbase.newKieSession(); kSession.insert( new Cheese( "stilton", 42 ) ); kSession.insert( new Person( "michael", "stilton", 42 ) ); List<String> list = new ArrayList<String>(); kSession.setGlobal( "list", list ); kSession.fireAllRules(); assertEquals( 1, list.size() ); }
@Test public void testPricing() throws Exception { final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler(); final List<DataListener> listeners = new ArrayList<DataListener>(); TemplateDataListener l1 = new TemplateDataListener(10, 3, "/templates/test_pricing1.drl"); listeners.add(l1); TemplateDataListener l2 = new TemplateDataListener(30, 3, "/templates/test_pricing2.drl"); listeners.add(l2); converter.compile("/data/ExamplePolicyPricing.xls", InputType.XLS, listeners); KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(l1.renderDRL().getBytes()), ResourceType.DRL); kbuilder.add(ResourceFactory.newByteArrayResource(l2.renderDRL().getBytes()), ResourceType.DRL); assertFalse(kbuilder.hasErrors()); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages(kbuilder.getKnowledgePackages()); KieSession kSession = kbase.newKieSession(); Driver driver = new Driver(); Policy policy = new Policy(); kSession.insert(driver); kSession.insert(policy); kSession.fireAllRules(); System.out.println("BASE PRICE IS: " + policy.getBasePrice()); System.out.println("DISCOUNT IS: " + policy.getDiscountPercent()); int basePrice = policy.getBasePrice(); assertEquals(120, basePrice); }