use of org.kie.api.KieBase in project drools by kiegroup.
the class AbstractConcurrentTest method serializeAndDeserializeKieBase.
private KieBase serializeAndDeserializeKieBase(final KieBase kieBase) {
try {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream out = new ObjectOutputStream(baos)) {
out.writeObject(kieBase);
out.flush();
}
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
return (KieBase) in.readObject();
}
} catch (final IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class TypeDeclarationTest method testCircularDeclaration.
@Test
public void testCircularDeclaration() throws Exception {
String rule = "package org.drools.compiler.test\n" + "declare FactA\n" + " fieldB: FactB\n" + "end\n" + "declare FactB\n" + " fieldA: FactA\n" + "end\n" + "rule R1 when\n" + " $fieldA : FactA( $fieldB : fieldB )\n" + " FactB( this == $fieldB, fieldA == $fieldA )\n" + "then\n" + "end";
KieBase kbase = new KieHelper().addContent(rule, ResourceType.DRL).build();
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);
ksession.insert(a);
ksession.insert(b);
int rules = ksession.fireAllRules();
assertEquals(1, rules);
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class TypeDeclarationTest method testCircularDeclarationWithExtension.
@Test
public void testCircularDeclarationWithExtension() throws Exception {
// DROOLS-640
String drl = "package org.drools.compiler.test\n" + "declare FactA\n" + " fieldB: FactB\n" + "end\n" + "declare FactB extends FactA end\n" + "rule R1 when\n" + " $a : FactA( )\n" + " $b : FactB( this == $a.fieldB )\n" + "then\n" + "end";
KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
FactType aType = kbase.getFactType("org.drools.compiler.test", "FactA");
FactType bType = kbase.getFactType("org.drools.compiler.test", "FactB");
Object a = aType.newInstance();
Object b = bType.newInstance();
aType.set(a, "fieldB", b);
ksession.insert(a);
ksession.insert(b);
int rules = ksession.fireAllRules();
assertEquals(1, rules);
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class TypeDeclarationTest method testRecursiveDeclaration.
@Test
public void testRecursiveDeclaration() throws Exception {
String rule = "package org.drools.compiler\n" + "declare Node\n" + " value: String\n" + " parent: Node\n" + "end\n" + "rule R1 when\n" + " $parent: Node( value == \"parent\" )\n" + " $child: Node( $value : value, parent == $parent )\n" + "then\n" + " System.out.println( $value );\n" + "end";
KieBase kbase = new KieHelper().addContent(rule, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
FactType nodeType = kbase.getFactType("org.drools.compiler", "Node");
Object parent = nodeType.newInstance();
nodeType.set(parent, "value", "parent");
ksession.insert(parent);
Object child = nodeType.newInstance();
nodeType.set(child, "value", "child");
nodeType.set(child, "parent", parent);
ksession.insert(child);
int rules = ksession.fireAllRules();
assertEquals(1, rules);
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class UnmarshallingTest method testMarshallWithNot.
@Test
public void testMarshallWithNot() throws Exception {
String whenBenNotVilgaxRule = "import " + getClass().getCanonicalName() + ".*\n" + "rule one\n" + "when\n" + " Ben()\n" + " not(Vilgax())\n" + "then\n" + " //System.out.println(\"Ben!\");\n" + "end\n" + "\n" + "rule two\n" + "when\n" + " Ben()\n" + "then\n" + " //System.out.println(\"Vilgax..\");\n" + "end\n";
KieBase knowledgeBase = initializeKnowledgeBase(whenBenNotVilgaxRule);
// Initialize Knowledge session and insert Ben
KieSession ksession = knowledgeBase.newKieSession();
ksession.insert(new Ben());
// Marshall
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MarshallerFactory.newMarshaller(knowledgeBase).marshall(baos, ksession);
// Clean up
// - mimicing when a session is reloaded from a database.
ksession.dispose();
// Re-initialize
knowledgeBase = initializeKnowledgeBase(whenBenNotVilgaxRule);
// Unmarshall
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try {
ksession = MarshallerFactory.newMarshaller(knowledgeBase).unmarshall(bais);
} catch (Throwable t) {
t.printStackTrace();
fail(t.getClass().getSimpleName() + " thrown when trying to unmarshall (see stack trace in output).");
}
int rules = ksession.fireAllRules();
Assert.assertEquals(2, rules);
}
Aggregations