use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class StreamsTest method testEventExpirationValue.
@Test(timeout = 10000)
public void testEventExpirationValue() throws Exception {
String drl1 = "package org.drools.pkg1\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + " @role(event)\n" + "end\n" + "rule X\n" + "when\n" + " StockTick()\n" + "then\n" + "end\n";
String drl2 = "package org.drools.pkg2\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + " @role(event)\n" + "end\n" + "rule X\n" + "when\n" + " StockTick()\n" + "then\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl1.getBytes()), ResourceType.DRL);
kbuilder.add(ResourceFactory.newByteArrayResource(drl2.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf);
kbase.addPackages(kbuilder.getKnowledgePackages());
List<ObjectTypeNode> otns = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
ObjectType stot = new ClassObjectType(StockTick.class);
for (ObjectTypeNode otn : otns) {
if (otn.getObjectType().isAssignableFrom(stot)) {
assertEquals(NEVER_EXPIRES, otn.getExpirationOffset());
}
}
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class MarshallingTest method testSinglePattern.
@Test
public void testSinglePattern() throws Exception {
String rule = "package org.drools.compiler.test;\n";
rule += "import org.drools.compiler.Person\n";
rule += "global java.util.List list\n";
rule += "rule \"Rule 1\"\n";
rule += "when\n";
rule += " $p : Person( ) \n";
rule += "then\n";
rule += " list.add( $p );\n";
rule += "end";
KieBase kBase = loadKnowledgeBaseFromString(rule);
// Make sure the rete node map is created correctly
Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
assertEquals(3, nodes.size());
assertEquals("Person", ((ClassObjectType) ((ObjectTypeNode) nodes.get(3)).getObjectType()).getClassType().getSimpleName());
assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(5)).getRule().getName());
KieSession session = kBase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
Person p = new Person("bobba fet", 32);
session.insert(p);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.fireAllRules();
assertEquals(1, ((List) session.getGlobal("list")).size());
assertEquals(p, ((List) session.getGlobal("list")).get(0));
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class FromTest method testFromSharingWithPropertyReactive.
@Test
public void testFromSharingWithPropertyReactive() {
// As above but with property reactive as default
String drl = "import " + ListsContainer.class.getCanonicalName() + "\n" + "global java.util.List output1;\n" + "global java.util.List output2;\n" + "rule R1 when\n" + " ListsContainer( $list : list1 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output1.add($s);\n" + "end\n" + "rule R2 when\n" + " ListsContainer( $list : list2 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output2.add($s);\n" + "end\n" + "rule R3 when\n" + " ListsContainer( $list : list2 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output2.add($s);\n" + "end\n";
// property reactive as default:
KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
ReteDumper.dumpRete(kbase);
List<String> output1 = new ArrayList<String>();
ksession.setGlobal("output1", output1);
List<String> output2 = new ArrayList<String>();
ksession.setGlobal("output2", output2);
FactHandle fh = ksession.insert(new ListsContainer());
ksession.fireAllRules();
assertEquals("bb", output1.get(0));
assertEquals("22", output2.get(0));
assertEquals("22", output2.get(1));
EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(ListsContainer.class));
// There are 2 LIAs, one for the list1 and the other for the list2
assertEquals(2, otn.getObjectSinkPropagator().size());
LeftInputAdapterNode lia0 = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
// There are only 2 FromNodes since R2 and R3 are sharing the second From
// The first FROM node has R1 has sink
LeftTupleSink[] sinks0 = lia0.getSinkPropagator().getSinks();
assertEquals(1, sinks0.length);
assertEquals(1, sinks0[0].getSinkPropagator().size());
// The second FROM node has both R2 and R3 as sinks
LeftInputAdapterNode lia1 = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[1];
LeftTupleSink[] sinks1 = lia1.getSinkPropagator().getSinks();
assertEquals(1, sinks1.length);
assertEquals(2, sinks1[0].getSinkPropagator().size());
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class FromTest method testFromSharing.
@Test
public void testFromSharing() {
// Keeping original test as non-property reactive by default, just allowed.
String drl = "import " + ListsContainer.class.getCanonicalName() + "\n" + "global java.util.List output1;\n" + "global java.util.List output2;\n" + "rule R1 when\n" + " ListsContainer( $list : list1 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output1.add($s);\n" + "end\n" + "rule R2 when\n" + " ListsContainer( $list : list2 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output2.add($s);\n" + "end\n" + "rule R3 when\n" + " ListsContainer( $list : list2 )\n" + " $s : String( length == 2 ) from $list\n" + "then\n" + " output2.add($s);\n" + "end\n";
KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(drl, ResourceType.DRL).build();
KieSession ksession = kbase.newKieSession();
ReteDumper.dumpRete(kbase);
List<String> output1 = new ArrayList<String>();
ksession.setGlobal("output1", output1);
List<String> output2 = new ArrayList<String>();
ksession.setGlobal("output2", output2);
FactHandle fh = ksession.insert(new ListsContainer());
ksession.fireAllRules();
assertEquals("bb", output1.get(0));
assertEquals("22", output2.get(0));
assertEquals("22", output2.get(1));
EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNodes().values().iterator().next();
ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(ListsContainer.class));
// There is only 1 LIA
assertEquals(1, otn.getObjectSinkPropagator().size());
LeftInputAdapterNode lian = (LeftInputAdapterNode) otn.getObjectSinkPropagator().getSinks()[0];
// There are only 2 FromNodes since R2 and R3 are sharing the second From
LeftTupleSink[] sinks = lian.getSinkPropagator().getSinks();
assertEquals(2, sinks.length);
// The first from has R1 has sink
assertEquals(1, sinks[0].getSinkPropagator().size());
// The second from has both R2 and R3 as sinks
assertEquals(2, sinks[1].getSinkPropagator().size());
}
use of org.drools.core.base.ClassObjectType in project drools by kiegroup.
the class MVELConsequenceBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() throws Exception {
PackageDescr pkgDescr = new PackageDescr("pkg1");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
pkgBuilder.addPackage(pkgDescr);
InternalKnowledgePackage pkg = pkgBuilder.getPackageRegistry("pkg1").getPackage();
final RuleDescr ruleDescr = new RuleDescr("rule 1");
ruleDescr.setNamespace("pkg1");
ruleDescr.setConsequence("modify (cheese) {price = 5 };\nretract (cheese)");
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final ObjectType cheeseObjeectType = new ClassObjectType(Cheese.class);
final Pattern pattern = new Pattern(0, cheeseObjeectType, "cheese");
final GroupElement subrule = new GroupElement(GroupElement.AND);
subrule.addChild(pattern);
final Map<String, Declaration> map = new HashMap<String, Declaration>();
map.put("cheese", pattern.getDeclaration());
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
final MVELConsequenceBuilder builder = new MVELConsequenceBuilder();
builder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
kBase.addPackage(pkg);
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
f0.removeLeftTuple(tuple);
final AgendaItem item = new AgendaItemImpl(0, tuple, 10, pctxFactory.createPropagationContext(1, PropagationContext.Type.DELETION, null, tuple != null ? (TerminalNode) tuple.getTupleSink() : null, null), new RuleTerminalNode(0, new CompositeObjectSinkAdapterTest.MockBetaNode(), context.getRule(), subrule, 0, new BuildContext(kBase)), null);
final DefaultKnowledgeHelper kbHelper = new DefaultKnowledgeHelper(ksession);
kbHelper.setActivation(item);
((MVELConsequence) context.getRule().getConsequence()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
context.getRule().getConsequence().evaluate(kbHelper, ksession);
assertEquals(5, cheddar.getPrice());
}
Aggregations