Search in sources :

Example 6 with DroolsError

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);
}
Also used : DroolsError(org.drools.compiler.compiler.DroolsError) ProcessLoadError(org.drools.compiler.compiler.ProcessLoadError) Test(org.junit.Test)

Example 7 with 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));
    }
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) DroolsError(org.drools.compiler.compiler.DroolsError) BaseResource(org.drools.core.io.impl.BaseResource) DescrResource(org.drools.core.io.impl.DescrResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Resource(org.kie.api.io.Resource) ReaderResource(org.drools.core.io.impl.ReaderResource) PMMLResource(org.drools.compiler.compiler.PMMLResource) InternalResource(org.drools.core.io.internal.InternalResource) ArrayList(java.util.ArrayList) ResourceTypeDeclarationWarning(org.drools.compiler.compiler.ResourceTypeDeclarationWarning) ChangeSet(org.kie.internal.ChangeSet) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ExpanderException(org.drools.compiler.lang.ExpanderException) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) SAXException(org.xml.sax.SAXException)

Example 8 with DroolsError

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);
        }
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) DroolsError(org.drools.compiler.compiler.DroolsError) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) WindowDeclaration(org.drools.core.rule.WindowDeclaration) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) RuleConditionBuilder(org.drools.compiler.rule.builder.RuleConditionBuilder) WindowDeclarationDescr(org.drools.compiler.lang.descr.WindowDeclarationDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 9 with DroolsError

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));
}
Also used : DroolsError(org.drools.compiler.compiler.DroolsError) KieBase(org.kie.api.KieBase) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) DrlParser(org.drools.compiler.compiler.DrlParser) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Test(org.junit.Test)

Example 10 with DroolsError

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);
}
Also used : RuleBuildError(org.drools.compiler.compiler.RuleBuildError) IntervalTimer(org.drools.core.time.impl.IntervalTimer) ExpressionIntervalTimer(org.drools.core.time.impl.ExpressionIntervalTimer) ExpressionIntervalTimer(org.drools.core.time.impl.ExpressionIntervalTimer) DroolsError(org.drools.compiler.compiler.DroolsError) StringTokenizer(java.util.StringTokenizer) MVELObjectExpression(org.drools.core.base.mvel.MVELObjectExpression) IntervalTimer(org.drools.core.time.impl.IntervalTimer) ExpressionIntervalTimer(org.drools.core.time.impl.ExpressionIntervalTimer) Timer(org.drools.core.time.impl.Timer) CronTimer(org.drools.core.time.impl.CronTimer) CronExpression(org.drools.core.time.impl.CronExpression) ParseException(java.text.ParseException) CronTimer(org.drools.core.time.impl.CronTimer)

Aggregations

DroolsError (org.drools.compiler.compiler.DroolsError)10 RuleBuildError (org.drools.compiler.compiler.RuleBuildError)5 ParseException (java.text.ParseException)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)2 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)2 Test (org.junit.Test)2 Resource (org.kie.api.io.Resource)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Calendar (java.util.Calendar)1 Collection (java.util.Collection)1 Date (java.util.Date)1 StringTokenizer (java.util.StringTokenizer)1 ExecutionException (java.util.concurrent.ExecutionException)1 Cheese (org.drools.compiler.Cheese)1 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)1 DrlParser (org.drools.compiler.compiler.DrlParser)1