Search in sources :

Example 1 with Primitives

use of org.drools.testcoverage.common.model.Primitives in project drools by kiegroup.

the class ContainsTest method testContainsInArray.

@Test
public void testContainsInArray() {
    final String drl = "package org.drools.compiler.integrationtests.operators\n" + "import " + Primitives.class.getCanonicalName() + " ;\n" + "global java.util.List list;\n" + "\n" + "rule \"contains in elements\"\n" + "    salience 10\n" + "    when\n" + "        Primitives( stringArray contains \"test1\" )\n" + "    then\n" + "        list.add( \"ok1\" );\n" + "end\n" + "\n" + "rule \"excludes in elements\"\n" + "    when\n" + "        Primitives( stringArray excludes \"test2\" )\n" + "    then\n" + "        list.add( \"ok2\" );\n" + "end";
    final KieBase kieBase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("contains-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kieBase.newKieSession();
    try {
        final List list = new ArrayList();
        ksession.setGlobal("list", list);
        final Primitives p = new Primitives();
        p.setStringArray(new String[] { "test1", "test3" });
        ksession.insert(p);
        ksession.fireAllRules();
        assertEquals(2, list.size());
        assertEquals("ok1", list.get(0));
        assertEquals("ok2", list.get(1));
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Primitives(org.drools.testcoverage.common.model.Primitives) Test(org.junit.Test)

Example 2 with Primitives

use of org.drools.testcoverage.common.model.Primitives in project drools by kiegroup.

the class EqualsTest method testNotEqualsOperator.

@Test
public void testNotEqualsOperator() {
    // JBRULES-3003: restriction evaluation returns 'false' for "trueField != falseField"
    final String str = "package org.drools.compiler.integrationtests.operators\n" + "import " + Primitives.class.getCanonicalName() + " ;\n" + "rule NotEquals\n" + "when\n" + "    Primitives( booleanPrimitive != booleanWrapper )\n" + "then\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("equals-test", kieBaseTestConfiguration, str);
    final KieSession ksession = kbase.newKieSession();
    try {
        final Primitives p = new Primitives();
        p.setBooleanPrimitive(true);
        p.setBooleanWrapper(Boolean.FALSE);
        ksession.insert(p);
        final int rules = ksession.fireAllRules();
        ksession.dispose();
        assertEquals(1, rules);
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Primitives(org.drools.testcoverage.common.model.Primitives) Test(org.junit.Test)

Example 3 with Primitives

use of org.drools.testcoverage.common.model.Primitives in project drools by kiegroup.

the class EqualsTest method testCharComparisons.

@Test
public void testCharComparisons() {
    final String drl = "package org.drools.compiler.integrationtests.operators\n" + "import " + Primitives.class.getCanonicalName() + " ;\n" + "global java.util.List results;\n" + "\n" + "rule \"test chars 1\"\n" + "    salience 100\n" + "when\n" + "    Primitives( charPrimitive == 'a' ) \n" + "then\n" + "    results.add( \"1\" );\n" + "end\n" + "\n" + "rule \"test chars 2\"\n" + "    salience 90\n" + "when\n" + "    Primitives( $c1: charPrimitive == 'a' ) \n" + "    Primitives( charPrimitive != $c1 )\n" + "then\n" + "    results.add( \"2\" );\n" + "end\n" + "\n" + "rule \"test chars 3\"\n" + "    salience 80\n" + "when\n" + "    Primitives( $c1: stringAttribute == 'a' ) \n" + "    Primitives( charPrimitive == $c1 )\n" + "then\n" + "    results.add( \"3\" );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("equals-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final List results = new ArrayList();
        ksession.setGlobal("results", results);
        final Primitives p1 = new Primitives();
        p1.setCharPrimitive('a');
        p1.setStringAttribute("b");
        final Primitives p2 = new Primitives();
        p2.setCharPrimitive('b');
        p2.setStringAttribute("a");
        ksession.insert(p1);
        ksession.insert(p2);
        ksession.fireAllRules();
        assertEquals(3, results.size());
        assertEquals("1", results.get(0));
        assertEquals("2", results.get(1));
        assertEquals("3", results.get(2));
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Primitives(org.drools.testcoverage.common.model.Primitives) Test(org.junit.Test)

Example 4 with Primitives

use of org.drools.testcoverage.common.model.Primitives in project drools by kiegroup.

the class JoinNodeRangeIndexingTest method testCoercionBigDecimalVsInt.

@Test
public void testCoercionBigDecimalVsInt() {
    final String drl = "package org.drools.compiler.integrationtests;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Primitives.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R\n" + "    when\n" + "        Cheese($price : price)\n" + "        p : Primitives(bigDecimal < $price)\n" + "    then\n" + "        list.add( p );\n" + "end";
    // Integer is coerced to BigDecimal
    final KieBase kbase = getKieBaseWithRangeIndexOption(drl);
    assertIndexedTrue(kbase, Primitives.class);
    final KieSession ksession = kbase.newKieSession();
    try {
        final List<Primitives> list = new ArrayList<>();
        ksession.setGlobal("list", list);
        final Primitives bd42 = new Primitives();
        bd42.setBigDecimal(new BigDecimal("42"));
        ksession.insert(bd42);
        final Primitives bd43 = new Primitives();
        bd43.setBigDecimal(new BigDecimal("43"));
        ksession.insert(bd43);
        ksession.insert(new Cheese("gorgonzola", 43));
        ksession.fireAllRules();
        Assertions.assertThat(list).containsExactly(bd42);
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Cheese(org.drools.testcoverage.common.model.Cheese) KieSession(org.kie.api.runtime.KieSession) Primitives(org.drools.testcoverage.common.model.Primitives) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 5 with Primitives

use of org.drools.testcoverage.common.model.Primitives in project drools by kiegroup.

the class LiteralTest method testBigDecimalIntegerLiteral.

@Test
public void testBigDecimalIntegerLiteral() {
    final String drl = "package org.drools.compiler.integrationtests.drl\n" + "\n" + "import " + Primitives.class.getCanonicalName() + ";\n" + "import java.math.BigDecimal;\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule \"BigDec\"\n" + "\n" + "    when\n" + "        p: Primitives(bigDecimal < 100.01)\n" + "    then\n" + "        System.err.println(\"rule1\");\n" + "        list.add( p );\n" + "end\n" + "\n" + "rule \"BigInt\"\n" + "    when\n" + "        p: Primitives(bigInteger < 100.1)\n" + "    then\n" + "    System.err.println(\"rule2\");\n" + "        list.add( p );\n" + "end\n" + "\n" + "rule \"BigDec2\"\n" + "\n" + "    when\n" + "        p: Primitives(bigDecimal == 42)\n" + "    then\n" + "    System.err.println(\"rule3\");\n" + "        list.add( p );\n" + "end\n" + "\n" + "rule \"BigInt2\"\n" + "    when\n" + "        p: Primitives(bigInteger == 42)\n" + "    then\n" + "    System.err.println(\"rule4\");\n" + "        list.add( p );\n" + "end\n" + "\n" + "rule \"BigDec3\"\n" + "\n" + "    when\n" + "        p: Primitives(bigDecimal != 100)\n" + "    then\n" + "    System.err.println(\"rule5\");\n" + "        list.add( p );\n" + "end\n" + "\n" + "rule \"BigInt3\"\n" + "    when\n" + "        p: Primitives(bigInteger != 100)\n" + "    then\n" + "    System.err.println(\"rule6\");\n" + "        list.add( p );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("literal-test", kieBaseTestConfiguration, drl);
    final KieSession session = kbase.newKieSession();
    try {
        final List list = new ArrayList();
        session.setGlobal("list", list);
        final Primitives bill = new Primitives();
        bill.setBigDecimal(new BigDecimal("42"));
        bill.setBigInteger(new BigInteger("42"));
        session.insert(bill);
        session.fireAllRules();
        assertEquals(6, ((List) session.getGlobal("list")).size());
    } finally {
        session.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Primitives(org.drools.testcoverage.common.model.Primitives) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

Primitives (org.drools.testcoverage.common.model.Primitives)28 Test (org.junit.Test)28 KieBase (org.kie.api.KieBase)28 KieSession (org.kie.api.runtime.KieSession)28 ArrayList (java.util.ArrayList)24 List (java.util.List)22 BigDecimal (java.math.BigDecimal)6 Cheese (org.drools.testcoverage.common.model.Cheese)2 BigInteger (java.math.BigInteger)1 Person (org.drools.testcoverage.common.model.Person)1