Search in sources :

Example 41 with KieBase

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

the class PatternTest method testParentheses.

@Test
public void testParentheses() throws Exception {
    final KieBase kbase = loadKnowledgeBase("test_ParenthesisUsage.drl");
    final List<Person> results = new ArrayList<>();
    final KieSession session = createKnowledgeSession(kbase);
    session.setGlobal("results", results);
    final Person bob = new Person("Bob", 20);
    bob.setAlive(true);
    final Person foo = new Person("Foo", 0);
    foo.setAlive(false);
    session.insert(bob);
    session.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(bob, results.get(0));
    session.insert(foo);
    session.fireAllRules();
    assertEquals(2, results.size());
    assertEquals(foo, results.get(1));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 42 with KieBase

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

the class RemoveRuleTest method testRuleRemovalWithJoinedRootPattern.

@Test
public void testRuleRemovalWithJoinedRootPattern() {
    String str = "";
    str += "package org.drools.compiler \n";
    str += "rule rule1 \n";
    str += "when \n";
    str += "  String() \n";
    str += "  Person() \n";
    str += "then \n";
    str += "end  \n";
    str += "rule rule2 \n";
    str += "when \n";
    str += "  String() \n";
    str += "  Cheese() \n";
    str += "then \n";
    str += "end  \n";
    final KieBase kbase = loadKnowledgeBaseFromString(str);
    final KieSession ksession = createKnowledgeSession(kbase);
    final DefaultFactHandle handle = (DefaultFactHandle) ksession.insert("hello");
    ksession.fireAllRules();
    LeftTuple leftTuple = handle.getFirstLeftTuple();
    assertNotNull(leftTuple);
    assertNotNull(leftTuple.getPeer());
    kbase.removeRule("org.drools.compiler", "rule2");
    leftTuple = handle.getFirstLeftTuple();
    assertNotNull(leftTuple);
    assertNull(leftTuple.getHandleNext());
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) LeftTuple(org.drools.core.reteoo.LeftTuple) Test(org.junit.Test)

Example 43 with KieBase

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

the class MarshallingIssuesTest method testJBRULES_1946_2.

@Test
public void testJBRULES_1946_2() {
    KieBase kbase = loadKnowledgeBase("../Sample.drl");
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DroolsObjectOutputStream oos = new DroolsObjectOutputStream(baos);
        oos.writeObject(kbase);
        oos.flush();
        oos.close();
        baos.flush();
        baos.close();
        byte[] serializedKb = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(serializedKb);
        DroolsObjectInputStream ois = new DroolsObjectInputStream(bais);
        KieBase kb2 = (KieBase) ois.readObject();
    } catch (OptionalDataException ode) {
        ode.printStackTrace();
        fail("EOF? " + ode.eof);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unexpected exception: " + e.getMessage());
    }
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) KieBase(org.kie.api.KieBase) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OptionalDataException(java.io.OptionalDataException) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) OptionalDataException(java.io.OptionalDataException) Test(org.junit.Test)

Example 44 with KieBase

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

the class MarshallingIssuesTest method testJBRULES_1946_3.

@Test
public void testJBRULES_1946_3() {
    KieBase kbase = loadKnowledgeBase("../Sample.drl");
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DroolsObjectOutputStream oos = new DroolsObjectOutputStream(baos);
        oos.writeObject(kbase);
        oos.flush();
        oos.close();
        baos.flush();
        baos.close();
        byte[] serializedKb = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(serializedKb);
        ObjectInputStream ois = new ObjectInputStream(bais);
        KieBase kb2 = (KieBase) ois.readObject();
        fail("Should have raised an IllegalArgumentException since the kbase was serialized with a Drools Stream but deserialized with a regular stream");
    } catch (IllegalArgumentException ode) {
    // success
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unexpected exception: " + e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) KieBase(org.kie.api.KieBase) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) OptionalDataException(java.io.OptionalDataException) ObjectInputStream(java.io.ObjectInputStream) DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Test(org.junit.Test)

Example 45 with KieBase

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

the class MarshallingIssuesTest method testJBRULES_1946.

@Test
public void testJBRULES_1946() {
    KieBase kbase = loadKnowledgeBase("../Sample.drl");
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(kbase);
        oos.flush();
        oos.close();
        baos.flush();
        baos.close();
        byte[] serializedKb = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(serializedKb);
        ObjectInputStream ois = new ObjectInputStream(bais);
        KieBase kb2 = (KieBase) ois.readObject();
    } catch (OptionalDataException ode) {
        ode.printStackTrace();
        fail("EOF? " + ode.eof);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unexpected exception: " + e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) KieBase(org.kie.api.KieBase) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DroolsObjectOutputStream(org.drools.core.common.DroolsObjectOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OptionalDataException(java.io.OptionalDataException) OptionalDataException(java.io.OptionalDataException) ObjectInputStream(java.io.ObjectInputStream) DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Test(org.junit.Test)

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