Search in sources :

Example 1 with ParserError

use of org.drools.drl.parser.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.drl.parser.ParserError) DrlParser(org.drools.drl.parser.DrlParser) Test(org.junit.Test)

Example 2 with ParserError

use of org.drools.drl.parser.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
 */
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.drl.parser.ParserError) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 3 with ParserError

use of org.drools.drl.parser.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.drl.parser.ParserError) StringReader(java.io.StringReader) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 4 with ParserError

use of org.drools.drl.parser.ParserError in project drools by kiegroup.

the class KnowledgeBuilderImpl method drlToPackageDescr.

PackageDescr drlToPackageDescr(Resource resource) throws DroolsParserException, IOException {
    PackageDescr pkg;
    boolean hasErrors = false;
    if (resource instanceof DescrResource) {
        pkg = (PackageDescr) ((DescrResource) resource).getDescr();
    } else {
        final DrlParser parser = new DrlParser(configuration.getLanguageLevel());
        pkg = parser.parse(resource);
        this.results.addAll(parser.getErrors());
        if (pkg == null) {
            addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
        }
        hasErrors = parser.hasErrors();
    }
    if (pkg != null) {
        pkg.setResource(resource);
    }
    return hasErrors ? null : pkg;
}
Also used : ParserError(org.drools.drl.parser.ParserError) DescrResource(org.drools.core.io.impl.DescrResource) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 5 with ParserError

use of org.drools.drl.parser.ParserError in project drools by kiegroup.

the class KnowledgeBuilderImpl method validateUniqueRuleNames.

protected void validateUniqueRuleNames(final PackageDescr packageDescr) {
    final Set<String> names = new HashSet<>();
    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.drl.parser.ParserError) RuleDescr(org.drools.drl.ast.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) 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)

Aggregations

ParserError (org.drools.drl.parser.ParserError)9 PackageDescr (org.drools.drl.ast.descr.PackageDescr)5 DrlParser (org.drools.drl.parser.DrlParser)4 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 DescrResource (org.drools.core.io.impl.DescrResource)2 InternalResource (org.drools.core.io.internal.InternalResource)2 RuleDescr (org.drools.drl.ast.descr.RuleDescr)2 Test (org.junit.Test)2 ParserConfiguration (com.github.javaparser.ParserConfiguration)1 Problem (com.github.javaparser.Problem)1 Range (com.github.javaparser.Range)1 TokenRange (com.github.javaparser.TokenRange)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 XStream (com.thoughtworks.xstream.XStream)1 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1