Search in sources :

Example 1 with DecisionTableConfiguration

use of org.kie.internal.builder.DecisionTableConfiguration in project drools by kiegroup.

the class KnowledgeBuilderImpl method decisionTableToPackageDescr.

PackageDescr decisionTableToPackageDescr(Resource resource, ResourceConfiguration configuration) throws DroolsParserException, IOException {
    DecisionTableConfiguration dtableConfiguration = configuration instanceof DecisionTableConfiguration ? (DecisionTableConfiguration) configuration : null;
    if (dtableConfiguration != null && !dtableConfiguration.getRuleTemplateConfigurations().isEmpty()) {
        List<String> generatedDrls = DecisionTableFactory.loadFromInputStreamWithTemplates(resource, dtableConfiguration);
        if (generatedDrls.size() == 1) {
            return generatedDrlToPackageDescr(resource, generatedDrls.get(0));
        }
        CompositePackageDescr compositePackageDescr = null;
        for (String generatedDrl : generatedDrls) {
            PackageDescr packageDescr = generatedDrlToPackageDescr(resource, generatedDrl);
            if (packageDescr != null) {
                if (compositePackageDescr == null) {
                    compositePackageDescr = new CompositePackageDescr(resource, packageDescr);
                } else {
                    compositePackageDescr.addPackageDescr(resource, packageDescr);
                }
            }
        }
        return compositePackageDescr;
    }
    String generatedDrl = DecisionTableFactory.loadFromResource(resource, dtableConfiguration);
    return generatedDrlToPackageDescr(resource, generatedDrl);
}
Also used : CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 2 with DecisionTableConfiguration

use of org.kie.internal.builder.DecisionTableConfiguration in project drools by kiegroup.

the class AbstractKieModule method addResourceToCompiler.

public final boolean addResourceToCompiler(CompositeKnowledgeBuilder ckbuilder, KieBaseModel kieBaseModel, String fileName, ResourceChangeSet rcs) {
    ResourceConfiguration conf = getResourceConfiguration(fileName);
    Resource resource = getResource(fileName);
    if (resource != null) {
        ResourceType resourceType = conf instanceof ResourceConfigurationImpl && ((ResourceConfigurationImpl) conf).getResourceType() != null ? ((ResourceConfigurationImpl) conf).getResourceType() : ResourceType.determineResourceType(fileName);
        if (resourceType == ResourceType.DTABLE && conf instanceof DecisionTableConfiguration) {
            for (RuleTemplateModel template : kieBaseModel.getRuleTemplates()) {
                if (template.getDtable().equals(fileName)) {
                    Resource templateResource = getResource(template.getTemplate());
                    if (templateResource != null) {
                        ((DecisionTableConfiguration) conf).addRuleTemplateConfiguration(templateResource, template.getRow(), template.getCol());
                    }
                }
            }
        }
        if (conf == null) {
            ckbuilder.add(resource, resourceType, rcs);
        } else {
            ckbuilder.add(resource, resourceType, conf, rcs);
        }
        return true;
    }
    return false;
}
Also used : DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) Resource(org.kie.api.io.Resource) ResourceConfigurationImpl(org.drools.core.builder.conf.impl.ResourceConfigurationImpl) ResourceType(org.kie.api.io.ResourceType) RuleTemplateModel(org.kie.api.builder.model.RuleTemplateModel) ResourceConfiguration(org.kie.api.io.ResourceConfiguration)

Example 3 with DecisionTableConfiguration

use of org.kie.internal.builder.DecisionTableConfiguration in project drools by kiegroup.

the class PricingRuleLauncher method executeExample.

public int executeExample() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
    dtconf.setInputType(DecisionTableInputType.XLS);
    kbuilder.add(ResourceFactory.newClassPathResource("/data/ExamplePolicyPricing.xls", getClass()), ResourceType.DTABLE, dtconf);
    if (kbuilder.hasErrors()) {
        throw new RuntimeException(kbuilder.getErrors().toString());
    }
    // BUILD RULEBASE
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    // NEW WORKING MEMORY
    final KieSession session = kbase.newKieSession();
    // now create some test data
    Driver driver = new Driver();
    Policy policy = new Policy();
    session.insert(driver);
    session.insert(policy);
    session.fireAllRules();
    System.out.println("BASE PRICE IS: " + policy.getBasePrice());
    System.out.println("DISCOUNT IS: " + policy.getDiscountPercent());
    return policy.getBasePrice();
}
Also used : Policy(org.acme.insurance.Policy) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) Driver(org.acme.insurance.Driver) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 4 with DecisionTableConfiguration

use of org.kie.internal.builder.DecisionTableConfiguration in project drools by kiegroup.

the class ColumnReplaceTest method testAutoFocusToLockOnActiveReplacement.

@Test
public void testAutoFocusToLockOnActiveReplacement() throws FileNotFoundException {
    DecisionTableConfiguration dTableConfiguration = KnowledgeBuilderFactory.newDecisionTableConfiguration();
    dTableConfiguration.setInputType(DecisionTableInputType.CSV);
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("columnReplaceTest.csv", getClass()), ResourceType.DTABLE, dTableConfiguration);
    if (kbuilder.hasErrors()) {
        System.out.println(kbuilder.getErrors());
        fail("Knowledge builder cannot compile package!");
    }
    System.out.println(DecisionTableFactory.loadFromInputStream(new FileInputStream(new File("src/test/resources/org/drools/decisiontable/columnReplaceTest.csv")), dTableConfiguration));
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    assertTrue(((RuleImpl) kbase.getRule("org.drools.decisiontable", "lockOnActiveRule")).isLockOnActive());
    // lock-on-active was not set on autoFocusRule, so it should be by default false
    assertFalse(((RuleImpl) kbase.getRule("org.drools.decisiontable", "autoFocusRule")).isLockOnActive());
    assertFalse(((RuleImpl) kbase.getRule("org.drools.decisiontable", "lockOnActiveRule")).getAutoFocus());
    // auto-focus was set to be true, so it should be true
    assertTrue(((RuleImpl) kbase.getRule("org.drools.decisiontable", "autoFocusRule")).getAutoFocus());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) File(java.io.File) FileInputStream(java.io.FileInputStream) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 5 with DecisionTableConfiguration

use of org.kie.internal.builder.DecisionTableConfiguration in project drools by kiegroup.

the class DumpGeneratedDrlTest method testDTableWithNullSrcPathIsCorrectlyDumped.

@Test
public void testDTableWithNullSrcPathIsCorrectlyDumped() {
    DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
    dtconf.setInputType(DecisionTableInputType.CSV);
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Resource resource = ResourceFactory.newByteArrayResource(DUMMY_DTABLE_CSV_SOURCE.getBytes(IoUtils.UTF8_CHARSET));
    kbuilder.add(resource, ResourceType.DTABLE, dtconf);
    if (kbuilder.hasErrors()) {
        Assert.fail("Unexpected Drools compilation errors: " + kbuilder.getErrors().toString());
    }
    assertGeneratedDrlExists(dumpDir, null);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) Resource(org.kie.api.io.Resource) Test(org.junit.Test)

Aggregations

DecisionTableConfiguration (org.kie.internal.builder.DecisionTableConfiguration)17 Test (org.junit.Test)10 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)10 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)6 ArrayList (java.util.ArrayList)5 Resource (org.kie.api.io.Resource)5 KieSession (org.kie.api.runtime.KieSession)5 DecisionTableConfigurationImpl (org.drools.core.builder.conf.impl.DecisionTableConfigurationImpl)3 Command (org.kie.api.command.Command)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 StringReader (java.io.StringReader)1 List (java.util.List)1 Driver (org.acme.insurance.Driver)1 Policy (org.acme.insurance.Policy)1 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)1 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)1 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)1 ResourceConfigurationImpl (org.drools.core.builder.conf.impl.ResourceConfigurationImpl)1 UrlResource (org.drools.core.io.impl.UrlResource)1