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();
}
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());
}
}
use of java.io.ObjectOutput in project drools by kiegroup.
the class FieldConstraintTest method testPredicateConstraint.
/**
* <pre>
*
* (Cheese (price ?price1 )
* (Cheese (price ?price2&:(= ?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));
}
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();
}
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);
}
Aggregations