Search in sources :

Example 31 with KieBase

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

the class DeclareTest method testDeclareAndFrom.

@Test
public void testDeclareAndFrom() throws Exception {
    final KieBase kbase = loadKnowledgeBase("test_DeclareWithFrom.drl");
    final FactType profileType = kbase.getFactType("org.drools.compiler", "Profile");
    final KieSession ksession = createKnowledgeSession(kbase);
    final Object profile = profileType.newInstance();
    final Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("internet", Integer.valueOf(2));
    profileType.set(profile, "pageFreq", map);
    ksession.insert(profile);
    ksession.fireAllRules();
    ksession.dispose();
}
Also used : HashMap(java.util.HashMap) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 32 with KieBase

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

the class DeclareTest method testDeclaredTypesDefaultHashCode.

@Test
public void testDeclaredTypesDefaultHashCode() {
    // JBRULES-3481
    final String str = "package com.sample\n" + "\n" + "global java.util.List list; \n" + "" + "declare Bean\n" + " id : int \n" + "end\n" + "\n" + "declare KeyedBean\n" + " id : int @key \n" + "end\n" + "\n" + "\n" + "rule Create\n" + "when\n" + "then\n" + " list.add( new Bean(1) ); \n" + " list.add( new Bean(2) ); \n" + " list.add( new KeyedBean(1) ); \n" + " list.add( new KeyedBean(1) ); \n" + "end\n";
    final KieBase kbase = loadKnowledgeBaseFromString(str);
    final KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    ksession.dispose();
    assertFalse(list.get(0).hashCode() == 34);
    assertFalse(list.get(1).hashCode() == 34);
    assertFalse(list.get(0).hashCode() == list.get(1).hashCode());
    assertNotSame(list.get(0), list.get(1));
    assertFalse(list.get(0).equals(list.get(1)));
    assertTrue(list.get(2).hashCode() == 32);
    assertTrue(list.get(3).hashCode() == 32);
    assertNotSame(list.get(2), list.get(3));
    assertTrue(list.get(2).equals(list.get(3)));
}
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) Test(org.junit.Test)

Example 33 with KieBase

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

the class LiteralTest method testLiteral.

@Test
public void testLiteral() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("literal_rule_test.drl"));
    KieSession session = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    session.setGlobal("list", list);
    final Cheese stilton = new Cheese("stilton", 5);
    session.insert(stilton);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    assertEquals("stilton", ((List) session.getGlobal("list")).get(0));
}
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) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 34 with KieBase

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

the class LiteralTest method testLiteralWithEscapes.

@Test
public void testLiteralWithEscapes() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_literal_with_escapes.drl"));
    KieSession session = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    session.setGlobal("list", list);
    final String expected = "s\tti\"lto\nn";
    final Cheese stilton = new Cheese(expected, 5);
    session.insert(stilton);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    final int fired = session.fireAllRules();
    assertEquals(1, fired);
    assertEquals(expected, ((List) session.getGlobal("list")).get(0));
}
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) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 35 with KieBase

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

the class LiteralTest method testBigDecimalIntegerLiteral.

@Test
public void testBigDecimalIntegerLiteral() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("big_decimal_and_literal.drl"));
    KieSession session = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    session.setGlobal("list", list);
    final PersonInterface bill = new Person("bill", null, 12);
    bill.setBigDecimal(new BigDecimal("42"));
    bill.setBigInteger(new BigInteger("42"));
    session.insert(bill);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    assertEquals(6, ((List) session.getGlobal("list")).size());
}
Also used : PersonInterface(org.drools.compiler.PersonInterface) 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) Person(org.drools.compiler.Person) BigDecimal(java.math.BigDecimal) 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