Search in sources :

Example 91 with ObjectOutput

use of java.io.ObjectOutput in project drools by kiegroup.

the class DroolsObjectIOTest method serialize.

private static byte[] serialize(Object obj) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    ObjectOutput out = new DroolsObjectOutputStream(bytes);
    out.writeObject(obj);
    out.flush();
    out.close();
    return bytes.toByteArray();
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 92 with ObjectOutput

use of java.io.ObjectOutput in project drools by kiegroup.

the class JavaDialectRuntimeData method writeExternal.

/**
 * Handles the write serialization of the PackageCompilationData. Patterns in Rules may reference generated data which cannot be serialized by
 * default methods. The PackageCompilationData holds a reference to the generated bytecode. The generated bytecode must be restored before any Rules.
 */
public void writeExternal(ObjectOutput stream) throws IOException {
    KeyStoreHelper helper = KeyStoreHelper.get();
    stream.writeBoolean(helper.isSigned());
    if (helper.isSigned()) {
        stream.writeObject(helper.getPvtKeyAlias());
    }
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(bos);
    out.writeInt(this.store.size());
    for (Entry<String, byte[]> entry : this.store.entrySet()) {
        out.writeObject(entry.getKey());
        out.writeObject(entry.getValue());
    }
    out.flush();
    out.close();
    byte[] buff = bos.toByteArray();
    stream.writeObject(buff);
    if (helper.isSigned()) {
        sign(stream, helper, buff);
    }
    stream.writeInt(this.invokerLookups.size());
    for (Entry<String, Wireable> entry : this.invokerLookups.entrySet()) {
        stream.writeObject(entry.getKey());
        stream.writeObject(entry.getValue());
    }
    stream.writeInt(this.classLookups.size());
    for (Entry<String, byte[]> entry : this.classLookups.entrySet()) {
        stream.writeObject(entry.getKey());
        stream.writeObject(entry.getValue());
    }
}
Also used : ObjectOutput(java.io.ObjectOutput) KeyStoreHelper(org.drools.core.util.KeyStoreHelper) Wireable(org.drools.core.spi.Wireable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 93 with ObjectOutput

use of java.io.ObjectOutput in project drools by kiegroup.

the class MVELKnowledgePackageImpl 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 {
    ByteArrayOutputStream bytes = null;
    ObjectOutput out;
    if (stream instanceof DroolsObjectOutputStream) {
        out = stream;
    } else {
        bytes = new ByteArrayOutputStream();
        out = new DroolsObjectOutputStream(bytes);
    }
    try {
        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.globals);
        out.writeBoolean(this.valid);
        out.writeBoolean(this.needStreamMode);
        out.writeObject(this.rules);
        out.writeObject(this.entryPointsIds);
        out.writeObject(this.windowDeclarations);
        out.writeObject(this.resourceTypePackages);
    } finally {
        // writing the whole stream as a byte array
        if (bytes != null) {
            bytes.flush();
            bytes.close();
            stream.writeObject(bytes.toByteArray());
        }
    }
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream)

Example 94 with ObjectOutput

use of java.io.ObjectOutput in project drools by kiegroup.

the class PackageDescrTest method createPackageDescrWithTypeDeclarationDescr.

@Test
public void createPackageDescrWithTypeDeclarationDescr() throws IOException {
    PackageDescrBuilder builder = PackageDescrBuilderImpl.newPackage();
    builder.newDeclare().type().name("java.lang.String");
    PackageDescr descr = builder.getDescr();
    OutputStream os = new ByteArrayOutputStream();
    ObjectOutput oo = new ObjectOutputStream(os);
    descr.writeExternal(oo);
    assertNotNull(os.toString());
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) ObjectOutput(java.io.ObjectOutput) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ObjectOutputStream(java.io.ObjectOutputStream) Test(org.junit.Test)

Example 95 with ObjectOutput

use of java.io.ObjectOutput in project drools by kiegroup.

the class RuleBaseEventSupportTest method setUp.

/* (non-Javadoc)
     * @see junit.framework.TestCase#setUp()
     */
@Before
public void setUp() throws Exception {
    kBase = KnowledgeBaseFactory.newKnowledgeBase();
    ;
    listener1 = new TestRuleBaseListener("(listener-1) ");
    listener2 = new TestRuleBaseListener("(listener-2) ");
    kBase.addEventListener(listener1);
    kBase.addEventListener(listener2);
    final RuleImpl rule1 = new RuleImpl("test1");
    final ClassObjectType cheeseObjectType = new ClassObjectType(Cheese.class);
    final Pattern pattern = new Pattern(0, cheeseObjectType);
    ClassFieldAccessorStore store = new ClassFieldAccessorStore();
    store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    store.setEagerWire(true);
    AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheeseTypeEqualsConstraint(store, "cheddar", useLambdaConstraint);
    pattern.addConstraint(constraint);
    rule1.addPattern(pattern);
    rule1.setConsequence(new Consequence() {

        private static final long serialVersionUID = 510l;

        public void evaluate(final KnowledgeHelper knowledgeHelper, final ReteEvaluator reteEvaluator) throws Exception {
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    });
    final RuleImpl rule2 = new RuleImpl("test2");
    final ClassObjectType cheeseObjectType2 = new ClassObjectType(Cheese.class);
    final Pattern pattern2 = new Pattern(0, cheeseObjectType2);
    AlphaNodeFieldConstraint constraint2 = ConstraintTestUtil.createCheeseTypeEqualsConstraint(store, "stilton", useLambdaConstraint);
    pattern2.addConstraint(constraint2);
    rule2.addPattern(pattern2);
    rule2.setConsequence(new Consequence() {

        private static final long serialVersionUID = 510l;

        public void evaluate(final KnowledgeHelper knowledgeHelper, final ReteEvaluator reteEvaluator) throws Exception {
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    });
    pkg = CoreComponentFactory.get().createKnowledgePackage("org.drools.test1");
    pkg.addRule(rule1);
    pkg.addRule(rule2);
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ClassFieldAccessorStore(org.drools.mvel.accessors.ClassFieldAccessorStore) IOException(java.io.IOException) IOException(java.io.IOException) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) ObjectInput(java.io.ObjectInput) Before(org.junit.Before)

Aggregations

ObjectOutput (java.io.ObjectOutput)123 ObjectOutputStream (java.io.ObjectOutputStream)88 ByteArrayOutputStream (java.io.ByteArrayOutputStream)76 IOException (java.io.IOException)63 ObjectInput (java.io.ObjectInput)38 ObjectInputStream (java.io.ObjectInputStream)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26 Test (org.junit.Test)23 Pattern (org.drools.core.rule.Pattern)16 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)15 Consequence (org.drools.core.spi.Consequence)15 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)15 FileOutputStream (java.io.FileOutputStream)13 OutputStream (java.io.OutputStream)12 WorkingMemory (org.drools.core.WorkingMemory)12 ClassObjectType (org.drools.core.base.ClassObjectType)10 Declaration (org.drools.core.rule.Declaration)10 Test (org.junit.jupiter.api.Test)9 File (java.io.File)8 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8