use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertionsBacking.
@Test(timeout = 10000)
public void testLogicalInsertionsBacking() throws Exception {
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsBacking.drl", getClass()), ResourceType.DRL);
Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kpkgs);
kbase = SerializationHelper.serializeObject(kbase);
KieSession session = createKnowledgeSession(kbase);
final Cheese cheese1 = new Cheese("c", 1);
final Cheese cheese2 = new Cheese(cheese1.getType(), 1);
FactHandle h1 = session.insert(cheese1);
session.fireAllRules();
session = getSerialisedStatefulKnowledgeSession(session, true);
Collection<?> list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
assertEquals(1, list.size());
// probably dangerous, as contains works with equals, not identity
assertEquals(cheese1.getType(), list.iterator().next());
FactHandle h2 = session.insert(cheese2);
session.fireAllRules();
session = getSerialisedStatefulKnowledgeSession(session, true);
list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
assertEquals(1, list.size());
assertEquals(cheese1.getType(), list.iterator().next());
assertEquals(3, session.getObjects().size());
h1 = getFactHandle(h1, session);
session.retract(h1);
session = getSerialisedStatefulKnowledgeSession(session, true);
session.fireAllRules();
session = getSerialisedStatefulKnowledgeSession(session, true);
list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
assertEquals("cheese-type " + cheese1.getType() + " was deleted, but should not. Backed by cheese2 => type.", 1, list.size());
assertEquals("cheese-type " + cheese1.getType() + " was deleted, but should not. Backed by cheese2 => type.", cheese1.getType(), list.iterator().next());
h2 = getFactHandle(h2, session);
session.retract(h2);
session = getSerialisedStatefulKnowledgeSession(session, true);
session.fireAllRules();
session = getSerialisedStatefulKnowledgeSession(session, true);
list = session.getObjects(new ClassObjectFilter(cheese1.getType().getClass()));
assertEquals("cheese-type " + cheese1.getType() + " was not deleted, but should have. Neither cheese1 => type nor cheese2 => type is true.", 0, list.size());
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class ImportsTest method testImportColision.
@Test
public void testImportColision() throws Exception {
final Collection<KiePackage> kpkgs1 = loadKnowledgePackages("nested1.drl");
final Collection<KiePackage> kpkgs2 = loadKnowledgePackages("nested2.drl");
final InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
kbase.addPackages(kpkgs1);
kbase.addPackages(kpkgs2);
final KieSession ksession = createKnowledgeSession(kbase);
SerializationHelper.serializeObject(kbase);
ksession.insert(new FirstClass());
ksession.insert(new SecondClass());
ksession.insert(new FirstClass.AlternativeKey());
ksession.insert(new SecondClass.AlternativeKey());
ksession.fireAllRules();
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class SerializedPackageMergeTwoSteps1Test method testBuildAndSerializePackagesInTwoSteps1.
@Test
public void testBuildAndSerializePackagesInTwoSteps1() throws IOException, ClassNotFoundException {
String str1 = "package com.sample.packageA\n" + "import org.drools.compiler.Person\n" + "global java.util.List list\n" + "rule R1 when\n" + " $p : Person( name == \"John\" )\n" + "then\n" + " list.add($p);" + "end\n";
String str2 = "package com.sample.packageB\n" + "import org.drools.compiler.Person\n" + "global java.util.List list\n" + "rule R2 when\n" + " $p : Person()\n" + "then\n" + " list.add($p);" + "end\n";
// Create 2 knowledgePackages separately
KnowledgeBuilder builder1 = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder1.add(ResourceFactory.newByteArrayResource(str1.getBytes()), ResourceType.DRL);
Collection<KiePackage> knowledgePackages1 = builder1.getKnowledgePackages();
// serialize the first package to a file
writeKnowledgePackage(knowledgePackages1, BINPKG[0]);
KnowledgeBuilder builder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder2.add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL);
Collection<KiePackage> knowledgePackages2 = builder2.getKnowledgePackages();
// serialize the second package to a file
writeKnowledgePackage(knowledgePackages2, BINPKG[1]);
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class SerializedPackageMergeTest method getSession.
private StatelessKieSession getSession(boolean serialize) throws Exception {
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
for (String drl : DRLs) {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream(drl)), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
Collection<KiePackage> newCollection = null;
if (serialize) {
newCollection = new ArrayList<KiePackage>();
for (KiePackage kpkg : kpkgs) {
kpkg = SerializationHelper.serializeObject(kpkg);
newCollection.add(kpkg);
}
} else {
newCollection = kpkgs;
}
kbase.addPackages(newCollection);
}
return kbase.newStatelessKieSession();
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class SerializedPackageMergeTwoSteps2Test method _deserializeFromBytes.
private Collection<KiePackage> _deserializeFromBytes(byte[] byteCode) {
Collection<KiePackage> ret = null;
ByteArrayInputStream bis = null;
ObjectInput in = null;
try {
bis = new ByteArrayInputStream(byteCode);
in = new DroolsObjectInputStream(bis);
ret = (Collection<KiePackage>) in.readObject();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (bis != null)
bis.close();
if (in != null)
in.close();
} catch (IOException e) {
}
}
return ret;
}
Aggregations