Search in sources :

Example 6 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class WorkingMemoryActionsSerializationTest method before.

@Before
public void before() {
    ruleCalls.clear();
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ksession = kbase.newKieSession();
    ksession.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void afterMatchFired(final AfterMatchFiredEvent event) {
            final String name = event.getMatch().getRule().getName();
            System.out.println(name + " fired!");
            synchronized (ruleCalls) {
                Integer calls = ruleCalls.get(name);
                if (calls == null) {
                    calls = 1;
                } else {
                    calls++;
                }
                ruleCalls.put(name, calls);
            }
        }
    });
    // Using fire until halt. If firaAllRules is called it works. But for other reasons, I need to run fireUntilHalt
    new Thread(new Runnable() {

        @Override
        public void run() {
            ksession.fireUntilHalt();
        }
    }).start();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Before(org.junit.Before)

Example 7 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class UnmarshallingTest method initializeKnowledgeBase.

private KieBase initializeKnowledgeBase(String rule) {
    // Setup knowledge base
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(rule)), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        throw new RuntimeException(kbuilder.getErrors().toString());
    }
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(config);
    knowledgeBase.addPackages(kbuilder.getKnowledgePackages());
    return knowledgeBase;
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StringReader(java.io.StringReader) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 8 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class MultithreadTest method testConcurrentQueries.

@Test(timeout = 5000)
public void testConcurrentQueries() {
    // DROOLS-175
    final StringBuilder drl = new StringBuilder();
    drl.append("package org.drools.test;\n" + "" + "query foo( ) \n" + "   Object() from new Object() \n" + "end\n" + "" + "rule XYZ when then end \n");
    final KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(ResourceFactory.newByteArrayResource(drl.toString().getBytes()), ResourceType.DRL);
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
    final KieSession ksession = kbase.newKieSession();
    final Executor executor = Executors.newCachedThreadPool(new ThreadFactory() {

        public Thread newThread(final Runnable r) {
            final Thread t = new Thread(r);
            t.setDaemon(true);
            return t;
        }
    });
    final int THREAD_NR = 5;
    final CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
    for (int i = 0; i < THREAD_NR; i++) {
        ecs.submit(new Callable<Boolean>() {

            public Boolean call() throws Exception {
                boolean succ = false;
                try {
                    final QueryResults res = ksession.getQueryResults("foo", Variable.v);
                    succ = (res.size() == 1);
                    return succ;
                } catch (final Exception e) {
                    e.printStackTrace();
                    return succ;
                }
            }
        });
    }
    boolean success = true;
    for (int i = 0; i < THREAD_NR; i++) {
        try {
            success = ecs.take().get() && success;
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }
    Assertions.assertThat(success).isTrue();
    ksession.dispose();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) EntryPoint(org.kie.api.runtime.rule.EntryPoint) QueryResults(org.kie.api.runtime.rule.QueryResults) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Executor(java.util.concurrent.Executor) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 9 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class TestUtil method buildSessionInSteps.

public static KieSession buildSessionInSteps(final boolean reuseKieBaseWhenAddingRules, final String... drls) {
    if (drls == null || drls.length == 0) {
        return KnowledgeBaseFactory.newKnowledgeBase().newKieSession();
    } else {
        String drl = drls[0];
        final KnowledgeBuilder kbuilder = createKnowledgeBuilder(null, drl);
        final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addPackages(kbuilder.getKnowledgePackages());
        final KieSession kSession = kbase.newKieSession();
        for (int i = 1; i < drls.length; i++) {
            drl = drls[i];
            final KnowledgeBuilder kbuilder2;
            if (reuseKieBaseWhenAddingRules) {
                kbuilder2 = createKnowledgeBuilder(kSession.getKieBase(), drl);
            } else {
                kbuilder2 = createKnowledgeBuilder(null, drl);
            }
            ((InternalKnowledgeBase) kSession.getKieBase()).addPackages(kbuilder2.getKnowledgePackages());
        }
        return kSession;
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 10 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class MarshallingTest method testSerializeAdd.

@Test
public void testSerializeAdd() throws Exception {
    Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Dynamic1.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    kBase.addPackages(kpkgs);
    kBase = SerializationHelper.serializeObject(kBase);
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    InternalFactHandle stilton = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
    InternalFactHandle brie = (InternalFactHandle) session.insert(new Cheese("brie", 10));
    session.fireAllRules();
    assertEquals(list.size(), 1);
    assertEquals("stilton", list.get(0));
    // now recreate the rulebase, deserialize the session and test it
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    list = (List) session.getGlobal("list");
    assertNotNull(list);
    assertEquals(list.size(), 1);
    assertEquals("stilton", list.get(0));
    kpkgs = loadKnowledgePackages("../test_Dynamic3.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    kBase.addPackages(kpkgs);
    InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
    InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 10));
    InternalFactHandle bob = (InternalFactHandle) session.insert(new Person("bob", 30));
    session.fireAllRules();
    assertEquals(list.size(), 3);
    assertEquals(bob.getObject(), list.get(2));
    assertEquals("stilton", list.get(1));
    session.dispose();
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Cheese(org.drools.compiler.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)368 Test (org.junit.Test)307 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)203 KieSession (org.kie.api.runtime.KieSession)193 ArrayList (java.util.ArrayList)163 List (java.util.List)99 KiePackage (org.kie.api.definition.KiePackage)79 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)45 FactHandle (org.kie.api.runtime.rule.FactHandle)39 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)38 InternalFactHandle (org.drools.core.common.InternalFactHandle)37 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)37 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)37 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)37 Cheese (org.drools.compiler.Cheese)36 Person (org.drools.compiler.Person)36 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)34 FactType (org.kie.api.definition.type.FactType)26 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)24 HashMap (java.util.HashMap)23