use of org.drools.compiler.compiler.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.compiler.compiler.DrlParser in project drools by kiegroup.
the class KnowledgeBuilderImpl method addPackageFromDrl.
/**
* Load a rule package from DRL source and associate all loaded artifacts
* with the given resource.
*
* @param reader
* @param sourceResource the source resource for the read artifacts
* @throws DroolsParserException
* @throws IOException
*/
public void addPackageFromDrl(final Reader reader, final Resource sourceResource) throws DroolsParserException, IOException {
this.resource = sourceResource;
final DrlParser parser = new DrlParser(configuration.getLanguageLevel());
final PackageDescr pkg = parser.parse(sourceResource, reader);
this.results.addAll(parser.getErrors());
if (pkg == null) {
addBuilderResult(new ParserError(sourceResource, "Parser returned a null Package", 0, 0));
}
if (!parser.hasErrors()) {
addPackage(pkg);
}
this.resource = null;
}
use of org.drools.compiler.compiler.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(""));
}
use of org.drools.compiler.compiler.DrlParser in project drools by kiegroup.
the class DRLDumperTest method testDumpers.
@Test
public void testDumpers() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
final Resource resource = new InputStreamResource(getClass().getResourceAsStream("test_Dumpers.drl"));
final PackageDescr pkg = parser.parse(resource);
if (parser.hasErrors()) {
for (final DroolsError error : parser.getErrors()) {
logger.warn(error.toString());
}
fail(parser.getErrors().toString());
}
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(pkg));
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
final Cheese brie = new Cheese("brie", 12);
ksession.insert(brie);
ksession.fireAllRules();
assertEquals(3, list.size());
assertEquals("3 1", list.get(0));
assertEquals("MAIN", list.get(1));
assertEquals("1 1", list.get(2));
final DrlDumper drlDumper = new DrlDumper();
final String drlResult = drlDumper.dump(pkg);
System.out.println(drlResult);
kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(drlResult));
ksession = kbase.newKieSession();
list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(brie);
ksession.fireAllRules();
assertEquals(3, list.size());
assertEquals("3 1", list.get(0));
assertEquals("MAIN", list.get(1));
assertEquals("1 1", list.get(2));
}
use of org.drools.compiler.compiler.DrlParser in project drools by kiegroup.
the class ChangeSetBuilder method diffResource.
private static ResourceChangeSet diffResource(String file, byte[] ob, byte[] cb) {
ResourceChangeSet pkgcs = new ResourceChangeSet(file, ChangeType.UPDATED);
ResourceType type = ResourceType.determineResourceType(file);
if (ResourceType.DRL.equals(type) || ResourceType.GDRL.equals(type) || ResourceType.RDRL.equals(type) || ResourceType.TDRL.equals(type)) {
try {
PackageDescr opkg = new DrlParser().parse(new ByteArrayResource(ob));
PackageDescr cpkg = new DrlParser().parse(new ByteArrayResource(cb));
String pkgName = isEmpty(cpkg.getName()) ? getDefaultPackageName() : cpkg.getName();
String oldPkgName = isEmpty(opkg.getName()) ? getDefaultPackageName() : opkg.getName();
if (!oldPkgName.equals(pkgName)) {
// so it is useless to further investigate other changes
return pkgcs;
}
for (RuleDescr crd : cpkg.getRules()) {
pkgcs.getLoadOrder().add(new ResourceChangeSet.RuleLoadOrder(pkgName, crd.getName(), crd.getLoadOrder()));
}
// needs to be cloned
List<RuleDescr> orules = new ArrayList<>(opkg.getRules());
diffDescrs(ob, cb, pkgcs, orules, cpkg.getRules(), ResourceChange.Type.RULE, RULE_CONVERTER);
// needs to be cloned
List<FunctionDescr> ofuncs = new ArrayList<>(opkg.getFunctions());
diffDescrs(ob, cb, pkgcs, ofuncs, cpkg.getFunctions(), ResourceChange.Type.FUNCTION, FUNC_CONVERTER);
// needs to be cloned
List<GlobalDescr> oglobals = new ArrayList<>(opkg.getGlobals());
diffDescrs(ob, cb, pkgcs, oglobals, cpkg.getGlobals(), ResourceChange.Type.GLOBAL, GLOBAL_CONVERTER);
} catch (Exception e) {
logger.error("Error analyzing the contents of " + file + ". Skipping.", e);
}
}
pkgcs.getChanges().sort(Comparator.comparingInt(r -> r.getChangeType().ordinal()));
return pkgcs;
}
Aggregations