Search in sources :

Example 51 with KieBase

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

the class MarshallingTest method testAccumulate2.

@Test
public void testAccumulate2() throws Exception {
    String str = "package org.drools\n" + "\n" + "import org.drools.compiler.Message\n" + "\n" + "rule MyRule\n" + "  when\n" + "    Number( intValue >= 5 ) from accumulate ( m: Message( ), count( m ) )\n" + "  then\n" + "    System.out.println(\"Found messages\");\n" + "end\n";
    KieBase kBase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kBase.newKieSession();
    ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.insert(new Message());
    ksession.insert(new Message());
    ksession.insert(new Message());
    ksession.insert(new Message());
    ksession.insert(new Message());
    ((InternalWorkingMemory) ksession).flushPropagations();
    assertEquals(1, ((InternalAgenda) ksession.getAgenda()).agendaSize());
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Message(org.drools.compiler.Message) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 52 with KieBase

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

the class RuleMetadataTest method testUpdate.

@Test
public void testUpdate() {
    String rule1 = "$a.setA( 20 );\n $a.setB( $bb );\n update( $a );";
    KieBase kbase = getKnowledgeBase(rule1);
    RuleImpl rule = getRule(kbase, "R0");
    ConsequenceMetaData consequenceMetaData = rule.getConsequenceMetaData();
    assertEquals(1, consequenceMetaData.getStatements().size());
    ConsequenceMetaData.Statement statment = consequenceMetaData.getStatements().get(0);
    assertEquals(ConsequenceMetaData.Statement.Type.MODIFY, statment.getType());
    assertEquals("org.drools.A", statment.getFactClassName());
    assertEquals(2, statment.getFields().size());
    ConsequenceMetaData.Field field1 = statment.getFields().get(0);
    assertEquals("a", field1.getName());
    assertEquals("20", field1.getValue());
    assertTrue(field1.isLiteral());
    ConsequenceMetaData.Field field2 = statment.getFields().get(1);
    assertEquals("b", field2.getName());
    assertEquals("$bb", field2.getValue());
    assertFalse(field2.isLiteral());
}
Also used : ConsequenceMetaData(org.drools.core.rule.ConsequenceMetaData) KieBase(org.kie.api.KieBase) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 53 with KieBase

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

the class RuleMetadataTest method testModify2.

@Test
public void testModify2() {
    String rule1 = "modify( $a ) { setC( $bc ) };\n modify( $b ) { c = \"Hello\" };";
    KieBase kbase = getKnowledgeBase(rule1);
    RuleImpl rule = getRule(kbase, "R0");
    ConsequenceMetaData consequenceMetaData = rule.getConsequenceMetaData();
    assertEquals(2, consequenceMetaData.getStatements().size());
    ConsequenceMetaData.Statement statment1 = consequenceMetaData.getStatements().get(0);
    assertEquals(ConsequenceMetaData.Statement.Type.MODIFY, statment1.getType());
    assertEquals("org.drools.A", statment1.getFactClassName());
    assertEquals(1, statment1.getFields().size());
    ConsequenceMetaData.Field field1 = statment1.getFields().get(0);
    assertEquals("c", field1.getName());
    assertEquals("$bc", field1.getValue());
    assertFalse(field1.isLiteral());
    ConsequenceMetaData.Statement statment2 = consequenceMetaData.getStatements().get(1);
    assertEquals(ConsequenceMetaData.Statement.Type.MODIFY, statment2.getType());
    assertEquals(RuleMetadataTest.B.class.getName(), statment2.getFactClassName());
    assertEquals(1, statment2.getFields().size());
    ConsequenceMetaData.Field field2 = statment2.getFields().get(0);
    assertEquals("c", field2.getName());
    assertEquals("\"Hello\"", field2.getValue());
    assertTrue(field2.isLiteral());
}
Also used : ConsequenceMetaData(org.drools.core.rule.ConsequenceMetaData) KieBase(org.kie.api.KieBase) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 54 with KieBase

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

the class RuleMetadataTest method testUpdate2.

@Test
public void testUpdate2() {
    String rule1 = "$a.setC( $bc );\n $b.c = \"Hello\";\n update( $a );\n update( $b );";
    KieBase kbase = getKnowledgeBase(rule1);
    RuleImpl rule = getRule(kbase, "R0");
    ConsequenceMetaData consequenceMetaData = rule.getConsequenceMetaData();
    assertEquals(2, consequenceMetaData.getStatements().size());
    ConsequenceMetaData.Statement statment1 = consequenceMetaData.getStatements().get(0);
    assertEquals(ConsequenceMetaData.Statement.Type.MODIFY, statment1.getType());
    assertEquals("org.drools.A", statment1.getFactClassName());
    assertEquals(1, statment1.getFields().size());
    ConsequenceMetaData.Field field1 = statment1.getFields().get(0);
    assertEquals("c", field1.getName());
    assertEquals("$bc", field1.getValue());
    assertFalse(field1.isLiteral());
    ConsequenceMetaData.Statement statment2 = consequenceMetaData.getStatements().get(1);
    assertEquals(ConsequenceMetaData.Statement.Type.MODIFY, statment2.getType());
    assertEquals(RuleMetadataTest.B.class.getName(), statment2.getFactClassName());
    assertEquals(1, statment2.getFields().size());
    ConsequenceMetaData.Field field2 = statment2.getFields().get(0);
    assertEquals("c", field2.getName());
    assertEquals("\"Hello\"", field2.getValue());
    assertTrue(field2.isLiteral());
}
Also used : ConsequenceMetaData(org.drools.core.rule.ConsequenceMetaData) KieBase(org.kie.api.KieBase) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 55 with KieBase

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

the class RuleMetadataTest method testRetract.

@Test
public void testRetract() {
    String rule1 = "retract( $b );";
    KieBase kbase = getKnowledgeBase(rule1);
    RuleImpl rule = getRule(kbase, "R0");
    ConsequenceMetaData consequenceMetaData = rule.getConsequenceMetaData();
    assertEquals(1, consequenceMetaData.getStatements().size());
    ConsequenceMetaData.Statement statment = consequenceMetaData.getStatements().get(0);
    assertEquals(ConsequenceMetaData.Statement.Type.RETRACT, statment.getType());
    assertEquals(RuleMetadataTest.B.class.getName(), statment.getFactClassName());
}
Also used : ConsequenceMetaData(org.drools.core.rule.ConsequenceMetaData) KieBase(org.kie.api.KieBase) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) 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