Search in sources :

Example 1 with DataProviderCompiler

use of org.drools.template.DataProviderCompiler in project drools by kiegroup.

the class DataDrivenTemplateExample method testCompiler.

public void testCompiler() {
    ArrayList<FeeScheduleRule> rules = new ArrayList<FeeScheduleRule>();
    FeeScheduleType standard = new FeeScheduleType("STANDARD");
    FeeScheduleType flat = new FeeScheduleType("FLAT");
    ProductType sblc = new ProductType("SBLC");
    ProductType rrc = new ProductType("RRC");
    ActivityType iss = new ActivityType("ISS");
    ActivityType osx = new ActivityType("OSX");
    FeeType commission = new FeeType("Commission");
    FeeType postage = new FeeType("Postage");
    FeeType telex = new FeeType("Telex");
    rules.add(createRule(1, flat, "", sblc, iss, commission, "Party 1", "USD", "", 0, 750, true));
    rules.add(createRule(2, standard, "Entity Branch 1", rrc, iss, commission, "", "YEN", "", 0, 1600, false));
    rules.add(createRule(3, standard, "", sblc, iss, postage, "", "YEN", "", 0, 40, true));
    rules.add(createRule(4, flat, "", sblc, osx, telex, "", "YEN", "<", 30000, 45, false));
    TestDataProvider tdp = new TestDataProvider(rules);
    final DataProviderCompiler converter = new DataProviderCompiler();
    final String drl = converter.compile(tdp, getTemplate());
    System.out.println(drl);
}
Also used : FeeType(org.drools.examples.templates.FeeType) FeeScheduleType(org.drools.examples.templates.FeeScheduleType) ArrayList(java.util.ArrayList) ProductType(org.drools.examples.templates.ProductType) ActivityType(org.drools.examples.templates.ActivityType) FeeScheduleRule(org.drools.examples.templates.FeeScheduleRule) DataProviderCompiler(org.drools.template.DataProviderCompiler)

Example 2 with DataProviderCompiler

use of org.drools.template.DataProviderCompiler in project drools by kiegroup.

the class TemplatesTest method loadingFromDLRArrayCorrectnessCheck.

@Test
public void loadingFromDLRArrayCorrectnessCheck() throws Exception {
    final String[][] rows = new String[3][6];
    rows[0] = new String[] { "tomato", "weight", "200", "1000", "6", "== Taste.GOOD || == Taste.EXCELENT" };
    rows[1] = new String[] { "cucumber", "length", "20", "40", "15", "== Taste.EXCELENT" };
    rows[2] = new String[] { "carrot", "weight", "0", "1000", "2", "== Taste.HORRIBLE" };
    final ArrayDataProvider adp = new ArrayDataProvider(rows);
    final DataProviderCompiler converter = new DataProviderCompiler();
    try (InputStream resourceStream = KieServices.Factory.get().getResources().newClassPathResource("template_1.drl", getClass()).getInputStream()) {
        final String drl = converter.compile(adp, resourceStream);
        // prints rules generated from template
        LOGGER.debug(drl);
        assertEqualsIgnoreWhitespace(EXPECTED_RULES.toString(), drl);
        testCorrectnessCheck(drl);
    }
}
Also used : InputStream(java.io.InputStream) ArrayDataProvider(org.drools.template.objects.ArrayDataProvider) DataProviderCompiler(org.drools.template.DataProviderCompiler) Test(org.junit.Test)

Example 3 with DataProviderCompiler

use of org.drools.template.DataProviderCompiler in project drools by kiegroup.

the class RuleTemplateModelDRLPersistenceImpl method marshal.

@Override
public String marshal(final RuleModel model) {
    // Build rule
    final String ruleTemplate = marshalRule(model);
    log.debug("ruleTemplate:\n{}", ruleTemplate);
    log.debug("generated template:\n{}", ruleTemplate);
    final DataProvider dataProvider = chooseDataProvider(model);
    final DataProviderCompiler tplCompiler = new DataProviderCompiler();
    final InputStream templateStream = new ByteArrayInputStream(ruleTemplate.getBytes(IoUtils.UTF8_CHARSET));
    final DefaultTemplateContainer tc = new DefaultTemplateContainer(templateStream, false);
    final TemplateDataListener listener = new TemplateDataListener(tc, false);
    final String generatedDrl = tplCompiler.compile(dataProvider, listener);
    log.debug("generated drl:\n{}", generatedDrl);
    return generatedDrl;
}
Also used : DataProvider(org.drools.template.DataProvider) ArrayDataProvider(org.drools.template.objects.ArrayDataProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DataProviderCompiler(org.drools.template.DataProviderCompiler) DefaultTemplateContainer(org.drools.template.parser.DefaultTemplateContainer) TemplateDataListener(org.drools.template.parser.TemplateDataListener)

Aggregations

DataProviderCompiler (org.drools.template.DataProviderCompiler)3 InputStream (java.io.InputStream)2 ArrayDataProvider (org.drools.template.objects.ArrayDataProvider)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1 ActivityType (org.drools.examples.templates.ActivityType)1 FeeScheduleRule (org.drools.examples.templates.FeeScheduleRule)1 FeeScheduleType (org.drools.examples.templates.FeeScheduleType)1 FeeType (org.drools.examples.templates.FeeType)1 ProductType (org.drools.examples.templates.ProductType)1 DataProvider (org.drools.template.DataProvider)1 DefaultTemplateContainer (org.drools.template.parser.DefaultTemplateContainer)1 TemplateDataListener (org.drools.template.parser.TemplateDataListener)1 Test (org.junit.Test)1