Search in sources :

Example 81 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class FirstOrderLogicTest method testRemoveIdentitiesSubNetwork.

@Test
public void testRemoveIdentitiesSubNetwork() throws Exception {
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(RemoveIdentitiesOption.YES);
    KieBase kbase = loadKnowledgeBase(conf, "test_removeIdentitiesSubNetwork.drl");
    KieSession workingMemory = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    workingMemory.setGlobal("results", list);
    final Person bob = new Person("bob", "stilton");
    workingMemory.insert(bob);
    final Person mark = new Person("mark", "stilton");
    workingMemory.insert(mark);
    final Cheese stilton1 = new Cheese("stilton", 6);
    final FactHandle stilton1Handle = (FactHandle) workingMemory.insert(stilton1);
    final Cheese stilton2 = new Cheese("stilton", 7);
    final FactHandle stilton2Handle = (FactHandle) workingMemory.insert(stilton2);
    workingMemory.fireAllRules();
    assertEquals(0, list.size());
    workingMemory.retract(stilton1Handle);
    workingMemory.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(mark, list.get(0));
    workingMemory.retract(stilton2Handle);
    workingMemory.fireAllRules();
    assertEquals(2, list.size());
    assertEquals(bob, list.get(1));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 82 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class FirstOrderLogicTest method testExists2.

@Test
public void testExists2() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_exists.drl");
    KieSession workingMemory = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    workingMemory.setGlobal("list", list);
    final Cheese cheddar = new Cheese("cheddar", 7);
    final Cheese provolone = new Cheese("provolone", 5);
    final Person edson = new Person("Edson", "cheddar");
    final Person bob = new Person("Bob", "muzzarela");
    workingMemory.insert(cheddar);
    workingMemory.fireAllRules();
    assertEquals(0, list.size());
    workingMemory.insert(provolone);
    workingMemory.fireAllRules();
    assertEquals(0, list.size());
    workingMemory.insert(edson);
    workingMemory.fireAllRules();
    assertEquals(1, list.size());
    workingMemory.insert(bob);
    workingMemory.fireAllRules();
    assertEquals(1, list.size());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 83 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class FunctionsTest method testFunctionException.

@Test
public void testFunctionException() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_FunctionException.drl");
    KieSession ksession = createKnowledgeSession(kbase);
    final Cheese brie = new Cheese("brie", 12);
    ksession.insert(brie);
    try {
        ksession.fireAllRules();
        fail("Should throw an Exception from the Function");
    } catch (final Exception e) {
        assertEquals("this should throw an exception", e.getCause().getMessage());
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 84 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class FunctionsTest method testFunction.

@SuppressWarnings("unchecked")
@Test
public void testFunction() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_FunctionInConsequence.drl");
    KieSession ksession = createKnowledgeSession(kbase);
    final List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);
    final Cheese stilton = new Cheese("stilton", 5);
    ksession.insert(stilton);
    ksession.fireAllRules();
    assertEquals(new Integer(5), ((List<Integer>) ksession.getGlobal("list")).get(0));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 85 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class MVELTest method testMVELSoundexNoCharParam.

@Test
public void testMVELSoundexNoCharParam() throws Exception {
    // read in the source
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("MVEL_soundexNPE2500.drl"));
    KieSession ksession = createKnowledgeSession(kbase);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    final Cheese foobarCheese = new Cheese("foobar", 2);
    final Cheese nullCheese = new Cheese(null, 2);
    final Cheese starCheese = new Cheese("*", 2);
    ksession.insert(foobarCheese);
    ksession.insert(nullCheese);
    ksession.insert(starCheese);
    ksession.fireAllRules();
    assertEquals(42, foobarCheese.getPrice());
    assertEquals(2, nullCheese.getPrice());
    assertEquals(2, starCheese.getPrice());
}
Also used : KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Aggregations

Cheese (org.drools.compiler.Cheese)222 Test (org.junit.Test)204 KieSession (org.kie.api.runtime.KieSession)195 ArrayList (java.util.ArrayList)152 KieBase (org.kie.api.KieBase)138 List (java.util.List)100 Person (org.drools.compiler.Person)92 FactHandle (org.kie.api.runtime.rule.FactHandle)53 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)36 KiePackage (org.kie.api.definition.KiePackage)22 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)21 InternalFactHandle (org.drools.core.common.InternalFactHandle)20 Cheesery (org.drools.compiler.Cheesery)14 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)14 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)13 HashMap (java.util.HashMap)10 Command (org.kie.api.command.Command)8 ExecutionResults (org.kie.api.runtime.ExecutionResults)8 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)7