Esempio n. 1
0
  @Test
  public void testUpdateSinkWithoutMemory() throws FactException, IntrospectionException {
    // An AlphaNode should try and repropagate from its source
    ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase();
    BuildContext buildContext =
        new BuildContext(ruleBase, ((ReteooRuleBase) ruleBase).getReteooBuilder().getIdGenerator());
    ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();

    final Rule rule = new Rule("test-rule");
    final PropagationContext context =
        new PropagationContextImpl(0, PropagationContext.INSERTION, null, null, null);

    final MockObjectSource source = new MockObjectSource(buildContext.getNextId());

    final InternalReadAccessor extractor =
        store.getReader(Cheese.class, "type", getClass().getClassLoader());

    final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");

    final MvelConstraint constraint =
        new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);

    final AlphaNode alphaNode =
        new AlphaNode(buildContext.getNextId(), constraint, source, buildContext); // no memory

    alphaNode.attach();

    final MockObjectSink sink1 = new MockObjectSink();
    alphaNode.addObjectSink(sink1);

    // Assert a single fact which should be in the AlphaNode memory and also
    // propagated to the
    // the tuple sink
    final Cheese cheese = new Cheese("cheddar", 0);
    final DefaultFactHandle handle1 = new DefaultFactHandle(1, cheese);
    // adding handle to the mock source
    source.addFact(handle1);

    alphaNode.assertObject(handle1, context, workingMemory);

    // Create a fact that should not be propagated, since the alpha node restriction will filter it
    // out
    final Cheese stilton = new Cheese("stilton", 10);
    final DefaultFactHandle handle2 = new DefaultFactHandle(2, stilton);
    // adding handle to the mock source
    source.addFact(handle2);

    alphaNode.assertObject(handle2, context, workingMemory);

    assertLength(1, sink1.getAsserted());

    // Attach a new tuple sink
    final MockObjectSink sink2 = new MockObjectSink();

    // Tell the alphanode to update the new node. Make sure the first sink1
    // is not updated
    // likewise the source should not do anything
    alphaNode.updateSink(sink2, context, workingMemory);

    assertLength(1, sink1.getAsserted());
    assertLength(1, sink2.getAsserted());
    assertEquals(1, source.getUdated());
  }
  public void testUpdateSinkWithMemory() throws FactException, IntrospectionException {
    // An AlphaNode with memory should not try and repropagate from its source
    // Also it should only update the latest tuple sinky
    RuleBaseConfiguration config = new RuleBaseConfiguration();
    config.setAlphaMemory(true);
    ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase(config);
    BuildContext buildContext =
        new BuildContext(ruleBase, ((ReteooRuleBase) ruleBase).getReteooBuilder().getIdGenerator());
    ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();

    final Rule rule = new Rule("test-rule");
    final PropagationContext context =
        new PropagationContextImpl(0, PropagationContext.ASSERTION, null, null);

    final MockObjectSource source = new MockObjectSource(buildContext.getNextId());

    final FieldExtractor extractor =
        cache.getExtractor(Cheese.class, "type", getClass().getClassLoader());

    final FieldValue field = FieldFactory.getFieldValue("cheddar");

    final Evaluator evaluator = ValueType.OBJECT_TYPE.getEvaluator(Operator.EQUAL);

    final LiteralConstraint constraint = new LiteralConstraint(extractor, evaluator, field);

    buildContext.setAlphaNodeMemoryAllowed(true);
    final AlphaNode alphaNode =
        new AlphaNode(buildContext.getNextId(), constraint, source, buildContext); // has memory

    alphaNode.attach();

    final MockObjectSink sink1 = new MockObjectSink();
    alphaNode.addObjectSink(sink1);

    // Assert a single fact which should be in the AlphaNode memory and also
    // propagated to the
    // the tuple sink
    final Cheese cheese = new Cheese("cheddar", 0);
    final DefaultFactHandle handle1 = new DefaultFactHandle(1, cheese);

    alphaNode.assertObject(handle1, context, workingMemory);

    // Create a fact that should not be propagated, since the alpha node restriction will filter it
    // out
    final Cheese stilton = new Cheese("stilton", 10);
    final DefaultFactHandle handle2 = new DefaultFactHandle(2, stilton);
    // adding handle to the mock source
    source.addFact(handle2);

    alphaNode.assertObject(handle2, context, workingMemory);
    assertLength(1, sink1.getAsserted());

    // Attach a new tuple sink
    final MockObjectSink sink2 = new MockObjectSink();

    // Tell the alphanode to update the new node. Make sure the first sink1
    // is not updated
    // likewise the source should not do anything
    alphaNode.updateSink(sink2, context, workingMemory);

    assertLength(1, sink1.getAsserted());
    assertLength(1, sink2.getAsserted());
    assertEquals(0, source.getUdated());
  }