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));
}
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;
}
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);
}
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());
}
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);
}
Aggregations