Ejemplo n.º 1
0
  @Test
  public void testNotWithBindings() throws Exception {
    final PackageBuilder builder = new PackageBuilder();
    builder.addPackageFromDrl(
        new InputStreamReader(getClass().getResourceAsStream("not_with_bindings_rule_test.drl")));
    final Package pkg = builder.getPackage();

    final Rule rule = pkg.getRules()[0];
    assertTrue(rule.isValid());
    assertEquals(0, builder.getErrors().getErrors().length);
    RuleBase ruleBase = getSinglethreadRuleBase();
    ruleBase.addPackage(pkg);
    ruleBase = SerializationHelper.serializeObject(ruleBase);
    final WorkingMemory workingMemory = ruleBase.newStatefulSession();

    final List list = new ArrayList();
    workingMemory.setGlobal("list", list);

    final Cheese stilton = new Cheese("stilton", 5);
    final FactHandle stiltonHandle = workingMemory.insert(stilton);
    final Cheese cheddar = new Cheese("cheddar", 7);
    final FactHandle cheddarHandle = workingMemory.insert(cheddar);

    final PersonInterface paul = new Person("paul", "stilton", 12);
    workingMemory.insert(paul);
    workingMemory.fireAllRules();

    assertEquals(0, list.size());

    workingMemory.retract(stiltonHandle);

    workingMemory.fireAllRules();

    assertEquals(1, list.size());
  }
Ejemplo n.º 2
0
 private boolean checkAndApplyIsDefeated(
     DefeasibleLogicalDependency potentialInferior,
     Rule rule,
     DefeasibleLogicalDependency potentialSuperior) {
   if (potentialSuperior.getDefeats() == null) {
     return false;
   }
   if (potentialSuperior.getStatus() == DefeasibilityStatus.DEFINITELY
       && potentialInferior.getStatus() != DefeasibilityStatus.DEFINITELY) {
     potentialSuperior.addDefeated(potentialInferior);
     return true;
   }
   // adds the references that defeat the current node
   if (Arrays.binarySearch(potentialSuperior.getDefeats(), rule.getName()) >= 0
       || Arrays.binarySearch(
               potentialSuperior.getDefeats(), rule.getPackage() + "." + rule.getName())
           >= 0) {
     potentialSuperior.addDefeated(potentialInferior);
     return true;
   }
   return false;
 }
Ejemplo n.º 3
0
    public void write(MarshallerWriteContext context) throws IOException {
      context.writeShort(WorkingMemoryAction.WorkingMemoryReteAssertAction);

      context.writeInt(this.factHandle.getId());
      context.writeBoolean(this.removeLogical);
      context.writeBoolean(this.updateEqualsMap);

      if (this.ruleOrigin != null) {
        context.writeBoolean(true);
        context.writeUTF(ruleOrigin.getPackage());
        context.writeUTF(ruleOrigin.getName());
      } else {
        context.writeBoolean(false);
      }

      if (this.leftTuple != null) {
        context.writeBoolean(true);
        context.writeInt(context.terminalTupleMap.get(this.leftTuple));
      } else {
        context.writeBoolean(false);
      }
    }
Ejemplo n.º 4
0
    public ProtobufMessages.ActionQueue.Action serialize(MarshallerWriteContext context) {
      ProtobufMessages.ActionQueue.Assert.Builder _assert =
          ProtobufMessages.ActionQueue.Assert.newBuilder();
      _assert
          .setHandleId(this.factHandle.getId())
          .setRemoveLogical(this.removeLogical)
          .setUpdateEqualsMap(this.updateEqualsMap);

      if (this.leftTuple != null) {
        ProtobufMessages.Tuple.Builder _tuple = ProtobufMessages.Tuple.newBuilder();
        for (LeftTuple entry = this.leftTuple; entry != null; entry = entry.getParent()) {
          _tuple.addHandleId(entry.getLastHandle().getId());
        }
        _assert
            .setOriginPkgName(ruleOrigin.getPackageName())
            .setOriginRuleName(ruleOrigin.getName())
            .setTuple(_tuple.build());
      }
      return ProtobufMessages.ActionQueue.Action.newBuilder()
          .setType(ProtobufMessages.ActionQueue.ActionType.ASSERT)
          .setAssert(_assert.build())
          .build();
    }
Ejemplo n.º 5
0
 public DialectRuntimeData removeRule(final Package pkg, final Rule rule) {
   DialectRuntimeData dialect = this.dialects.get(rule.getDialect());
   dialect.removeRule(pkg, rule);
   return dialect;
 }
Ejemplo n.º 6
0
  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);
  }