use of org.drools.core.common.DroolsObjectOutputStream in project drools by kiegroup.
the class EventAccessorRestoreTest method saveSession.
public void saveSession(FileOutputStream output, KieSession ksession) throws IOException {
DroolsObjectOutputStream droolsOut = new DroolsObjectOutputStream(output);
droolsOut.writeObject(ksession.getKieBase());
Marshaller mas = createMarshaller(ksession.getKieBase());
mas.marshall(droolsOut, ksession);
droolsOut.flush();
droolsOut.close();
}
use of org.drools.core.common.DroolsObjectOutputStream in project drools by kiegroup.
the class DMNModelImpl method writeExternal.
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(serializedAs);
out.writeObject(resource);
if (!(out instanceof DroolsObjectOutputStream)) {
throw new UnsupportedOperationException();
// TODO assume some defaults
}
DroolsObjectOutputStream os = (DroolsObjectOutputStream) out;
DMNCompilerImpl compiler = (DMNCompilerImpl) os.getCustomExtensions().get(DMNAssemblerService.DMN_COMPILER_CACHE_KEY);
List<DMNExtensionRegister> dmnRegisteredExtensions = compiler.getRegisteredExtensions();
String output = DMNMarshallerFactory.newMarshallerWithExtensions(dmnRegisteredExtensions).marshal(this.definitions);
out.writeObject(output);
}
use of org.drools.core.common.DroolsObjectOutputStream in project drools by kiegroup.
the class LargeRuleBase method bigBlobCompile.
private static void bigBlobCompile() throws DroolsParserException, IOException, Exception {
StringBuilder buf = new StringBuilder();
buf.append(getHeader());
for (int i = 0; i < 1; i++) {
String name = "x" + i;
int status = i;
String r = getTemplate1(name, status);
buf.append(r);
}
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(buf.toString().getBytes()), ResourceType.DRL);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
File f = new File("foo.rulebase");
if (f.exists())
f.delete();
ObjectOutput out = new DroolsObjectOutputStream(new FileOutputStream(f));
out.writeObject(kbase);
out.flush();
out.close();
ObjectInputStream in = new DroolsObjectInputStream(new FileInputStream(f));
KieBase rb_ = (KieBase) in.readObject();
}
use of org.drools.core.common.DroolsObjectOutputStream in project drools by kiegroup.
the class MarshallingIssuesTest method testMarshallWithAccumulate.
@Test
public void testMarshallWithAccumulate() throws Exception {
String drl1 = "import java.util.concurrent.atomic.AtomicInteger\n" + "global java.util.List list;\n" + "rule R when\n" + " $a : AtomicInteger( get() > 3 )\n" + " $i : Integer( this == $a.get() )\n" + " accumulate ( $s : String( length == $i ), $result : count( ) )\n" + "then\n" + " list.add($result);\n" + "end";
KieBase kb1 = new KieHelper().addContent(drl1, ResourceType.DRL).build();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DroolsObjectOutputStream oos = new DroolsObjectOutputStream(baos);
oos.writeObject(kb1);
oos.flush();
oos.close();
baos.flush();
baos.close();
byte[] serializedKb = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(serializedKb);
DroolsObjectInputStream ois = new DroolsObjectInputStream(bais);
ois.close();
bais.close();
KieBase kb2 = (KieBase) ois.readObject();
assertTrue(ReteComparator.areEqual(kb1, kb2));
}
use of org.drools.core.common.DroolsObjectOutputStream in project drools by kiegroup.
the class MarshallingTest method testDroolsObjectOutputInputStream.
/**
* test that creates a new knowledge base, new stateful session, inserts new
* fact, serializes the knowledge base and session and fact using one output
* stream then deserializes and verifies that fact in the session is the
* same as fact that was deserialized,
*
* from the ObjectOutputStream API: "Multiple references to a single object
* are encoded using a reference sharing mechanism so that graphs of objects
* can be restored to the same shape as when the original was written."
*
* This is still not fixed, as mentioned in the JIRA
*
* JBRULES-2048
*
* @throws Exception
*/
@Test
@Ignore
public void testDroolsObjectOutputInputStream() throws Exception {
KieBase kbase = loadKnowledgeBase("org/drools/compiler/integrationtests/test_Serializable.drl");
KieSession session = kbase.newKieSession();
Person bob = new Person();
session.insert(bob);
assertSame("these two object references should be same", bob, session.getObjects().iterator().next());
Marshaller marshaller = createSerializableMarshaller(kbase);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new DroolsObjectOutputStream(baos);
out.writeObject(bob);
out.writeObject(kbase);
marshaller.marshall(out, session);
out.flush();
out.close();
ObjectInputStream in = new DroolsObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
Person deserializedBob = (Person) in.readObject();
kbase = (InternalKnowledgeBase) in.readObject();
marshaller = createSerializableMarshaller(kbase);
session = (StatefulKnowledgeSession) marshaller.unmarshall(in);
assertSame("these two object references should be same", deserializedBob, session.getObjects().iterator().next());
in.close();
}
Aggregations