コード例 #1
0
ファイル: JavaDialectTest.java プロジェクト: arblack/drools
  @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));
  }
コード例 #2
0
ファイル: JavaDialectTest.java プロジェクト: arblack/drools
  @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());
    }
  }
コード例 #3
0
  public void build(
      final RuleBuildContext context,
      final BoundIdentifiers usedIdentifiers,
      final Declaration[] previousDeclarations,
      final Declaration[] localDeclarations,
      final PredicateConstraint predicate,
      final PredicateDescr predicateDescr,
      final AnalysisResult analysis) {
    MVELDialect dialect = (MVELDialect) context.getDialect(context.getDialect().getId());

    try {
      Map<String, Class<?>> declIds =
          context.getDeclarationResolver().getDeclarationClasses(context.getRule());

      Pattern p = (Pattern) context.getBuildStack().peek();
      if (p.getObjectType() instanceof ClassObjectType) {
        declIds.put("this", ((ClassObjectType) p.getObjectType()).getClassType());
      }

      MVELCompilationUnit unit =
          dialect.getMVELCompilationUnit(
              (String) predicateDescr.getContent(),
              analysis,
              previousDeclarations,
              localDeclarations,
              null,
              context);

      MVELPredicateExpression expr =
          new MVELPredicateExpression(unit, context.getDialect().getId());
      predicate.setPredicateExpression(expr);

      MVELDialectRuntimeData data =
          (MVELDialectRuntimeData)
              context
                  .getPkg()
                  .getDialectRuntimeRegistry()
                  .getDialectData(context.getDialect().getId());
      data.addCompileable(predicate, expr);

      expr.compile(context.getPackageBuilder().getRootClassLoader());
    } catch (final Exception e) {
      context
          .getErrors()
          .add(
              new DescrBuildError(
                  context.getParentDescr(),
                  predicateDescr,
                  e,
                  "Unable to build expression for 'inline-eval' : "
                      + e.getMessage()
                      + "'"
                      + predicateDescr.getContent()
                      + "'\n"
                      + e.getMessage()));
    }
  }
コード例 #4
0
  @Test
  public void test1() {
    KnowledgePackage pkg1 = getKnowledgePackage1();
    KnowledgePackage pkg2 = getKnowledgePackage1();
    KnowledgePackage pkg3 = getKnowledgePackage2();

    Rule rule1 = ((KnowledgePackageImp) pkg1).pkg.getRule("rule1");
    Rule rule2 = ((KnowledgePackageImp) pkg2).pkg.getRule("rule1");
    Rule rule3 = ((KnowledgePackageImp) pkg3).pkg.getRule("rule1");

    // test return value
    Pattern p1 = (Pattern) rule1.getLhs().getChildren().get(0);
    VariableConstraint rvc1 = (VariableConstraint) p1.getConstraints().get(0);
    ReturnValueExpression rve1 = ((ReturnValueRestriction) rvc1.getRestriction()).getExpression();

    Pattern p2 = (Pattern) rule2.getLhs().getChildren().get(0);
    VariableConstraint rvc2 = (VariableConstraint) p2.getConstraints().get(0);
    ReturnValueExpression rve2 = ((ReturnValueRestriction) rvc2.getRestriction()).getExpression();

    assertNotSame(rve1, rve2);
    assertEquals(rve1, rve2);

    Pattern p3 = (Pattern) rule3.getLhs().getChildren().get(0);
    VariableConstraint rvc3 = (VariableConstraint) p3.getConstraints().get(0);
    ReturnValueExpression rve3 = ((ReturnValueRestriction) rvc3.getRestriction()).getExpression();

    assertNotSame(rve1, rve3);
    assertThat(rve1, not(equalTo(rve3)));

    // test inline eval
    PredicateConstraint pc1 = (PredicateConstraint) p1.getConstraints().get(1);
    PredicateExpression pe1 = (PredicateExpression) pc1.getPredicateExpression();

    PredicateConstraint pc2 = (PredicateConstraint) p2.getConstraints().get(1);
    PredicateExpression pe2 = (PredicateExpression) pc2.getPredicateExpression();
    assertNotSame(pe1, pe2);
    assertEquals(pe1, pe2);

    PredicateConstraint pc3 = (PredicateConstraint) p3.getConstraints().get(1);
    PredicateExpression pe3 = (PredicateExpression) pc3.getPredicateExpression();
    assertNotSame(pe1, pe3);
    assertThat(pe1, not(equalTo(pe3)));

    // test eval
    EvalCondition ec1 = (EvalCondition) rule1.getLhs().getChildren().get(1);
    EvalExpression ee1 = (EvalExpression) ec1.getEvalExpression();

    EvalCondition ec2 = (EvalCondition) rule2.getLhs().getChildren().get(1);
    EvalExpression ee2 = (EvalExpression) ec2.getEvalExpression();
    assertNotSame(ee1, ee2);
    assertEquals(ee1, ee2);

    EvalCondition ec3 = (EvalCondition) rule3.getLhs().getChildren().get(1);
    EvalExpression ee3 = (EvalExpression) ec3.getEvalExpression();
    assertNotSame(ee1, ee3);
    assertThat(ee1, not(equalTo(ee3)));

    // test consequence
    assertNotSame(rule1.getConsequence(), rule2.getConsequence());
    assertEquals(rule1.getConsequence(), rule2.getConsequence());
    assertNotSame(rule1.getConsequence(), rule3.getConsequence());
    assertThat(rule1.getConsequence(), not(equalTo(rule3.getConsequence())));

    // check LHS equals
    assertNotSame(rule1.getLhs(), rule2.getLhs());
    assertEquals(rule1.getLhs(), rule2.getLhs());

    assertNotSame(rule1.getLhs(), rule3.getLhs());
    assertThat(rule1.getLhs(), not(equalTo(rule3.getLhs())));
  }