Search in sources :

Example 6 with DroolsObjectInputStream

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();
    }
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 7 with DroolsObjectInputStream

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;
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) DMNCompilerImpl(org.kie.dmn.core.compiler.DMNCompilerImpl) Definitions(org.kie.dmn.model.v1_1.Definitions) DMNExtensionRegister(org.kie.dmn.api.marshalling.v1_1.DMNExtensionRegister)

Example 8 with DroolsObjectInputStream

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();
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) ObjectOutput(java.io.ObjectOutput) FileInputStream(java.io.FileInputStream) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) FileOutputStream(java.io.FileOutputStream) File(java.io.File) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream)

Example 9 with DroolsObjectInputStream

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));
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) Test(org.junit.Test)

Example 10 with DroolsObjectInputStream

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();
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Marshaller(org.kie.api.marshalling.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Person(org.drools.compiler.Person) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

DroolsObjectInputStream (org.drools.core.common.DroolsObjectInputStream)16 ByteArrayInputStream (java.io.ByteArrayInputStream)12 DroolsObjectOutputStream (org.drools.core.common.DroolsObjectOutputStream)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Test (org.junit.Test)6 KieBase (org.kie.api.KieBase)6 ObjectInputStream (java.io.ObjectInputStream)5 KieSession (org.kie.api.runtime.KieSession)5 ObjectOutputStream (java.io.ObjectOutputStream)4 ArrayList (java.util.ArrayList)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 KiePackage (org.kie.api.definition.KiePackage)3 Marshaller (org.kie.api.marshalling.Marshaller)3 IOException (java.io.IOException)2 Collection (java.util.Collection)2 EOFException (java.io.EOFException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ObjectInput (java.io.ObjectInput)1