Search in sources :

Example 76 with InternalKnowledgeBase

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

the class ExtendsTest method testMultipleInheritanceWithPosition1.

@Test
public void testMultipleInheritanceWithPosition1() throws Exception {
    // DROOLS-249
    String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 //@position(0)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 //@position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end";
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
    }
    assertFalse(kBuilder.hasErrors());
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
    FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait");
    assertEquals(5, sw.getFields().size());
    assertEquals("field0", sw.getFields().get(0).getName());
    assertEquals("field1", sw.getFields().get(1).getName());
    assertEquals("mfield1", sw.getFields().get(2).getName());
    assertEquals("field2", sw.getFields().get(3).getName());
    assertEquals("mfield0", sw.getFields().get(4).getName());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 77 with InternalKnowledgeBase

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

the class ExtendsTest method testExtendOverride.

@Test
public void testExtendOverride() {
    String drl = "package test.beans;\n" + "\n" + "import java.util.List;\n" + "import java.util.ArrayList;\n" + "\n" + "global List ans;" + "\n" + "\n" + "declare ArrayList\n" + "end\n" + "" + "declare Bean extends ArrayList\n" + "  fld : int = 4 \n" + "  myField : String = \"xxx\" \n" + "end\n" + "\n" + "declare Bean2 extends Bean\n" + "  moref : double\n" + "  myField : String\n" + "end\n" + "rule \"Init\"\n" + "when\n" + "then\n" + "  Bean b = new Bean2();\n" + "  ans.add(b);" + "  System.out.println(b);\t\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = createKnowledgeSession(kbase);
    List out = new ArrayList();
    ksession.setGlobal("ans", out);
    ksession.fireAllRules();
    Object x = out.get(0);
    FactType type = kbase.getFactType("test.beans", "Bean2");
    assertEquals(4, type.get(x, "fld"));
    assertEquals("xxx", type.get(x, "myField"));
    assertEquals(0.0, type.get(x, "moref"));
    assertTrue(x instanceof ArrayList);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 78 with InternalKnowledgeBase

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

the class FailureOnRemovalTest method runTest.

private void runTest(boolean shareBetaNodes) throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) createKnowledgeBase(shareBetaNodes);
    Collection<KiePackage> rule1 = compileRule(RULE_1);
    kbase.addPackages(rule1);
    // we need to add at least two rules. Test will not fail with only one rule.
    Collection<KiePackage> rule2 = compileRule(RULE_2);
    kbase.addPackages(rule2);
    kbase.removeRule(PACKAGE, RULE_1);
    KieSession ksession = createKnowledgeSession(kbase);
    int fired = ksession.fireAllRules();
    ksession.dispose();
    assertEquals(1, fired);
    Collection<KiePackage> rule3 = compileRule(RULE_3);
    kbase.addPackages(rule3);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 79 with InternalKnowledgeBase

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

the class FireUntilHaltAccumulateTest method setUp.

@Before
public void setUp() {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        System.err.println(kbuilder.getErrors().toString());
    }
    final KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(config);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    final KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    this.statefulSession = kbase.newKieSession(sessionConfig, null);
    this.stockFactory = new StockFactory(kbase);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Before(org.junit.Before)

Example 80 with InternalKnowledgeBase

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

the class FirstOrderLogicTest method testOrWithVariableResolution2.

// JBRULES-2526
@Test
public void testOrWithVariableResolution2() throws Exception {
    // KieBase kbase = loadKnowledgeBase( "test_OrCEFollowedByMultipleEval.drl");
    // KieSession workingMemory = createKnowledgeSession(kbase);
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_OrCEFollowedByMultipleEval2.drl", FirstOrderLogicTest.class), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    final KieSession ksession = createKnowledgeSession(kbase);
    final AgendaEventListener al = mock(AgendaEventListener.class);
    ksession.addEventListener(al);
    ksession.insert(new FactA("a"));
    ksession.insert(new FactB("b"));
    ksession.insert(new FactC("c"));
    ksession.fireAllRules();
    verify(al, times(8)).afterMatchFired(any(AfterMatchFiredEvent.class));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) FactA(org.drools.compiler.FactA) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactB(org.drools.compiler.FactB) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) FactC(org.drools.compiler.FactC) Test(org.junit.Test)

Aggregations

InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)394 Test (org.junit.Test)321 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)231 KieSession (org.kie.api.runtime.KieSession)221 ArrayList (java.util.ArrayList)173 List (java.util.List)108 KiePackage (org.kie.api.definition.KiePackage)80 FactHandle (org.kie.api.runtime.rule.FactHandle)46 InternalFactHandle (org.drools.core.common.InternalFactHandle)43 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)42 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)41 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)41 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)40 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)39 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)38 Person (org.drools.compiler.Person)37 Cheese (org.drools.compiler.Cheese)36 FactType (org.kie.api.definition.type.FactType)27 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)25 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)24