Search code examples
marshallingglobaldroolsunmarshallingkie

Drools marshall/unmarshall not save global


I have a Test to unmarshall kiesession, with data in Global, but the unmarshall not return global.

The code is that :

Java Test

    KieServices kieServices = KieServices.Factory.get();

    KieContainer kContainer = kieServices.getKieClasspathContainer();

    KieBase kBase1 = kContainer.getKieBase("KBase1");
    KieSession kieSession1 = kContainer.newKieSession("KSession2_1");

    Map<String, Object> map = new ConcurrentHashMap<String, Object>();

    int tam = 10000;

    for (int i = 0; i < tam; i++) {
        map.put("map" + i, i);
    }

    kieSession1.setGlobal("map", map);

    for (int i = 0; i < tam; i++) {

        Client client = new Client();
        client.setName("test");
        client.setEdad(10);

        kieSession1.insert(client);
    }

    kieSession1.fireAllRules();

    Marshaller marshaller = MarshallerFactory.newMarshaller(kBase1);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        marshaller.marshall(baos, kieSession1);
    } catch (IOException e) {
        fail("error");
    }

    byte[] data = baos.toByteArray();

    try {
        baos.close();
    } catch (IOException e) {
        fail("error");
    }
    //
    kieSession1.dispose();

    InputStream is = new ByteArrayInputStream(data);
    try {

        kieSession1 = marshaller.unmarshall(is);
    } catch (ClassNotFoundException e) {
        fail("error");
    } catch (IOException e) {
        fail("error : " + e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
        }
    }

    assertEquals(tam, kieSession1.getFactCount());

    assertNotNull("No existe Global !!", kieSession1.getGlobal("map"));

Drools Rule

 global java.util.Map map

 rule "test"

    when
    then
        System.out.println("test !!" + map.size());
end

The version are :

  • org.drools:drools-compiler:jar:6.1.0.Final
  • org.drools:drools-core:jar:6.1.0.Final
  • org.kie:kie-api:jar:6.1.0.Final.1.2
  • org.kie:kie-internal:jar:6.1.0.Final

Solution

  • Globals are not inserted into the Working Memory, consequently they are not saved with the KieSession's state.

    Globals have to be inserted every time you restore KieSession's state.