Search in sources :

Example 6 with ParserError

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

the class XmlBifParser method loadBif.

public static Bif loadBif(Resource resource, ArrayList<KnowledgeBuilderError> 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 ? createNonTrustingXStream(new DomDriver(encoding)) : createNonTrustingXStream();
        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.drl.parser.ParserError) InputStream(java.io.InputStream) XStream(com.thoughtworks.xstream.XStream) XStreamUtils.createNonTrustingXStream(org.kie.utll.xml.XStreamUtils.createNonTrustingXStream) BayesNetworkAssemblerError(org.drools.beliefs.bayes.assembler.BayesNetworkAssemblerError) IOException(java.io.IOException) IOException(java.io.IOException)

Example 7 with ParserError

use of org.drools.drl.parser.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.drl.ast.descr.PatternDescr) ParserError(org.drools.drl.parser.ParserError) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FieldConstraintDescr(org.drools.drl.ast.descr.FieldConstraintDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) LiteralRestrictionDescr(org.drools.drl.ast.descr.LiteralRestrictionDescr) Test(org.junit.Test)

Example 8 with ParserError

use of org.drools.drl.parser.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.drl.parser.ParserError) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) IOException(java.io.IOException) RecognitionException(org.antlr.runtime.RecognitionException)

Example 9 with ParserError

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

the class DrlxCompiler method toPackageDescr.

public PackageDescr toPackageDescr(Resource resource) throws IOException {
    ParseStart<CompilationUnit> context = ParseStart.DRLX_COMPILATION_UNIT;
    MvelParser mvelParser = new MvelParser(new ParserConfiguration(), false);
    ParseResult<CompilationUnit> result = mvelParser.parse(context, provider(resource.getReader()));
    if (result.isSuccessful()) {
        DrlxVisitor drlxCompiler = new DrlxVisitor();
        drlxCompiler.visit(result.getResult().get(), null);
        PackageDescr pkg = drlxCompiler.getPackageDescr();
        if (pkg == null) {
            this.results.add(new ParserError(resource, "Parser returned a null Package", 0, 0));
            return null;
        } else {
            pkg.setResource(resource);
            return pkg;
        }
    } else {
        for (Problem problem : result.getProblems()) {
            TokenRange tokenRange = problem.getLocation().get();
            Range range = tokenRange.getBegin().getRange().get();
            int lineCount = range.getLineCount();
            this.results.add(new ParserError(problem.getMessage(), lineCount, -1));
        }
        return null;
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MvelParser(org.drools.mvel.parser.MvelParser) ParserError(org.drools.drl.parser.ParserError) Problem(com.github.javaparser.Problem) TokenRange(com.github.javaparser.TokenRange) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Range(com.github.javaparser.Range) TokenRange(com.github.javaparser.TokenRange) ParserConfiguration(com.github.javaparser.ParserConfiguration)

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