Search in sources :

Example 11 with KnowledgeBuilderErrors

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

the class SpreadsheetCompilerUnitTest method readKnowledgeBase.

private KieBase readKnowledgeBase(String resource) throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    DecisionTableConfiguration config = KnowledgeBuilderFactory.newDecisionTableConfiguration();
    config.setInputType(DecisionTableInputType.XLS);
    kbuilder.add(ResourceFactory.newClassPathResource(resource, getClass()), ResourceType.DTABLE, config);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            System.err.println(error);
        }
        throw new IllegalArgumentException("Could not parse knowledge.");
    }
    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) DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 12 with KnowledgeBuilderErrors

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

the class JavaDialectTest method testEvalDetectionInAlphaNode.

@Test
public void testEvalDetectionInAlphaNode() {
    // 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 += "   $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());
    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;
        }
    }
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    PredicateConstraint c = (PredicateConstraint) alphanode.getConstraint();
    assertTrue(c.getPredicateExpression() instanceof PredicateExpression);
    assertTrue(c.getPredicateExpression() instanceof CompiledInvoker);
    alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        FieldValue fieldVal = ((MvelConstraint) constraint).getField();
        assertEquals("xxx", fieldVal.getValue());
    }
}
Also used : KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) PredicateExpression(org.drools.core.spi.PredicateExpression) AlphaNode(org.drools.core.reteoo.AlphaNode) CompiledInvoker(org.drools.core.spi.CompiledInvoker) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) StringReader(java.io.StringReader) PredicateConstraint(org.drools.core.rule.PredicateConstraint) FieldValue(org.drools.core.spi.FieldValue) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

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