Search in sources :

Example 16 with DrlParser

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

the class Misc2Test method testEvalConstraintWithMvelOperator.

@Test
public void testEvalConstraintWithMvelOperator() {
    String drl = "rule \"yeah\" " + "\tdialect \"mvel\"\n when " + "Foo( eval( field soundslike \"water\" ) )" + " then " + "end";
    DrlParser drlParser = new DrlParser();
    PackageDescr packageDescr;
    try {
        packageDescr = drlParser.parse(true, drl);
    } catch (DroolsParserException e) {
        throw new RuntimeException(e);
    }
    RuleDescr r = packageDescr.getRules().get(0);
    PatternDescr pd = (PatternDescr) r.getLhs().getDescrs().get(0);
    assertEquals(1, pd.getConstraint().getDescrs().size());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) Test(org.junit.Test)

Example 17 with DrlParser

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

the class AnnotationsTest method testCollectAnnotationsParsingAndBuilding.

@Test
public void testCollectAnnotationsParsingAndBuilding() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(new PackageDescr("org.drools"));
    String ruleDrl = "package org.drools.compiler; " + " " + "dialect 'mvel' " + " " + "import java.util.Collection; " + "import " + Annot.class.getCanonicalName() + "; " + " " + "rule \"test collect with annotation\" " + "    when " + "        Collection() from collect ( " + "            String() @Annot " + "        ) " + "    then " + "end " + "";
    final PackageDescr pkgDescr = parser.parse(new StringReader(ruleDrl));
    // just checking there is no parsing errors
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
    final RuleImpl rule = kBuilder.getPackage(pkgDescr.getName()).getRule("test collect with annotation");
    List<? extends RuleConditionElement> nested = ((Pattern) rule.getLhs().getChildren().get(0)).getSource().getNestedElements();
    assertEquals(1, nested.size());
    Map<String, AnnotationDefinition> annotations = ((Pattern) nested.get(0)).getAnnotations();
    assertEquals(1, annotations.size());
    assertNotNull(annotations.keySet().iterator().next());
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) Pattern(org.drools.core.rule.Pattern) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 18 with DrlParser

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

the class AnnotationsTest method testAnnotationOnLHSAndMerging.

@Test
public void testAnnotationOnLHSAndMerging() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    String ruleDrl = "package org.drools.compiler; " + " " + "import " + Annot.class.getCanonicalName() + "; " + " " + "rule \"test collect with annotation\" " + "    when " + "       ( and @Annot " + "         String() " + "         Integer() ) " + "    then " + "end " + "";
    final PackageDescr pkgDescr = parser.parse(new StringReader(ruleDrl));
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 19 with DrlParser

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

the class DumperTestHelper method DrlFile.

public static void DrlFile(String filename) throws Exception {
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final PackageDescr pkgOriginal = parser.parse(new InputStreamReader(DumperTestHelper.class.getResourceAsStream(filename)));
    final DrlDumper dumper = new DrlDumper();
    String result1 = dumper.dump(pkgOriginal);
    final PackageDescr pkgDerivated = parser.parse(new StringReader(result1));
    String result2 = dumper.dump(pkgDerivated);
    System.out.println(result1);
    Assertions.assertThat(result1).isEqualToIgnoringWhitespace(result2);
}
Also used : InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper)

Example 20 with DrlParser

use of org.drools.compiler.compiler.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.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Aggregations

DrlParser (org.drools.compiler.compiler.DrlParser)44 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)43 Test (org.junit.Test)32 InputStreamReader (java.io.InputStreamReader)10 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)9 StringReader (java.io.StringReader)8 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)6 ArrayList (java.util.ArrayList)5 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 ParserError (org.drools.compiler.compiler.ParserError)5 DrlDumper (org.drools.compiler.lang.DrlDumper)4 Reader (java.io.Reader)3 List (java.util.List)3 DroolsParserException (org.drools.compiler.compiler.DroolsParserException)3 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 Ignore (org.junit.Ignore)3 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)2 MVELConsequence (org.drools.core.base.mvel.MVELConsequence)2