Exemple #1
0
  @Test
  public void testNewConstructor() {
    String str =
        ""
            + "package org.drools.compiler.test \n"
            + "import "
            + Person.class.getCanonicalName()
            + "\n"
            + "import "
            + Address.class.getCanonicalName()
            + "\n"
            + "global java.util.List list \n"
            + "rule \"show\" \n"
            + "when  \n"
            + "    $m : Person( address == new Address('s1')) \n"
            + "then \n"
            + "    list.add('r1'); \n"
            + "end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);

    if (kbuilder.hasErrors()) {
      fail(kbuilder.getErrors().toString());
    }

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

    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);

    Person p = new Person("yoda");
    p.setAddress(new Address("s1"));

    ksession.insert(p);

    ksession.fireAllRules();

    assertEquals("r1", list.get(0));

    // Check it was built with MVELReturnValueExpression constraint
    List<ObjectTypeNode> nodes = ((InternalKnowledgeBase) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
      if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
        node = n;
        break;
      }
    }

    AlphaNode alphanode = (AlphaNode) node.getSinkPropagator().getSinks()[0];
    AlphaNodeFieldConstraint constraint = alphanode.getConstraint();

    if (constraint instanceof MvelConstraint) {
      assertTrue(((MvelConstraint) constraint).getFieldExtractor() instanceof ClassFieldReader);
      FieldValue r = ((MvelConstraint) constraint).getField();
      assertEquals(p.getAddress(), r.getValue());
    }
  }
 private void initPathMemories() {
   pathMemories = new PathMemory[3];
   NamedEntryPoint ep = (NamedEntryPoint) epManipulators[8].getEntryPoiny();
   InternalWorkingMemory wm = ((NamedEntryPoint) ep).getInternalWorkingMemory();
   ObjectTypeNode otn =
       ((NamedEntryPoint) ep)
           .getEntryPointNode()
           .getObjectTypeNodes()
           .values()
           .iterator()
           .next();
   AlphaNode alpha = (AlphaNode) otn.getSinkPropagator().getSinks()[0];
   ObjectSink[] sinks = alpha.getSinkPropagator().getSinks();
   for (int i = 0; i < sinks.length; i++) {
     BetaNode beta = (BetaNode) sinks[i];
     RuleTerminalNode rtn = (RuleTerminalNode) beta.getSinkPropagator().getSinks()[0];
     pathMemories[i] = (PathMemory) wm.getNodeMemory(rtn);
   }
 }
    public Boolean call() throws Exception {
      EntryPoint ep = ksession.getEntryPoint("EP" + index);

      InternalWorkingMemory wm = ((NamedEntryPoint) ep).getInternalWorkingMemory();
      ObjectTypeNode otn =
          ((NamedEntryPoint) ep)
              .getEntryPointNode()
              .getObjectTypeNodes()
              .values()
              .iterator()
              .next();
      AlphaNode alpha = (AlphaNode) otn.getSinkPropagator().getSinks()[0];
      BetaNode beta = (BetaNode) alpha.getSinkPropagator().getSinks()[0];
      BetaMemory memory = (BetaMemory) wm.getNodeMemory(beta);
      memory.getSegmentMemory();

      for (int i = 0; i < 100; i++) {
        Thread.sleep(RANDOM.nextInt(100));
        if (fh == null) {
          fh = ep.insert("" + index);
        } else {
          if (RANDOM.nextInt(100) < 70) {
            ep.delete(fh);
            fh = null;
          } else {
            ep.update(fh, "" + index);
          }
        }
      }

      if (index == deleteIndex) {
        if (fh != null) {
          ep.delete(fh);
          fh = null;
        }
      } else if (fh == null) {
        fh = ep.insert("" + index);
      }

      return true;
    }