Search in sources :

Example 11 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class SegmentPropagationTest method createContext.

public BuildContext createContext() {
    RuleBaseConfiguration conf = new RuleBaseConfiguration();
    KnowledgeBaseImpl rbase = new KnowledgeBaseImpl("ID", conf);
    BuildContext buildContext = new BuildContext(rbase);
    RuleImpl rule = new RuleImpl("rule1").setPackage("org.pkg1");
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
    pkg.getDialectRuntimeRegistry().setDialectData("mvel", new MVELDialectRuntimeData());
    pkg.addRule(rule);
    buildContext.setRule(rule);
    return buildContext;
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 12 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class JavaConsequenceBuilderPRAlwaysTest method setupTest.

private void setupTest(String consequence, Map<String, Object> namedConsequences) {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
    pkg.addImport(new ImportDeclaration("org.drools.compiler.Cheese"));
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    // Although it should be the default, for completeness we explicit this is the test cases how RHS should be rewritten as:
    conf.setOption(PropertySpecificOption.ALWAYS);
    KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl(pkg, conf);
    ruleDescr = new RuleDescr("test consequence builder");
    ruleDescr.setConsequence(consequence);
    for (Entry<String, Object> entry : namedConsequences.entrySet()) {
        ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
    }
    RuleImpl rule = ruleDescr.toRule();
    PackageRegistry pkgRegistry = kBuilder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry reg = kBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    context = new RuleBuildContext(kBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
    rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
    Pattern p = new Pattern(1, new ClassObjectType(Person.class), "$persone");
    Declaration declr = p.addDeclaration("age");
    final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, new BindingDescr("age", "age"), p, "age", declr, true);
    rule.addPattern(p);
    context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
    context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
    for (String name : namedConsequences.keySet()) {
        context.getDialect().getConsequenceBuilder().build(context, name);
    }
    context.getDialect().addRule(context);
    pkgRegistry.getPackage().addRule(context.getRule());
    kBuilder.compileAll();
    kBuilder.reloadAll();
}
Also used : Pattern(org.drools.core.rule.Pattern) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) Person(org.drools.compiler.Person) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 13 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class JavaConsequenceBuilderTest method setupTest.

private void setupTest(String consequence, Map<String, Object> namedConsequences) {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
    pkg.addImport(new ImportDeclaration("org.drools.compiler.Cheese"));
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    // this test was originally intended with PropertyReactive.ALLOWED:
    conf.setOption(PropertySpecificOption.ALLOWED);
    KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl(pkg, conf);
    ruleDescr = new RuleDescr("test consequence builder");
    ruleDescr.setConsequence(consequence);
    for (Entry<String, Object> entry : namedConsequences.entrySet()) {
        ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
    }
    RuleImpl rule = ruleDescr.toRule();
    PackageRegistry pkgRegistry = kBuilder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry reg = kBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    context = new RuleBuildContext(kBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
    rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
    Pattern p = new Pattern(1, new ClassObjectType(Person.class), "$persone");
    Declaration declr = p.addDeclaration("age");
    final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, new BindingDescr("age", "age"), p, "age", declr, true);
    rule.addPattern(p);
    context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
    context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
    for (String name : namedConsequences.keySet()) {
        context.getDialect().getConsequenceBuilder().build(context, name);
    }
    context.getDialect().addRule(context);
    pkgRegistry.getPackage().addRule(context.getRule());
    kBuilder.compileAll();
    kBuilder.reloadAll();
}
Also used : Pattern(org.drools.core.rule.Pattern) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) Person(org.drools.compiler.Person) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 14 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class KnowledgeBuilderTest method testRepeatedDeclarationInMultiplePackages.

@Test
public void testRepeatedDeclarationInMultiplePackages() {
    String str = "package org.drools.test1;\n" + "import org.drools.compiler.Cheese;\n" + "" + "rule R\n" + "when Cheese() then end \n" + "";
    String str2 = "package org.drools.test2;\n" + "import org.drools.compiler.Cheese;\n" + "" + "rule S\n" + "when Cheese() then end \n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL);
    assertEquals(3, kbuilder.getKnowledgePackages().size());
    for (KiePackage kp : kbuilder.getKnowledgePackages()) {
        KnowledgePackageImpl kpi = (KnowledgePackageImpl) kp;
        TypeDeclaration cheez = kpi.getTypeDeclaration("Cheese");
        if ("org.drools.compiler".equals(kpi.getName())) {
            assertNotNull(cheez);
        } else {
            assertNull(cheez);
        }
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Test(org.junit.Test)

Example 15 with KnowledgePackageImpl

use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.

the class DisposeCommandPublicAPITest method testDisposeCommand.

@Test
public void testDisposeCommand() {
    InternalKnowledgeBase kBase;
    RuleImpl rule;
    InternalKnowledgePackage pkg;
    kBase = KnowledgeBaseFactory.newKnowledgeBase();
    pkg = new KnowledgePackageImpl("org.droos.test");
    pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    JavaDialectRuntimeData data = new JavaDialectRuntimeData();
    data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
    pkg.getDialectRuntimeRegistry().setDialectData("java", data);
    rule = new RuleImpl("Test");
    rule.setDialect("java");
    rule.setConsequence(new Consequence() {

        public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
        }

        public String getName() {
            return "default";
        }
    });
    pkg.addRule(rule);
    kBase.addPackage(pkg);
    KieSession session = kBase.newKieSession();
    Command dispose = KieServices.Factory.get().getCommands().newDispose();
    session.insert("whatever");
    session.fireAllRules();
    session.execute(dispose);
    try {
        session.insert("whatever");
    } catch (Exception e) {
        Assert.assertEquals(e.getMessage(), "Illegal method call. This session was previously disposed.");
    }
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) Command(org.kie.api.command.Command) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) KieSession(org.kie.api.runtime.KieSession) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)41 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)26 Test (org.junit.Test)19 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)18 ClassObjectType (org.drools.core.base.ClassObjectType)13 Pattern (org.drools.core.rule.Pattern)11 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)9 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)9 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)8 WorkingMemory (org.drools.core.WorkingMemory)7 Consequence (org.drools.core.spi.Consequence)7 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)7 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)6 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)6 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)6 Declaration (org.drools.core.rule.Declaration)6 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)6 IOException (java.io.IOException)5 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)5 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)5