use of org.drools.compiler.Address in project drools by kiegroup.
the class MVELTest method testNewConstructor.
@Test
public void testNewConstructor() {
final String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when \n" + " $m : Person( address == new Address('s1')) \n" + "then \n" + " list.add('r1'); \n" + "end \n";
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSession ksession = createKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.setGlobal("list", list);
final Person p = new Person("yoda");
p.setAddress(new Address("s1"));
ksession.insert(p);
ksession.fireAllRules();
assertEquals("r1", list.get(0));
// Check it was built with MVELReturnValueExpression constraint
final List<ObjectTypeNode> nodes = ((InternalKnowledgeBase) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (final ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
node = n;
break;
}
}
final AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
final AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
if (constraint instanceof MvelConstraint) {
assertTrue(((MvelConstraint) constraint).getFieldExtractor() instanceof ClassFieldReader);
final FieldValue r = ((MvelConstraint) constraint).getField();
assertEquals(p.getAddress(), r.getValue());
}
}
use of org.drools.compiler.Address in project drools by kiegroup.
the class MapConstraintTest method testMapNullConstraint.
@Test
public void testMapNullConstraint() throws Exception {
final KieBase kbase = loadKnowledgeBase("test_mapNullConstraints.drl");
final KieSession ksession = createKnowledgeSession(kbase);
final org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
ksession.addEventListener(ael);
new WorkingMemoryConsoleLogger((WorkingMemory) ksession);
final Map addresses = new HashMap();
addresses.put("home", new Address("home street"));
final Person bob = new Person("Bob");
bob.setNamedAddresses(addresses);
ksession.insert(bob);
ksession.fireAllRules();
final ArgumentCaptor<AfterMatchFiredEvent> arg = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
verify(ael, times(4)).afterMatchFired(arg.capture());
org.kie.api.event.rule.AfterMatchFiredEvent aaf = arg.getAllValues().get(0);
assertThat(aaf.getMatch().getRule().getName(), is("1. home != null"));
aaf = arg.getAllValues().get(1);
assertThat(aaf.getMatch().getRule().getName(), is("2. not home == null"));
aaf = arg.getAllValues().get(2);
assertThat(aaf.getMatch().getRule().getName(), is("7. work == null"));
aaf = arg.getAllValues().get(3);
assertThat(aaf.getMatch().getRule().getName(), is("8. not work != null"));
}
use of org.drools.compiler.Address in project drools by kiegroup.
the class BackwardChainingTest method testQueriesWithNestedAcecssorsMixedArgs.
@Test(timeout = 10000)
public void testQueriesWithNestedAcecssorsMixedArgs() throws Exception {
String str = "" + "package org.drools.compiler.test \n" + "import org.drools.compiler.Person \n" + "global java.util.List list\n" + "query peeps( String $name, String $likes, String $street ) \n" + " Person( $name := name, $likes := likes, $street := address.street ) \n" + "end\n";
str += "rule x1\n" + "when\n" + " $s : String()\n" + // output, output, ,input
" ?peeps($name1; $likes1 : $likes, $street : $s )\n" + "then\n" + " list.add( $name1 + \" : \" + $likes1 + \" : \" + $s );\n" + "end \n";
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
KieSession ksession = createKnowledgeSession(kbase);
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
Person p1 = new Person("darth", "stilton", 100);
p1.setAddress(new Address("s1"));
Person p2 = new Person("yoda", "stilton", 300);
p2.setAddress(new Address("s2"));
ksession.insert(p1);
ksession.insert(p2);
ksession.insert("s1");
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(1, list.size());
assertTrue(list.contains("darth : stilton : s1"));
list.clear();
ksession.insert("s2");
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(1, list.size());
assertTrue(list.contains("yoda : stilton : s2"));
}
use of org.drools.compiler.Address in project drools by kiegroup.
the class BackwardChainingTest method testQueriesWithNestedAcecssorsAllOutputs.
@Test(timeout = 10000)
public void testQueriesWithNestedAcecssorsAllOutputs() throws Exception {
String str = "" + "package org.drools.compiler.test \n" + "import org.drools.compiler.Person \n" + "global java.util.List list\n" + "query peeps( String $name, String $likes, String $street ) \n" + " Person( $name := name, $likes := likes, $street := address.street ) \n" + "end\n";
str += "rule x1\n" + "when\n" + " String( this == \"go1\" )\n" + // output, output, ,output
" ?peeps($name1; $likes1 : $likes, $street1 : $street )\n" + "then\n" + " list.add( $name1 + \" : \" + $likes1 + \" : \" + $street1 );\n" + "end \n";
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
KieSession ksession = createKnowledgeSession(kbase);
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
Person p1 = new Person("darth", "stilton", 100);
p1.setAddress(new Address("s1"));
Person p2 = new Person("yoda", "stilton", 300);
p2.setAddress(new Address("s2"));
ksession.insert(p1);
ksession.insert(p2);
ksession.insert("go1");
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(2, list.size());
assertTrue(list.contains("darth : stilton : s1"));
assertTrue(list.contains("yoda : stilton : s2"));
}
use of org.drools.compiler.Address in project drools by kiegroup.
the class CustomOperatorTest method testCustomOperatorOnKieModule.
@Test
public void testCustomOperatorOnKieModule() {
String str = "import org.drools.compiler.Person\n" + "import org.drools.compiler.Address\n" + "rule R when\n" + " $alice : Person(name == \"Alice\")\n" + " $bob : Person(name == \"Bob\", addresses supersetOf $alice.addresses)\n" + "then\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieSession ksession = new KieHelper().setKieModuleModel(ks.newKieModuleModel().setConfigurationProperty("drools.evaluator.supersetOf", SupersetOfEvaluatorDefinition.class.getName())).addContent(str, ResourceType.DRL).build().newKieSession();
Person alice = new Person("Alice", 30);
alice.addAddress(new Address("Large Street", "BigTown", "12345"));
Person bob = new Person("Bob", 30);
bob.addAddress(new Address("Large Street", "BigTown", "12345"));
bob.addAddress(new Address("Long Street", "SmallTown", "54321"));
ksession.insert(alice);
ksession.insert(bob);
assertEquals(1, ksession.fireAllRules());
}
Aggregations