Search in sources :

Example 16 with DrlParser

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

the class DRLIncompleteCodeTest method testIncompleteCode6.

@Test
public void testIncompleteCode6() throws DroolsParserException, RecognitionException {
    String input = "packe 1111.111 import a.b.c.*" + " rule MyRule when Class ( property memberOf collection ) then end " + " query MyQuery Class ( property memberOf collection ) end ";
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    PackageDescr descr = parser.parse(true, input);
    assertNotNull(descr);
}
Also used : DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 17 with DrlParser

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

the class DRLIncompleteCodeTest method testIncompleteCode1.

@Test
@Ignore
public void testIncompleteCode1() throws DroolsParserException, RecognitionException {
    String input = "package a.b.c import a.b.c.* rule MyRule when Class ( property memberOf collexction ";
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    PackageDescr descr = parser.parse(true, input);
    System.out.println(parser.getErrors());
    assertNotNull(descr);
    assertEquals("a.b.c", descr.getNamespace());
    assertEquals("a.b.c.*", descr.getImports().get(0).getTarget());
    assertEquals(Location.LOCATION_LHS_INSIDE_CONDITION_END, getLastIntegerValue(parser.getEditorSentences().get(2).getContent()));
}
Also used : DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 18 with DrlParser

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

the class TestBaseOld method getTestData.

@SuppressWarnings("unchecked")
public Collection<? extends Object> getTestData(InputStream stream, VerifierData data) throws Exception {
    Reader drlReader = new InputStreamReader(stream);
    PackageDescr descr = new DrlParser(LanguageLevelOption.DRL5).parse(drlReader);
    PackageDescrVisitor packageDescrVisitor = new PackageDescrVisitor(data, Collections.EMPTY_LIST);
    packageDescrVisitor.visitPackageDescr(descr);
    // Rules with relations
    return data.getAll();
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) PackageDescrVisitor(org.drools.verifier.visitor.PackageDescrVisitor)

Example 19 with DrlParser

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

the class KnowledgeBuilderImpl method dslrReaderToPackageDescr.

private PackageDescr dslrReaderToPackageDescr(Resource resource, Reader dslrReader) throws DroolsParserException {
    boolean hasErrors;
    PackageDescr pkg;
    DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    DefaultExpander expander = getDslExpander();
    try {
        try {
            if (expander == null) {
                expander = new DefaultExpander();
            }
            String str = expander.expand(dslrReader);
            if (expander.hasErrors()) {
                for (ExpanderException error : expander.getErrors()) {
                    error.setResource(resource);
                    addBuilderResult(error);
                }
            }
            pkg = parser.parse(resource, str);
            this.results.addAll(parser.getErrors());
            hasErrors = parser.hasErrors();
        } finally {
            if (dslrReader != null) {
                dslrReader.close();
            }
        }
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
    return hasErrors ? null : pkg;
}
Also used : DrlParser(org.drools.drl.parser.DrlParser) ExpanderException(org.drools.drl.parser.lang.ExpanderException) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander)

Example 20 with DrlParser

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

Aggregations

DrlParser (org.drools.drl.parser.DrlParser)41 PackageDescr (org.drools.drl.ast.descr.PackageDescr)38 Test (org.junit.Test)32 RuleDescr (org.drools.drl.ast.descr.RuleDescr)9 InputStreamReader (java.io.InputStreamReader)8 StringReader (java.io.StringReader)8 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 ParserError (org.drools.drl.parser.ParserError)4 DrlDumper (org.drools.mvel.DrlDumper)4 Reader (java.io.Reader)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)3 PatternDescr (org.drools.drl.ast.descr.PatternDescr)3 Ignore (org.junit.Ignore)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 InputStreamResource (org.drools.core.io.impl.InputStreamResource)2 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)2 MVELConsequence (org.drools.mvel.expr.MVELConsequence)2