Ejemplo n.º 1
0
    public void execute(InternalWorkingMemory workingMemory) {
      DroolsQuery query = (DroolsQuery) factHandle.getObject();
      RightTupleList rightTuples = query.getResultInsertRightTupleList();
      query.setResultInsertRightTupleList(
          null); // null so further operations happen on a new stack element

      for (RightTuple rightTuple = rightTuples.getFirst(); rightTuple != null; ) {
        RightTuple tmp = (RightTuple) rightTuple.getNext();
        rightTuples.remove(rightTuple);
        for (LeftTuple childLeftTuple = rightTuple.firstChild;
            childLeftTuple != null;
            childLeftTuple = (LeftTuple) childLeftTuple.getRightParentNext()) {
          node.getSinkPropagator()
              .doPropagateAssertLeftTuple(
                  context, workingMemory, childLeftTuple, childLeftTuple.getLeftTupleSink());
        }
        rightTuple = tmp;
      }

      // @FIXME, this should work, but it's closing needed fact handles
      // actually an evaluation 34 appears on the stack twice....
      //            if ( !node.isOpenQuery() ) {
      //                workingMemory.getFactHandleFactory().destroyFactHandle( this.factHandle );
      //            }
    }
Ejemplo n.º 2
0
  @SuppressWarnings("unchecked")
  public QueryResults getQueryResults(final String queryName, final Object[] arguments) {

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

      this.ruleBase.executeQueuedActions();
      executeQueuedActions();

      DroolsQuery queryObject =
          new DroolsQuery(
              queryName, arguments, getQueryListenerInstance(), false, null, null, null, null);

      InternalFactHandle handle = this.handleFactory.newFactHandle(queryObject, null, this, this);

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

      BaseNode[] tnodes = evalQuery(queryName, queryObject, handle, pCtx);

      List<Map<String, Declaration>> decls = new ArrayList<Map<String, Declaration>>();
      if (tnodes != null) {
        for (BaseNode node : tnodes) {
          decls.add(((QueryTerminalNode) node).getSubRule().getOuterDeclarations());
        }
      }

      executeQueuedActions();

      this.handleFactory.destroyFactHandle(handle);

      return new QueryResults(
          (List<QueryRowWithSubruleIndex>) queryObject.getQueryResultCollector().getResults(),
          decls.toArray(new Map[decls.size()]),
          this,
          (queryObject.getQuery() != null)
              ? queryObject.getQuery().getParameters()
              : new Declaration[0]);
    } finally {
      this.lock.unlock();
      this.ruleBase.readUnlock();
      endOperation();
    }
  }
Ejemplo n.º 3
0
    public void execute(InternalWorkingMemory workingMemory) {
      DroolsQuery query = (DroolsQuery) factHandle.getObject();
      RightTupleList rightTuples = query.getResultRetractRightTupleList();
      query.setResultRetractRightTupleList(
          null); // null so further operations happen on a new stack element

      for (RightTuple rightTuple = rightTuples.getFirst(); rightTuple != null; ) {
        RightTuple tmp = (RightTuple) rightTuple.getNext();
        rightTuples.remove(rightTuple);
        this.node
            .getSinkPropagator()
            .propagateRetractRightTuple(rightTuple, context, workingMemory);
        rightTuple = tmp;
      }
    }
Ejemplo n.º 4
0
  public LiveQuery openLiveQuery(
      final String query, final Object[] arguments, final ViewChangedEventListener listener) {

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

      this.ruleBase.executeQueuedActions();
      executeQueuedActions();

      DroolsQuery queryObject =
          new DroolsQuery(
              query,
              arguments,
              new OpenQueryViewChangedEventListenerAdapter(listener),
              true,
              null,
              null,
              null,
              null);
      InternalFactHandle handle = this.handleFactory.newFactHandle(queryObject, null, this, this);

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

      BaseNode[] tnodes = evalQuery(queryObject.getName(), queryObject, handle, pCtx);

      executeQueuedActions();

      return new LiveQueryImpl(this, handle);
    } finally {
      this.lock.unlock();
      this.ruleBase.readUnlock();
      endOperation();
    }
  }
Ejemplo n.º 5
0
  private BaseNode[] evalQuery(
      String queryName,
      DroolsQuery queryObject,
      InternalFactHandle handle,
      PropagationContext pCtx) {
    BaseNode[] tnodes = (BaseNode[]) ruleBase.getReteooBuilder().getTerminalNodes(queryName);
    if (this.ruleBase.getConfiguration().isPhreakEnabled()) {
      if (tnodes == null) {
        throw new RuntimeException("Query '" + queryName + "' does not exist");
      }

      QueryTerminalNode tnode = (QueryTerminalNode) tnodes[0];
      LeftTupleSource lts = tnode.getLeftTupleSource();
      while (lts.getType() != NodeTypeEnums.LeftInputAdapterNode) {
        lts = lts.getLeftTupleSource();
      }
      LeftInputAdapterNode lian = (LeftInputAdapterNode) lts;
      LiaNodeMemory lmem = (LiaNodeMemory) getNodeMemory((MemoryFactory) lts);
      SegmentMemory lsmem = lmem.getSegmentMemory();
      if (lsmem == null) {
        lsmem = SegmentUtilities.createSegmentMemory(lts, this);
      }
      LeftInputAdapterNode.doInsertObject(
          handle, pCtx, lian, this, lmem, false, queryObject.isOpen());

      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 {
      // no need to call retract, as no leftmemory used.
      getEntryPointNode().assertQuery(handle, pCtx, this);

      pCtx.evaluateActionQueue(this);
    }
    return tnodes;
  }