Search in sources :

Example 16 with KnowledgeBuilderError

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

the class MapPersistenceTest method createPersistentSessionWithRules.

@Test
public void createPersistentSessionWithRules() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    String rule = "package org.drools.persistence.map.impl\n";
    rule += "import org.drools.persistence.map.impl.Buddy;\n";
    rule += "rule \"echo2\" \n";
    rule += "dialect \"mvel\"\n";
    rule += "when\n";
    rule += "    $m : Buddy()\n";
    rule += "then\n";
    rule += "    System.out.println(\"buddy inserted\")";
    rule += "end\n";
    kbuilder.add(new ByteArrayResource(rule.getBytes()), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors != null && errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            logger.warn("Error: " + error.getMessage());
        }
        Assert.fail("KnowledgeBase did not build");
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = createSession(kbase);
    FactHandle buddyFactHandle = ksession.insert(new Buddy());
    ksession.fireAllRules();
    Assert.assertEquals(1, ksession.getObjects().size());
    ksession = disposeAndReloadSession(ksession, kbase);
    Assert.assertNotNull(ksession);
    Assert.assertEquals(1, ksession.getObjects().size());
    Assert.assertNull("An object can't be retrieved with a FactHandle from a disposed session", ksession.getObject(buddyFactHandle));
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) FactHandle(org.kie.api.runtime.rule.FactHandle) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 17 with KnowledgeBuilderError

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

the class TimerAndCalendarTest method buildKnowledgePackage.

private Collection<KiePackage> buildKnowledgePackage(Resource resource, ResourceType resourceType) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(resource, resourceType);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors != null && errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            System.err.println("Error: " + error.getMessage());
        }
        Assert.fail("KnowledgeBase did not build");
    }
    Collection<KiePackage> packages = kbuilder.getKnowledgePackages();
    return packages;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) KiePackage(org.kie.api.definition.KiePackage)

Example 18 with KnowledgeBuilderError

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

the class ParserTest method testErrorLineNumbers.

@Test
public void testErrorLineNumbers() throws Exception {
    // this test aims to test semantic errors
    // parser errors are another test case
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("errors_in_rule.drl", getClass()), ResourceType.DRL);
    final KnowledgeBuilderError[] errors = kbuilder.getErrors().toArray(new KnowledgeBuilderError[0]);
    assertEquals(3, errors.length);
    final DescrBuildError stiltonError = (DescrBuildError) errors[0];
    assertTrue(stiltonError.getMessage().contains("Stilton"));
    assertNotNull(stiltonError.getDescr());
    assertTrue(stiltonError.getLine() != -1);
    // check that its getting it from the ruleDescr
    assertEquals(stiltonError.getLine(), stiltonError.getDescr().getLine());
    // check the absolute error line number (there are more).
    assertEquals(26, stiltonError.getLine());
    final DescrBuildError poisonError = (DescrBuildError) errors[1];
    assertTrue(poisonError.getMessage().contains("Poison"));
    assertEquals(28, poisonError.getLine());
    final KnowledgeBuilderConfigurationImpl cfg = new KnowledgeBuilderConfigurationImpl();
    final JavaDialectConfiguration javaConf = (JavaDialectConfiguration) cfg.getDialectConfiguration("java");
    switch(javaConf.getCompiler()) {
        case NATIVE:
            assertTrue(errors[2].getMessage().contains("illegal"));
            break;
        case ECLIPSE:
            assertTrue(errors[2].getMessage().contains("add"));
            break;
        case JANINO:
            assertTrue(errors[2].getMessage().contains("Unexpected"));
            break;
        default:
            fail("Unknown compiler used");
    }
    // now check the RHS, not being too specific yet, as long as it has the
    // rules line number, not zero
    final DescrBuildError rhsError = (DescrBuildError) errors[2];
    assertTrue(rhsError.getLine() >= 23 && rhsError.getLine() <= 32);
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) JavaDialectConfiguration(org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration) Test(org.junit.Test)

Example 19 with KnowledgeBuilderError

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

the class DeclareTest method testMvelFunctionWithDeclaredTypeArgForGuvnor.

@Test
public void testMvelFunctionWithDeclaredTypeArgForGuvnor() throws Exception {
    // JBRULES-3562
    final String function = "function String getFieldValue(Bean bean) {" + " return bean.getField();" + "}\n";
    final String declaredFactType = "declare Bean \n" + " field : String \n" + "end \n";
    final String rule = "rule R2 \n" + "dialect 'mvel'\n" + "when \n" + " $bean : Bean( ) \n" + "then \n" + " System.out.println( getFieldValue($bean) ); \n" + "end\n";
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(declaredFactType.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(function.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
    for (final KnowledgeBuilderError error : kbuilder.getErrors()) {
        System.out.println("ERROR:");
        System.out.println(error.getMessage());
    }
    assertFalse(kbuilder.hasErrors());
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Test(org.junit.Test)

Example 20 with KnowledgeBuilderError

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

the class CommonTestMethodBase method genSession.

public KieSession genSession(String[] sources, int numerrors) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    for (String source : sources) kbuilder.add(ResourceFactory.newClassPathResource(source, getClass()), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (kbuilder.getErrors().size() > 0) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.err.println(error);
        }
    }
    assertEquals(numerrors, errors.size());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return createKnowledgeSession(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)

Aggregations

KnowledgeBuilderError (org.kie.internal.builder.KnowledgeBuilderError)25 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)21 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)11 KnowledgeBuilderErrors (org.kie.internal.builder.KnowledgeBuilderErrors)10 Test (org.junit.Test)9 HashMap (java.util.HashMap)4 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)4 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)3 ClassPathResource (org.drools.core.io.impl.ClassPathResource)3 KiePackage (org.kie.api.definition.KiePackage)3 Resource (org.kie.api.io.Resource)3 KieSession (org.kie.api.runtime.KieSession)3 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)2 Process (org.kie.api.definition.process.Process)2 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)2 CompositeKnowledgeBuilder (org.kie.internal.builder.CompositeKnowledgeBuilder)2 KnowledgeBuilderConfiguration (org.kie.internal.builder.KnowledgeBuilderConfiguration)2 KnowledgeBuilderResult (org.kie.internal.builder.KnowledgeBuilderResult)2