use of org.drools.core.common.DroolsObjectInputStream in project drools by kiegroup.
the class ReteooBuilder method readExternal.
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
boolean isDrools = in instanceof DroolsObjectInputStream;
DroolsObjectInputStream droolsStream;
ByteArrayInputStream bytes;
if (isDrools) {
bytes = null;
droolsStream = (DroolsObjectInputStream) in;
} else {
bytes = new ByteArrayInputStream((byte[]) in.readObject());
droolsStream = new DroolsObjectInputStream(bytes);
}
this.rules = (Map<String, BaseNode[]>) droolsStream.readObject();
this.queries = (Map<String, BaseNode[]>) droolsStream.readObject();
this.namedWindows = (Map<String, WindowNode>) droolsStream.readObject();
this.idGenerator = (IdGenerator) droolsStream.readObject();
if (!isDrools) {
droolsStream.close();
bytes.close();
}
}
use of org.drools.core.common.DroolsObjectInputStream in project drools by kiegroup.
the class DMNModelImpl method readExternal.
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
this.serializedAs = (SerializationFormat) in.readObject();
this.resource = (Resource) in.readObject();
String xml = (String) in.readObject();
if (!(in instanceof DroolsObjectInputStream)) {
throw new UnsupportedOperationException();
// TODO assume some defaults
}
DroolsObjectInputStream is = (DroolsObjectInputStream) in;
DMNCompilerImpl compiler = (DMNCompilerImpl) is.getCustomExtensions().get(DMNAssemblerService.DMN_COMPILER_CACHE_KEY);
List<DMNExtensionRegister> dmnRegisteredExtensions = compiler.getRegisteredExtensions();
Definitions definitions = DMNMarshallerFactory.newMarshallerWithExtensions(dmnRegisteredExtensions).unmarshal(xml);
this.definitions = definitions;
DMNModelImpl compiledModel = (DMNModelImpl) compiler.compile(definitions);
this.inputs = compiledModel.inputs;
this.decisions = compiledModel.decisions;
this.bkms = compiledModel.bkms;
this.itemDefs = compiledModel.itemDefs;
this.messages = compiledModel.messages;
this.types = compiledModel.types;
this.runtimeTypeCheck = compiledModel.runtimeTypeCheck;
}
use of org.drools.core.common.DroolsObjectInputStream 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.DroolsObjectInputStream 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.DroolsObjectInputStream 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