use of org.drools.compiler.compiler.DroolsError in project drools by kiegroup.
the class RuleFlowErrorTest method testError.
@Test
public void testError() {
ProcessLoadError err = new ProcessLoadError(null, "XXX", null);
assertEquals("XXX", err.getMessage());
Exception e = new RuntimeException("Q");
err = new ProcessLoadError(null, "X", e);
assertNotNull(err.getMessage());
assertTrue(err instanceof DroolsError);
}
use of org.drools.compiler.compiler.DroolsError in project drools by kiegroup.
the class KnowledgeBuilderImpl method registerBuildResource.
public void registerBuildResource(final Resource resource, ResourceType type) {
InternalResource ires = (InternalResource) resource;
if (ires.getResourceType() == null) {
ires.setResourceType(type);
} else if (ires.getResourceType() != type) {
addBuilderResult(new ResourceTypeDeclarationWarning(resource, ires.getResourceType(), type));
}
if (ResourceType.CHANGE_SET == type) {
try {
ChangeSet changeSet = parseChangeSet(resource);
List<Resource> resources = new ArrayList<Resource>();
resources.add(resource);
for (Resource addedRes : changeSet.getResourcesAdded()) {
resources.add(addedRes);
}
for (Resource modifiedRes : changeSet.getResourcesModified()) {
resources.add(modifiedRes);
}
for (Resource removedRes : changeSet.getResourcesRemoved()) {
resources.add(removedRes);
}
buildResources.push(resources);
} catch (Exception e) {
results.add(new DroolsError() {
public String getMessage() {
return "Unable to register changeset resource " + resource;
}
public int[] getLines() {
return new int[0];
}
});
}
} else {
buildResources.push(Collections.singletonList(resource));
}
}
use of org.drools.compiler.compiler.DroolsError in project drools by kiegroup.
the class KnowledgeBuilderImpl method processWindowDeclarations.
private void processWindowDeclarations(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
for (WindowDeclarationDescr wd : packageDescr.getWindowDeclarations()) {
WindowDeclaration window = new WindowDeclaration(wd.getName(), packageDescr.getName());
// TODO: process annotations
// process pattern
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
DialectCompiletimeRegistry ctr = pkgRegistry.getDialectCompiletimeRegistry();
RuleDescr dummy = new RuleDescr(wd.getName() + " Window Declaration");
dummy.setResource(packageDescr.getResource());
dummy.addAttribute(new AttributeDescr("dialect", "java"));
RuleBuildContext context = new RuleBuildContext(this, dummy, ctr, pkg, ctr.getDialect(pkgRegistry.getDialect()));
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(wd.getPattern().getClass());
if (builder != null) {
final Pattern pattern = (Pattern) builder.build(context, wd.getPattern(), null);
if (pattern.getXpathConstraint() != null) {
context.addError(new DescrBuildError(wd, context.getParentDescr(), null, "OOpath expression " + pattern.getXpathConstraint() + " not allowed in window declaration\n"));
}
window.setPattern(pattern);
} else {
throw new RuntimeException("BUG: assembler not found for descriptor class " + wd.getPattern().getClass());
}
if (!context.getErrors().isEmpty()) {
for (DroolsError error : context.getErrors()) {
addBuilderResult(error);
}
} else {
pkgRegistry.getPackage().addWindowDeclaration(window);
}
}
}
use of org.drools.compiler.compiler.DroolsError in project drools by kiegroup.
the class DRLDumperTest method testDumpers.
@Test
public void testDumpers() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
final Resource resource = new InputStreamResource(getClass().getResourceAsStream("test_Dumpers.drl"));
final PackageDescr pkg = parser.parse(resource);
if (parser.hasErrors()) {
for (final DroolsError error : parser.getErrors()) {
logger.warn(error.toString());
}
fail(parser.getErrors().toString());
}
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(pkg));
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
final Cheese brie = new Cheese("brie", 12);
ksession.insert(brie);
ksession.fireAllRules();
assertEquals(3, list.size());
assertEquals("3 1", list.get(0));
assertEquals("MAIN", list.get(1));
assertEquals("1 1", list.get(2));
final DrlDumper drlDumper = new DrlDumper();
final String drlResult = drlDumper.dump(pkg);
System.out.println(drlResult);
kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(drlResult));
ksession = kbase.newKieSession();
list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(brie);
ksession.fireAllRules();
assertEquals(3, list.size());
assertEquals("3 1", list.get(0));
assertEquals("MAIN", list.get(1));
assertEquals("1 1", list.get(2));
}
use of org.drools.compiler.compiler.DroolsError in project drools by kiegroup.
the class RuleBuilder method buildTimer.
private static void buildTimer(RuleImpl rule, String timerString, RuleBuildContext context) {
if (timerString.indexOf('(') >= 0) {
timerString = timerString.substring(timerString.indexOf('(') + 1, timerString.lastIndexOf(')')).trim();
}
int colonPos = timerString.indexOf(":");
int semicolonPos = timerString.indexOf(";");
// default protocol
String protocol = "int";
if (colonPos == -1) {
if (timerString.startsWith("int") || timerString.startsWith("cron") || timerString.startsWith("expr")) {
DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Incorrect timer definition '" + timerString + "' - missing colon?");
context.addError(err);
return;
}
} else {
protocol = timerString.substring(0, colonPos);
}
String startDate = extractParam(timerString, "start");
String endDate = extractParam(timerString, "end");
String repeatLimitString = extractParam(timerString, "repeat-limit");
int repeatLimit = repeatLimitString != null ? Integer.parseInt(repeatLimitString) : -1;
String body = timerString.substring(colonPos + 1, semicolonPos > 0 ? semicolonPos : timerString.length()).trim();
Timer timer;
if ("cron".equals(protocol)) {
try {
timer = new CronTimer(createMVELExpr(startDate, context), createMVELExpr(endDate, context), repeatLimit, new CronExpression(body));
} catch (ParseException e) {
DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Unable to build set timer '" + timerString + "'");
context.addError(err);
return;
}
} else if ("int".equals(protocol)) {
String[] times = body.trim().split("\\s");
long delay = 0;
long period = 0;
if (times.length > 2) {
DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Incorrect number of arguments for interval timer '" + timerString + "'");
context.addError(err);
return;
}
try {
if (times.length == 1) {
// only defines a delay
delay = TimeUtils.parseTimeString(times[0]);
} else {
// defines a delay and a period for intervals
delay = TimeUtils.parseTimeString(times[0]);
period = TimeUtils.parseTimeString(times[1]);
}
} catch (RuntimeException e) {
DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Incorrect timer definition '" + timerString + "' " + e.getMessage());
context.addError(err);
return;
}
timer = new IntervalTimer(createMVELExpr(startDate, context), createMVELExpr(endDate, context), repeatLimit, delay, period);
} else if ("expr".equals(protocol)) {
body = body.trim();
StringTokenizer tok = new StringTokenizer(body, ",;");
if (tok.countTokens() > 2) {
DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Incorrect number of arguments for expression timer '" + timerString + "'");
context.addError(err);
return;
}
MVELObjectExpression times = MVELObjectExpressionBuilder.build(tok.nextToken().trim(), context);
MVELObjectExpression period = tok.hasMoreTokens() ? MVELObjectExpressionBuilder.build(tok.nextToken().trim(), context) : MVELObjectExpressionBuilder.build("0", context);
timer = new ExpressionIntervalTimer(createMVELExpr(startDate, context), createMVELExpr(endDate, context), repeatLimit, times, period);
} else {
DroolsError err = new RuleBuildError(rule, context.getParentDescr(), null, "Protocol for timer does not exist '" + timerString + "'");
context.addError(err);
return;
}
rule.setTimer(timer);
}
Aggregations