Search in sources :

Example 6 with ParserError

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

the class KnowledgeBuilderImpl method validateUniqueRuleNames.

protected void validateUniqueRuleNames(final PackageDescr packageDescr) {
    final Set<String> names = new HashSet<String>();
    PackageRegistry packageRegistry = this.pkgRegistryMap.get(packageDescr.getNamespace());
    InternalKnowledgePackage pkg = null;
    if (packageRegistry != null) {
        pkg = packageRegistry.getPackage();
    }
    for (final RuleDescr rule : packageDescr.getRules()) {
        validateRule(packageDescr, rule);
        final String name = rule.getName();
        if (names.contains(name)) {
            addBuilderResult(new ParserError(rule.getResource(), "Duplicate rule name: " + name, rule.getLine(), rule.getColumn(), packageDescr.getNamespace()));
        }
        if (pkg != null) {
            RuleImpl duplicatedRule = pkg.getRule(name);
            if (duplicatedRule != null) {
                Resource resource = rule.getResource();
                Resource duplicatedResource = duplicatedRule.getResource();
                if (resource == null || duplicatedResource == null || duplicatedResource.getSourcePath() == null || duplicatedResource.getSourcePath().equals(resource.getSourcePath())) {
                    addBuilderResult(new DuplicateRule(rule, packageDescr, this.configuration));
                } else {
                    addBuilderResult(new ParserError(rule.getResource(), "Duplicate rule name: " + name, rule.getLine(), rule.getColumn(), packageDescr.getNamespace()));
                }
            }
        }
        names.add(name);
    }
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) DuplicateRule(org.drools.compiler.compiler.DuplicateRule) ParserError(org.drools.compiler.compiler.ParserError) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) 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) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) HashSet(java.util.HashSet) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 7 with ParserError

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

the class KnowledgeBuilderImpl method generatedDrlToPackageDescr.

private PackageDescr generatedDrlToPackageDescr(Resource resource, String generatedDrl) throws DroolsParserException {
    // dump the generated DRL if the dump dir was configured
    if (this.configuration.getDumpDir() != null) {
        dumpDrlGeneratedFromDTable(this.configuration.getDumpDir(), generatedDrl, resource.getSourcePath());
    }
    DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    PackageDescr pkg = parser.parse(resource, new StringReader(generatedDrl));
    this.results.addAll(parser.getErrors());
    if (pkg == null) {
        addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
    } else {
        pkg.setResource(resource);
    }
    return parser.hasErrors() ? null : pkg;
}
Also used : ParserError(org.drools.compiler.compiler.ParserError) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 8 with ParserError

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

the class KnowledgeBuilderImpl method addPackageFromDrl.

/**
 * Load a rule package from DRL source and associate all loaded artifacts
 * with the given resource.
 *
 * @param reader
 * @param sourceResource the source resource for the read artifacts
 * @throws DroolsParserException
 * @throws IOException
 */
public void addPackageFromDrl(final Reader reader, final Resource sourceResource) throws DroolsParserException, IOException {
    this.resource = sourceResource;
    final DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    final PackageDescr pkg = parser.parse(sourceResource, reader);
    this.results.addAll(parser.getErrors());
    if (pkg == null) {
        addBuilderResult(new ParserError(sourceResource, "Parser returned a null Package", 0, 0));
    }
    if (!parser.hasErrors()) {
        addPackage(pkg);
    }
    this.resource = null;
}
Also used : ParserError(org.drools.compiler.compiler.ParserError) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 9 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)

Example 10 with ParserError

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

the class ParserTest method testErrorsParser.

@Test
public void testErrorsParser() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    assertEquals(0, parser.getErrors().size());
    parser.parse(new InputStreamReader(getClass().getResourceAsStream("errors_parser_multiple.drl")));
    assertTrue(parser.hasErrors());
    assertTrue(parser.getErrors().size() > 0);
    assertTrue(parser.getErrors().get(0) instanceof ParserError);
    final ParserError first = ((ParserError) parser.getErrors().get(0));
    assertTrue(first.getMessage() != null);
    assertFalse(first.getMessage().equals(""));
}
Also used : InputStreamReader(java.io.InputStreamReader) ParserError(org.drools.compiler.compiler.ParserError) DrlParser(org.drools.compiler.compiler.DrlParser) Test(org.junit.Test)

Aggregations

ParserError (org.drools.compiler.compiler.ParserError)10 DrlParser (org.drools.compiler.compiler.DrlParser)5 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)5 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)4 IOException (java.io.IOException)3 StringReader (java.io.StringReader)3 ArrayList (java.util.ArrayList)2 PMMLResource (org.drools.compiler.compiler.PMMLResource)2 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)2 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 DescrResource (org.drools.core.io.impl.DescrResource)2 InternalResource (org.drools.core.io.internal.InternalResource)2 Test (org.junit.Test)2 Resource (org.kie.api.io.Resource)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 HashSet (java.util.HashSet)1