Search in sources :

Example 6 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder 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 KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder 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 KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder 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 KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class BindTest method testBingWrongFieldName.

private void testBingWrongFieldName(final String drl) {
    try {
        final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
        if (!kbuilder.hasErrors()) {
            fail("KnowledgeBuilder should have errors");
        }
    } catch (final Exception e) {
        e.printStackTrace();
        fail("Exception should not be thrown ");
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder)

Example 10 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class BindTest method testBindingToMissingField.

@Test
public void testBindingToMissingField() throws Exception {
    // JBRULES-3047
    String rule1 = "package org.drools.compiler\n";
    rule1 += "rule rule1\n";
    rule1 += "when\n";
    rule1 += "    Integer( $i : noSuchField ) \n";
    rule1 += "    eval( $i > 0 )\n";
    rule1 += "then \n";
    rule1 += "end\n";
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(rule1.getBytes()), ResourceType.DRL);
    if (!kbuilder.hasErrors()) {
        fail("this should have errors");
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Test(org.junit.Test)

Aggregations

KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)376 Test (org.junit.Test)302 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)231 KieSession (org.kie.api.runtime.KieSession)149 ArrayList (java.util.ArrayList)112 List (java.util.List)65 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)52 KiePackage (org.kie.api.definition.KiePackage)39 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)38 FactHandle (org.kie.api.runtime.rule.FactHandle)36 FactType (org.kie.api.definition.type.FactType)27 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)24 Person (org.drools.compiler.Person)23 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)23 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)19 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)17 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)17 StringReader (java.io.StringReader)15 Resource (org.kie.api.io.Resource)15