Пример #1
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;
  }
Пример #2
0
  @Test
  public void testQueryTerminalNode() {
    final ClassObjectType queryObjectType = new ClassObjectType(DroolsQuery.class);
    final ObjectTypeNode queryObjectTypeNode =
        new ObjectTypeNode(
            this.buildContext.getNextId(), this.entryPoint, queryObjectType, buildContext);
    queryObjectTypeNode.attach(buildContext);

    ClassFieldReader extractor = store.getReader(DroolsQuery.class, "name");

    MvelConstraint constraint =
        new MvelConstraintTestUtil(
            "name == \"query-1\"", FieldFactory.getInstance().getFieldValue("query-1"), extractor);

    final QueryImpl query = new QueryImpl("query-1");
    buildContext.setRule(query);
    AlphaNode alphaNode =
        new AlphaNode(this.buildContext.getNextId(), constraint, queryObjectTypeNode, buildContext);
    alphaNode.attach(buildContext);

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

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

    extractor = store.getReader(Cheese.class, "type");

    constraint =
        new MvelConstraintTestUtil(
            "type == \"stilton\"", FieldFactory.getInstance().getFieldValue("stilton"), extractor);

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

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

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

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

    queryNode.attach(buildContext);

    final KnowledgePackageImpl pkg = new KnowledgePackageImpl("com.drools.test");
    pkg.addRule(query);
    ((KnowledgeBaseImpl) kBase).addPackages(Arrays.asList(new InternalKnowledgePackage[] {pkg}));

    KieSession kSession = kBase.newKieSession();
    QueryResults results = kSession.getQueryResults("query-1");

    assertEquals(0, results.size());

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

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

    assertEquals(1, results.size());

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

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

    assertEquals(1, results.size());

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

    final FactHandle handle2 = kSession.insert(stilton2);

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

    assertEquals(2, results.size());

    /**
     * QueryResultsRow result = results.get( 0 ); assertEquals( 1, result.size() ); assertEquals(
     * stilton1, result.get( 0 ) );
     *
     * <p>result = results.get( 1 ); assertEquals( 1, result.size() ); assertEquals( stilton2,
     * result.get( 0 ) );
     */
    int i = 0;
    for (final Iterator<QueryResultsRow> it = results.iterator(); it.hasNext(); ) {
      QueryResultsRow resultRow = it.next();
      if (i == 1) {
        //                assertSame( stilton2, result.g( 0 ) );
      } else {
        //                assertSame( stilton1, result.get( 0 ) );
      }
      i++;
    }

    kSession.delete(handle1);
    results = kSession.getQueryResults("query-1");

    assertEquals(1, results.size());

    kSession.delete(handle2);
    results = kSession.getQueryResults("query-1");

    assertEquals(0, results.size());
  }