public void closeLiveQuery(final InternalFactHandle factHandle) {

    try {
      startOperation();
      this.ruleBase.readLock();
      this.lock.lock();

      final PropagationContext pCtx =
          new PropagationContextImpl(
              getNextPropagationIdCounter(),
              PropagationContext.INSERTION,
              null,
              null,
              factHandle,
              getEntryPoint());

      if (this.ruleBase.getConfiguration().isPhreakEnabled()) {
        LeftInputAdapterNode lian =
            (LeftInputAdapterNode)
                factHandle.getFirstLeftTuple().getLeftTupleSink().getLeftTupleSource();
        LiaNodeMemory lmem = (LiaNodeMemory) getNodeMemory((MemoryFactory) lian);
        SegmentMemory lsmem = lmem.getSegmentMemory();

        LeftTuple childLeftTuple =
            factHandle.getFirstLeftTuple(); // there is only one, all other LTs are peers
        LeftInputAdapterNode.doDeleteObject(
            childLeftTuple, childLeftTuple.getPropagationContext(), lsmem, this, lian, false, lmem);

        List<PathMemory> rmems = lmem.getSegmentMemory().getPathMemories();
        for (int i = 0, length = rmems.size(); i < length; i++) {
          PathMemory rm = rmems.get(i);

          RuleNetworkEvaluatorActivation evaluator =
              agenda.createRuleNetworkEvaluatorActivation(
                  Integer.MAX_VALUE, rm, (TerminalNode) rm.getNetworkNode());
          evaluator.evaluateNetwork(this, 0, -1);
        }
      } else {
        getEntryPointNode().retractQuery(factHandle, pCtx, this);

        pCtx.evaluateActionQueue(this);
      }

      getFactHandleFactory().destroyFactHandle(factHandle);

    } finally {
      this.lock.unlock();
      this.ruleBase.readUnlock();
      endOperation();
    }
  }
  @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());
  }
Exemple #3
0
  public void modifyActivation(
      final InternalFactHandle factHandle,
      final PropagationContext context,
      final InternalWorkingMemory workingMemory) {
    if (activationNode == null) {
      this.activationNode = objectTypeNodes.get(ClassObjectType.Match_ObjectType);
    }

    if (activationNode != null) {
      ModifyPreviousTuples modifyPreviousTuples =
          new ModifyPreviousTuples(
              factHandle.getFirstLeftTuple(), factHandle.getFirstRightTuple(), unlinkingEnabled);
      factHandle.clearLeftTuples();
      factHandle.clearRightTuples();

      // There may be no queries defined
      this.activationNode.modifyObject(factHandle, modifyPreviousTuples, context, workingMemory);
      modifyPreviousTuples.retractTuples(context, workingMemory);
    }
  }
Exemple #4
0
  public void modifyObject(
      final InternalFactHandle handle,
      final PropagationContext context,
      final ObjectTypeConf objectTypeConf,
      final InternalWorkingMemory wm) {
    if (log.isTraceEnabled()) {
      log.trace("Update {}", handle.toString());
    }

    // checks if shadow is enabled
    if (objectTypeConf.isShadowEnabled()) {
      // the user has implemented the ShadowProxy interface, let their implementation
      // know it is safe to update the information the engine can see.
      ((ShadowProxy) handle.getObject()).updateProxy();
    }

    ObjectTypeNode[] cachedNodes = objectTypeConf.getObjectTypeNodes();

    // make a reference to the previous tuples, then null then on the handle
    ModifyPreviousTuples modifyPreviousTuples =
        new ModifyPreviousTuples(
            handle.getFirstLeftTuple(), handle.getFirstRightTuple(), unlinkingEnabled);
    handle.clearLeftTuples();
    handle.clearRightTuples();

    for (int i = 0, length = cachedNodes.length; i < length; i++) {
      cachedNodes[i].modifyObject(handle, modifyPreviousTuples, context, wm);

      // remove any right tuples that matches the current OTN before continue the modify on the next
      // OTN cache entry
      if (i < cachedNodes.length - 1) {
        RightTuple rightTuple = modifyPreviousTuples.peekRightTuple();
        while (rightTuple != null
            && ((BetaNode) rightTuple.getRightTupleSink()).getObjectTypeNode() == cachedNodes[i]) {
          modifyPreviousTuples.removeRightTuple();

          if (unlinkingEnabled) {
            BetaMemory bm = BetaNode.getBetaMemory((BetaNode) rightTuple.getRightTupleSink(), wm);
            BetaNode.doDeleteRightTuple(rightTuple, wm, bm);
          } else {
            ((BetaNode) rightTuple.getRightTupleSink()).retractRightTuple(rightTuple, context, wm);
          }

          rightTuple = modifyPreviousTuples.peekRightTuple();
        }

        LeftTuple leftTuple;
        ObjectTypeNode otn;
        while (true) {
          leftTuple = modifyPreviousTuples.peekLeftTuple();
          otn = null;
          if (leftTuple != null) {
            LeftTupleSink leftTupleSink = leftTuple.getLeftTupleSink();
            if (leftTupleSink instanceof LeftTupleSource) {
              otn = ((LeftTupleSource) leftTupleSink).getLeftTupleSource().getObjectTypeNode();
            } else if (leftTupleSink instanceof RuleTerminalNode) {
              otn = ((RuleTerminalNode) leftTupleSink).getObjectTypeNode();
            }
          }

          if (otn == null || otn == cachedNodes[i + 1]) break;

          modifyPreviousTuples.removeLeftTuple();

          if (unlinkingEnabled) {
            LeftInputAdapterNode liaNode =
                (LeftInputAdapterNode) leftTuple.getLeftTupleSink().getLeftTupleSource();
            LiaNodeMemory lm = (LiaNodeMemory) wm.getNodeMemory(liaNode);
            LeftInputAdapterNode.doDeleteObject(
                leftTuple, context, lm.getSegmentMemory(), wm, liaNode, true, lm);
          } else {
            leftTuple.getLeftTupleSink().retractLeftTuple(leftTuple, context, wm);
          }
        }
      }
    }
    modifyPreviousTuples.retractTuples(context, wm);
  }