Search in sources :

Example 1 with DroolsParserException

use of org.drools.compiler.compiler.DroolsParserException in project drools by kiegroup.

the class ErrorsParserTest method testExpanderErrorsAfterExpansion.

@Test
public void testExpanderErrorsAfterExpansion() throws Exception {
    String name = "expander_post_errors.dslr";
    Expander expander = new DefaultExpander();
    String expanded = expander.expand(this.getReader(name));
    DRLParser parser = parse(name, expanded);
    parser.compilationUnit();
    assertTrue(parser.hasErrors());
    assertEquals(1, parser.getErrors().size());
    DroolsParserException err = (DroolsParserException) parser.getErrors().get(0);
    assertEquals(6, err.getLineNumber());
}
Also used : DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 2 with DroolsParserException

use of org.drools.compiler.compiler.DroolsParserException in project drools by kiegroup.

the class Misc2Test method testEvalConstraintWithMvelOperator.

@Test
public void testEvalConstraintWithMvelOperator() {
    String drl = "rule \"yeah\" " + "\tdialect \"mvel\"\n when " + "Foo( eval( field soundslike \"water\" ) )" + " then " + "end";
    DrlParser drlParser = new DrlParser();
    PackageDescr packageDescr;
    try {
        packageDescr = drlParser.parse(true, drl);
    } catch (DroolsParserException e) {
        throw new RuntimeException(e);
    }
    RuleDescr r = packageDescr.getRules().get(0);
    PatternDescr pd = (PatternDescr) r.getLhs().getDescrs().get(0);
    assertEquals(1, pd.getConstraint().getDescrs().size());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) Test(org.junit.Test)

Example 3 with DroolsParserException

use of org.drools.compiler.compiler.DroolsParserException in project drools by kiegroup.

the class RuleModelDRLPersistenceImpl method parseDrl.

private RuleDescr parseDrl(final ExpandedDRLInfo expandedDRLInfo) {
    DrlParser drlParser = new DrlParser();
    PackageDescr packageDescr;
    try {
        packageDescr = drlParser.parse(true, expandedDRLInfo.plainDrl);
        if (drlParser.hasErrors()) {
            throw new RuleModelUnmarshallingException();
        }
    } catch (DroolsParserException e) {
        throw new RuntimeException(e);
    }
    expandedDRLInfo.registerGlobalDescrs(packageDescr.getGlobals());
    return packageDescr.getRules().get(0);
}
Also used : DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DroolsParserException(org.drools.compiler.compiler.DroolsParserException)

Example 4 with DroolsParserException

use of org.drools.compiler.compiler.DroolsParserException in project jbpm by kiegroup.

the class ProcessBuilderImpl method buildProcess.

public void buildProcess(final Process process, Resource resource) {
    if (resource != null) {
        ((org.jbpm.process.core.Process) process).setResource(resource);
    }
    boolean hasErrors = false;
    ProcessValidator validator = ProcessValidatorRegistry.getInstance().getValidator(process, resource);
    if (validator == null) {
        logger.warn("Could not find validator for process {}.", ((Process) process).getType());
        logger.warn("Continuing without validation of the process {} [{}]", process.getName(), process.getId());
    } else {
        ProcessValidationError[] errors = validator.validateProcess((WorkflowProcess) process);
        if (errors.length != 0) {
            hasErrors = true;
            for (int i = 0; i < errors.length; i++) {
                this.errors.add(new ParserError(resource, errors[i].toString(), -1, -1));
            }
        }
    }
    if (!hasErrors) {
        // generate and add rule for process
        String rules = "package " + process.getPackageName() + "\n";
        // NPE for validator
        if (validator != null && validator.compilationSupported()) {
            rules = generateRules(process);
        }
        try {
            knowledgeBuilder.addPackageFromDrl(new StringReader(rules), resource);
        } catch (IOException e) {
            // should never occur
            logger.error("IOException during addPackageFromDRL", e);
        } catch (DroolsParserException e) {
            // should never occur
            logger.error("DroolsParserException during addPackageFromDRL", e);
        }
        PackageRegistry pkgRegistry = this.knowledgeBuilder.getPackageRegistry(process.getPackageName());
        if (pkgRegistry != null) {
            InternalKnowledgePackage p = pkgRegistry.getPackage();
            if (p != null) {
                if (validator != null) {
                    // NPE for validator
                    if (validator.compilationSupported()) {
                        ProcessDescr processDescr = new ProcessDescr();
                        processDescr.setName(process.getPackageName() + "." + process.getName());
                        processDescr.setResource(resource);
                        processDescr.setProcessId(process.getId());
                        DialectCompiletimeRegistry dialectRegistry = pkgRegistry.getDialectCompiletimeRegistry();
                        Dialect dialect = dialectRegistry.getDialect("java");
                        dialect.init(processDescr);
                        ProcessBuildContext buildContext = new ProcessBuildContext(this.knowledgeBuilder, p, process, processDescr, dialectRegistry, dialect);
                        buildContexts((ContextContainer) process, buildContext);
                        if (process instanceof WorkflowProcess) {
                            buildNodes((WorkflowProcess) process, buildContext);
                        }
                    }
                    Process duplicateProcess = p.getRuleFlows().get(process.getId());
                    if (duplicateProcess != null) {
                        Resource duplicatedResource = duplicateProcess.getResource();
                        if (resource == null || duplicatedResource == null || duplicatedResource.getSourcePath() == null || duplicatedResource.getSourcePath().equals(resource.getSourcePath())) {
                            this.errors.add(new DuplicateProcess(process, this.knowledgeBuilder.getBuilderConfiguration()));
                        } else {
                            this.errors.add(new ParserError(resource, "Process with same id already exists: " + process.getId(), -1, -1));
                        }
                    }
                    p.addProcess(process);
                    // NPE for validator
                    if (validator.compilationSupported()) {
                        pkgRegistry.compileAll();
                        pkgRegistry.getDialectRuntimeRegistry().onBeforeExecute();
                    }
                }
            }
        } else {
            // name of the process
            throw new RuntimeException("invalid package name");
        }
    }
}
Also used : ParserError(org.drools.compiler.compiler.ParserError) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) Resource(org.kie.api.io.Resource) DuplicateProcess(org.drools.compiler.compiler.DuplicateProcess) Process(org.kie.api.definition.process.Process) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess) ProcessDescr(org.drools.compiler.lang.descr.ProcessDescr) IOException(java.io.IOException) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) Constraint(org.jbpm.workflow.core.Constraint) ProcessValidationError(org.jbpm.process.core.validation.ProcessValidationError) DuplicateProcess(org.drools.compiler.compiler.DuplicateProcess) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) StringReader(java.io.StringReader) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect) Dialect(org.drools.compiler.compiler.Dialect) ProcessBuildContext(org.jbpm.process.builder.ProcessBuildContext) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess) ProcessValidator(org.jbpm.process.core.validation.ProcessValidator) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 5 with DroolsParserException

use of org.drools.compiler.compiler.DroolsParserException in project drools-wb by kiegroup.

the class FactModelPersistence method toModel.

private static List<FactMetaModel> toModel(String drl) throws DroolsParserException {
    Preconditions.checkNotNull(drl, "The string representing DRL can't be null!");
    if (drl.startsWith("#advanced") || drl.startsWith("//advanced")) {
        throw new DroolsParserException("Using advanced editor");
    }
    final DrlParser parser = new DrlParser();
    final StringReader reader = new StringReader(drl);
    final PackageDescr pkg = parser.parse(reader);
    if (parser.hasErrors()) {
        throw new DroolsParserException("The model drl " + drl + " is not valid");
    }
    if (pkg == null) {
        return emptyList();
    }
    final List<TypeDeclarationDescr> types = pkg.getTypeDeclarations();
    final List<FactMetaModel> list = new ArrayList<>(types.size());
    for (final TypeDeclarationDescr td : types) {
        final FactMetaModel mm = new FactMetaModel();
        mm.setName(td.getTypeName());
        mm.setSuperType(td.getSuperTypeName());
        final Map<String, TypeFieldDescr> fields = td.getFields();
        for (Map.Entry<String, TypeFieldDescr> en : fields.entrySet()) {
            final String fieldName = en.getKey();
            final TypeFieldDescr descr = en.getValue();
            final FieldMetaModel fm = new FieldMetaModel(fieldName, descr.getPattern().getObjectType());
            mm.getFields().add(fm);
        }
        for (final AnnotationDescr descr : td.getAnnotations()) {
            final String annotationName = descr.getName();
            final Map<String, String> values = extractStringValues(descr);
            final AnnotationMetaModel am = new AnnotationMetaModel(annotationName, values);
            mm.getAnnotations().add(am);
        }
        list.add(mm);
    }
    return list;
}
Also used : TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) ArrayList(java.util.ArrayList) FactMetaModel(org.drools.workbench.screens.factmodel.model.FactMetaModel) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) FieldMetaModel(org.drools.workbench.screens.factmodel.model.FieldMetaModel) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) AnnotationMetaModel(org.drools.workbench.screens.factmodel.model.AnnotationMetaModel) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

DroolsParserException (org.drools.compiler.compiler.DroolsParserException)7 DrlParser (org.drools.compiler.compiler.DrlParser)3 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)3 StringReader (java.io.StringReader)2 FactMetaModel (org.drools.workbench.screens.factmodel.model.FactMetaModel)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Dialect (org.drools.compiler.compiler.Dialect)1 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)1 DuplicateProcess (org.drools.compiler.compiler.DuplicateProcess)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 ParserError (org.drools.compiler.compiler.ParserError)1 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)1 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)1 ProcessDescr (org.drools.compiler.lang.descr.ProcessDescr)1