@Test
  public void testMemory() {
    ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase();
    BuildContext buildContext =
        new BuildContext(ruleBase, ruleBase.getReteooBuilder().getIdGenerator());

    this.workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();

    final MockObjectSource objectSource = new MockObjectSource(1);
    final MockTupleSource tupleSource = new MockTupleSource(1);

    final AccumulateNode accumulateNode =
        new AccumulateNode(
            3,
            tupleSource,
            objectSource,
            new AlphaNodeFieldConstraint[0],
            EmptyBetaConstraints.getInstance(),
            EmptyBetaConstraints.getInstance(),
            this.accumulate,
            false,
            buildContext);

    final BetaMemory memory =
        ((AccumulateMemory) this.workingMemory.getNodeMemory(accumulateNode)).betaMemory;

    assertNotNull(memory);
  }
  @Before
  public void setUp() {
    this.ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase();

    pkg = new org.drools.rule.Package("org.droos.test");
    pkg.setClassFieldAccessorCache(
        new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));

    JavaDialectRuntimeData data = new JavaDialectRuntimeData();
    data.onAdd(pkg.getDialectRuntimeRegistry(), ruleBase.getRootClassLoader());
    pkg.getDialectRuntimeRegistry().setDialectData("java", data);

    // we need to add one rule to the package because the previous deadlock was encountered
    // while removing rules from a package when said package is removed from the rulebase
    rule = new Rule("Test");
    rule.setDialect("java");
    rule.setConsequence(
        new Consequence() {
          public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory)
              throws Exception {}

          public String getName() {
            return "default";
          }
        });
    pkg.addRule(rule);

    ruleBase.addPackage(pkg);
  }
  /** Test method for {@link org.drools.reteoo.BetaNode#equals(java.lang.Object)}. */
  public void testEqualsObject() {
    final TupleSource ts = new MockTupleSource(1);
    final ObjectSource os = new MockObjectSource(2);

    ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase();
    BuildContext buildContext =
        new BuildContext(ruleBase, ruleBase.getReteooBuilder().getIdGenerator());

    final BetaNode j1 = new JoinNode(1, ts, os, EmptyBetaConstraints.getInstance(), buildContext);
    final BetaNode j2 = new JoinNode(2, ts, os, EmptyBetaConstraints.getInstance(), buildContext);
    final BetaNode n1 = new NotNode(3, ts, os, EmptyBetaConstraints.getInstance(), buildContext);
    final BetaNode n2 = new NotNode(4, ts, os, EmptyBetaConstraints.getInstance(), buildContext);

    assertEquals(j1, j1);
    assertEquals(j2, j2);
    assertEquals(j1, j2);
    assertEquals(n1, n1);
    assertEquals(n2, n2);
    assertEquals(n1, n2);

    assertFalse(j1.equals(n1));
    assertFalse(j1.equals(n2));
    assertFalse(n1.equals(j1));
    assertFalse(n1.equals(j2));
  }
  public void testLiteralConstraintAssertObjectWithoutMemory() throws Exception {
    RuleBaseConfiguration config = new RuleBaseConfiguration();
    config.setAlphaMemory(false);
    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 ClassFieldExtractor 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);

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

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

    final Cheese cheddar = new Cheese("cheddar", 5);
    final DefaultFactHandle f0 = (DefaultFactHandle) workingMemory.insert(cheddar);

    // check sink is empty
    assertLength(0, sink.getAsserted());

    // check alpha memory is empty
    final AlphaMemory memory = (AlphaMemory) workingMemory.getNodeMemory(alphaNode);

    assertNull(memory.facts);

    // object should assert as it passes text
    alphaNode.assertObject(f0, context, workingMemory);

    assertEquals(1, sink.getAsserted().size());
    assertNull(memory.facts);
    Object[] list = (Object[]) sink.getAsserted().get(0);
    assertSame(cheddar, workingMemory.getObject((DefaultFactHandle) list[0]));

    final Cheese stilton = new Cheese("stilton", 6);
    final DefaultFactHandle f1 = new DefaultFactHandle(1, stilton);

    // object should NOT assert as it does not pass test
    alphaNode.assertObject(f1, context, workingMemory);

    assertLength(1, sink.getAsserted());
    assertNull(memory.facts);
    list = (Object[]) sink.getAsserted().get(0);
    assertSame(cheddar, workingMemory.getObject((DefaultFactHandle) list[0]));
  }
Example #5
0
    public void execute(InternalWorkingMemory workingMemory) {

      final PropagationContext context =
          new PropagationContextImpl(
              workingMemory.getNextPropagationIdCounter(),
              PropagationContext.INSERTION,
              this.ruleOrigin,
              this.leftTuple,
              this.factHandle);
      ReteooRuleBase ruleBase = (ReteooRuleBase) workingMemory.getRuleBase();
      ruleBase.assertObject(this.factHandle, this.factHandle.getObject(), context, workingMemory);
      context.evaluateActionQueue(workingMemory);
    }
  public void testIsMemoryAllowedOverride() throws Exception {
    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 ClassFieldExtractor 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);

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

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

    final Cheese cheddar = new Cheese("cheddar", 5);
    final DefaultFactHandle f0 = (DefaultFactHandle) workingMemory.insert(cheddar);

    // check sink is empty
    assertLength(0, sink.getAsserted());

    // check alpha memory is empty
    final AlphaMemory memory = (AlphaMemory) workingMemory.getNodeMemory(alphaNode);

    assertNull(memory.facts);

    // object should assert as it passes text
    alphaNode.assertObject(f0, context, workingMemory);

    assertEquals(1, sink.getAsserted().size());
    // memory should be one, as even though isAlphaMemory is on for the configuration, the build
    // never allows memory
    assertNull(memory.facts);
  }
Example #7
0
  /*
   *  This just test AlphaNode With a different Constraint type.
   */
  @Test
  public void testReturnValueConstraintAssertObject() throws Exception {
    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);
    final MockObjectSink sink = new MockObjectSink();
    alphaNode.addObjectSink(sink);

    final Cheese cheddar = new Cheese("cheddar", 5);

    final DefaultFactHandle f0 = (DefaultFactHandle) workingMemory.insert(cheddar);

    assertLength(0, sink.getAsserted());

    // object should assert as it passes text
    alphaNode.assertObject(f0, context, workingMemory);

    assertLength(1, sink.getAsserted());
    final Object[] list = (Object[]) sink.getAsserted().get(0);
    assertSame(cheddar, workingMemory.getObject((DefaultFactHandle) list[0]));

    final Cheese stilton = new Cheese("stilton", 6);
    f0.setObject(stilton);

    sink.getAsserted().clear();

    // object should not assert as it does not pass text
    alphaNode.assertObject(f0, context, workingMemory);

    assertLength(0, sink.getAsserted());
  }
  /* (non-Javadoc)
   * @see junit.framework.TestCase#setUp()
   */
  @Before
  public void setUp() throws Exception {
    this.rule = new Rule("test-rule");
    this.context = new PropagationContextImpl(0, PropagationContext.ASSERTION, null, null, null);

    ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase();
    BuildContext buildContext =
        new BuildContext(ruleBase, ruleBase.getReteooBuilder().getIdGenerator());

    this.workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();

    this.tupleSource = new MockTupleSource(4);
    this.objectSource = new MockObjectSource(4);
    this.sink = new MockLeftTupleSink();

    this.accumulator = new MockAccumulator();

    final ObjectType srcObjType = new ClassObjectType(String.class);
    final Pattern sourcePattern = new Pattern(0, srcObjType);
    this.accumulate =
        new Accumulate(
            sourcePattern, new Declaration[0], new Accumulator[] {this.accumulator}, false);

    this.node =
        new AccumulateNode(
            15,
            this.tupleSource,
            this.objectSource,
            new AlphaNodeFieldConstraint[0],
            EmptyBetaConstraints.getInstance(),
            EmptyBetaConstraints.getInstance(),
            this.accumulate,
            false,
            buildContext);

    this.node.addTupleSink(this.sink);

    this.memory = ((AccumulateMemory) this.workingMemory.getNodeMemory(this.node)).betaMemory;

    // check memories are empty
    assertEquals(0, this.memory.getLeftTupleMemory().size());
    assertEquals(0, this.memory.getRightTupleMemory().size());
  }
  public void testMemory() {
    RuleBaseConfiguration config = new RuleBaseConfiguration();
    config.setAlphaMemory(false);
    ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase(config);
    BuildContext buildContext =
        new BuildContext(ruleBase, ((ReteooRuleBase) ruleBase).getReteooBuilder().getIdGenerator());
    ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();

    final ClassFieldExtractor 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);

    final AlphaNode alphaNode =
        new AlphaNode(buildContext.getNextId(), constraint, null, buildContext);

    final AlphaMemory memory = (AlphaMemory) workingMemory.getNodeMemory(alphaNode);

    assertNotNull(memory);
  }
  /**
   * Test just tuple assertions
   *
   * @throws AssertionException
   */
  @Test
  public void testAssertTupleSequentialMode() throws Exception {
    RuleBaseConfiguration conf = new RuleBaseConfiguration();
    conf.setSequential(true);

    ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase();
    BuildContext buildContext =
        new BuildContext(ruleBase, ruleBase.getReteooBuilder().getIdGenerator());
    buildContext.setTupleMemoryEnabled(false);
    // overide the original node, so we an set the BuildContext
    this.node =
        new AccumulateNode(
            15,
            this.tupleSource,
            this.objectSource,
            new AlphaNodeFieldConstraint[0],
            EmptyBetaConstraints.getInstance(),
            EmptyBetaConstraints.getInstance(),
            this.accumulate,
            false,
            buildContext);

    this.node.addTupleSink(this.sink);

    this.workingMemory =
        new ReteooWorkingMemory(1, (ReteooRuleBase) RuleBaseFactory.newRuleBase(conf));

    this.memory = ((AccumulateMemory) this.workingMemory.getNodeMemory(this.node)).betaMemory;

    final DefaultFactHandle f0 =
        (DefaultFactHandle)
            this.workingMemory.getFactHandleFactory().newFactHandle("cheese", null, null, null);
    final DefaultFactHandle f1 =
        (DefaultFactHandle)
            this.workingMemory
                .getFactHandleFactory()
                .newFactHandle("other cheese", null, null, null);

    final LeftTupleImpl tuple0 = new LeftTupleImpl(f0, null, true);

    this.node.assertObject(f0, this.context, this.workingMemory);
    this.node.assertObject(f1, this.context, this.workingMemory);

    // assert tuple, should not add to left memory, since we are in sequential mode
    this.node.assertLeftTuple(
        tuple0,
        new PropagationContextImpl(0, PropagationContext.ASSERTION, null, null, f0),
        this.workingMemory);
    // check memories
    assertNull(this.memory.getLeftTupleMemory());
    assertEquals(2, this.memory.getRightTupleMemory().size());
    assertEquals(
        "Wrong number of elements in matching objects list ",
        2,
        this.accumulator.getMatchingObjects().size());

    // assert tuple, should not add left memory
    final LeftTupleImpl tuple1 = new LeftTupleImpl(f1, null, true);
    this.node.assertLeftTuple(
        tuple1,
        new PropagationContextImpl(0, PropagationContext.ASSERTION, null, null, f1),
        this.workingMemory);
    assertNull(this.memory.getLeftTupleMemory());
    assertEquals(
        "Wrong number of elements in matching objects list ",
        2,
        this.accumulator.getMatchingObjects().size());

    assertEquals("Two tuples should have been propagated", 2, this.sink.getAsserted().size());
  }
Example #11
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 testQueryTerminalNode() {
    final ClassObjectType queryObjectType = new ClassObjectType(DroolsQuery.class);
    final ObjectTypeNode queryObjectTypeNode =
        new ObjectTypeNode(this.buildContext.getNextId(), queryObjectType, buildContext);
    queryObjectTypeNode.attach();

    ClassFieldExtractor extractor =
        cache.getExtractor(DroolsQuery.class, "name", DroolsQuery.class.getClassLoader());

    FieldValue field = FieldFactory.getFieldValue("query-1");

    final Evaluator evaluator = ValueType.STRING_TYPE.getEvaluator(Operator.EQUAL);
    LiteralConstraint constraint = new LiteralConstraint(extractor, evaluator, field);

    AlphaNode alphaNode =
        new AlphaNode(this.buildContext.getNextId(), constraint, queryObjectTypeNode, buildContext);
    alphaNode.attach();

    final LeftInputAdapterNode liaNode =
        new LeftInputAdapterNode(this.buildContext.getNextId(), alphaNode, this.buildContext);
    liaNode.attach();

    final ClassObjectType cheeseObjectType = new ClassObjectType(Cheese.class);
    final ObjectTypeNode cheeseObjectTypeNode =
        new ObjectTypeNode(this.buildContext.getNextId(), cheeseObjectType, buildContext);
    cheeseObjectTypeNode.attach();

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

    field = FieldFactory.getFieldValue("stilton");

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

    alphaNode =
        new AlphaNode(
            this.buildContext.getNextId(), constraint, cheeseObjectTypeNode, buildContext);
    alphaNode.attach();

    BuildContext buildContext =
        new BuildContext(ruleBase, ruleBase.getReteooBuilder().getIdGenerator());
    buildContext.setTupleMemoryEnabled(false);

    final JoinNode joinNode =
        new JoinNode(
            this.buildContext.getNextId(),
            liaNode,
            alphaNode,
            EmptyBetaConstraints.getInstance(),
            buildContext);
    joinNode.attach();

    final Query query = new Query("query-1");

    final QueryTerminalNode queryNode =
        new QueryTerminalNode(this.buildContext.getNextId(), joinNode, query, query.getLhs());

    queryNode.attach();

    final org.drools.rule.Package pkg = new org.drools.rule.Package("com.drools.test");
    pkg.addRule(query);

    try {
      final Field pkgField = ruleBase.getClass().getSuperclass().getDeclaredField("pkgs");
      pkgField.setAccessible(true);
      final Map pkgs = (Map) pkgField.get(ruleBase);
      pkgs.put(pkg.getName(), pkg);
    } catch (final Exception e) {
      Assert.fail("Should not throw any exception: " + e.getMessage());
    }

    final WorkingMemory workingMemory = ruleBase.newStatefulSession();
    QueryResults results = workingMemory.getQueryResults("query-1");

    assertEquals(0, results.size());

    final Cheese stilton1 = new Cheese("stilton", 100);
    final FactHandle handle1 = workingMemory.insert(stilton1);

    results = workingMemory.getQueryResults("query-1");

    assertEquals(1, results.size());

    final Cheese cheddar = new Cheese("cheddar", 55);
    workingMemory.insert(cheddar);

    results = workingMemory.getQueryResults("query-1");

    assertEquals(1, results.size());

    final Cheese stilton2 = new Cheese("stilton", 5);

    final FactHandle handle2 = workingMemory.insert(stilton2);

    results = workingMemory.getQueryResults("query-1");

    assertEquals(2, results.size());

    QueryResult result = results.get(0);
    assertEquals(1, result.size());
    assertEquals(stilton2, result.get(0));

    result = results.get(1);
    assertEquals(1, result.size());
    assertEquals(stilton1, result.get(0));

    int i = 0;
    for (final Iterator it = results.iterator(); it.hasNext(); ) {
      result = (QueryResult) it.next();
      assertEquals(1, result.size());
      if (i == 1) {
        assertSame(stilton1, result.get(0));
      } else {
        assertSame(stilton2, result.get(0));
      }
      i++;
    }

    workingMemory.retract(handle1);
    results = workingMemory.getQueryResults("query-1");

    assertEquals(1, results.size());

    workingMemory.retract(handle2);
    results = workingMemory.getQueryResults("query-1");

    assertEquals(0, results.size());
  }
  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());
  }
  public void setUp(int... type) {
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf);
    buildContext = new BuildContext(ruleBase, ruleBase.getReteooBuilder().getIdGenerator());

    PropagationContextFactory pctxFactory =
        ruleBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
    context =
        pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, null);

    MockTupleSource mockTupleSource = new MockTupleSource(9);

    rule1 = new Rule("rule1");
    rule2 = new Rule("rule2");
    rule3 = new Rule("rule3");

    ObjectTypeNode otn =
        new ObjectTypeNode(2, null, new ClassObjectType(String.class), buildContext);
    liaNode = new LeftInputAdapterNode(3, otn, buildContext);

    // 3, 4, 5, 6 are in same shared segment
    n1 = createBetaNode(10, type[0], liaNode);
    n2 = createBetaNode(11, type[1], n1);
    RuleTerminalNode rtn1 = new RuleTerminalNode(18, n2, rule1, rule1.getLhs(), 0, buildContext);
    rtn1.attach();

    n3 = createBetaNode(12, type[2], n1);
    n4 = createBetaNode(13, type[3], n3);
    n5 = createBetaNode(14, type[4], n4);
    n6 = createBetaNode(15, type[5], n5);
    RuleTerminalNode rtn2 = new RuleTerminalNode(19, n6, rule2, rule2.getLhs(), 0, buildContext);
    rtn2.attach();

    n7 = createBetaNode(16, type[6], n6);
    n8 = createBetaNode(17, type[7], n7);
    RuleTerminalNode rtn3 = new RuleTerminalNode(20, n8, rule3, rule3.getLhs(), 0, buildContext);
    rtn3.attach();

    // n1 -> n2 -> r1
    //  \
    //   n3 -> n4 -> n5 -> n6 -> r2
    //                      \
    //                      n7 -> n8 -> r3

    n1.getAssociations().put(rule1, null);
    n1.getAssociations().put(rule2, null);
    n1.getAssociations().put(rule3, null);
    n2.getAssociations().put(rule1, null);
    n2.getAssociations().put(rule2, null);
    n2.getAssociations().put(rule3, null);

    n3.getAssociations().put(rule2, null);
    n3.getAssociations().put(rule3, null);
    n4.getAssociations().put(rule2, null);
    n4.getAssociations().put(rule3, null);
    n5.getAssociations().put(rule2, null);
    n5.getAssociations().put(rule3, null);
    n6.getAssociations().put(rule2, null);
    n6.getAssociations().put(rule3, null);

    n7.getAssociations().put(rule3, null);
    n8.getAssociations().put(rule3, null);
  }
  @Test
  public void testSingleNodeinSegment() {

    rule1 = new Rule("rule1");
    rule2 = new Rule("rule2");
    rule3 = new Rule("rule3");

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf);
    BuildContext buildContext =
        new BuildContext(ruleBase, ruleBase.getReteooBuilder().getIdGenerator());

    MockObjectSource mockObjectSource = new MockObjectSource(8);
    MockTupleSource mockTupleSource = new MockTupleSource(9);

    // n2 is only node in it's segment
    ObjectTypeNode otn =
        new ObjectTypeNode(2, null, new ClassObjectType(String.class), buildContext);
    BetaNode n1 =
        new JoinNode(
            10,
            new LeftInputAdapterNode(3, otn, buildContext),
            mockObjectSource,
            new EmptyBetaConstraints(),
            buildContext);
    BetaNode n2 = new JoinNode(11, n1, mockObjectSource, new EmptyBetaConstraints(), buildContext);
    BetaNode n3 = new JoinNode(12, n1, mockObjectSource, new EmptyBetaConstraints(), buildContext);
    BetaNode n4 = new JoinNode(13, n2, mockObjectSource, new EmptyBetaConstraints(), buildContext);
    BetaNode n5 = new JoinNode(14, n2, mockObjectSource, new EmptyBetaConstraints(), buildContext);

    n1.addAssociation(rule1, null);
    n1.addAssociation(rule2, null);
    n1.addAssociation(rule3, null);

    n2.addAssociation(rule2, null);
    n2.addAssociation(rule3, null);

    n3.addAssociation(rule1, null);
    n4.addAssociation(rule2, null);
    n5.addAssociation(rule3, null);

    mockObjectSource.attach();
    mockTupleSource.attach();
    n1.attach();
    n2.attach();
    n3.attach();
    n4.attach();
    n5.attach();

    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    createSegmentMemory(n2, wm);

    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n1);
    assertNull(bm.getSegmentMemory());

    bm = (BetaMemory) wm.getNodeMemory(n3);
    assertNull(bm.getSegmentMemory());

    bm = (BetaMemory) wm.getNodeMemory(n4);
    assertNull(bm.getSegmentMemory());

    bm = (BetaMemory) wm.getNodeMemory(n2);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(1, bm.getSegmentMemory().getAllLinkedMaskTest());
  }