public AlphaNodeFieldConstraint getLiteralConstraint( final Pattern pattern, final String fieldName, final String evaluatorString, final String value) throws IntrospectionException { final Class<?> clazz = ((ClassObjectType) pattern.getObjectType()).getClassType(); final InternalReadAccessor extractor = store.getReader(clazz, fieldName, getClass().getClassLoader()); FieldValue fieldValue = FieldFactory.getInstance().getFieldValue(value, extractor.getValueType(), null); return new MvelConstraintTestUtil(fieldName + evaluatorString + value, fieldValue, extractor); }
/* (non-Javadoc) * @see junit.framework.TestCase#setUp() */ @Before public void setUp() throws Exception { kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(); listener1 = new TestRuleBaseListener("(listener-1) "); listener2 = new TestRuleBaseListener("(listener-2) "); kBase.addEventListener(listener1); kBase.addEventListener(listener2); final RuleImpl rule1 = new RuleImpl("test1"); final ClassObjectType cheeseObjectType = new ClassObjectType(Cheese.class); final Pattern pattern = new Pattern(0, cheeseObjectType); ClassFieldAccessorStore store = new ClassFieldAccessorStore(); store.setClassFieldAccessorCache( new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader())); store.setEagerWire(true); final ClassFieldReader extractor = store.getReader(Cheese.class, "type"); final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar"); final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor); pattern.addConstraint(constraint); rule1.addPattern(pattern); rule1.setConsequence( new Consequence() { private static final long serialVersionUID = 510l; public void evaluate( final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {} public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {} public void writeExternal(ObjectOutput out) throws IOException {} public String getName() { return "default"; } }); final RuleImpl rule2 = new RuleImpl("test2"); final ClassObjectType cheeseObjectType2 = new ClassObjectType(Cheese.class); final Pattern pattern2 = new Pattern(0, cheeseObjectType2); final FieldValue field2 = FieldFactory.getInstance().getFieldValue("stilton"); final MvelConstraint constraint2 = new MvelConstraintTestUtil("type == \"stilton\"", field, extractor); pattern2.addConstraint(constraint2); rule2.addPattern(pattern2); rule2.setConsequence( new Consequence() { private static final long serialVersionUID = 510l; public void evaluate( final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {} public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {} public void writeExternal(ObjectOutput out) throws IOException {} public String getName() { return "default"; } }); pkg = new KnowledgePackageImpl("org.drools.test1"); pkg.addRule(rule1); pkg.addRule(rule2); }
public class KieComponentFactory implements Serializable { public static KieComponentFactory DEFAULT = new KieComponentFactory(); public static KieComponentFactory getDefault() { return DEFAULT; } private FactHandleFactory handleFactory = new ReteooFactHandleFactory(); public FactHandleFactory getFactHandleFactoryService() { return handleFactory; } public void setHandleFactoryProvider(FactHandleFactory provider) { handleFactory = provider; } public void setDefaultHandleFactoryProvider() { handleFactory = new ReteooFactHandleFactory(); } public static FactHandleFactory getDefaultHandleFactoryProvider() { return new ReteooFactHandleFactory(); } private WorkingMemoryFactory wmFactory = new PhreakWorkingMemoryFactory(); public WorkingMemoryFactory getWorkingMemoryFactory() { return wmFactory; } public void setWorkingMemoryFactory(WorkingMemoryFactory wmFactory) { this.wmFactory = wmFactory; } private NodeFactory nodeFactory = new PhreakNodeFactory(); public NodeFactory getNodeFactoryService() { return nodeFactory; } public void setNodeFactoryProvider(NodeFactory provider) { nodeFactory = provider; } public void setDefaultNodeFactoryProvider() { nodeFactory = new PhreakNodeFactory(); } public static NodeFactory getDefaultNodeFactoryProvider() { return new PhreakNodeFactory(); } private PropagationContextFactory propagationFactory = new PhreakPropagationContextFactory(); public void setPropagationContextFactory(PropagationContextFactory factory) { propagationFactory = factory; } public PropagationContextFactory getPropagationContextFactory() { return propagationFactory; } private BeliefSystemFactory bsFactory = new PhreakBeliefSystemFactory(); public void setPropagationContextFactory(BeliefSystemFactory factory) { bsFactory = factory; } public BeliefSystemFactory getBeliefSystemFactory() { return bsFactory; } private RuleBuilderFactory ruleBuilderFactory = new ReteooRuleBuilderFactory(); public RuleBuilderFactory getRuleBuilderFactory() { return ruleBuilderFactory; } public void setRuleBuilderProvider(RuleBuilderFactory provider) { ruleBuilderFactory = provider; } public void setDefaultRuleBuilderProvider() { ruleBuilderFactory = new ReteooRuleBuilderFactory(); } public static RuleBuilderFactory getDefaultRuleBuilderFactory() { return new ReteooRuleBuilderFactory(); } private AgendaFactory agendaFactory = new DefaultAgendaFactory(); public AgendaFactory getAgendaFactory() { return agendaFactory; } public void setAgendaFactory(AgendaFactory provider) { agendaFactory = provider; } public void setDefaultAgendaFactory() { agendaFactory = new DefaultAgendaFactory(); } public static AgendaFactory getDefaultAgendaFactory() { return new DefaultAgendaFactory(); } private FieldDataFactory fieldFactory = FieldFactory.getInstance(); public FieldDataFactory getFieldFactory() { return fieldFactory; } public void setFieldDataFactory(FieldDataFactory provider) { fieldFactory = provider; } public void setDefaultFieldFactory() { fieldFactory = FieldFactory.getInstance(); } public static FieldDataFactory getDefaultFieldFactory() { return FieldFactory.getInstance(); } private TripleFactory tripleFactory = new TripleFactoryImpl(); public TripleFactory getTripleFactory() { return tripleFactory; } public void setTripleFactory(TripleFactory provider) { tripleFactory = provider; } public void setDefaultTripleFactory() { tripleFactory = new TripleFactoryImpl(); } public static TripleFactory getDefaultTripleFactory() { return new TripleFactoryImpl(); } private KnowledgeHelperFactory knowledgeHelperFactory = new DefaultKnowledgeHelperFactory(); public KnowledgeHelperFactory getKnowledgeHelperFactory() { return knowledgeHelperFactory; } public void setKnowledgeHelperFactory(KnowledgeHelperFactory provider) { knowledgeHelperFactory = provider; } public void setDefaultKnowledgeHelperFactory() { knowledgeHelperFactory = new DefaultKnowledgeHelperFactory(); } public static KnowledgeHelperFactory getDefaultKnowledgeHelperFactory() { return new DefaultKnowledgeHelperFactory(); } private LogicTransformerFactory logicTransformerFactory = new DefaultLogicTransformerFactory(); public LogicTransformerFactory getLogicTransformerFactory() { return logicTransformerFactory; } public void setLogicTransformerFactory(LogicTransformerFactory provider) { logicTransformerFactory = provider; } public void setDefaultLogicTransformerFactory() { logicTransformerFactory = new DefaultLogicTransformerFactory(); } public static LogicTransformerFactory getDefaultLogicTransformerFactory() { return new DefaultLogicTransformerFactory(); } private TraitFactory traitFactory = new TraitFactory(); public TraitFactory getTraitFactory() { return traitFactory; } public void setTraitFactory(TraitFactory tf) { traitFactory = tf; } public void setDefaultTraitFactory() { traitFactory = new TraitFactory(); } public static TraitFactory getDefaultTraitFactory() { return new TraitFactory(); } private TraitRegistry traitRegistry = new TraitRegistry(); public TraitRegistry getTraitRegistry() { return traitRegistry; } public void setTraitFactory(TraitRegistry tr) { traitRegistry = tr; } public void setDefaultTraitRegistry() { traitRegistry = new TraitRegistry(); } public static TraitRegistry getDefaultTraitRegistry() { return new TraitRegistry(); } private TripleStore tripleStore = new TripleStore(); public TripleStore getTripleStore() { return tripleStore; } public void setTraitFactory(TripleStore tr) { tripleStore = tr; } public void setDefaultTripleStore() { tripleStore = new TripleStore(); } public static TripleStore getDefaultTripleStore() { return new TripleStore(); } private ClassBuilderFactory classBuilderFactory = new ClassBuilderFactory(); public ClassBuilderFactory getClassBuilderFactory() { return classBuilderFactory; } public void setClassBuilderFactory(ClassBuilderFactory tf) { classBuilderFactory = tf; } public void setDefaultClassBuilderFactory() { classBuilderFactory = new ClassBuilderFactory(); } public static ClassBuilderFactory getDefaultClassBuilderFactory() { return new ClassBuilderFactory(); } private Class<?> baseTraitProxyClass = TraitProxy.class; public Class<?> getBaseTraitProxyClass() { return baseTraitProxyClass; } public void setBaseTraitProxyClass(Class<?> baseTraitProxyClass) { this.baseTraitProxyClass = baseTraitProxyClass; } }
public static FieldDataFactory getDefaultFieldFactory() { return FieldFactory.getInstance(); }
public void setDefaultFieldFactory() { fieldFactory = FieldFactory.getInstance(); }
@Test public void testAlphaNode() { final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, null); final StatefulKnowledgeSessionImpl workingMemory = new StatefulKnowledgeSessionImpl(1L, kBase); final ClassFieldReader extractor = store.getReader(Cheese.class, "type"); final MvelConstraint constraint = new MvelConstraintTestUtil( "type == \"stilton\"", FieldFactory.getInstance().getFieldValue("stilton"), extractor); final List list = new ArrayList(); final Cheese cheese1 = new Cheese("cheddar", 20); final Cheese cheese2 = new Cheese("brie", 20); list.add(cheese1); list.add(cheese2); final MockDataProvider dataProvider = new MockDataProvider(list); final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class)); From fromCe = new From(dataProvider); fromCe.setResultPattern(pattern); final ReteFromNode from = new ReteFromNode( 3, dataProvider, new MockTupleSource(80), new AlphaNodeFieldConstraint[] {constraint}, null, true, buildContext, fromCe); final MockLeftTupleSink sink = new MockLeftTupleSink(5); from.addTupleSink(sink); final Person person1 = new Person("xxx1", 30); final FactHandle person1Handle = workingMemory.insert(person1); final LeftTuple tuple1 = new LeftTupleImpl((DefaultFactHandle) person1Handle, from, true); from.assertLeftTuple(tuple1, context, workingMemory); // nothing should be asserted, as cheese1 is cheddar and we are filtering on stilton assertEquals(0, sink.getAsserted().size()); // Set cheese1 to stilton and it should now propagate cheese1.setType("stilton"); final Person person2 = new Person("xxx2", 30); final FactHandle person2Handle = workingMemory.insert(person2); final LeftTuple tuple2 = new LeftTupleImpl((DefaultFactHandle) person2Handle, from, true); from.assertLeftTuple(tuple2, context, workingMemory); final List asserted = sink.getAsserted(); assertEquals(1, asserted.size()); Tuple tuple = (Tuple) ((Object[]) asserted.get(0))[0]; assertSame(person2, tuple.getObject(0)); assertSame(cheese1, tuple.getObject(1)); cheese2.setType("stilton"); final Person person3 = new Person("xxx2", 30); final FactHandle person3Handle = workingMemory.insert(person3); final LeftTuple tuple3 = new LeftTupleImpl((DefaultFactHandle) person3Handle, from, true); from.assertLeftTuple(tuple3, context, workingMemory); assertEquals(3, asserted.size()); tuple = (Tuple) ((Object[]) asserted.get(1))[0]; assertSame(person3, tuple.getObject(0)); assertSame(cheese1, tuple.getObject(1)); tuple = (Tuple) ((Object[]) asserted.get(2))[0]; assertSame(person3, tuple.getObject(0)); assertSame(cheese2, tuple.getObject(1)); assertNotSame(cheese1, cheese2); }
@Test public void testRestract() { final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, null); final StatefulKnowledgeSessionImpl workingMemory = new StatefulKnowledgeSessionImpl( 1L, (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase()); final ClassFieldReader extractor = store.getReader(Cheese.class, "type"); final MvelConstraint constraint = new MvelConstraintTestUtil( "type == \"stilton\"", FieldFactory.getInstance().getFieldValue("stilton"), extractor); final List list = new ArrayList(); final Cheese cheese1 = new Cheese("stilton", 5); final Cheese cheese2 = new Cheese("stilton", 15); list.add(cheese1); list.add(cheese2); final MockDataProvider dataProvider = new MockDataProvider(list); final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class)); From fromCe = new From(dataProvider); fromCe.setResultPattern(pattern); final ReteFromNode from = new ReteFromNode( 3, dataProvider, new MockTupleSource(30), new AlphaNodeFieldConstraint[] {constraint}, null, true, buildContext, fromCe); final MockLeftTupleSink sink = new MockLeftTupleSink(5); from.addTupleSink(sink); final List asserted = sink.getAsserted(); final Person person1 = new Person("xxx2", 30); final FactHandle person1Handle = workingMemory.insert(person1); final LeftTuple tuple = new LeftTupleImpl((DefaultFactHandle) person1Handle, from, true); from.assertLeftTuple(tuple, context, workingMemory); assertEquals(2, asserted.size()); final FromMemory memory = (FromMemory) workingMemory.getNodeMemory(from); assertEquals(1, memory.getBetaMemory().getLeftTupleMemory().size()); assertNull(memory.getBetaMemory().getRightTupleMemory()); RightTuple rightTuple2 = tuple.getFirstChild().getRightParent(); RightTuple rightTuple1 = tuple.getFirstChild().getHandleNext().getRightParent(); assertFalse(rightTuple1.equals(rightTuple2)); assertNull(tuple.getFirstChild().getHandleNext().getHandleNext()); final InternalFactHandle handle2 = rightTuple2.getFactHandle(); final InternalFactHandle handle1 = rightTuple1.getFactHandle(); assertEquals(handle1.getObject(), cheese2); assertEquals(handle2.getObject(), cheese1); from.retractLeftTuple(tuple, context, workingMemory); assertEquals(0, memory.getBetaMemory().getLeftTupleMemory().size()); assertNull(memory.getBetaMemory().getRightTupleMemory()); }
@Test public void testQueryTerminalNode() { final ClassObjectType queryObjectType = new ClassObjectType(DroolsQuery.class); final ObjectTypeNode queryObjectTypeNode = new ObjectTypeNode( this.buildContext.getNextId(), this.entryPoint, queryObjectType, buildContext); queryObjectTypeNode.attach(buildContext); ClassFieldReader extractor = store.getReader(DroolsQuery.class, "name"); MvelConstraint constraint = new MvelConstraintTestUtil( "name == \"query-1\"", FieldFactory.getInstance().getFieldValue("query-1"), extractor); final QueryImpl query = new QueryImpl("query-1"); buildContext.setRule(query); AlphaNode alphaNode = new AlphaNode(this.buildContext.getNextId(), constraint, queryObjectTypeNode, buildContext); alphaNode.attach(buildContext); final LeftInputAdapterNode liaNode = new LeftInputAdapterNode(this.buildContext.getNextId(), alphaNode, this.buildContext); liaNode.attach(buildContext); final ClassObjectType cheeseObjectType = new ClassObjectType(Cheese.class); final ObjectTypeNode cheeseObjectTypeNode = new ObjectTypeNode( this.buildContext.getNextId(), this.entryPoint, cheeseObjectType, buildContext); cheeseObjectTypeNode.attach(buildContext); extractor = store.getReader(Cheese.class, "type"); constraint = new MvelConstraintTestUtil( "type == \"stilton\"", FieldFactory.getInstance().getFieldValue("stilton"), extractor); alphaNode = new AlphaNode( this.buildContext.getNextId(), constraint, cheeseObjectTypeNode, buildContext); alphaNode.attach(buildContext); BuildContext buildContext = new BuildContext(kBase, kBase.getReteooBuilder().getIdGenerator()); buildContext.setTupleMemoryEnabled(false); final JoinNode joinNode = new JoinNode( this.buildContext.getNextId(), liaNode, alphaNode, EmptyBetaConstraints.getInstance(), buildContext); joinNode.attach(buildContext); final QueryTerminalNode queryNode = new QueryTerminalNode( this.buildContext.getNextId(), joinNode, query, ((QueryImpl) query).getLhs(), 0, buildContext); queryNode.attach(buildContext); final KnowledgePackageImpl pkg = new KnowledgePackageImpl("com.drools.test"); pkg.addRule(query); ((KnowledgeBaseImpl) kBase).addPackages(Arrays.asList(new InternalKnowledgePackage[] {pkg})); KieSession kSession = kBase.newKieSession(); QueryResults results = kSession.getQueryResults("query-1"); assertEquals(0, results.size()); final Cheese stilton1 = new Cheese("stilton", 100); final FactHandle handle1 = kSession.insert(stilton1); results = kSession.getQueryResults("query-1"); assertEquals(1, results.size()); final Cheese cheddar = new Cheese("cheddar", 55); kSession.insert(cheddar); results = kSession.getQueryResults("query-1"); assertEquals(1, results.size()); final Cheese stilton2 = new Cheese("stilton", 5); final FactHandle handle2 = kSession.insert(stilton2); results = kSession.getQueryResults("query-1"); assertEquals(2, results.size()); /** * QueryResultsRow result = results.get( 0 ); assertEquals( 1, result.size() ); assertEquals( * stilton1, result.get( 0 ) ); * * <p>result = results.get( 1 ); assertEquals( 1, result.size() ); assertEquals( stilton2, * result.get( 0 ) ); */ int i = 0; for (final Iterator<QueryResultsRow> it = results.iterator(); it.hasNext(); ) { QueryResultsRow resultRow = it.next(); if (i == 1) { // assertSame( stilton2, result.g( 0 ) ); } else { // assertSame( stilton1, result.get( 0 ) ); } i++; } kSession.delete(handle1); results = kSession.getQueryResults("query-1"); assertEquals(1, results.size()); kSession.delete(handle2); results = kSession.getQueryResults("query-1"); assertEquals(0, results.size()); }