Beispiel #1
0
  @Test
  public void testPopulatedSingleRuleNoSharing() throws Exception {
    KieBaseConfiguration kconf =
        (KieBaseConfiguration) KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf);
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newStatefulKnowledgeSession());

    wm.insert(new A(1));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.insert(new D(1));
    wm.insert(new E(1));

    wm.fireAllRules();

    kbase.addKnowledgePackages(buildKnowledgePackage("r1", "   A() B() C(object == 2) D() E()\n"));
    List list = new ArrayList();
    wm.setGlobal("list", list);

    ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class);
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getSinkPropagator().getSinks()[0];

    LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst());

    wm.fireAllRules();
    assertNull(sm.getStagedLeftTuples().getInsertFirst());
    assertEquals(1, list.size());

    assertEquals("r1", ((Match) list.get(0)).getRule().getName());
  }
Beispiel #2
0
  private KnowledgeBase buildKnowledgeBase(String ruleName, String rule) {
    String str = "";
    str += "package org.kie \n";
    str += "import " + A.class.getCanonicalName() + "\n";
    str += "import " + B.class.getCanonicalName() + "\n";
    str += "import " + C.class.getCanonicalName() + "\n";
    str += "import " + D.class.getCanonicalName() + "\n";
    str += "import " + E.class.getCanonicalName() + "\n";
    str += "global java.util.List list \n";

    int i = 0;
    str += "rule " + ruleName + "  when \n";
    str += rule;
    str += "then \n";
    str += " list.add( kcontext.getMatch() );\n";
    str += "end \n";

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);

    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf);
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;
  }
  @Test
  public void testAllLinkedInWithNotNodesOnly() {
    setUp(NOT_NODE);

    assertEquals(NotNode.class, n3.getClass()); // make sure it created NotNodes

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));

    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n3);
    createSegmentMemory(n3, wm);
    assertTrue(bm.getSegmentMemory().isSegmentLinked()); // not nodes start off linked

    DefaultFactHandle f1 = (DefaultFactHandle) wm.insert("test1"); // unlinked after first assertion
    n3.assertObject(f1, context, wm);

    // this doesn't unlink on the assertObject, as the node's memory must be processed. So use the
    // helper method the main network evaluator uses.
    PhreakNotNode.unlinkNotNodeOnRightInsert((NotNode) n3, bm, wm);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());

    n3.retractRightTuple(f1.getFirstRightTuple(), context, wm);
    assertTrue(bm.getSegmentMemory().isSegmentLinked());
    // assertFalse( bm.getSegmentMemory().isSigmentLinked() ); // check retraction unlinks again
  }
  @Test
  public void testAllLinkedInWithExistsNodesOnly() {
    setUp(EXISTS_NODE);

    assertEquals(ExistsNode.class, n3.getClass()); // make sure it created ExistsNodes

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    DefaultFactHandle f1 = (DefaultFactHandle) wm.insert("test1");
    n3.assertObject(f1, context, wm);

    BetaMemory bm = (BetaMemory) wm.getNodeMemory(n3);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());

    n4.assertObject(f1, context, wm);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());

    n5.assertObject(f1, context, wm);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());

    n6.assertObject(f1, context, wm);
    assertTrue(
        bm.getSegmentMemory()
            .isSegmentLinked()); // only after all 4 nodes are populated, is the segment linked in

    n6.retractRightTuple(f1.getLastRightTuple(), context, wm);
    assertFalse(bm.getSegmentMemory().isSegmentLinked()); // check retraction unlinks again
  }
  @Test
  public void testLiaNodeInitialisation() {
    setUp(JOIN_NODE);
    // Initialise from lian
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    SegmentUtilities.createSegmentMemory(liaNode, wm);
    liaNode.assertObject((InternalFactHandle) wm.insert("str"), context, wm);

    LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    assertEquals(1, liaMem.getNodePosMaskBit());
    assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());

    BetaMemory bm1 = (BetaMemory) wm.getNodeMemory(n1);
    assertEquals(2, bm1.getNodePosMaskBit());
    assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());

    // Initialise from n1
    wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    n1.assertObject((InternalFactHandle) wm.insert("str"), context, wm);

    liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    assertEquals(1, liaMem.getNodePosMaskBit());
    assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());

    bm1 = (BetaMemory) wm.getNodeMemory(n1);
    assertEquals(2, bm1.getNodePosMaskBit());
    assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());
  }
 private KieBaseConfiguration getKnowledgeBaseConfiguration(
     KieBaseModelImpl kBaseModel, ClassLoader cl) {
   KieBaseConfiguration kbConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null, cl);
   kbConf.setOption(kBaseModel.getEqualsBehavior());
   kbConf.setOption(kBaseModel.getEventProcessingMode());
   kbConf.setOption(kBaseModel.getDeclarativeAgenda());
   return kbConf;
 }
Beispiel #7
0
  public void init(boolean exitOnClose) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    kbuilder
        .batch()
        .add(newClassPathResource("init.drl", getClass()), DRL)
        .add(newClassPathResource("game.drl", getClass()), DRL)
        .add(newClassPathResource("keys.drl", getClass()), DRL)
        .add(newClassPathResource("move.drl", getClass()), DRL)
        .add(newClassPathResource("collision.drl", getClass()), DRL)
        .add(newClassPathResource("ui.drl", getClass()), DRL)
        .build();
    if (kbuilder.hasErrors()) {
      throw new RuntimeException(kbuilder.getErrors().toString());
    }

    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(config);
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    // ksession.addEventListener( new DebugAgendaEventListener() );
    PongConfiguration pconf = new PongConfiguration();
    pconf.setExitOnClose(exitOnClose);
    ksession.setGlobal("pconf", pconf);

    //        ksession.addEventListener( new DefaultAgendaEventListener() {
    //            public void beforeMatchFired(BeforeActivationFiredEvent event)  {
    //                System.out.println( "b: " + event.getActivation().getRule().getName() + " : "
    // + event.getActivation().toFactHandles() );
    //            }
    //            public void afterMatchFired(AfterActivationFiredEvent event)  {
    //                System.out.println( "a: " + event.getActivation().getRule().getName() + " : "
    // + event.getActivation().toFactHandles() );
    //            }
    ////            public void matchCreated(ActivationCreatedEvent event)  {
    ////                System.out.println( "cr: " + event.getActivation().getRule().getName() + " :
    // " + event.getActivation().toFactHandles() );
    ////            }
    ////            public void matchCancelled(ActivationCancelledEvent event)  {
    ////                System.out.println( "cl: " + event.getActivation().getRule().getName() + " :
    // " + event.getActivation().toFactHandles() );
    ////            }
    //
    //        });

    runKSession(ksession);
  }
  @Test
  public void testLiaNodeLinking() {
    setUp(JOIN_NODE);
    // Initialise from lian
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));

    SegmentUtilities.createSegmentMemory(liaNode, wm);

    InternalFactHandle fh1 = (InternalFactHandle) wm.insert("str1");
    n1.assertObject(fh1, context, wm);

    LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
    assertEquals(1, liaMem.getNodePosMaskBit());
    assertEquals(3, liaMem.getSegmentMemory().getAllLinkedMaskTest());

    BetaMemory bm1 = (BetaMemory) wm.getNodeMemory(n1);
    assertEquals(2, bm1.getNodePosMaskBit());
    assertEquals(3, bm1.getSegmentMemory().getAllLinkedMaskTest());

    // still unlinked
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());

    // now linked
    InternalFactHandle fh2 = (InternalFactHandle) wm.insert("str2");
    liaNode.assertObject(fh2, context, wm);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());

    // test unlink after one retract
    liaNode.retractLeftTuple(fh2.getFirstLeftTuple(), context, wm);
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());

    // check counter, after multiple asserts
    InternalFactHandle fh3 = (InternalFactHandle) wm.insert("str3");
    InternalFactHandle fh4 = (InternalFactHandle) wm.insert("str4");
    liaNode.assertObject(fh3, context, wm);
    liaNode.assertObject(fh4, context, wm);

    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());

    liaNode.retractLeftTuple(fh3.getFirstLeftTuple(), context, wm);
    assertTrue(liaMem.getSegmentMemory().isSegmentLinked());

    liaNode.retractLeftTuple(fh4.getFirstLeftTuple(), context, wm);
    assertFalse(liaMem.getSegmentMemory().isSegmentLinked());
  }
  private KieBase createKieBase(
      KieBaseModelImpl kBaseModel,
      KieProject kieProject,
      ResultsImpl messages,
      KieBaseConfiguration conf) {
    ClassLoader cl = kieProject.getClassLoader();
    InternalKieModule kModule = kieProject.getKieModuleForKBase(kBaseModel.getName());

    Collection<KnowledgePackage> pkgs =
        kModule.getKnowledgePackagesForKieBase(kBaseModel.getName());

    if (pkgs == null) {
      KnowledgeBuilder kbuilder = buildKnowledgePackages(kBaseModel, kieProject, messages);
      if (kbuilder.hasErrors()) {
        // Messages already populated by the buildKnowlegePackages
        return null;
      }
    }

    // if we get to here, then we know the pkgs is now cached
    pkgs = kModule.getKnowledgePackagesForKieBase(kBaseModel.getName());

    if (kBaseModel.getEventProcessingMode() == EventProcessingOption.CLOUD
        && (conf == null
            || conf.getOption(EventProcessingOption.class) == EventProcessingOption.CLOUD)) {
      for (KnowledgePackage kpkg : pkgs) {
        if (((KnowledgePackageImpl) kpkg).needsStreamMode()) {
          throw new RuntimeException(
              "The requested KieBase \""
                  + kBaseModel.getName()
                  + "\" has been set to run in CLOUD mode but requires features only available in STREAM mode");
        }
      }
    }

    if (conf == null) {
      conf = getKnowledgeBaseConfiguration(kBaseModel, cl);
    } else if (conf instanceof RuleBaseConfiguration) {
      ((RuleBaseConfiguration) conf).setClassLoader(cl);
    }
    InternalKnowledgeBase kBase =
        (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(conf);

    kBase.addKnowledgePackages(pkgs);
    return kBase;
  }
 private InternalKnowledgeBase createKnowledgeBase() {
   KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
   config.setOption(EventProcessingOption.STREAM);
   return (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(config);
 }
Beispiel #11
0
  @Test
  public void testEventTimestamp() {
    // DROOLS-268
    String drl =
        "\n"
            + "import org.drools.reteoo.integrationtests.CepEspTest.Event; \n"
            + "global java.util.List list; \n"
            + "global org.drools.core.time.SessionPseudoClock clock; \n"
            + ""
            + "declare Event \n"
            + " @role( event )\n"
            + " @timestamp( time ) \n"
            + " @expires( 10000000 ) \n"
            + "end \n"
            + ""
            + ""
            + "rule \"inform about E1\"\n"
            + "when\n"
            + " $event1 : Event( type == 1 )\n"
            + " //there is an event (T2) with value 0 between 0,2m after doorClosed\n"
            + " $event2: Event( type == 2, value == 1, this after [0, 1200ms] $event1, $timestamp : time )\n"
            + " //there is no newer event (T2) within the timeframe\n"
            + " not Event( type == 2, this after [0, 1200ms] $event1, time > $timestamp ) \n"
            + "then\n"
            + " list.add( clock.getCurrentTime() ); \n "
            + "end\n"
            + "\n";

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
      fail(kbuilder.getErrors().toString());
    }
    KieBaseConfiguration baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    baseConfig.setOption(EventProcessingOption.STREAM);
    baseConfig.setOption(RuleEngineOption.RETEOO);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig);
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));

    // init stateful knowledge session
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(sessionConfig, null);
    ArrayList list = new ArrayList();
    ksession.setGlobal("list", list);

    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    ksession.setGlobal("clock", clock);

    ksession.insert(new Event(1, -1, clock.getCurrentTime())); // 0
    clock.advanceTime(600, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    ksession.insert(new Event(2, 0, clock.getCurrentTime())); // 600
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    ksession.insert(new Event(2, 0, clock.getCurrentTime())); // 700
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    ksession.insert(new Event(2, 0, clock.getCurrentTime())); // 1000
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    ksession.insert(new Event(2, 1, clock.getCurrentTime())); // 1100
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    ksession.insert(new Event(2, 0, clock.getCurrentTime())); // 1300

    clock.advanceTime(1000, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();

    assertFalse(list.isEmpty());
    assertEquals(1, list.size());
    Long time = (Long) list.get(0);

    assertTrue(time > 1000 && time < 1500);

    ksession.dispose();
  }
  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 tesMultiNodeSegmentDifferentInitialisationPoints() {
    setUp(JOIN_NODE);
    // Initialise from n3
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(RuleEngineOption.PHREAK);
    AbstractWorkingMemory wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    createSegmentMemory(n3, wm);

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

    bm = (BetaMemory) wm.getNodeMemory(n3);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n4);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n5);
    assertEquals(4, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n6);
    assertEquals(8, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    // Initialise from n4
    wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    bm = createSegmentMemory(n4, wm);

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

    bm = (BetaMemory) wm.getNodeMemory(n3);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n4);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n5);
    assertEquals(4, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n6);
    assertEquals(8, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    // Initialise from n5
    wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    createSegmentMemory(n5, wm);

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

    bm = (BetaMemory) wm.getNodeMemory(n3);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n4);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n5);
    assertEquals(4, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n6);
    assertEquals(8, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    // Initialise from n6
    wm =
        new AbstractWorkingMemory(
            1, (ReteooRuleBase) RuleBaseFactory.newRuleBase((RuleBaseConfiguration) kconf));
    createSegmentMemory(n6, wm);

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

    bm = (BetaMemory) wm.getNodeMemory(n3);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n4);
    assertEquals(2, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n5);
    assertEquals(4, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());

    bm = (BetaMemory) wm.getNodeMemory(n6);
    assertEquals(8, bm.getNodePosMaskBit());
    assertEquals(15, bm.getSegmentMemory().getAllLinkedMaskTest());
  }
  @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());
  }