use of org.drools.drl.parser.DrlParser in project drools by kiegroup.
the class KnowledgeBuilderImpl method generatedDrlToPackageDescr.
private PackageDescr generatedDrlToPackageDescr(Resource resource, String generatedDrl) throws DroolsParserException {
// dump the generated DRL if the dump dir was configured
if (this.configuration.getDumpDir() != null) {
dumpDrlGeneratedFromDTable(this.configuration.getDumpDir(), generatedDrl, resource.getSourcePath());
}
DrlParser parser = new DrlParser(configuration.getLanguageLevel());
PackageDescr pkg = parser.parse(resource, new StringReader(generatedDrl));
this.results.addAll(parser.getErrors());
if (pkg == null) {
addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
} else {
pkg.setResource(resource);
}
return parser.hasErrors() ? null : pkg;
}
use of org.drools.drl.parser.DrlParser in project drools by kiegroup.
the class KnowledgeBuilderImpl method drlToPackageDescr.
PackageDescr drlToPackageDescr(Resource resource) throws DroolsParserException, IOException {
PackageDescr pkg;
boolean hasErrors = false;
if (resource instanceof DescrResource) {
pkg = (PackageDescr) ((DescrResource) resource).getDescr();
} else {
final DrlParser parser = new DrlParser(configuration.getLanguageLevel());
pkg = parser.parse(resource);
this.results.addAll(parser.getErrors());
if (pkg == null) {
addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
}
hasErrors = parser.hasErrors();
}
if (pkg != null) {
pkg.setResource(resource);
}
return hasErrors ? null : pkg;
}
use of org.drools.drl.parser.DrlParser in project drools by kiegroup.
the class MVELDebugTest method testDebug.
@Test
public void testDebug() throws Exception {
String rule = "package com.sample; dialect \"mvel\" rule myRule when then\n System.out.println( \"test\" ); end";
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
PackageDescr packageDescr = parser.parse(null, rule);
RuleDescr ruleDescr = packageDescr.getRules().get(0);
builder = new KnowledgeBuilderImpl();
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage("com.sample");
MVELConsequence consequence = (MVELConsequence) pkg.getRule("myRule").getConsequence();
String ruleName = ruleDescr.getNamespace() + "." + ruleDescr.getClassName();
System.out.println(ruleName);
assertEquals("com.sample.Rule_myRule", ruleName);
}
use of org.drools.drl.parser.DrlParser in project drools by kiegroup.
the class MVELConsequenceBuilderTest method testMVELDebugSymbols.
@Test
public void testMVELDebugSymbols() throws DroolsParserException {
MVELDebugHandler.setDebugMode(true);
try {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
final PackageDescr pkgDescr = parser.parse(new InputStreamReader(getClass().getResourceAsStream("mvel_rule.drl")));
// just checking there is no parsing errors
assertFalse(parser.getErrors().toString(), parser.hasErrors());
InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("org.drools");
final RuleDescr ruleDescr = pkgDescr.getRules().get(0);
final KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
Dialect dialect = dialectRegistry.getDialect("mvel");
RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, dialect);
RuleBuilder.build(context);
assertTrue(context.getErrors().toString(), context.getErrors().isEmpty());
final RuleImpl rule = context.getRule();
MVELConsequence mvelCons = (MVELConsequence) rule.getConsequence();
mvelCons.compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
String s = DebugTools.decompile(mvelCons.getCompExpr());
int fromIndex = 0;
int count = 0;
while ((fromIndex = s.indexOf("DEBUG_SYMBOL", fromIndex + 1)) > -1) {
count++;
}
assertEquals(4, count);
} finally {
MVELDebugHandler.setDebugMode(false);
}
}
use of org.drools.drl.parser.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());
}
Aggregations