Search in sources :

Example 1 with KnowledgeBuilderErrors

use of org.kie.internal.builder.KnowledgeBuilderErrors in project drools by kiegroup.

the class StrEvaluatorTest method readKnowledgeBase.

private KieBase readKnowledgeBase() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("strevaluator_test.drl")), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            System.err.println(error);
        }
        throw new IllegalArgumentException("Could not parse knowledge." + errors.toArray());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return kbase;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 2 with KnowledgeBuilderErrors

use of org.kie.internal.builder.KnowledgeBuilderErrors in project drools by kiegroup.

the class RHSTest method testRHSClone.

@Test
public void testRHSClone() {
    // JBRULES-3539
    final String str = "import java.util.Map;\n" + "dialect \"mvel\"\n" + "rule \"RHSClone\"\n" + "when\n" + "   Map($valOne : this['keyOne'] !=null)\n" + "then\n" + "   System.out.println( $valOne.clone() );\n" + "end\n";
    final KnowledgeBuilderConfigurationImpl pkgBuilderCfg = new KnowledgeBuilderConfigurationImpl();
    final MVELDialectConfiguration mvelConf = (MVELDialectConfiguration) pkgBuilderCfg.getDialectConfiguration("mvel");
    mvelConf.setStrict(false);
    mvelConf.setLangLevel(5);
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(pkgBuilderCfg);
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    final KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        for (final KnowledgeBuilderError error : errors) {
            System.err.println(error);
        }
        fail("Could not parse knowledge");
    }
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) MVELDialectConfiguration(org.drools.compiler.rule.builder.dialect.mvel.MVELDialectConfiguration) Test(org.junit.Test)

Example 3 with KnowledgeBuilderErrors

use of org.kie.internal.builder.KnowledgeBuilderErrors in project drools by kiegroup.

the class KnowledgeBuilderImpl method newKnowledgeBase.

public KieBase newKnowledgeBase(KieBaseConfiguration conf) {
    KnowledgeBuilderErrors errors = getErrors();
    if (errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            logger.error(error.toString());
        }
        throw new IllegalArgumentException("Could not parse knowledge. See the logs for details.");
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(conf);
    kbase.addPackages(Arrays.asList(getPackages()));
    return kbase;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 4 with KnowledgeBuilderErrors

use of org.kie.internal.builder.KnowledgeBuilderErrors in project drools by kiegroup.

the class JavaDialectTest method testEvalDetectionInBetaNode.

@Test
public void testEvalDetectionInBetaNode() {
    // Tests evals are generated and executed with Java dialect
    String drl = "";
    drl += "package org.drools.compiler.test\n";
    drl += "import org.drools.compiler.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());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
            node = n;
            break;
        }
    }
    BetaNode betaanode = (BetaNode) node.getObjectSinkPropagator().getSinks()[0];
    BetaNodeFieldConstraint[] constraint = betaanode.getConstraints();
    PredicateConstraint c = (PredicateConstraint) constraint[0];
    assertTrue(c.getPredicateExpression() instanceof PredicateExpression);
    assertTrue(c.getPredicateExpression() instanceof CompiledInvoker);
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) PredicateExpression(org.drools.core.spi.PredicateExpression) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) CompiledInvoker(org.drools.core.spi.CompiledInvoker) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StringReader(java.io.StringReader) PredicateConstraint(org.drools.core.rule.PredicateConstraint) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 5 with KnowledgeBuilderErrors

use of org.kie.internal.builder.KnowledgeBuilderErrors in project drools by kiegroup.

the class DynamicEvalTest method loadPackages.

public void loadPackages(Resource res, ResourceType type) {
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(res, type);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            System.err.println(error);
        }
        throw new IllegalArgumentException("Could not parse knowledge.");
    }
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors)

Aggregations

KnowledgeBuilderErrors (org.kie.internal.builder.KnowledgeBuilderErrors)12 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)9 KnowledgeBuilderError (org.kie.internal.builder.KnowledgeBuilderError)9 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 Test (org.junit.Test)5 StringReader (java.io.StringReader)2 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)2 PredicateConstraint (org.drools.core.rule.PredicateConstraint)2 CompiledInvoker (org.drools.core.spi.CompiledInvoker)2 PredicateExpression (org.drools.core.spi.PredicateExpression)2 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)1 MVELDialectConfiguration (org.drools.compiler.rule.builder.dialect.mvel.MVELDialectConfiguration)1 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)1 AlphaNode (org.drools.core.reteoo.AlphaNode)1 BetaNode (org.drools.core.reteoo.BetaNode)1 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)1 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)1 BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)1 FieldValue (org.drools.core.spi.FieldValue)1 KiePackage (org.kie.api.definition.KiePackage)1