Search in sources :

Example 66 with ObjectOutput

use of java.io.ObjectOutput 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 67 with ObjectOutput

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

the class KnowledgeBaseImpl 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.
 */
public void writeExternal(final ObjectOutput out) throws IOException {
    ObjectOutput droolsStream;
    boolean isDrools = out instanceof DroolsObjectOutputStream;
    ByteArrayOutputStream bytes;
    out.writeBoolean(isDrools);
    if (isDrools) {
        droolsStream = out;
        bytes = null;
    } else {
        bytes = new ByteArrayOutputStream();
        droolsStream = new DroolsObjectOutputStream(bytes);
    }
    // must write this option first in order to properly deserialize later
    droolsStream.writeBoolean(this.config.isClassLoaderCacheEnabled());
    droolsStream.writeObject(((ProjectClassLoader) rootClassLoader).getStore());
    droolsStream.writeObject(this.config);
    droolsStream.writeObject(this.pkgs);
    // Rules must be restored by an ObjectInputStream that can resolve using a given ClassLoader to handle seaprately by storing as
    // a byte[]
    droolsStream.writeObject(this.id);
    droolsStream.writeInt(this.workingMemoryCounter.get());
    droolsStream.writeObject(this.processes);
    droolsStream.writeUTF(this.factHandleFactory.getClass().getName());
    droolsStream.writeObject(buildGlobalMapForSerialization());
    this.eventSupport.removeEventListener(KieBaseEventListener.class);
    droolsStream.writeObject(this.eventSupport);
    droolsStream.writeObject(this.reteooBuilder);
    droolsStream.writeObject(this.rete);
    droolsStream.writeObject(this.resolvedReleaseId);
    if (!isDrools) {
        droolsStream.flush();
        droolsStream.close();
        bytes.close();
        out.writeObject(bytes.toByteArray());
    }
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream)

Example 68 with ObjectOutput

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

the class FieldConstraintTest method testPredicateConstraint.

/**
 * <pre>
 *
 *                (Cheese (price ?price1 )
 *                (Cheese (price ?price2&amp;:(= ?price2 (* 2 ?price1) )
 *
 * </pre>
 */
@Test
public void testPredicateConstraint() {
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final InternalReadAccessor priceExtractor = store.getReader(Cheese.class, "price");
    Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    // Bind the extractor to a decleration
    // Declarations know the pattern they derive their value form
    final Declaration price1Declaration = new Declaration("price1", priceExtractor, pattern);
    pattern = new Pattern(1, new ClassObjectType(Cheese.class));
    // Bind the extractor to a decleration
    // Declarations know the pattern they derive their value form
    final Declaration price2Declaration = new Declaration("price2", priceExtractor, pattern);
    final PredicateExpression evaluator = new PredicateExpression() {

        private static final long serialVersionUID = 510l;

        public boolean evaluate(InternalFactHandle handle, Tuple tuple, Declaration[] previousDeclarations, Declaration[] localDeclarations, WorkingMemory workingMemory, Object context) {
            int price1 = previousDeclarations[0].getIntValue((InternalWorkingMemory) workingMemory, tuple.getObject(previousDeclarations[0]));
            int price2 = localDeclarations[0].getIntValue((InternalWorkingMemory) workingMemory, handle.getObject());
            return (price2 == (price1 * 2));
        }

        public Object createContext() {
            return null;
        }

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

        public void writeExternal(ObjectOutput out) throws IOException {
        }
    };
    final PredicateConstraint constraint1 = new PredicateConstraint(evaluator, new Declaration[] { price1Declaration }, new Declaration[] { price2Declaration });
    final Cheese cheddar0 = new Cheese("cheddar", 5);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar0);
    LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
    final Cheese cheddar1 = new Cheese("cheddar", 10);
    final InternalFactHandle f1 = (InternalFactHandle) ksession.insert(cheddar1);
    tuple = new LeftTupleImpl(tuple, new RightTupleImpl(f1, null), null, true);
    final PredicateContextEntry context = (PredicateContextEntry) constraint1.createContextEntry();
    context.updateFromTuple(ksession, tuple);
    assertTrue(constraint1.isAllowedCachedLeft(context, f1));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Cheese(org.drools.core.test.model.Cheese) PredicateExpression(org.drools.core.spi.PredicateExpression) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) PredicateContextEntry(org.drools.core.rule.PredicateConstraint.PredicateContextEntry) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) ObjectInput(java.io.ObjectInput) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 69 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 70 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 = (InternalKnowledgeBase) 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);
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
    final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
    pattern.addConstraint(constraint);
    rule1.addPattern(pattern);
    rule1.setConsequence(new Consequence() {

        private static final long serialVersionUID = 510l;

        public void evaluate(final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) 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);
    final FieldValue field2 = FieldFactory.getInstance().getFieldValue("stilton");
    final MvelConstraint constraint2 = new MvelConstraintTestUtil("type == \"stilton\"", field, extractor);
    pattern2.addConstraint(constraint2);
    rule2.addPattern(pattern2);
    rule2.setConsequence(new Consequence() {

        private static final long serialVersionUID = 510l;

        public void evaluate(final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {
        }

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

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    });
    pkg = new KnowledgePackageImpl("org.drools.test1");
    pkg.addRule(rule1);
    pkg.addRule(rule2);
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ClassFieldAccessorStore(org.drools.core.base.ClassFieldAccessorStore) IOException(java.io.IOException) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) IOException(java.io.IOException) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) ClassFieldReader(org.drools.core.base.ClassFieldReader) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) FieldValue(org.drools.core.spi.FieldValue) ObjectInput(java.io.ObjectInput) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) Before(org.junit.Before)

Aggregations

ObjectOutput (java.io.ObjectOutput)77 ObjectOutputStream (java.io.ObjectOutputStream)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 IOException (java.io.IOException)33 ObjectInput (java.io.ObjectInput)25 Test (org.junit.Test)20 ObjectInputStream (java.io.ObjectInputStream)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 WorkingMemory (org.drools.core.WorkingMemory)13 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)12 Pattern (org.drools.core.rule.Pattern)12 Consequence (org.drools.core.spi.Consequence)12 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)12 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 Declaration (org.drools.core.rule.Declaration)8 IntrospectionException (java.beans.IntrospectionException)7 InvalidRuleException (org.drools.core.rule.InvalidRuleException)7 ConsequenceException (org.drools.core.spi.ConsequenceException)7 OutputStream (java.io.OutputStream)6 ClassObjectType (org.drools.core.base.ClassObjectType)6