Search in sources :

Example 11 with Primitives

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

the class ArrayTest method testEqualsOnIntArray.

@Test
public void testEqualsOnIntArray() throws Exception {
    String str = "";
    str += "package org.drools.compiler;\n";
    str += "global java.util.List list;\n";
    str += "rule \"contains in array\"\n";
    str += "     salience 10\n";
    str += "     when\n";
    str += "         Primitives( primitiveIntArray[0] == 1 )\n";
    str += "     then\n";
    str += "        list.add( \"ok1\" );\n";
    str += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    kbase = SerializationHelper.serializeObject(kbase);
    final KieSession ksession = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Primitives p1 = new Primitives();
    p1.setPrimitiveIntArray(new int[] { 1, 2 });
    final FactHandle p1h = ksession.insert(p1);
    ksession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : 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) Primitives(org.drools.compiler.Primitives) Test(org.junit.Test)

Example 12 with Primitives

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

the class ArrayTest method testContainsCharArray.

@Test
public void testContainsCharArray() throws Exception {
    final KieSession kieSession = createKieSessionFromDrl(getDrl("Character", false, "'c'"));
    final List list = new ArrayList();
    addGlobalsToSession(kieSession, list, 'c', new Character[] { 'a', 'b', 'c' });
    final Primitives p1 = new Primitives();
    p1.setPrimitiveCharArray(new char[] { 'a', 'b', 'c' });
    p1.setCharPrimitive('a');
    final Primitives p2 = new Primitives();
    p2.setPrimitiveCharArray(new char[] { 'a', 'b', 'c' });
    p2.setCharPrimitive('c');
    testArrayContains(kieSession, p1, p2, list);
}
Also used : ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Primitives(org.drools.compiler.Primitives) Test(org.junit.Test)

Example 13 with Primitives

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

the class ArrayTest method testNotContainsBooleanArray.

@Test
public void testNotContainsBooleanArray() throws Exception {
    String str = "";
    str += "package org.drools.compiler;\n";
    str += "global java.util.List list;\n";
    str += "global Boolean nGlobal;\n";
    str += "global Object arrayGlobal;\n";
    str += "rule \"contains in array\"\n";
    str += "     salience 10\n";
    str += "     when\n";
    str += "         Primitives( $b : booleanPrimitive, intPrimitive == 10, $array1 :  primitiveBooleanArray ) \n";
    str += "         Primitives( booleanPrimitive == false, intPrimitive != 10, $array2 : primitiveBooleanArray not contains nGlobal,  primitiveBooleanArray not contains $b, ";
    str += "                     booleanPrimitive not memberOf $array2, booleanPrimitive not memberOf $array1, booleanPrimitive not memberOf arrayGlobal )\n";
    str += "     then\n";
    str += "        list.add( \"ok1\" );\n";
    str += "end\n";
    final KieSession kieSession = createKieSessionFromDrl(str);
    final List list = new ArrayList();
    addGlobalsToSession(kieSession, list, false, new Boolean[] { true, true });
    final Primitives p1 = new Primitives();
    p1.setIntPrimitive(10);
    p1.setPrimitiveBooleanArray(new boolean[] { true, true });
    p1.setBooleanPrimitive(false);
    final Primitives p2 = new Primitives();
    p2.setPrimitiveBooleanArray(new boolean[] { true, true });
    p2.setBooleanPrimitive(false);
    testArrayContains(kieSession, p1, p2, list);
}
Also used : ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Primitives(org.drools.compiler.Primitives) Test(org.junit.Test)

Example 14 with Primitives

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

the class ArrayTest method testContainsIntArray.

@Test
public void testContainsIntArray() throws Exception {
    final KieSession kieSession = createKieSessionFromDrl(getDrl("Integer", false, "10"));
    final List list = new ArrayList();
    addGlobalsToSession(kieSession, list, 10, new Integer[] { 5, 10, 20 });
    final Primitives p1 = new Primitives();
    p1.setPrimitiveIntArray(new int[] { 5, 10, 20 });
    p1.setIntPrimitive(5);
    final Primitives p2 = new Primitives();
    p2.setPrimitiveIntArray(new int[] { 5, 10, 20 });
    p2.setIntPrimitive(10);
    testArrayContains(kieSession, p1, p2, list);
}
Also used : ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Primitives(org.drools.compiler.Primitives) Test(org.junit.Test)

Example 15 with Primitives

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

the class ArrayTest method testContainsBooleanArray.

@Test
public void testContainsBooleanArray() throws Exception {
    final KieSession kieSession = createKieSessionFromDrl(getDrl("Boolean", false, "true"));
    final List list = new ArrayList();
    addGlobalsToSession(kieSession, list, true, new Boolean[] { true, false });
    final Primitives p1 = new Primitives();
    p1.setPrimitiveBooleanArray(new boolean[] { true, false });
    p1.setBooleanPrimitive(false);
    final Primitives p2 = new Primitives();
    p2.setPrimitiveBooleanArray(new boolean[] { true, false });
    p2.setBooleanPrimitive(true);
    testArrayContains(kieSession, p1, p2, list);
}
Also used : ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Primitives(org.drools.compiler.Primitives) Test(org.junit.Test)

Aggregations

Primitives (org.drools.compiler.Primitives)27 Test (org.junit.Test)27 KieSession (org.kie.api.runtime.KieSession)27 ArrayList (java.util.ArrayList)23 List (java.util.List)23 KieBase (org.kie.api.KieBase)10 FactHandle (org.kie.api.runtime.rule.FactHandle)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Person (org.drools.compiler.Person)1 PersonInterface (org.drools.compiler.PersonInterface)1 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 KiePackage (org.kie.api.definition.KiePackage)1