use of org.drools.core.common.DroolsObjectOutputStream in project drools by kiegroup.
the class KnowledgePackageImpl method writeExternal.
/**
* Handles the write serialization of the Package. Patterns in Rules may
* reference generated data which cannot be serialized by default methods.
* The Package uses PackageCompilationData to hold a reference to the
* generated bytecode. The generated bytecode must be restored before any
* Rules.
*
* @param stream out the stream to write the object to; should be an instance
* of DroolsObjectOutputStream or OutputStream
*/
public void writeExternal(ObjectOutput stream) throws IOException {
boolean isDroolsStream = stream instanceof DroolsObjectOutputStream;
ByteArrayOutputStream bytes = null;
ObjectOutput out;
if (isDroolsStream) {
out = stream;
} else {
bytes = new ByteArrayOutputStream();
out = new DroolsObjectOutputStream(bytes);
}
out.writeObject(this.name);
out.writeObject(this.classFieldAccessorStore);
out.writeObject(this.dialectRuntimeRegistry);
out.writeObject(this.typeDeclarations);
out.writeObject(this.imports);
out.writeObject(this.staticImports);
out.writeObject(this.functions);
out.writeObject(this.accumulateFunctions);
out.writeObject(this.factTemplates);
out.writeObject(this.ruleFlows);
out.writeObject(this.globals);
out.writeBoolean(this.valid);
out.writeBoolean(this.needStreamMode);
out.writeObject(this.rules);
out.writeObject(this.entryPointsIds);
out.writeObject(this.windowDeclarations);
out.writeObject(this.traitRegistry);
out.writeObject(this.resourceTypePackages);
// writing the whole stream as a byte array
if (!isDroolsStream) {
bytes.flush();
bytes.close();
stream.writeObject(bytes.toByteArray());
}
}
use of org.drools.core.common.DroolsObjectOutputStream in project drools by kiegroup.
the class MarshallingIssuesTest method testJBRULES_1946_2.
@Test
public void testJBRULES_1946_2() {
KieBase kbase = loadKnowledgeBase("../Sample.drl");
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DroolsObjectOutputStream oos = new DroolsObjectOutputStream(baos);
oos.writeObject(kbase);
oos.flush();
oos.close();
baos.flush();
baos.close();
byte[] serializedKb = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(serializedKb);
DroolsObjectInputStream ois = new DroolsObjectInputStream(bais);
KieBase kb2 = (KieBase) ois.readObject();
} catch (OptionalDataException ode) {
ode.printStackTrace();
fail("EOF? " + ode.eof);
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception: " + e.getMessage());
}
}
use of org.drools.core.common.DroolsObjectOutputStream in project drools by kiegroup.
the class MarshallingIssuesTest method testJBRULES_1946_3.
@Test
public void testJBRULES_1946_3() {
KieBase kbase = loadKnowledgeBase("../Sample.drl");
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DroolsObjectOutputStream oos = new DroolsObjectOutputStream(baos);
oos.writeObject(kbase);
oos.flush();
oos.close();
baos.flush();
baos.close();
byte[] serializedKb = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(serializedKb);
ObjectInputStream ois = new ObjectInputStream(bais);
KieBase kb2 = (KieBase) ois.readObject();
fail("Should have raised an IllegalArgumentException since the kbase was serialized with a Drools Stream but deserialized with a regular stream");
} catch (IllegalArgumentException ode) {
// success
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception: " + e.getMessage());
}
}
use of org.drools.core.common.DroolsObjectOutputStream in project drools by kiegroup.
the class SerializedPackageMergeTwoSteps1Test method writeKnowledgePackage.
public void writeKnowledgePackage(Collection<KiePackage> pkgs, String filePath) {
FileOutputStream fileOutStream = null;
DroolsObjectOutputStream out = null;
try {
fileOutStream = new FileOutputStream(filePath);
out = new DroolsObjectOutputStream(fileOutStream);
out.writeObject(pkgs);
out.flush();
} catch (Exception ex) {
// TODO
} finally {
try {
if (out != null)
out.close();
if (fileOutStream != null)
fileOutStream.close();
} catch (Exception e) {
}
}
}
use of org.drools.core.common.DroolsObjectOutputStream 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();
}
Aggregations