Search in sources :

Example 1 with Primitives

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

the class JBRULESTest method testJBRULES_2995.

@Test
public void testJBRULES_2995() {
    final String str = "package org.drools.mvel.compiler\n" + "rule r1\n" + "when\n" + "    Primitives( classAttr == java.lang.String.class, \n" + "                eval(classAttr.equals( java.lang.String.class ) ),\n" + "                classAttr == String.class )\n" + "then\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    final Primitives primitives = new Primitives();
    primitives.setClassAttr(String.class);
    ksession.insert(primitives);
    final int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Primitives(org.drools.mvel.compiler.Primitives) Test(org.junit.Test)

Example 2 with Primitives

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

the class FieldAccessTest method testAccessClassTypeField.

@Test
public void testAccessClassTypeField() {
    final String str = "package org.drools.mvel.compiler\n" + "rule r1\n" + "when\n" + "    Primitives( classAttr == null )" + "then\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new Primitives());
    final int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Primitives(org.drools.mvel.compiler.Primitives) Test(org.junit.Test)

Example 3 with Primitives

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

the class MarshallingTest method testSerializationOfIndexedWM.

@Test
public void testSerializationOfIndexedWM() throws Exception {
    Collection<KiePackage> kpkgs = loadKnowledgePackages("test_Serializable2.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    kBase.addPackages(kpkgs);
    org.kie.api.definition.rule.Rule[] rules = (org.kie.api.definition.rule.Rule[]) kBase.getPackage("org.drools.compiler").getRules().toArray(new org.kie.api.definition.rule.Rule[0]);
    assertEquals(3, rules.length);
    KieSession session = kBase.newKieSession();
    session.setGlobal("list", new ArrayList());
    final Primitives p = new Primitives();
    p.setBytePrimitive((byte) 1);
    p.setShortPrimitive((short) 2);
    p.setIntPrimitive((int) 3);
    session.insert(p);
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    assertEquals(1, session.getObjects().size());
    Assert.assertEquals(p, IteratorToList.convert(session.getObjects().iterator()).get(0));
    assertEquals(3, ((InternalAgenda) session.getAgenda()).getAgendaGroupsManager().agendaSize());
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    final List list = (List) session.getGlobal("list");
    assertEquals(3, list.size());
    // because of agenda-groups
    assertEquals("1", list.get(0));
    assertEquals("2", list.get(1));
    assertEquals("3", list.get(2));
}
Also used : ArrayList(java.util.ArrayList) InternalAgenda(org.drools.core.common.InternalAgenda) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) Primitives(org.drools.mvel.compiler.Primitives) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 4 with Primitives

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

the class NullTest method testNullConstraint.

@Test
public void testNullConstraint() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "null_constraint.drl");
    KieSession session = kbase.newKieSession();
    final List foo = new ArrayList();
    session.setGlobal("messages", foo);
    final PersonInterface p1 = new Person(null, "food", 40);
    final Primitives p2 = new Primitives();
    p2.setArrayAttribute(null);
    session.insert(p1);
    session.insert(p2);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    assertEquals(2, ((List) session.getGlobal("messages")).size());
}
Also used : PersonInterface(org.drools.mvel.compiler.PersonInterface) 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.mvel.compiler.Primitives) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 5 with Primitives

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

the class TypeCoercionTest method testRuntimeTypeCoercion2.

@Test
public void testRuntimeTypeCoercion2() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_RuntimeTypeCoercion2.drl");
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("results", list);
    final Primitives fact = new Primitives();
    fact.setBooleanPrimitive(true);
    fact.setBooleanWrapper(Boolean.TRUE);
    fact.setObject(Boolean.TRUE);
    fact.setCharPrimitive('X');
    final FactHandle handle = ksession.insert(fact);
    ksession.fireAllRules();
    int index = 0;
    assertEquals(list.toString(), 4, list.size());
    assertEquals("boolean", list.get(index++));
    assertEquals("boolean wrapper", list.get(index++));
    assertEquals("boolean object", list.get(index++));
    assertEquals("char", list.get(index++));
    fact.setBooleanPrimitive(false);
    fact.setBooleanWrapper(null);
    fact.setCharPrimitive('\0');
    fact.setObject('X');
    ksession.update(handle, fact);
    ksession.fireAllRules();
    assertEquals(5, list.size());
    assertEquals("char object", list.get(index++));
    fact.setObject(null);
    ksession.update(handle, fact);
    ksession.fireAllRules();
    assertEquals(6, list.size());
    assertEquals("null object", list.get(index));
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) 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.mvel.compiler.Primitives) Test(org.junit.Test)

Aggregations

Primitives (org.drools.mvel.compiler.Primitives)5 Test (org.junit.Test)5 KieSession (org.kie.api.runtime.KieSession)5 KieBase (org.kie.api.KieBase)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 InternalAgenda (org.drools.core.common.InternalAgenda)1 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)1 Person (org.drools.mvel.compiler.Person)1 PersonInterface (org.drools.mvel.compiler.PersonInterface)1 IteratorToList (org.drools.mvel.integrationtests.IteratorToList)1 KiePackage (org.kie.api.definition.KiePackage)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1