Search in sources :

Example 1 with TemplateDataListener

use of org.drools.template.parser.TemplateDataListener in project drools by kiegroup.

the class ExternalSpreadsheetCompiler method compile.

public String compile(final InputStream xlsStream, final String worksheetName, final InputStream templateStream, int startRow, int startCol) {
    TemplateContainer tc = new DefaultTemplateContainer(templateStream);
    closeStream(templateStream);
    return compile(xlsStream, worksheetName, new TemplateDataListener(startRow, startCol, tc));
}
Also used : TemplateContainer(org.drools.template.parser.TemplateContainer) DefaultTemplateContainer(org.drools.template.parser.DefaultTemplateContainer) DefaultTemplateContainer(org.drools.template.parser.DefaultTemplateContainer) TemplateDataListener(org.drools.template.parser.TemplateDataListener)

Example 2 with TemplateDataListener

use of org.drools.template.parser.TemplateDataListener in project drools by kiegroup.

the class ExternalSpreadsheetCompilerTest method testPricing.

@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);
    // COMPILE
    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();
    // now create some test data
    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);
}
Also used : Policy(org.acme.insurance.Policy) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) Driver(org.acme.insurance.Driver) TemplateDataListener(org.drools.template.parser.TemplateDataListener) DataListener(org.drools.template.parser.DataListener) KieSession(org.kie.api.runtime.KieSession) TemplateDataListener(org.drools.template.parser.TemplateDataListener) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 3 with TemplateDataListener

use of org.drools.template.parser.TemplateDataListener in project drools by kiegroup.

the class DataProviderCompiler method compile.

/**
 * Generates DRL from a data provider for the spreadsheet data and templates.
 *
 * @param dataProvider   the data provider for the spreadsheet data
 * @param templateStream the InputStream for reading the templates
 * @return the generated DRL text as a String
 */
public String compile(final DataProvider dataProvider, final InputStream templateStream, boolean replaceOptionals) {
    DefaultTemplateContainer tc = new DefaultTemplateContainer(templateStream, replaceOptionals);
    closeStream(templateStream);
    return compile(dataProvider, new TemplateDataListener(tc));
}
Also used : DefaultTemplateContainer(org.drools.template.parser.DefaultTemplateContainer) TemplateDataListener(org.drools.template.parser.TemplateDataListener)

Example 4 with TemplateDataListener

use of org.drools.template.parser.TemplateDataListener in project drools by kiegroup.

the class ObjectDataCompiler method compile.

/**
 * Compile templates, substituting from a collection of maps or objects
 * into the given template.
 *
 * @param objs           objs the collection of maps or objects
 * @param templateStream the template as a stream
 * @return the expanded rules as a string
 */
public String compile(final Collection<?> objs, final InputStream templateStream) {
    TemplateContainer tc = new DefaultTemplateContainer(templateStream);
    closeStream(templateStream);
    return compile(new ObjectDataProvider(tc, objs), new TemplateDataListener(tc));
}
Also used : TemplateContainer(org.drools.template.parser.TemplateContainer) DefaultTemplateContainer(org.drools.template.parser.DefaultTemplateContainer) DefaultTemplateContainer(org.drools.template.parser.DefaultTemplateContainer) ObjectDataProvider(org.drools.template.objects.ObjectDataProvider) TemplateDataListener(org.drools.template.parser.TemplateDataListener)

Example 5 with TemplateDataListener

use of org.drools.template.parser.TemplateDataListener in project drools by kiegroup.

the class ResultSetGenerator method compile.

/**
 * Generates DRL from a data provider for the spreadsheet data and templates.
 *
 * @param rs             the resultset for the table data
 * @param templateStream the InputStream for reading the templates
 * @return the generated DRL text as a String
 */
public String compile(final ResultSet rs, final InputStream templateStream) {
    TemplateContainer tc = new DefaultTemplateContainer(templateStream);
    closeStream(templateStream);
    return compile(rs, new TemplateDataListener(tc));
}
Also used : TemplateContainer(org.drools.template.parser.TemplateContainer) DefaultTemplateContainer(org.drools.template.parser.DefaultTemplateContainer) DefaultTemplateContainer(org.drools.template.parser.DefaultTemplateContainer) TemplateDataListener(org.drools.template.parser.TemplateDataListener)

Aggregations

TemplateDataListener (org.drools.template.parser.TemplateDataListener)7 DefaultTemplateContainer (org.drools.template.parser.DefaultTemplateContainer)6 TemplateContainer (org.drools.template.parser.TemplateContainer)4 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Driver (org.acme.insurance.Driver)1 Policy (org.acme.insurance.Policy)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 DataProvider (org.drools.template.DataProvider)1 DataProviderCompiler (org.drools.template.DataProviderCompiler)1 ArrayDataProvider (org.drools.template.objects.ArrayDataProvider)1 ObjectDataProvider (org.drools.template.objects.ObjectDataProvider)1 DataListener (org.drools.template.parser.DataListener)1 Test (org.junit.Test)1 KieSession (org.kie.api.runtime.KieSession)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1