Beispiel #1
0
  public void updateSink(
      final ObjectSink sink,
      final PropagationContext context,
      final InternalWorkingMemory workingMemory) {
    // @todo
    // JBRULES-612: the cache MUST be invalidated when a new node type is added to the network, so
    // iterate and reset all caches.
    final ObjectTypeNode node = (ObjectTypeNode) sink;

    final ObjectType newObjectType = node.getObjectType();

    InternalWorkingMemoryEntryPoint wmEntryPoint =
        (InternalWorkingMemoryEntryPoint)
            workingMemory.getWorkingMemoryEntryPoint(this.entryPoint.getEntryPointId());

    for (ObjectTypeConf objectTypeConf :
        wmEntryPoint.getObjectTypeConfigurationRegistry().values()) {
      if (newObjectType.isAssignableFrom(
          objectTypeConf.getConcreteObjectTypeNode().getObjectType())) {
        objectTypeConf.resetCache();
        ObjectTypeNode sourceNode = objectTypeConf.getConcreteObjectTypeNode();
        Iterator it =
            ((ObjectTypeNodeMemory) workingMemory.getNodeMemory(sourceNode)).memory.iterator();
        for (ObjectEntry entry = (ObjectEntry) it.next();
            entry != null;
            entry = (ObjectEntry) it.next()) {
          sink.assertObject((InternalFactHandle) entry.getValue(), context, workingMemory);
        }
      }
    }
  }
 public static Class<?> getNodeClass(ObjectTypeNode objectTypeNode) {
   if (objectTypeNode == null) {
     return null;
   }
   ObjectType objectType = objectTypeNode.getObjectType();
   return objectType != null && objectType instanceof ClassObjectType
       ? ((ClassObjectType) objectType).getClassType()
       : null;
 }
 public ObjectTypeNode getObjectTypeNode(KnowledgeBase kbase, Class<?> nodeClass) {
   List<ObjectTypeNode> nodes =
       ((InternalRuleBase) ((KnowledgeBaseImpl) kbase).ruleBase).getRete().getObjectTypeNodes();
   for (ObjectTypeNode n : nodes) {
     if (((ClassObjectType) n.getObjectType()).getClassType() == nodeClass) {
       return n;
     }
   }
   return null;
 }
 /**
  * Constructus a LeftInputAdapterNode with a unique id that receives <code>FactHandle</code> from
  * a parent <code>ObjectSource</code> and adds it to a given pattern in the resulting Tuples.
  *
  * @param id The unique id of this node in the current Rete network
  * @param source The parent node, where Facts are propagated from
  */
 public LeftInputAdapterNode(final int id, final ObjectSource source, final BuildContext context) {
   super(
       id,
       context.getPartitionId(),
       context.getRuleBase().getConfiguration().isMultithreadEvaluation());
   this.objectSource = source;
   this.leftTupleMemoryEnabled = context.isTupleMemoryEnabled();
   ObjectSource current = source;
   while (!(current instanceof ObjectTypeNode)) {
     current = current.getParentObjectSource();
   }
   ObjectTypeNode otn = (ObjectTypeNode) current;
   rootQueryNode = ClassObjectType.DroolsQuery_ObjectType.isAssignableFrom(otn.getObjectType());
 }
Beispiel #5
0
  @Test
  public void testEvalDetectionInBetaNode() {
    // Tests evals are generated and executed with Java dialect

    String drl = "";
    drl += "package org.test\n";
    drl += "import org.drools.Person\n";
    drl += "global java.util.List list\n";
    drl += "rule test1\n";
    drl += "when\n";
    drl += "   $s  : String()\n";
    drl += "   $p1 : Person( eval( name \n != $s ), name == ( new String($s+\"xxx\") ) )\n";
    drl += "then\n";
    drl += "end\n";

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (kbuilder.hasErrors()) {
      fail(kbuilder.getErrors().toString());
    }
    assertFalse(kbuilder.hasErrors());

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

    List<ObjectTypeNode> nodes =
        ((InternalRuleBase) ((KnowledgeBaseImpl) kbase).ruleBase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
      if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
        node = n;
        break;
      }
    }

    BetaNode betaanode = (BetaNode) node.getSinkPropagator().getSinks()[0];
    BetaNodeFieldConstraint[] constraint = (BetaNodeFieldConstraint[]) betaanode.getConstraints();
    PredicateConstraint c = (PredicateConstraint) constraint[0];
    assertTrue(c.getPredicateExpression() instanceof PredicateExpression);
    assertTrue(c.getPredicateExpression() instanceof CompiledInvoker);
    assertTrue(!(c.getPredicateExpression() instanceof MVELPredicateExpression));

    ReturnValueRestriction r =
        (ReturnValueRestriction) ((VariableConstraint) constraint[1]).getRestriction();
    assertTrue(r.getExpression() instanceof ReturnValueExpression);
    assertTrue(r.getExpression() instanceof CompiledInvoker);
    assertTrue(!(r.getExpression() instanceof MVELReturnValueExpression));
  }
Beispiel #6
0
  @Test
  public void testEvalDetectionInAlphaNode() {
    // Tests evals are generated and executed with Java dialect
    String drl = "";
    drl += "package org.test\n";
    drl += "import org.drools.Person\n";
    drl += "global java.util.List list\n";
    drl += "rule test1\n";
    drl += "when\n";
    drl += "   $p1 : Person( eval( name \n != null ), name == ( new String(\"xxx\") ) )\n";
    drl += "then\n";
    drl += "end\n";

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (kbuilder.hasErrors()) {
      fail(kbuilder.getErrors().toString());
    }
    assertFalse(kbuilder.hasErrors());

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

    List<ObjectTypeNode> nodes =
        ((InternalRuleBase) ((KnowledgeBaseImpl) kbase).ruleBase).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];
    PredicateConstraint c = (PredicateConstraint) alphanode.getConstraint();
    assertTrue(c.getPredicateExpression() instanceof PredicateExpression);
    assertTrue(c.getPredicateExpression() instanceof CompiledInvoker);
    assertTrue(!(c.getPredicateExpression() instanceof MVELPredicateExpression));

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

    if (constraint instanceof LiteralConstraint) {
      FieldValue fieldVal = ((LiteralConstraint) constraint).getField();
      assertEquals("xxx", fieldVal.getValue());
    }
  }
Beispiel #7
0
  @Test
  public void testMapAccessorWithStaticFieldAccess() {
    String str =
        ""
            + "package org.test \n"
            + "import "
            + Person.class.getCanonicalName()
            + "\n"
            + "import "
            + Address.class.getCanonicalName()
            + "\n"
            + "import "
            + TestEnum.class.getCanonicalName()
            + "\n"
            + "global java.util.List list \n"
            + "rule \"show\" \n"
            + "when  \n"
            + "    $m : Person( namedAddresses[TestEnum.ONE] == new Address('s1'), namedAddresses[TestEnum.ONE].street == new Address('s1').street ) \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.getNamedAddresses().put(TestEnum.ONE, new Address("s1"));

    ksession.insert(p);

    ksession.fireAllRules();

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

    // Check it was built with MVELReturnValueExpression constraint
    List<ObjectTypeNode> nodes =
        ((InternalRuleBase) ((KnowledgeBaseImpl) kbase).ruleBase).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) alphanode.getConstraint()).getFieldExtractor()
              instanceof MVELObjectClassFieldReader);
      assertEquals(
          new Address("s1"), ((MvelConstraint) alphanode.getConstraint()).getField().getValue());
    }

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

    if (constraint instanceof MvelConstraint) {
      assertTrue(
          ((MvelConstraint) alphanode.getConstraint()).getFieldExtractor()
              instanceof MVELObjectClassFieldReader);
      assertEquals(
          new Address("s1").getStreet(),
          ((MvelConstraint) alphanode.getConstraint()).getField().getValue());
    }
  }
Beispiel #8
0
 protected void removeObjectSink(final ObjectSink objectSink) {
   final ObjectTypeNode node = (ObjectTypeNode) objectSink;
   this.objectTypeNodes.remove(node.getObjectType());
 }
Beispiel #9
0
 /**
  * Adds the <code>ObjectSink</code> so that it may receive <code>Objects</code> propagated from
  * this <code>ObjectSource</code>.
  *
  * @param objectSink The <code>ObjectSink</code> to receive propagated <code>Objects</code>. Rete
  *     only accepts <code>ObjectTypeNode</code>s as parameters to this method, though.
  */
 public void addObjectSink(final ObjectSink objectSink) {
   final ObjectTypeNode node = (ObjectTypeNode) objectSink;
   this.objectTypeNodes.put(node.getObjectType(), node);
 }
 /* (non-Javadoc)
  * @see org.drools.management.ObjectTypeNodeMonitorMbean#isEvent()
  */
 public boolean isEvent() {
   return node.getObjectType().isEvent();
 }
 /* (non-Javadoc)
  * @see org.drools.management.ObjectTypeNodeMonitorMbean#getObjectType()
  */
 public String getObjectType() {
   return node.getObjectType().toString();
 }