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());
}
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());
}
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());
}
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);
}
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());
}
Aggregations