use of org.kie.internal.builder.KnowledgeBuilderError in project drools by kiegroup.
the class JBRULESTest method testJBRULES2872.
@Test
public void testJBRULES2872() {
final String str = "package org.drools.compiler.test\n" + "import org.drools.compiler.FactA\n" + "rule X\n" + "when\n" + " FactA( enumVal == TestEnum.ONE || == TestEnum.TWO )\n" + "then\n" + "end\n";
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertTrue(kbuilder.hasErrors());
final KnowledgeBuilderErrors errors = kbuilder.getErrors();
assertEquals(1, errors.size());
final KnowledgeBuilderError error = errors.iterator().next();
assertEquals(5, error.getLines()[0]);
}
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 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);
}
use of org.kie.internal.builder.KnowledgeBuilderError in project jbpm by kiegroup.
the class MemoryLeakTest method createKnowledgeBase.
private KieBase createKnowledgeBase() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("memory/BPMN2-RuleTaskWithInsertProcessInstance.bpmn2"), ResourceType.BPMN2);
kbuilder.add(ResourceFactory.newClassPathResource("memory/ProcessInstanceRule.drl"), ResourceType.DRL);
if (!kbuilder.getErrors().isEmpty()) {
Iterator<KnowledgeBuilderError> errIter = kbuilder.getErrors().iterator();
while (errIter.hasNext()) {
KnowledgeBuilderError err = errIter.next();
StringBuilder lines = new StringBuilder("");
if (err.getLines().length > 0) {
lines.append(err.getLines()[0]);
for (int i = 1; i < err.getLines().length; ++i) {
lines.append(", " + err.getLines()[i]);
}
}
logger.warn(err.getMessage() + " (" + lines.toString() + ")");
}
throw new IllegalArgumentException("Errors while parsing knowledge base");
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
return kbase;
}
use of org.kie.internal.builder.KnowledgeBuilderError in project jbpm by kiegroup.
the class PersistentStatefulSessionTest method testPersistenceVariables.
@Test
public void testPersistenceVariables() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("VariablesProcess.rf"), ResourceType.DRF);
for (KnowledgeBuilderError error : kbuilder.getErrors()) {
logger.debug(error.toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
long id = ksession.getIdentifier();
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("name", "John Doe");
ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess", parameters);
TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
WorkItem workItem = handler.getWorkItem();
assertNotNull(workItem);
assertEquals("John Doe", workItem.getParameter("name"));
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNotNull(processInstance);
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
workItem = handler.getWorkItem();
assertNotNull(workItem);
assertEquals("John Doe", workItem.getParameter("text"));
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNotNull(processInstance);
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
workItem = handler.getWorkItem();
assertNull(workItem);
ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNull(processInstance);
}
Aggregations