use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.
the class ReteooRuleBuilderTest method testAddRuleWithPatterns.
@Test
public void testAddRuleWithPatterns() {
final RuleImpl rule = new RuleImpl("only patterns");
final Pattern c1 = new Pattern(0, new ClassObjectType(String.class));
final Pattern c2 = new Pattern(1, new ClassObjectType(String.class));
final Pattern c3 = new Pattern(2, new ClassObjectType(String.class));
final GroupElement lhsroot = GroupElementFactory.newAndInstance();
lhsroot.addChild(c1);
lhsroot.addChild(c2);
lhsroot.addChild(c3);
rule.setLhs(lhsroot);
final Consequence consequence = new Consequence() {
public void evaluate(KnowledgeHelper knowledgeHelper, ReteEvaluator reteEvaluator) throws Exception {
System.out.println("Consequence!");
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
};
rule.setConsequence(consequence);
final List terminals = this.builder.addRule(rule, this.rulebase, Collections.emptyList());
assertEquals("Rule must have a single terminal node", 1, terminals.size());
final RuleTerminalNode terminal = (RuleTerminalNode) terminals.get(0);
}
use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.
the class MarshallingTest method testScheduledActivation.
@Test
@Ignore("This test is suspicious to say the least...")
public void testScheduledActivation() {
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
InternalKnowledgePackage impl = CoreComponentFactory.get().createKnowledgePackage("test");
BuildContext buildContext = new BuildContext(knowledgeBase, Collections.emptyList());
// simple rule that fires after 10 seconds
final RuleImpl rule = new RuleImpl("test-rule");
new RuleTerminalNode(1, new MockTupleSource(2, buildContext), rule, rule.getLhs(), 0, buildContext);
final List<String> fired = new ArrayList<String>();
rule.setConsequence(new Consequence() {
public void evaluate(KnowledgeHelper knowledgeHelper, ReteEvaluator reteEvaluator) 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.common.ReteEvaluator in project drools by kiegroup.
the class RuleExecutor method evaluateNetworkAndFire.
public int evaluateNetworkAndFire(ActivationsManager activationsManager, AgendaFilter filter, int fireCount, int fireLimit) {
ReteEvaluator reteEvaluator = activationsManager.getReteEvaluator();
reEvaluateNetwork(activationsManager);
if (reteEvaluator.getSessionConfiguration().isDirectFiring()) {
RuleTerminalNode rtn = (RuleTerminalNode) pmem.getPathEndNode();
RuleImpl rule = rtn.getRule();
int directFirings = tupleList.size();
for (Tuple tuple = tupleList.getFirst(); tuple != null; tuple = tupleList.getFirst()) {
if (cancelAndContinue(reteEvaluator, rtn, rule, tuple, filter)) {
directFirings--;
} else {
innerFireActivation(reteEvaluator, activationsManager, (Activation) tuple, ((Activation) tuple).getConsequence());
}
removeLeftTuple(tuple);
}
ruleAgendaItem.remove();
return directFirings;
}
return fire(reteEvaluator, activationsManager, filter, fireCount, fireLimit);
}
use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.
the class InterpretedRuleUnit method internalCreateInstance.
@Override
public RuleUnitInstance<T> internalCreateInstance(T data) {
KnowledgeBuilder kBuilder = new KnowledgeBuilderImpl();
Class<? extends RuleUnitData> wmClass = data.getClass();
String canonicalName = wmClass.getCanonicalName();
// transform foo.bar.Baz to /foo/bar/Baz.drl
// this currently only works for single files
InputStream resourceAsStream = wmClass.getResourceAsStream(String.format("/%s.drl", canonicalName.replace('.', '/')));
kBuilder.add(new InputStreamResource(resourceAsStream), ResourceType.DRL);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addPackages(kBuilder.getKnowledgePackages());
ReteEvaluator reteEvaluator = new RuleUnitExecutorImpl(kBase);
return new InterpretedRuleUnitInstance<>(this, data, reteEvaluator);
}
use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.
the class ReteooRuleBaseMultiThreadedTest method setUp.
@Before
public void setUp() {
this.kBase = KnowledgeBaseFactory.newKnowledgeBase();
pkg = CoreComponentFactory.get().createKnowledgePackage("org.droos.test");
JavaDialectRuntimeData data = new JavaDialectRuntimeData();
data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
pkg.getDialectRuntimeRegistry().setDialectData("java", data);
// we need to add one rule to the package because the previous deadlock was encountered
// while removing rules from a package when said package is removed from the rulebase
rule = new RuleImpl("Test");
rule.setDialect("java");
rule.setConsequence(new Consequence() {
public void evaluate(KnowledgeHelper knowledgeHelper, ReteEvaluator reteEvaluator) throws Exception {
}
public String getName() {
return "default";
}
});
pkg.addRule(rule);
kBase.addPackage(pkg);
}
Aggregations