1

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
iflores
  • 43
  • 7

2 Answers2

1

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.

antmendoza
  • 391
  • 2
  • 4
  • Is there any reason for that? For me that's a bug, but maybe I am not understanding the logic behind – iflores Jan 22 '15 at 13:25
  • Hi @iflores, There isn't a bug, globals has a different purpose. [Drools documentation](http://docs.jboss.org/drools/release/6.1.0.Final/drools-docs/html_single/index.html#d0e5849) "Typically, they are used to provide data or services that the rules use, especially application services used in rule consequences, and to return data from the rules, like logs or values added in rule consequences, or for the rules to interact with the application, doing callbacks." – antmendoza Feb 14 '15 at 16:33
1

Just ran into this awesome behaviour, so here's a solution for loading:

You can initialise your globals BEFORE loading the session by registering a resolver with the environment:

Environment environment = kieServices.getEnvironment();
        MapGlobalResolver resolver = new MapGlobalResolver(droolsProvider.globals());
        environment.set(EnvironmentName.GLOBALS, resolver);

The MapGlobalResolver is the default resolver anyway. By using this approach, the resolver will be pre-initialised with the correct globals. Personally, I am thinking of writing an InjectionResolver so that Guice will inject globals on demand, but that might not be for everyone's need.

Then loading is as simple as passing the correct environment in:

KieSession loadedKieSession = kieServices.getKieService().getStoreServices().loadKieSession(session.getId(), kieBase, ksConf, environment);

Where the objects are the corresponding config object that are needed to set up the Environment.

pandaadb
  • 6,306
  • 2
  • 22
  • 41
  • Can you give any reference of this or any example ? – Prog_G Nov 08 '18 at 10:05
  • i'm sorry, i wrote this in '15 and don't have access to my code from that time anymore. The above example, as far as I can tell, just configures the session and demonstrates how to set globals on a resumed one – pandaadb Nov 09 '18 at 10:42