use of java.io.ObjectOutput in project drools by kiegroup.
the class CrossProductTest method setUp.
@Before
public void setUp() throws Exception {
final ObjectType list1ObjectType = new ClassObjectType(String.class);
final ObjectType list2ObjectType = new ClassObjectType(String.class);
final RuleImpl rule = new RuleImpl("rule-1");
final Pattern list1Pattern = new Pattern(0, list1ObjectType, "s1");
final Pattern list2Pattern = new Pattern(1, list2ObjectType, "s2");
rule.addPattern(list1Pattern);
rule.addPattern(list2Pattern);
final Declaration s1Declaration = rule.getDeclaration("s1");
final Declaration s2Declaration = rule.getDeclaration("s2");
this.values = new ArrayList();
rule.setConsequence(new Consequence() {
private static final long serialVersionUID = 510l;
public void evaluate(final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {
final String s1 = (String) knowledgeHelper.get(s1Declaration);
final String s2 = (String) knowledgeHelper.get(s2Declaration);
CrossProductTest.this.values.add(new String[] { s1, s2 });
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
});
this.pkg = new KnowledgePackageImpl("org.drools");
this.pkg.addRule(rule);
}
use of java.io.ObjectOutput in project drools by kiegroup.
the class ReteooRuleBuilderTest method testAddRuleWithPatterns.
@Test
public void testAddRuleWithPatterns() {
final RuleImpl rule = new RuleImpl("only patterns");
final Pattern c1 = new Pattern(0, new ClassObjectType(String.class));
final Pattern c2 = new Pattern(1, new ClassObjectType(String.class));
final Pattern c3 = new Pattern(2, new ClassObjectType(String.class));
final GroupElement lhsroot = GroupElementFactory.newAndInstance();
lhsroot.addChild(c1);
lhsroot.addChild(c2);
lhsroot.addChild(c3);
rule.setLhs(lhsroot);
final Consequence consequence = new Consequence() {
public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
System.out.println("Consequence!");
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
};
rule.setConsequence(consequence);
final List terminals = this.builder.addRule(rule, this.rulebase);
assertEquals("Rule must have a single terminal node", 1, terminals.size());
final RuleTerminalNode terminal = (RuleTerminalNode) terminals.get(0);
}
use of java.io.ObjectOutput in project drools by kiegroup.
the class ProcessContextTest method testProcessContextGetAssignment.
@Test
public void testProcessContextGetAssignment() {
KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
KieSession ksession = kbase.newKieSession();
assertNotNull(ksession);
CaseInformation caseInfo = new CaseInformation();
caseInfo.assign("owner", new OrganizationalEntity() {
@Override
public String getId() {
return "testUser";
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
});
ksession.insert(caseInfo);
ProcessContext processContext = new ProcessContext(ksession);
CaseAssignment caseAssignment = processContext.getCaseAssignment();
assertNotNull(caseAssignment);
Collection<OrganizationalEntity> forRole = caseAssignment.getAssignments("owner");
assertNotNull(forRole);
assertEquals(1, forRole.size());
}
use of java.io.ObjectOutput in project kie-wb-common by kiegroup.
the class KieAfterDecorator method readObjectFromADifferentClassloader.
private KieTuple readObjectFromADifferentClassloader(Object o) {
ObjectInput in = null;
ObjectOutput out;
ByteArrayInputStream bis;
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();
out = new ObjectOutputStream(bos);
out.writeObject(o);
out.flush();
byte[] objBytes = bos.toByteArray();
bis = new ByteArrayInputStream(objBytes);
in = new ObjectInputStream(bis);
Object newObj = in.readObject();
return new KieTuple(newObj);
} catch (NotSerializableException nse) {
nse.printStackTrace();
StringBuilder sb = new StringBuilder("NotSerializableException:").append(nse.getMessage());
return new KieTuple(sb.toString());
} catch (IOException ioe) {
StringBuilder sb = new StringBuilder("IOException:").append(ioe.getMessage());
return new KieTuple(sb.toString());
} catch (ClassNotFoundException cnfe) {
StringBuilder sb = new StringBuilder("ClassNotFoundException:").append(cnfe.getMessage());
return new KieTuple(sb.toString());
} catch (Exception e) {
StringBuilder sb = new StringBuilder("Exception:").append(e.getMessage());
return new KieTuple(sb.toString());
} finally {
try {
if (bos != null) {
bos.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
logger.error(ex.getMessage());
}
}
}
use of java.io.ObjectOutput in project instructure-android by instructure.
the class FileUtils method SerializableToFile.
/**
* Converts a serializable object to the specified file.
*
* @param context
* @param cacheFileName
* @param serializable
* @return
*/
public static boolean SerializableToFile(Context context, String cacheFileName, Serializable serializable) {
if (context == null || cacheFileName == null || serializable == null) {
return false;
}
try {
cacheFileName += FILE_SUFFIX;
File f = new File(context.getFilesDir(), FILE_DIRECTORY);
File file = new File(f, cacheFileName);
file.getParentFile().mkdirs();
file.createNewFile();
// Write to file.
OutputStream outputStream = new FileOutputStream(file);
OutputStream buffer = new BufferedOutputStream(outputStream);
ObjectOutput output = new ObjectOutputStream(buffer);
output.writeObject(serializable);
output.flush();
output.close();
return true;
} catch (Exception E) {
return false;
}
}
Aggregations