use of org.drools.compiler.lang.descr.PackageDescr 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.lang.descr.PackageDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method removeObjectsGeneratedFromResource.
public ResourceRemovalResult removeObjectsGeneratedFromResource(Resource resource) {
boolean modified = false;
if (pkgRegistryMap != null) {
for (PackageRegistry packageRegistry : pkgRegistryMap.values()) {
modified = packageRegistry.removeObjectsGeneratedFromResource(resource) || modified;
}
}
if (results != null) {
Iterator<KnowledgeBuilderResult> i = results.iterator();
while (i.hasNext()) {
if (resource.equals(i.next().getResource())) {
i.remove();
}
}
}
if (processBuilder != null && processBuilder.getErrors() != null) {
Iterator<? extends KnowledgeBuilderResult> i = processBuilder.getErrors().iterator();
while (i.hasNext()) {
if (resource.equals(i.next().getResource())) {
i.remove();
}
}
}
if (results.size() == 0) {
// TODO Error attribution might be bugged
for (PackageRegistry packageRegistry : pkgRegistryMap.values()) {
packageRegistry.getPackage().resetErrors();
}
}
Collection<String> removedTypes = typeBuilder.removeTypesGeneratedFromResource(resource);
for (List<PackageDescr> pkgDescrs : packages.values()) {
for (PackageDescr pkgDescr : pkgDescrs) {
pkgDescr.removeObjectsGeneratedFromResource(resource);
}
}
if (kBase != null) {
modified = kBase.removeObjectsGeneratedFromResource(resource) || modified;
}
return new ResourceRemovalResult(modified, removedTypes);
}
use of org.drools.compiler.lang.descr.PackageDescr 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.lang.descr.PackageDescr 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.lang.descr.PackageDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method addPackage.
public synchronized void addPackage(InternalKnowledgePackage newPkg) {
PackageRegistry pkgRegistry = this.pkgRegistryMap.get(newPkg.getName());
InternalKnowledgePackage pkg = null;
if (pkgRegistry != null) {
pkg = pkgRegistry.getPackage();
}
if (pkg == null) {
PackageDescr packageDescr = new PackageDescr(newPkg.getName());
pkgRegistry = getOrCreatePackageRegistry(packageDescr);
mergePackage(this.pkgRegistryMap.get(packageDescr.getNamespace()), packageDescr);
pkg = pkgRegistry.getPackage();
}
// first merge anything related to classloader re-wiring
pkg.getDialectRuntimeRegistry().merge(newPkg.getDialectRuntimeRegistry(), this.rootClassLoader);
if (newPkg.getFunctions() != null) {
for (Map.Entry<String, Function> entry : newPkg.getFunctions().entrySet()) {
if (pkg.getFunctions().containsKey(entry.getKey())) {
addBuilderResult(new DuplicateFunction(entry.getValue(), this.configuration));
}
pkg.addFunction(entry.getValue());
}
}
pkg.getClassFieldAccessorStore().merge(newPkg.getClassFieldAccessorStore());
pkg.getDialectRuntimeRegistry().onBeforeExecute();
// we have to do this before the merging, as it does some classloader resolving
TypeDeclaration lastType = null;
try {
// Resolve the class for the type declaation
if (newPkg.getTypeDeclarations() != null) {
// add type declarations
for (TypeDeclaration type : newPkg.getTypeDeclarations().values()) {
lastType = type;
type.setTypeClass(this.rootClassLoader.loadClass(type.getTypeClassName()));
}
}
} catch (ClassNotFoundException e) {
throw new RuntimeException("unable to resolve Type Declaration class '" + lastType.getTypeName() + "'");
}
// now merge the new package into the existing one
mergePackage(pkg, newPkg);
}
Aggregations