use of org.kie.api.marshalling.Marshaller in project drools by kiegroup.
the class EventAccessorRestoreTest method loadSession.
public KieSession loadSession(FileInputStream input) throws IOException, ClassNotFoundException {
KieSession ksession = null;
DroolsObjectInputStream droolsIn = new DroolsObjectInputStream(input, this.getClass().getClassLoader());
try {
KieBase kbase = (KieBase) droolsIn.readObject();
Marshaller mas = createMarshaller(kbase);
ksession = mas.unmarshall(droolsIn);
} catch (EOFException e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
droolsIn.close();
}
return ksession;
}
use of org.kie.api.marshalling.Marshaller in project drools by kiegroup.
the class Misc2Test method testKsessionSerializationWithInsertLogical.
@Test
public void testKsessionSerializationWithInsertLogical() {
List<String> firedRules = new ArrayList<String>();
String str = "import java.util.Date;\n" + "import org.drools.compiler.integrationtests.Misc2Test.Promotion;\n" + "\n" + "declare Person\n" + " name : String\n" + " dateOfBirth : Date\n" + "end\n" + "\n" + "declare Employee extends Person\n" + " job : String\n" + "end\n" + "\n" + "rule \"Insert Alice\"\n" + " when\n" + " then\n" + " Employee alice = new Employee(\"Alice\", new Date(1973, 7, 2), \"Vet\");\n" + " insert(alice);\n" + " System.out.println(\"Insert Alice\");\n" + "end\n" + "\n" + "rule \"Insert Bob\"\n" + " when\n" + " Person(name == \"Alice\")\n" + " then\n" + " Person bob = new Person(\"Bob\", new Date(1973, 7, 2));\n" + " insertLogical(bob);\n" + " System.out.println(\"InsertLogical Bob\");\n" + "end\n" + "\n" + "rule \"Insert Claire\"\n" + " when\n" + " Person(name == \"Bob\")\n" + " then\n" + " Employee claire = new Employee(\"Claire\", new Date(1973, 7, 2), \"Student\");\n" + " insert(claire);\n" + " System.out.println(\"Insert Claire\");\n" + "end\n" + "\n" + "rule \"Promote\"\n" + " when\n" + " p : Promotion(n : name, j : job)\n" + " e : Employee(name == n)\n" + " then\n" + " modify(e) {\n" + " setJob(j)\n" + " }\n" + " delete(p);\n" + " System.out.printf(\"Promoted %s to %s%n\", n, j);\n" + "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = kbase.newKieSession();
// insertLogical Person(Bob)
ksession.fireAllRules();
// Serialize and Deserialize
try {
Marshaller marshaller = MarshallerFactory.newMarshaller(kbase);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshall(baos, ksession);
marshaller = MarshallerFactory.newMarshaller(kbase);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
baos.close();
ksession = (StatefulKnowledgeSession) marshaller.unmarshall(bais);
bais.close();
} catch (Exception e) {
e.printStackTrace();
fail("unexpected exception :" + e.getMessage());
}
ksession.insert(new Promotion("Claire", "Scientist"));
int result = ksession.fireAllRules();
assertEquals(1, result);
}
use of org.kie.api.marshalling.Marshaller in project drools by kiegroup.
the class CommonTestMethodBase method marshallAndUnmarshall.
public static KieSession marshallAndUnmarshall(KieServices ks, KieBase kbase, KieSession ksession, KieSessionConfiguration sessionConfig) {
// Serialize and Deserialize
try {
Marshaller marshaller = ks.getMarshallers().newMarshaller(kbase);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshall(baos, ksession);
marshaller = MarshallerFactory.newMarshaller(kbase);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
baos.close();
ksession = marshaller.unmarshall(bais, sessionConfig, null);
bais.close();
} catch (Exception e) {
e.printStackTrace();
fail("unexpected exception :" + e.getMessage());
}
return ksession;
}
use of org.kie.api.marshalling.Marshaller in project drools by kiegroup.
the class CepEspTest method testDeleteOfDeserializedJob.
@Test
public void testDeleteOfDeserializedJob() throws Exception {
// DROOLS-1660
String drl = "import " + EventA.class.getCanonicalName() + "\n" + "import java.util.Date\n" + "global java.util.List list\n" + "declare EventA\n" + " @role(event)\n" + " @timestamp(timestamp)\n" + "end\n" + "rule test\n" + " when\n" + " $event : EventA(value == 1)\n" + " not(EventA(value == 1, this after [1ms,4m] $event))\n" + " then\n" + " list.add(\"Fired \"+ $event);\n" + "end\n";
KieBase kieBase = new KieHelper().addContent(drl, ResourceType.DRL).build(EventProcessingOption.STREAM);
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kieBase.newKieSession(sessionConfig, null);
List<String> list = new ArrayList<>();
List<EventA> events = new ArrayList<>();
events.add(new EventA("2010-01-01 02:00:00", 0));
events.add(new EventA("2010-01-01 03:00:00", 1));
events.add(new EventA("2010-01-01 03:01:00", 0));
events.add(new EventA("2010-01-01 03:02:00", 1));
events.add(new EventA("2010-01-01 03:03:00", 0));
events.add(new EventA("2010-01-01 03:04:00", 0));
events.add(new EventA("2010-01-01 03:05:00", 0));
events.add(new EventA("2010-01-01 03:06:00", 0));
events.add(new EventA("2010-01-01 03:07:00", 0));
// set clock reference
SessionPseudoClock clock = ksession.getSessionClock();
clock.advanceTime(events.get(0).getTimestamp().getTime(), TimeUnit.MILLISECONDS);
byte[] serializedSession = null;
try {
Marshaller marshaller = KieServices.Factory.get().getMarshallers().newMarshaller(kieBase);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshall(baos, ksession);
serializedSession = baos.toByteArray();
} catch (IOException e2) {
e2.printStackTrace();
}
for (EventA current : events) {
KieSession ksession2 = null;
Marshaller marshaller = KieServices.Factory.get().getMarshallers().newMarshaller(kieBase);
try {
ByteArrayInputStream bais = new ByteArrayInputStream(serializedSession);
ksession2 = marshaller.unmarshall(bais, sessionConfig, null);
ksession2.setGlobal("list", list);
clock = ksession2.getSessionClock();
bais.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
long currTime = clock.getCurrentTime();
long nextTime = current.getTimestamp().getTime();
while (currTime <= (nextTime - 1000)) {
clock.advanceTime(1000, TimeUnit.MILLISECONDS);
ksession2.fireAllRules();
currTime += 1000;
}
long diff = nextTime - currTime;
if (diff > 0) {
clock.advanceTime(diff, TimeUnit.MILLISECONDS);
}
ksession2.insert(current);
ksession2.fireAllRules();
// serialize knowledge session
try {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshall(baos, ksession2);
serializedSession = baos.toByteArray();
} catch (IOException e2) {
e2.printStackTrace();
}
ksession2.dispose();
}
assertEquals(1, list.size());
assertEquals("Fired EventA at 2010-01-01 03:02:00", list.get(0));
}
use of org.kie.api.marshalling.Marshaller in project drools by kiegroup.
the class SerializationWithCollectTest method test.
/**
* BZ 1193600
*/
@Test
public void test() throws Exception {
Marshaller marshaller = MarshallerFactory.newMarshaller(kbase);
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
marshaller.marshall(baos, ksession);
try (final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) {
marshaller = MarshallerFactory.newMarshaller(kbase);
ksession = marshaller.unmarshall(bais);
}
} catch (NullPointerException e) {
Assertions.fail("Consider reopening BZ 1193600!", e);
}
}
Aggregations