use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class MarshallingTest method testSerializeAdd.
@Test
public void testSerializeAdd() throws Exception {
Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Dynamic1.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
kBase.addPackages(kpkgs);
kBase = SerializationHelper.serializeObject(kBase);
KieSession session = kBase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
InternalFactHandle stilton = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
InternalFactHandle brie = (InternalFactHandle) session.insert(new Cheese("brie", 10));
session.fireAllRules();
assertEquals(list.size(), 1);
assertEquals("stilton", list.get(0));
// now recreate the rulebase, deserialize the session and test it
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
list = (List) session.getGlobal("list");
assertNotNull(list);
assertEquals(list.size(), 1);
assertEquals("stilton", list.get(0));
kpkgs = loadKnowledgePackages("../test_Dynamic3.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
kBase.addPackages(kpkgs);
InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 10));
InternalFactHandle bob = (InternalFactHandle) session.insert(new Person("bob", 30));
session.fireAllRules();
assertEquals(list.size(), 3);
assertEquals(bob.getObject(), list.get(2));
assertEquals("stilton", list.get(1));
session.dispose();
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class SerializedPackageMergeTest method testBuildAndSerializePackagesWithSamePackageName.
@Test
public void testBuildAndSerializePackagesWithSamePackageName() throws IOException, ClassNotFoundException {
// RHBRMS-2773
String str1 = "package com.sample\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\n" + "import org.drools.compiler.Person\n" + "global java.util.List list\n" + "rule R2 when\n" + " $p : Person( name == \"Paul\" )\n" + "then\n" + " list.add($p);" + "end\n";
// Create 2 knowledgePackages separately (but these rules have the same package name)
KnowledgeBuilder builder1 = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder1.add(ResourceFactory.newByteArrayResource(str1.getBytes()), ResourceType.DRL);
Collection<KiePackage> knowledgePackages1 = builder1.getKnowledgePackages();
KnowledgeBuilder builder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder2.add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL);
Collection<KiePackage> knowledgePackages2 = builder2.getKnowledgePackages();
// Combine the knowledgePackages
InternalKnowledgeBase knowledgeBase1 = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase1.addPackages(knowledgePackages1);
knowledgeBase1.addPackages(knowledgePackages2);
Collection<KiePackage> knowledgePackagesCombined = knowledgeBase1.getKiePackages();
// serialize
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new DroolsObjectOutputStream(baos);
out.writeObject(knowledgePackagesCombined);
out.flush();
out.close();
// deserialize
ObjectInputStream in = new DroolsObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
Collection<KiePackage> deserializedPackages = (Collection<KiePackage>) in.readObject();
// Use the deserialized knowledgePackages
InternalKnowledgeBase knowledgeBase2 = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase2.addPackages(deserializedPackages);
KieSession ksession = knowledgeBase2.newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert(new org.drools.compiler.Person("John"));
ksession.insert(new org.drools.compiler.Person("Paul"));
ksession.fireAllRules();
assertEquals(2, list.size());
ksession.dispose();
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class StatelessSessionTest method getSession2.
private StatelessKieSession getSession2(final Resource resource) throws Exception {
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(resource, ResourceType.DRL);
if (kbuilder.hasErrors()) {
System.out.println(kbuilder.getErrors());
}
assertFalse(kbuilder.hasErrors());
final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
kbase = SerializationHelper.serializeObject(kbase);
final StatelessKieSession session = kbase.newStatelessKieSession();
session.setGlobal("list", this.list);
session.setGlobal("cheesery", this.cheesery);
return session;
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class MarshallingTest method testSerializabilityWithJarFacts.
/**
* In this case we are dealing with facts which are not on the systems classpath.
*/
@Test
public void testSerializabilityWithJarFacts() throws Exception {
MapBackedClassLoader loader = new MapBackedClassLoader(this.getClass().getClassLoader());
JarInputStream jis = new JarInputStream(this.getClass().getResourceAsStream("/billasurf.jar"));
JarEntry entry = null;
byte[] buf = new byte[1024];
int len = 0;
while ((entry = jis.getNextJarEntry()) != null) {
if (!entry.isDirectory()) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((len = jis.read(buf)) >= 0) {
out.write(buf, 0, len);
}
loader.addResource(entry.getName(), out.toByteArray());
}
}
String drl = "package foo.bar \n" + "import com.billasurf.Board\n" + "rule 'MyGoodRule' \n dialect 'mvel' \n when " + " Board() " + "then \n" + " System.err.println(42); \n" + "end\n";
KnowledgeBuilderConfiguration kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader);
Collection<KiePackage> kpkgs = loadKnowledgePackagesFromString(kbuilderConf, drl);
kpkgs = SerializationHelper.serializeObject(kpkgs, loader);
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class MarshallingTest method testSignedSerialization4.
/*
* A client environment configured to use signed serialization
* should refuse any non-signed serialized rulebase
*/
@Test
public void testSignedSerialization4() throws Exception {
Collection<KiePackage> kpkgs = loadKnowledgePackages(null, false, "../test_Dynamic1_0.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
kBase.addPackages(kpkgs);
kBase = SerializationHelper.serializeObject(kBase);
try {
// set only the deserialisation properties, but not the serialization
setPublicKeyProperties();
kBase = SerializationHelper.serializeObject(kBase);
fail("Should not deserialize an unsigned rulebase on an environment configured to work with signed rulebases.");
} catch (Exception e) {
// success
} finally {
unsetPublicKeyProperties();
}
}
Aggregations