@Test public void testFromWithOr() throws Exception { KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder(); builder.add( ResourceFactory.newInputStreamResource( getClass().getResourceAsStream("test_FromWithOr.drl")), ResourceType.DRL); if (builder.hasErrors()) { logger.info(builder.getErrors().toString()); } assertFalse(builder.hasErrors()); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(builder.getKnowledgePackages()); final StatefulKnowledgeSession session = createKnowledgeSession(kbase); final List<Address> results = new ArrayList<Address>(); session.setGlobal("results", results); Address a1 = new Address(); a1.setZipCode("12345"); Address a2 = new Address(); a2.setZipCode("54321"); Address a3 = new Address(); a3.setZipCode("99999"); Person p = new Person(); p.addAddress(a1); p.addAddress(a2); p.addAddress(a3); session.insert(p); session.fireAllRules(); assertEquals(2, results.size()); assertTrue(results.contains(a1)); assertTrue(results.contains(a2)); }
@Test public void testArrayAccessorWithStaticFieldAccess() { String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + Triangle.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when \n" + " $m : Person( addresses[Triangle.ZERO] == new Address('s1'), addresses[Triangle.ZERO].street == new Address('s1').street ) \n" + "then \n" + " list.add('r1'); \n" + "end \n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); List list = new ArrayList(); ksession.setGlobal("list", list); Person p = new Person("yoda"); p.addAddress(new Address("s1")); ksession.insert(p); ksession.fireAllRules(); assertEquals("r1", list.get(0)); // Check it was built with MVELReturnValueExpression constraint List<ObjectTypeNode> nodes = ((InternalKnowledgeBase) kbase).getRete().getObjectTypeNodes(); ObjectTypeNode node = null; for (ObjectTypeNode n : nodes) { if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) { node = n; break; } } AlphaNode alphanode = (AlphaNode) node.getSinkPropagator().getSinks()[0]; AlphaNodeFieldConstraint constraint = alphanode.getConstraint(); if (constraint instanceof MvelConstraint) { assertTrue( ((MvelConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader); assertEquals( new Address("s1"), ((MvelConstraint) alphanode.getConstraint()).getField().getValue()); } alphanode = (AlphaNode) alphanode.getSinkPropagator().getSinks()[0]; constraint = alphanode.getConstraint(); if (constraint instanceof MvelConstraint) { assertTrue( ((MvelConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader); assertEquals( new Address("s1").getStreet(), ((MvelConstraint) alphanode.getConstraint()).getField().getValue()); } }