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 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());
}
}
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());
}
}
}
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());
}
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);
}
Aggregations