Search in sources :

Example 31 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class SequentialTest method testSequentialWithRulebaseUpdate.

// JBRULES-1567 - ArrayIndexOutOfBoundsException in sequential execution after calling RuleBase.addPackage(..)
@Test
public void testSequentialWithRulebaseUpdate() throws Exception {
    final KieModule kieModule = KieUtil.getKieModuleFromClasspathResources("test", getClass(), kieBaseTestConfiguration, "simpleSalience.drl");
    final InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, SequentialOption.YES);
    StatelessKieSession ksession = kbase.newStatelessKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.execute(new Person("pob"));
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", DynamicRulesTest.class, kieBaseTestConfiguration, "test_Dynamic3.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    ksession = kbase.newStatelessKieSession();
    ksession.setGlobal("list", list);
    Person person = new Person("bop");
    ksession.execute(person);
    assertEquals(7, list.size());
    assertEquals("rule 3", list.get(0));
    assertEquals("rule 2", list.get(1));
    assertEquals("rule 1", list.get(2));
    assertEquals("rule 3", list.get(3));
    assertEquals("rule 2", list.get(4));
    assertEquals("rule 1", list.get(5));
    assertEquals(person, list.get(6));
}
Also used : DynamicRulesTest(org.drools.mvel.integrationtests.DynamicRulesTest) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) KieModule(org.kie.api.builder.KieModule) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) DynamicRulesTest(org.drools.mvel.integrationtests.DynamicRulesTest) Test(org.junit.Test)

Example 32 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class ReteEvaluatorTest method getKBase.

private InternalKnowledgeBase getKBase(String... stringRules) {
    KieServices ks = KieServices.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "kjar-test-" + UUID.randomUUID(), "1.0");
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writePomXML(getPom(releaseId));
    for (int i = 0; i < stringRules.length; i++) {
        kfs.write(String.format("src/main/resources/r%d.drl", i), stringRules[i]);
    }
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll(ExecutableModelProject.class);
    List<Message> messages = kieBuilder.getResults().getMessages();
    if (!messages.isEmpty()) {
        fail(messages.toString());
    }
    return (InternalKnowledgeBase) ks.newKieContainer(releaseId).getKieBase();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

Example 33 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class CommonTraitTest method loadKnowledgeBaseFromString.

protected KieBase loadKnowledgeBaseFromString(String... drlContentStrings) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    for (String drlContentString : drlContentStrings) {
        kbuilder.add(ResourceFactory.newByteArrayResource(drlContentString.getBytes()), ResourceType.DRL);
    }
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return kbase;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

Example 34 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class TraitTest method testWrapperSize.

@Test
public void testWrapperSize() {
    String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Resource res = ResourceFactory.newClassPathResource(source);
    assertNotNull(res);
    kbuilder.add(res, ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
    kb.addPackages(kbuilder.getKnowledgePackages());
    TraitFactoryImpl.setMode(mode, kb);
    TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb);
    try {
        FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
        TraitableBean imp = (TraitableBean) impClass.newInstance();
        FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = traitClass.getFactClass();
        TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait);
        Map<String, Object> virtualFields = imp._getDynamicProperties();
        Map<String, Object> wrapper = proxy.getFields();
        assertEquals(3, wrapper.size());
        assertEquals(1, virtualFields.size());
        impClass.set(imp, "name", "john");
        assertEquals(3, wrapper.size());
        assertEquals(1, virtualFields.size());
        proxy.getFields().put("school", "skol");
        assertEquals(3, wrapper.size());
        assertEquals(1, virtualFields.size());
        proxy.getFields().put("surname", "xxx");
        assertEquals(4, wrapper.size());
        assertEquals(2, virtualFields.size());
        // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
        // TraitableBean ind = (TraitableBean) indClass.newInstance();
        TraitableBean ind = new Entity();
        TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait);
        Map virtualFields2 = ind._getDynamicProperties();
        Map wrapper2 = proxy2.getFields();
        assertEquals(3, wrapper2.size());
        assertEquals(3, virtualFields2.size());
        traitClass.set(proxy2, "name", "john");
        assertEquals(3, wrapper2.size());
        assertEquals(3, virtualFields2.size());
        proxy2.getFields().put("school", "skol");
        assertEquals(3, wrapper2.size());
        assertEquals(3, virtualFields2.size());
        proxy2.getFields().put("surname", "xxx");
        assertEquals(4, wrapper2.size());
        assertEquals(4, virtualFields2.size());
        FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role");
        Class trait2 = traitClass2.getFactClass();
        // TraitableBean ind2 = (TraitableBean) indClass.newInstance();
        TraitableBean ind2 = new Entity();
        TraitProxyImpl proxy99 = (TraitProxyImpl) tFactory.getProxy(ind2, trait2);
        proxy99.getFields().put("surname", "xxx");
        proxy99.getFields().put("name", "xyz");
        proxy99.getFields().put("school", "skol");
        assertEquals(3, proxy99.getFields().size());
        TraitProxyImpl proxy100 = (TraitProxyImpl) tFactory.getProxy(ind2, trait);
        assertEquals(4, proxy100.getFields().size());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) Entity(org.drools.traits.core.factmodel.Entity) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) LogicalTypeInconsistencyException(org.drools.traits.core.factmodel.LogicalTypeInconsistencyException) FactType(org.kie.api.definition.type.FactType) TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Map(java.util.Map) HashMap(java.util.HashMap) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 35 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class TraitTypeGenerationTest method testMultipleInheritanceWithPosition4.

@Test
public void testMultipleInheritanceWithPosition4() throws Exception {
    // DROOLS-249
    String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 @position(1)\n" + " field1 : int = 101 @position(0)\n" + " field2 : int = 102 @position(0)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 @position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end";
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
    }
    assertFalse(kBuilder.hasErrors());
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
    FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait");
    assertEquals(5, sw.getFields().size());
    assertEquals("field1", sw.getFields().get(0).getName());
    assertEquals("field2", sw.getFields().get(1).getName());
    assertEquals("mfield0", sw.getFields().get(2).getName());
    assertEquals("field0", sw.getFields().get(3).getName());
    assertEquals("mfield1", sw.getFields().get(4).getName());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Aggregations

InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)268 Test (org.junit.Test)213 KieSession (org.kie.api.runtime.KieSession)136 ArrayList (java.util.ArrayList)99 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)98 KiePackage (org.kie.api.definition.KiePackage)82 List (java.util.List)55 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)39 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)34 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)33 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)24 Cheese (org.drools.mvel.compiler.Cheese)24 FactType (org.kie.api.definition.type.FactType)23 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)22 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)21 BetaMemory (org.drools.core.reteoo.BetaMemory)18 PackageDescr (org.drools.drl.ast.descr.PackageDescr)18 Resource (org.kie.api.io.Resource)18 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)17