Search in sources :

Example 6 with Marshaller

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;
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Marshaller(org.kie.api.marshalling.Marshaller) KieBase(org.kie.api.KieBase) EOFException(java.io.EOFException) KieSession(org.kie.api.runtime.KieSession)

Example 7 with Marshaller

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);
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) Test(org.junit.Test)

Example 8 with Marshaller

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;
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 9 with Marshaller

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));
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 10 with Marshaller

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);
    }
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

Marshaller (org.kie.api.marshalling.Marshaller)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 IOException (java.io.IOException)9 Test (org.junit.Test)9 KieSession (org.kie.api.runtime.KieSession)9 KieBase (org.kie.api.KieBase)6 KieMarshallers (org.kie.api.marshalling.KieMarshallers)6 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 DroolsObjectInputStream (org.drools.core.common.DroolsObjectInputStream)3 DroolsObjectOutputStream (org.drools.core.common.DroolsObjectOutputStream)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Ignore (org.junit.Ignore)2 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)2 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)2