use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class MarshallingTest method testScheduledActivation.
@Test
@Ignore("This test is suspicious to say the least...")
public void testScheduledActivation() {
KnowledgeBaseImpl knowledgeBase = (KnowledgeBaseImpl) KnowledgeBaseFactory.newKnowledgeBase();
KnowledgePackageImpl impl = new KnowledgePackageImpl("test");
BuildContext buildContext = new BuildContext(knowledgeBase);
// simple rule that fires after 10 seconds
final RuleImpl rule = new RuleImpl("test-rule");
new RuleTerminalNode(1, new MockTupleSource(2), rule, rule.getLhs(), 0, buildContext);
final List<String> fired = new ArrayList<String>();
rule.setConsequence(new Consequence() {
public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
fired.add("a");
}
public String getName() {
return "default";
}
});
rule.setTimer(new DurationTimer(10000));
rule.setPackage("test");
impl.addRule(rule);
knowledgeBase.addPackages(Collections.singleton(impl));
SessionConfiguration config = SessionConfiguration.newInstance();
config.setClockType(ClockType.PSEUDO_CLOCK);
KieSession ksession = knowledgeBase.newKieSession(config, KieServices.get().newEnvironment());
PseudoClockScheduler scheduler = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
Marshaller marshaller = MarshallerFactory.newMarshaller(knowledgeBase);
ksession.insert("cheese");
assertTrue(fired.isEmpty());
// marshall, then unmarshall session
readWrite(knowledgeBase, ksession, config);
// the activations should fire after 10 seconds
assertTrue(fired.isEmpty());
scheduler.advanceTime(12, TimeUnit.SECONDS);
assertFalse(fired.isEmpty());
}
use of org.drools.core.definitions.rule.impl.RuleImpl 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());
}
use of org.drools.core.definitions.rule.impl.RuleImpl 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());
}
use of org.drools.core.definitions.rule.impl.RuleImpl 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());
}
use of org.drools.core.definitions.rule.impl.RuleImpl 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());
}
Aggregations