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);
}
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);
}
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);
}
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));
}
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));
}
Aggregations