Search in sources :

Example 96 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class KieCompilationCacheTest method testCompilationCache.

@Test
public void testCompilationCache() throws Exception {
    String drl = "package org.drools.compiler\n" + "declare type X\n" + "    foo : String\n" + "end\n" + "rule R1 when\n" + "   $m : X( foo == \"Hello World\" )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    ks.newKieBuilder(kfs).buildAll();
    ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
    InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
    byte[] jar = kieModule.getBytes();
    MemoryFileSystem mfs = MemoryFileSystem.readFromJar(jar);
    File file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase1"));
    assertNotNull(file);
    Resource jarRes = ks.getResources().newByteArrayResource(jar);
    KieModule km = ks.getRepository().addKieModule(jarRes);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieBase kbase = kc.getKieBase();
    FactType type = kbase.getFactType("org.drools.compiler", "X");
    FactField foo = type.getField("foo");
    Object x = type.newInstance();
    foo.set(x, "Hello World");
    KieSession ksession = kc.newKieSession();
    ksession.insert(x);
    int count = ksession.fireAllRules();
    assertEquals(1, count);
}
Also used : FactField(org.kie.api.definition.type.FactField) KieFileSystem(org.kie.api.builder.KieFileSystem) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) FactType(org.kie.api.definition.type.FactType) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) File(org.drools.compiler.compiler.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 97 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class KieHelloWorldTest method testHelloWorldWithKBaseInclude.

@Test
public void testHelloWorldWithKBaseInclude() throws Exception {
    String drl = "package org.drools.compiler.integrationtests\n" + "declare CancelFact\n" + " cancel : boolean = true\n" + "end\n" + "rule R1 when\n" + " $m : CancelFact( cancel == true )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    KieModuleModel module = ks.newKieModuleModel();
    // define first kbase
    final String defaultBaseName = "defaultKBase";
    KieBaseModel defaultBase = module.newKieBaseModel(defaultBaseName);
    defaultBase.setDefault(true);
    defaultBase.addPackage("*");
    defaultBase.newKieSessionModel("defaultKSession").setDefault(true);
    // define second kbase including resources of the first one
    final String includingBaseName = "includingKBase";
    KieBaseModel includingBase = module.newKieBaseModel(includingBaseName);
    includingBase.setDefault(false);
    includingBase.addInclude(defaultBaseName);
    includingBase.newKieSessionModel("includingKSession").setDefault(false);
    kfs.writeKModuleXML(module.toXML());
    KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, kb.getResults().getMessages().size());
    KieSession ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    FactType factType = ksession.getKieBase().getFactType("org.drools.compiler.integrationtests", "CancelFact");
    assertNotNull(factType);
    ksession.insert(factType.newInstance());
    int count = ksession.fireAllRules();
    assertEquals(1, count);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 98 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class KnowledgeBuilderTest method testCompositeKnowledgeBuilder.

@Test
public void testCompositeKnowledgeBuilder() throws Exception {
    String rule = "package org.drools.compiler.test\n" + "rule R1 when\n" + "   $fieldA : FactA( $fieldB : fieldB )\n" + "   FactB( this == $fieldB, fieldA == $fieldA )\n" + "then\n" + "end";
    String declarationA = "package org.drools.compiler.test\n" + "declare FactA\n" + "    fieldB: FactB\n" + "end\n";
    String declarationB = "package org.drools.compiler.test\n" + "declare FactB\n" + "    fieldA: FactA\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(declarationA.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(declarationB.getBytes()), ResourceType.DRL);
    assertTrue(kbuilder.hasErrors());
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(declarationA.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(declarationB.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
    assertTrue(kbuilder.hasErrors());
    KnowledgeBuilder kbuilder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder2.batch().add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL).add(ResourceFactory.newByteArrayResource(declarationA.getBytes()), ResourceType.DRL).add(ResourceFactory.newByteArrayResource(declarationB.getBytes()), ResourceType.DRL).build();
    if (kbuilder2.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder2.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    FactType aType = kbase.getFactType("org.drools.compiler.test", "FactA");
    Object a = aType.newInstance();
    FactType bType = kbase.getFactType("org.drools.compiler.test", "FactB");
    Object b = bType.newInstance();
    aType.set(a, "fieldB", b);
    bType.set(b, "fieldA", a);
    // JBRULES-3683 - check that the recurisive type declaration doesn't cause a StackOverflowError
    a.toString();
    b.toString();
    ksession.insert(a);
    ksession.insert(b);
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 99 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class KnowledgeBuilderTest method testUndoTypeDeclaration.

@Test
@Ignore("All the classes generated by type declarations are now defined in the ProjectClassLoader")
public void testUndoTypeDeclaration() throws Exception {
    String rule = "package org.drools.compiler.test\n" + "import org.drools.compiler.test.FactA\n" + "import org.drools.compiler.test.FactB\n" + "rule R1 when\n" + "   FactA( i == 1 )\n" + "   FactB( i == 1 )\n" + "then\n" + "   list.add(\"OK\");" + "end\n";
    String declarationA = "package org.drools.compiler.test\n" + "global java.util.List list\n" + "declare FactA\n" + "    j : int\n" + "end\n";
    String declarationB = "package org.drools.compiler.test\n" + "declare FactB\n" + "    i : int\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(declarationB.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.hasErrors());
    kbuilder.batch().type(ResourceType.DRL).add(ResourceFactory.newByteArrayResource(rule.getBytes())).add(ResourceFactory.newByteArrayResource(declarationA.getBytes())).build();
    assertTrue(kbuilder.hasErrors());
    kbuilder.undo();
    assertFalse(kbuilder.hasErrors());
    declarationA = "package org.drools.compiler.test\n" + "global java.util.List list\n" + "declare FactA\n" + "    i : int\n" + "end\n";
    kbuilder.batch().type(ResourceType.DRL).add(ResourceFactory.newByteArrayResource(rule.getBytes())).add(ResourceFactory.newByteArrayResource(declarationA.getBytes())).build();
    assertFalse(kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    FactType aType = kbase.getFactType("org.drools.compiler.test", "FactA");
    Object a = aType.newInstance();
    aType.set(a, "i", 1);
    FactType bType = kbase.getFactType("org.drools.compiler.test", "FactB");
    Object b = bType.newInstance();
    bType.set(b, "i", 1);
    ksession.insert(a);
    ksession.insert(b);
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
    assertEquals("OK", list.get(0));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 100 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class KnowledgeBuilderTest method testUndoRule.

@Test
public void testUndoRule() throws Exception {
    String rule = "package org.drools.compiler.test\n" + "global java.util.List list\n" + "import org.drools.compiler.test.FactA\n" + "import org.drools.compiler.test.FactB\n" + "rule R1 when\n" + "   FactA( j == 1 )\n" + "   FactB( i == 1 )\n" + "then\n" + "   list.add(\"OK\");" + "end\n";
    String declarationA = "package org.drools.compiler.test\n" + "declare FactA\n" + "    i : int\n" + "end\n";
    String declarationB = "package org.drools.compiler.test\n" + "declare FactB\n" + "    i : int\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(declarationB.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(declarationA.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.hasErrors());
    kbuilder.batch().type(ResourceType.DRL).add(ResourceFactory.newByteArrayResource(rule.getBytes())).build();
    assertTrue(kbuilder.hasErrors());
    kbuilder.undo();
    assertFalse(kbuilder.hasErrors());
    rule = "package org.drools.compiler.test\n" + "global java.util.List list\n" + "import org.drools.compiler.test.FactA\n" + "import org.drools.compiler.test.FactB\n" + "rule R1 when\n" + "   FactA( i == 1 )\n" + "   FactB( i == 1 )\n" + "then\n" + "   list.add(\"OK\");" + "end\n";
    kbuilder.batch().type(ResourceType.DRL).add(ResourceFactory.newByteArrayResource(rule.getBytes())).add(ResourceFactory.newByteArrayResource(declarationA.getBytes())).build();
    assertFalse(kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    FactType aType = kbase.getFactType("org.drools.compiler.test", "FactA");
    Object a = aType.newInstance();
    aType.set(a, "i", 1);
    FactType bType = kbase.getFactType("org.drools.compiler.test", "FactB");
    Object b = bType.newInstance();
    bType.set(b, "i", 1);
    ksession.insert(a);
    ksession.insert(b);
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
    assertEquals("OK", list.get(0));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Aggregations

FactType (org.kie.api.definition.type.FactType)208 Test (org.junit.Test)193 KieSession (org.kie.api.runtime.KieSession)114 KieBase (org.kie.api.KieBase)63 DroolsAbstractPMMLTest (org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest)45 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)31 ArrayList (java.util.ArrayList)28 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)27 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)27 KieServices (org.kie.api.KieServices)22 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)18 Resource (org.kie.api.io.Resource)17 KieBuilder (org.kie.api.builder.KieBuilder)16 ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)16 KieFileSystem (org.kie.api.builder.KieFileSystem)15 KieContainer (org.kie.api.runtime.KieContainer)15 List (java.util.List)10 Results (org.kie.api.builder.Results)10 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)9 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)9