use of org.drools.compiler.compiler.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 {
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();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (dslrReader != null) {
try {
dslrReader.close();
} catch (IOException e) {
}
}
}
return hasErrors ? null : pkg;
}
use of org.drools.compiler.compiler.DrlParser in project drools by kiegroup.
the class KnowledgeBuilderImpl method generatedResourcesToPackageDescr.
private List<PackageDescr> generatedResourcesToPackageDescr(Resource resource, List<PMMLResource> resources) throws DroolsParserException {
List<PackageDescr> pkgDescrs = new ArrayList<>();
DrlParser parser = new DrlParser(configuration.getLanguageLevel());
for (PMMLResource res : resources) {
for (String key : res.getRules().keySet()) {
String src = res.getRules().get(key);
PackageDescr descr = null;
descr = parser.parse(false, src);
if (descr != null) {
descr.setResource(resource);
pkgDescrs.add(descr);
dumpGeneratedRule(descr, key, src);
} else {
addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
}
}
}
return pkgDescrs;
}
use of org.drools.compiler.compiler.DrlParser in project drools by kiegroup.
the class KnowledgeBuilderImpl method addPackageFromDrl.
/**
* Load a rule package from DRL source using the supplied DSL configuration.
*
* @param source The source of the rules.
* @param dsl The source of the domain specific language configuration.
* @throws DroolsParserException
* @throws IOException
*/
public void addPackageFromDrl(final Reader source, final Reader dsl) throws DroolsParserException, IOException {
this.resource = new ReaderResource(source, ResourceType.DSLR);
final DrlParser parser = new DrlParser(configuration.getLanguageLevel());
final PackageDescr pkg = parser.parse(source, dsl);
this.results.addAll(parser.getErrors());
if (!parser.hasErrors()) {
addPackage(pkg);
}
this.resource = null;
}
use of org.drools.compiler.compiler.DrlParser in project drools by kiegroup.
the class RuleBuilderTest method testBuild.
@Test
public void testBuild() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
kBuilder.addPackage(new PackageDescr("org.drools"));
InternalKnowledgePackage pkg = kBuilder.getPackage("org.drools");
final PackageDescr pkgDescr = parser.parse(new InputStreamReader(getClass().getResourceAsStream("nestedConditionalElements.drl")));
// just checking there is no parsing errors
assertFalse(parser.getErrors().toString(), parser.hasErrors());
pkg.addGlobal("results", List.class);
final RuleDescr ruleDescr = pkgDescr.getRules().get(0);
final String ruleClassName = "RuleClassName.java";
ruleDescr.setClassName(ruleClassName);
ruleDescr.addAttribute(new AttributeDescr("dialect", "java"));
kBuilder.addPackage(pkgDescr);
assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
final RuleImpl rule = kBuilder.getPackage("org.drools.compiler").getRule("test nested CEs");
assertEquals("There should be 2 rule level declarations", 2, rule.getDeclarations().size());
// second GE should be a not
final GroupElement not = (GroupElement) rule.getLhs().getChildren().get(1);
assertTrue(not.isNot());
// not has no outer declarations
assertTrue(not.getOuterDeclarations().isEmpty());
assertEquals(1, not.getInnerDeclarations().size());
assertTrue(not.getInnerDeclarations().keySet().contains("$state"));
// second not
final GroupElement not2 = (GroupElement) ((GroupElement) not.getChildren().get(0)).getChildren().get(1);
assertTrue(not2.isNot());
// not has no outer declarations
assertTrue(not2.getOuterDeclarations().isEmpty());
assertEquals(1, not2.getInnerDeclarations().size());
assertTrue(not2.getInnerDeclarations().keySet().contains("$likes"));
}
use of org.drools.compiler.compiler.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 = new KnowledgePackageImpl("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);
}
}
Aggregations