Search in sources :

Example 6 with KieBase

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

the class TruthMaintenanceTest method testStatedShadowLogicalWithSingleOccurance.

@Test(timeout = 10000)
public void testStatedShadowLogicalWithSingleOccurance() {
    String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + "    String( this == 'go1' ) " + "then \n" + "    insertLogical( 'f1' ); \n" + "end \n" + "rule StillHere \n" + "when \n" + "    String( this == 'go2' ) " + "    s : String( this == 'f1' ) " + "then \n" + "    list.add( s ); \n" + "end \n" + "";
    KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kieConf.setOption(EqualityBehaviorOption.IDENTITY);
    KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
    KieSession session = kbase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
    InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
    session.insert("go1");
    session.fireAllRules();
    session.insert("go2");
    session.fireAllRules();
    // Make sure f1 only occurs once
    assertEquals(1, list.size());
    assertEquals("f1", list.get(0));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 7 with KieBase

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

the class TruthMaintenanceTest method testLogicalInsertionsWithExists.

@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsWithExists() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionWithExists.drl");
    KieSession ksession = kbase.newKieSession();
    final Person p1 = new Person("p1", "stilton", 20);
    p1.setStatus("europe");
    FactHandle c1FactHandle = ksession.insert(p1);
    final Person p2 = new Person("p2", "stilton", 30);
    p2.setStatus("europe");
    FactHandle c2FactHandle = ksession.insert(p2);
    final Person p3 = new Person("p3", "stilton", 40);
    p3.setStatus("europe");
    FactHandle c3FactHandle = ksession.insert(p3);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    // all 3 in europe, so, 2 cheese
    Collection cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(2, cheeseList.size());
    // europe=[ 1, 2 ], america=[ 3 ]
    p3.setStatus("america");
    c3FactHandle = getFactHandle(c3FactHandle, ksession);
    ksession.update(c3FactHandle, p3);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(1, cheeseList.size());
    // europe=[ 1 ], america=[ 2, 3 ]
    p2.setStatus("america");
    c2FactHandle = getFactHandle(c2FactHandle, ksession);
    ksession.update(c2FactHandle, p2);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(1, cheeseList.size());
    // europe=[ ], america=[ 1, 2, 3 ]
    p1.setStatus("america");
    c1FactHandle = getFactHandle(c1FactHandle, ksession);
    ksession.update(c1FactHandle, p1);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(2, cheeseList.size());
    // europe=[ 2 ], america=[ 1, 3 ]
    p2.setStatus("europe");
    c2FactHandle = getFactHandle(c2FactHandle, ksession);
    ksession.update(c2FactHandle, p2);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(1, cheeseList.size());
    // europe=[ 1, 2 ], america=[ 3 ]
    p1.setStatus("europe");
    c1FactHandle = getFactHandle(c1FactHandle, ksession);
    ksession.update(c1FactHandle, p1);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(1, cheeseList.size());
    // europe=[ 1, 2, 3 ], america=[ ]
    p3.setStatus("europe");
    c3FactHandle = getFactHandle(c3FactHandle, ksession);
    ksession.update(c3FactHandle, p3);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    cheeseList = ksession.getObjects(new ClassObjectFilter(Cheese.class));
    assertEquals(2, cheeseList.size());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 8 with KieBase

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

the class TruthMaintenanceTest method testLogicalInsertions3.

// (timeout=10000)
@Test
public void testLogicalInsertions3() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_logicalInsertions3.drl");
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("events", list);
    // asserting the sensor object
    final Sensor sensor = new Sensor(150, 100);
    FactHandle sensorHandle = ksession.insert(sensor);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    // alarm must sound
    assertEquals(2, list.size());
    assertEquals(2, ksession.getObjects().size());
    // modifying sensor
    sensor.setTemperature(125);
    sensorHandle = getFactHandle(sensorHandle, ksession);
    ksession.update(sensorHandle, sensor);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    // alarm must continue to sound
    assertEquals(3, list.size());
    assertEquals(2, ksession.getObjects().size());
    // modifying sensor
    sensor.setTemperature(80);
    sensorHandle = getFactHandle(sensorHandle, ksession);
    ksession.update(sensorHandle, sensor);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    // no alarms anymore
    assertEquals(3, list.size());
    assertEquals(1, ksession.getObjects().size());
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Sensor(org.drools.compiler.Sensor) Test(org.junit.Test)

Example 9 with KieBase

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

the class XSDResourceTest method testXSDResourceNotBreakingCompilation.

@Test
public void testXSDResourceNotBreakingCompilation() {
    final KieContainer kcontainer = KieServices.Factory.get().getKieClasspathContainer();
    final KieBase kieBase = kcontainer.getKieBase("xsdKieBase");
    Assertions.assertThat(kieBase).as("Created KieBase with XSD should not be null").isNotNull();
}
Also used : KieBase(org.kie.api.KieBase) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 10 with KieBase

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

the class AbstractConcurrentTest method getKieBase.

protected synchronized KieBase getKieBase(final String... drls) {
    final KieHelper kieHelper = new KieHelper();
    for (final String drl : drls) {
        kieHelper.addContent(drl, ResourceType.DRL);
    }
    KieBase kieBase;
    if (enforcedJitting) {
        kieBase = kieHelper.build(ConstraintJittingThresholdOption.get(0));
    } else {
        kieBase = kieHelper.build();
    }
    if (serializeKieBase) {
        kieBase = serializeAndDeserializeKieBase(kieBase);
    }
    return kieBase;
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper)

Aggregations

KieBase (org.kie.api.KieBase)1272 Test (org.junit.Test)1191 KieSession (org.kie.api.runtime.KieSession)1011 ArrayList (java.util.ArrayList)592 List (java.util.List)392 Person (org.drools.compiler.Person)214 FactHandle (org.kie.api.runtime.rule.FactHandle)176 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)168 KieHelper (org.kie.internal.utils.KieHelper)156 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)154 Cheese (org.drools.compiler.Cheese)139 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)99 Arrays.asList (java.util.Arrays.asList)87 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)86 QueryResults (org.kie.api.runtime.rule.QueryResults)78 KieServices (org.kie.api.KieServices)74 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)67 Model (org.drools.model.Model)64 Rule (org.drools.model.Rule)64 ModelImpl (org.drools.model.impl.ModelImpl)64