Search in sources :

Example 1 with DrlParser

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

the class NestingTest method testNesting.

@Test
public void testNesting() throws Exception {
    final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "\n" + "dialect \"mvel\"\n" + "\n" + "import " + Person.class.getCanonicalName() + ";\n" + "import " + Address.class.getCanonicalName() + ";\n" + "\n" + "rule \"test something\"\n" + "\n" + "    when\n" + "        p: Person( name==\"Michael\",\n" + "                                (addresses[1].street == \"Low\" &&\n" + "                                addresses[0].street == \"High\"  )\n" + "                                )\n" + "    then\n" + "        p.name = \"goober\";\n" + "        System.out.println(p.name);\n" + "        insert(new Address(\"Latona\"));\n" + "end";
    final Person p = new Person();
    p.setName("Michael");
    final Address add1 = new Address();
    add1.setStreet("High");
    final Address add2 = new Address();
    add2.setStreet("Low");
    final List<Address> l = new ArrayList<>();
    l.add(add1);
    l.add(add2);
    p.setAddresses(l);
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final PackageDescr desc = parser.parse(new StringReader(drl));
    final List packageAttrs = desc.getAttributes();
    assertEquals(1, desc.getRules().size());
    assertEquals(1, packageAttrs.size());
    final RuleDescr rule = desc.getRules().get(0);
    final Map<String, AttributeDescr> ruleAttrs = rule.getAttributes();
    assertEquals(1, ruleAttrs.size());
    assertEquals("mvel", ruleAttrs.get("dialect").getValue());
    assertEquals("dialect", ruleAttrs.get("dialect").getName());
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("nesting-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        ksession.insert(p);
        ksession.fireAllRules();
    } finally {
        ksession.dispose();
    }
}
Also used : Address(org.drools.testcoverage.common.model.Address) ArrayList(java.util.ArrayList) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) RuleDescr(org.drools.drl.ast.descr.RuleDescr) DrlParser(org.drools.drl.parser.DrlParser) ArrayList(java.util.ArrayList) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Person(org.drools.testcoverage.common.model.Person) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Test(org.junit.Test)

Example 2 with DrlParser

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

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

the class NestedPatternsTest method runVisitor.

@Test
public void runVisitor() throws Exception {
    VerifierData data = VerifierReportFactory.newVerifierData();
    PackageDescrVisitor visitor = new PackageDescrVisitor(data, Collections.EMPTY_LIST);
    assertNotNull(data);
    Reader drlReader = new InputStreamReader(getClass().getResourceAsStream("NestedPatterns.drl"));
    PackageDescr packageDescr = new DrlParser(LanguageLevelOption.DRL5).parse(drlReader);
    assertNotNull(packageDescr);
    visitor.visitPackageDescr(packageDescr);
    Collection<VerifierComponent> all = data.getAll();
    int patternCount = 0;
    for (VerifierComponent verifierComponent : all) {
        if (verifierComponent.getVerifierComponentType().equals(VerifierComponentType.PATTERN)) {
            patternCount++;
        }
    }
    assertEquals(4, patternCount);
    Collection<Pattern> patterns = data.getAll(VerifierComponentType.PATTERN);
    // for ( Pattern pattern : patterns ) {
    // System.out.println( pattern.getPath() + " " + pattern );
    // }
    assertNotNull(patterns);
    assertEquals(4, patterns.size());
    Collection<Restriction> restrictions = data.getAll(VerifierComponentType.RESTRICTION);
    // for ( Restriction restriction : restrictions ) {
    // System.out.println( restriction.getPath() + " " + restriction );
    // }
    assertNotNull(restrictions);
    assertEquals(3, restrictions.size());
}
Also used : Pattern(org.drools.verifier.components.Pattern) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) VerifierComponent(org.drools.verifier.data.VerifierComponent) Restriction(org.drools.verifier.components.Restriction) VerifierData(org.drools.verifier.data.VerifierData) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 4 with DrlParser

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

the class VerifierComponentTest method testVisit.

@Test
public void testVisit() throws Exception {
    // Drools Package description from Drl file
    Reader drlReader = new InputStreamReader(Verifier.class.getResourceAsStream("Misc3.drl"));
    PackageDescr descr = new DrlParser(LanguageLevelOption.DRL5).parse(drlReader);
    // Drools Verifier objects
    VerifierData verifierData = VerifierReportFactory.newVerifierData();
    PackageDescrVisitor visitor = new PackageDescrVisitor(verifierData, Collections.EMPTY_LIST);
    visitor.visitPackageDescr(descr);
    // Collect the results.
    Collection<VerifierComponent> datas = verifierData.getAll();
    VerifierComponent[] components = datas.toArray(new VerifierComponent[datas.size()]);
    // Misc3.drl
    assertVerifierComponent((TextConsequence) components[0], 51);
    assertVerifierComponent((TextConsequence) components[1], 42);
    assertVerifierComponent((Field) components[2], 48);
    assertVerifierComponent((Field) components[3], 39);
    assertVerifierComponent((Field) components[4], 40);
    assertVerifierComponent((Field) components[5], 41);
    assertVerifierComponent((Import) components[6], 19);
    assertVerifierComponent((ObjectType) components[7], 48);
    assertVerifierComponent((ObjectType) components[8], 19);
    assertVerifierComponent((ObjectType) components[9], 39);
    assertVerifierComponent((ObjectType) components[10], 40);
    assertVerifierComponent((ObjectType) components[11], 41);
    assertVerifierComponent((RuleOperatorDescr) components[12], 48);
    assertVerifierComponent((PatternOperatorDescr) components[13], 48);
    assertVerifierComponent((PatternOperatorDescr) components[14], 49);
    assertVerifierComponent((PatternOperatorDescr) components[15], 50);
    assertVerifierComponent((RuleOperatorDescr) components[16], 39);
    assertVerifierComponent((PatternOperatorDescr) components[17], 39);
    assertVerifierComponent((PatternOperatorDescr) components[18], 40);
    assertVerifierComponent((PatternOperatorDescr) components[19], 41);
    assertVerifierComponent((Pattern) components[20], 48);
    assertVerifierComponent((Pattern) components[21], 49);
    assertVerifierComponent((Pattern) components[22], 50);
    assertVerifierComponent((Pattern) components[23], 39);
    assertVerifierComponent((Pattern) components[24], 40);
    assertVerifierComponent((Pattern) components[25], 41);
    assertVerifierComponent((NumberRestriction) components[26], 48);
    assertVerifierComponent((NumberRestriction) components[27], 49);
    assertVerifierComponent((NumberRestriction) components[28], 50);
    assertVerifierComponent((NumberRestriction) components[29], 39);
    assertVerifierComponent((NumberRestriction) components[30], 40);
    assertVerifierComponent((NumberRestriction) components[31], 41);
    assertVerifierComponent((NumberRestriction) components[32], 41);
    assertVerifierComponent((VerifierRule) components[33], 46);
    assertVerifierComponent((VerifierRule) components[34], 37);
    assertVerifierComponent((RulePackage) components[35], 17);
    assertVerifierComponent((SubPattern) components[36], 48);
    assertVerifierComponent((SubPattern) components[37], 49);
    assertVerifierComponent((SubPattern) components[38], 50);
    assertVerifierComponent((SubPattern) components[39], 39);
    assertVerifierComponent((SubPattern) components[40], 40);
    assertVerifierComponent((SubPattern) components[41], 41);
    assertVerifierComponent((SubRule) components[42], 46);
    assertVerifierComponent((SubRule) components[43], 37);
    assertVerifierComponent((WorkingMemory) components[44], -1);
}
Also used : InputStreamReader(java.io.InputStreamReader) VerifierData(org.drools.verifier.data.VerifierData) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) DrlParser(org.drools.drl.parser.DrlParser) Verifier(org.drools.verifier.Verifier) PackageDescr(org.drools.drl.ast.descr.PackageDescr) VerifierComponent(org.drools.verifier.data.VerifierComponent) Test(org.junit.Test)

Example 5 with DrlParser

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

the class DescrResourceSetTest method drlFilesTest.

@Test
public void drlFilesTest() throws Exception {
    Set<File> drlFiles = getDrlFiles();
    for (File drl : drlFiles) {
        final DrlParser parser = new DrlParser(((KnowledgeBuilderConfigurationImpl) conf).getLanguageLevel());
        InputStreamResource resource = new InputStreamResource(new FileInputStream(drl));
        PackageDescr pkgDescr = parser.parse(resource);
        if (parser.hasErrors()) {
            continue;
        }
        visitor.visit(pkgDescr);
    }
    logger.debug(drlFiles.size() + " drl tested.");
}
Also used : DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) File(java.io.File) FileInputStream(java.io.FileInputStream) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Test(org.junit.Test)

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