Search in sources :

Example 46 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class KieContainerTest method testFileSystemResourceBuilding.

@Test
public void testFileSystemResourceBuilding() {
    // DROOLS-2339
    KieServices kieServices = KieServices.Factory.get();
    KieResources kieResources = kieServices.getResources();
    KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
    File drlFile = new File("src/test/resources/org/drools/testcoverage/functional/parser/drl/asterisk-imports.drl");
    kieFileSystem.write(kieResources.newFileSystemResource(drlFile, "UTF-8"));
    KieModuleModel kmodel = kieServices.newKieModuleModel();
    kieFileSystem.writeKModuleXML(kmodel.toXML());
    KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
    kieBuilder.buildAll();
    KieContainer kieContainer = kieServices.newKieContainer(kieBuilder.getKieModule().getReleaseId());
    KieBaseConfiguration kieBaseConfiguration = kieServices.newKieBaseConfiguration();
    KieBase kieBase = kieContainer.newKieBase(kieBaseConfiguration);
    Assertions.assertThat(kieBase.getKiePackages()).isNotEmpty();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) File(java.io.File) KieResources(org.kie.api.io.KieResources) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 47 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class WaltzDbBenchmark method main.

public static void main(final String[] args) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("waltzdb.drl", WaltzDbBenchmark.class), ResourceType.DRL);
    Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    KieBaseConfiguration kbaseConfiguration = RuleBaseFactory.newKnowledgeBaseConfiguration();
    kbaseConfiguration.setProperty("drools.removeIdentities", "true");
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseConfiguration);
    // final RuleBase ruleBase = RuleBaseFactory.newRuleBase( RuleBase.RETEOO,
    // conf );
    kbase.addPackages(pkgs);
    KieSession ksession = kbase.newKieSession();
    // 12,8,4
    List<Line> lines = WaltzDbBenchmark.loadLines("waltzdb16.dat");
    // 12,8,4
    List<Label> labels = WaltzDbBenchmark.loadLabels("waltzdb16.dat");
    long now = System.currentTimeMillis();
    for (Line line : lines) {
        ksession.insert(line);
        System.out.println(line.getP1() + " " + line.getP2());
    }
    for (Label label : labels) {
        ksession.insert(label);
        System.out.println(label.getId() + " " + label.getType());
    }
    Stage stage = new Stage(Stage.DUPLICATE);
    ksession.insert(stage);
    ksession.fireAllRules();
    System.out.println("Time: " + (System.currentTimeMillis() - now));
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

Example 48 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class CompositeAgendaTest method testCreateHaltDisposeAgenda.

@Test(timeout = 600_000)
public void testCreateHaltDisposeAgenda() {
    final String drl = " import " + A.class.getCanonicalName() + ";\n" + " declare A @role( event ) end\n" + " global java.util.concurrent.atomic.LongAdder firings;\n" + " rule R0 when\n" + "     A( value > 0,$Aid: id )\n" + " then\n" + "     firings.add(1);\n" + " end\n" + " rule R1 when\n" + "     A(value > 1)\n" + " then\n" + "     firings.add(1);\n" + " end\n" + " rule R2 when\n" + "     A(value > 2)\n" + " then\n" + "     firings.add(1);\n" + " end\n" + " rule R3 when\n" + "     A(value > 3)\n" + " then\n" + "     firings.add(1);\n" + " end\n" + " rule R4 when\n" + "     A(value > 4)\n" + " then\n" + "     firings.add(1);\n" + " end\n" + " rule R5 when\n" + "     A(value > 5)\n" + " then\n" + "     firings.add(1);\n" + " end\n" + " rule R6 when\n" + "     A(value > 6)\n" + " then\n" + "     firings.add(1);\n" + " end\n" + " rule R7 when\n" + "     A(value > 7)\n" + " then\n" + "     firings.add(1);\n" + " end";
    final KieBaseConfiguration kieBaseConfiguration = KieBaseTestConfiguration.STREAM_IDENTITY.getKieBaseConfiguration();
    kieBaseConfiguration.setOption(MultithreadEvaluationOption.YES);
    final KieBase kieBase = new KieHelper().addContent(drl, ResourceType.DRL).build(kieBaseConfiguration);
    final KieSession kieSession = kieBase.newKieSession();
    final LongAdder firingCounter = new LongAdder();
    kieSession.setGlobal("firings", firingCounter);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    executor.submit((Runnable) kieSession::fireUntilHalt);
    try {
        final EventInsertThread eventInsertThread = new EventInsertThread(kieSession);
        executor.submit(eventInsertThread);
        try {
            Thread.sleep(5000);
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }
        eventInsertThread.setActive(false);
    } finally {
        try {
            // This may hit GC overhead limit exceeded
            kieSession.halt();
        } catch (Throwable th) {
            th.printStackTrace();
            throw th;
        } finally {
            kieSession.dispose();
            executor.shutdown();
            try {
                if (!executor.awaitTermination(10, TimeUnit.MILLISECONDS)) {
                    executor.shutdownNow();
                }
            } catch (final InterruptedException e) {
                e.printStackTrace();
                Assert.fail(e.getMessage());
            }
        }
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) LongAdder(java.util.concurrent.atomic.LongAdder) KieBase(org.kie.api.KieBase) ExecutorService(java.util.concurrent.ExecutorService) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 49 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class MarshallingTest method testMarshallEntryPointsWithNot.

@Test
public void testMarshallEntryPointsWithNot() throws Exception {
    String str = "package org.domain.test \n" + "import " + getClass().getCanonicalName() + ".*\n" + "global java.util.List list\n" + "declare A\n" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "declare B\n" + "" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "" + "rule a1\n" + "when\n" + "   $a : A() from entry-point 'a-ep'\n" + "   not B( this after[0s, 10s] $a) from entry-point 'a-ep'\n" + "then\n" + "list.add( $a );" + "end\n";
    KieBaseConfiguration config = RuleBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KieBase kBase = loadKnowledgeBaseFromString(config, str);
    KieSessionConfiguration ksconf = RuleBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.PSEUDO);
    ksconf.setOption(TimerJobFactoryOption.get("trackable"));
    KieSession ksession = kBase.newKieSession(ksconf, null);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    EntryPoint aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    timeService.advanceTime(3, TimeUnit.SECONDS);
    ksession = marsallStatefulKnowledgeSession(ksession);
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession(ksession);
    assertEquals(0, list.size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) FactA(org.drools.mvel.compiler.FactA) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 50 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class MarshallingTest method testMarshallEntryPointsWithSlidingLengthWindow.

@Test
public void testMarshallEntryPointsWithSlidingLengthWindow() throws Exception {
    String str = "package org.domain.test \n" + "import " + getClass().getCanonicalName() + ".*\n" + "import java.util.List\n" + "global java.util.List list\n" + "declare A\n" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "declare B\n" + "" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "" + "rule a1\n" + "when\n" + "   $l : List() from collect( A()  over window:length(3) from entry-point 'a-ep') \n" + "then\n" + "   list.add( $l );" + "end\n";
    KieBaseConfiguration conf = RuleBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBaseFromString(conf, str);
    KieSessionConfiguration ksconf = RuleBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.PSEUDO);
    ksconf.setOption(TimerJobFactoryOption.get("trackable"));
    KieSession ksession = createKnowledgeSession(kbase, ksconf);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    EntryPoint aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    list.clear();
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession(ksession);
    assertEquals(2, ((List) list.get(0)).size());
    aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    list.clear();
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession(ksession);
    assertEquals(3, ((List) list.get(0)).size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) FactA(org.drools.mvel.compiler.FactA) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Aggregations

KieBaseConfiguration (org.kie.api.KieBaseConfiguration)124 Test (org.junit.Test)94 KieSession (org.kie.api.runtime.KieSession)77 KieBase (org.kie.api.KieBase)69 ArrayList (java.util.ArrayList)54 List (java.util.List)38 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)25 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)23 FactHandle (org.kie.api.runtime.rule.FactHandle)21 EntryPoint (org.kie.api.runtime.rule.EntryPoint)18 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)16 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)16 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 StockTick (org.drools.compiler.StockTick)11 KieContainer (org.kie.api.runtime.KieContainer)11 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 Match (org.kie.api.runtime.rule.Match)10 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)9 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)7