Search in sources :

Example 1 with ParserError

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

the class KnowledgeBuilderImpl method generatedResourcesToPackageDescr.

private List<PackageDescr> generatedResourcesToPackageDescr(Resource resource, List<PMMLResource> resources) throws DroolsParserException {
    List<PackageDescr> pkgDescrs = new ArrayList<>();
    DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    for (PMMLResource res : resources) {
        for (String key : res.getRules().keySet()) {
            String src = res.getRules().get(key);
            PackageDescr descr = null;
            descr = parser.parse(false, src);
            if (descr != null) {
                descr.setResource(resource);
                pkgDescrs.add(descr);
                dumpGeneratedRule(descr, key, src);
            } else {
                addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
            }
        }
    }
    return pkgDescrs;
}
Also used : PMMLResource(org.drools.compiler.compiler.PMMLResource) ParserError(org.drools.compiler.compiler.ParserError) ArrayList(java.util.ArrayList) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 2 with ParserError

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

the class XmlBifParser method loadBif.

public static Bif loadBif(Resource resource, KnowledgeBuilderErrors errors) {
    InputStream is = null;
    try {
        is = resource.getInputStream();
    } catch (IOException e) {
        errors.add(new ParserError(resource, "Exception opening Stream:\n" + e.toString(), 0, 0));
        return null;
    }
    try {
        String encoding = resource instanceof InternalResource ? ((InternalResource) resource).getEncoding() : null;
        XStream xstream = encoding != null ? createTrustingXStream(new DomDriver(encoding)) : createTrustingXStream();
        initXStream(xstream);
        Bif bif = (Bif) xstream.fromXML(is);
        return bif;
    } catch (Exception e) {
        errors.add(new BayesNetworkAssemblerError(resource, "Unable to parse opening Stream:\n" + e.toString()));
        return null;
    }
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) ParserError(org.drools.compiler.compiler.ParserError) InputStream(java.io.InputStream) XStream(com.thoughtworks.xstream.XStream) XStreamUtils.createTrustingXStream(org.kie.soup.commons.xstream.XStreamUtils.createTrustingXStream) BayesNetworkAssemblerError(org.drools.beliefs.bayes.assembler.BayesNetworkAssemblerError) IOException(java.io.IOException) IOException(java.io.IOException)

Example 3 with ParserError

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

the class KnowledgeBuilderTest method testDuplicateRuleNames.

@Test
public void testDuplicateRuleNames() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    RuleDescr ruleDescr;
    AndDescr lhs;
    PatternDescr patternDescr;
    FieldConstraintDescr literalDescr;
    final PackageDescr packageDescr = createBasicPackageWithOneRule(1, 1);
    ruleDescr = new RuleDescr("rule-1");
    ruleDescr.setLocation(42, 43);
    packageDescr.addRule(ruleDescr);
    lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
    literalDescr = new FieldConstraintDescr("type");
    literalDescr.addRestriction(new LiteralRestrictionDescr("!=", null));
    patternDescr.addConstraint(literalDescr);
    ruleDescr.setConsequence("");
    ruleDescr = new RuleDescr("rule-2");
    ruleDescr.setLocation(42, 43);
    packageDescr.addRule(ruleDescr);
    lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
    literalDescr = new FieldConstraintDescr("type");
    literalDescr.addRestriction(new LiteralRestrictionDescr("!=", null));
    patternDescr.addConstraint(literalDescr);
    ruleDescr.setConsequence("");
    builder.addPackage(packageDescr);
    assertLength(2, builder.getErrors().getErrors());
    final ParserError err = (ParserError) builder.getErrors().getErrors()[0];
    assertEquals(42, err.getRow());
    assertEquals(43, err.getCol());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) ParserError(org.drools.compiler.compiler.ParserError) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) FieldConstraintDescr(org.drools.compiler.lang.descr.FieldConstraintDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) LiteralRestrictionDescr(org.drools.compiler.lang.descr.LiteralRestrictionDescr) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 4 with ParserError

use of org.drools.compiler.compiler.ParserError 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 ParserError

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

the class DSLTokenizedMappingFile method parseAndLoad.

@Override
public boolean parseAndLoad(Reader dsl) throws IOException {
    List<ParserError> errors = new ArrayList<ParserError>();
    String text = readFile(dsl);
    dsl = new StringReader(text);
    try {
        DSLMapping mapping = buildFileMapping(errors, dsl);
        mapping.setOptions(optionSet);
        setMapping(mapping);
        List<ParserError> moderr = new ArrayList<ParserError>();
        for (ParserError err : errors) {
            int row = err.getRow();
            int col = err.getCol();
            if (row > 0) {
                int len;
                while ((len = lineLengths.get(row)) < col) {
                    col -= len + 1;
                    row++;
                }
            }
            moderr.add(new ParserError(err.getMessage(), row, col));
        }
        errors = moderr;
    } catch (Exception e) {
        final String msg = "Error parsing DSL mapping: " + e.getMessage();
        ParserError parserError = new ParserError(msg, -1, 0);
        errors.add(parserError);
    }
    setErrors(errors);
    return errors.isEmpty();
}
Also used : ParserError(org.drools.compiler.compiler.ParserError) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) IOException(java.io.IOException) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

ParserError (org.drools.compiler.compiler.ParserError)6 IOException (java.io.IOException)3 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 DrlParser (org.drools.compiler.compiler.DrlParser)2 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)2 Test (org.junit.Test)2 XStream (com.thoughtworks.xstream.XStream)1 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 RecognitionException (org.antlr.runtime.RecognitionException)1 BayesNetworkAssemblerError (org.drools.beliefs.bayes.assembler.BayesNetworkAssemblerError)1 Dialect (org.drools.compiler.compiler.Dialect)1 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)1 DroolsParserException (org.drools.compiler.compiler.DroolsParserException)1 DuplicateProcess (org.drools.compiler.compiler.DuplicateProcess)1 PMMLResource (org.drools.compiler.compiler.PMMLResource)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 AndDescr (org.drools.compiler.lang.descr.AndDescr)1