Details
-
Bug
-
Resolution: Done
-
Major
-
None
-
None
Description
ResourceToPkgDescrMapper is a nested interface within org.drools.compiler.builder.impl.CompositeKnowledgeBuilderImpl (module drools-compiler)
Each (public static final) field within this interface defines a lambda function of the form
PackageDescr map(KnowledgeBuilderImpl kBuilder, ResourceDescr resourceDescr) throws Exception;
for instance:
ResourceToPkgDescrMapper DRL_TO_PKG_DESCR = ( kBuilder, resourceDescr ) -> kBuilder.drlToPackageDescr(resourceDescr.resource);
delegates the call to a method on kbuilder (i.e. drlToPackageDescr())
The dependency on the entire kBuilder (i.e. org.drools.compiler.builder.impl.KnowledgeBuilderImpl) is often unnecessary for most of these methods. In fact, in this case the entire kBuilder is passed, but only the configuration field is really necessary:
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; }
both `this.results.addAll()` and `addBuilderResult()` accumulate compilation errors on the same `this.results` field.
Therefore, this method can be moved in a class, of the form:
public class DrlProcessor { private final KnowledgeBuilderConfigurationImpl configuration; private final Collection<KnowledgeBuilderResult> results = new ArrayList<>(); public DrlProcessor(KnowledgeBuilderConfigurationImpl configuration) { this.configuration = configuration; } public PackageDescr process(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) { this.results.add(new ParserError(resource, "Parser returned a null Package", 0, 0)); } hasErrors = parser.hasErrors(); } if (pkg != null) { pkg.setResource(resource); } return hasErrors ? null : pkg; } }
and the entire lambda can be substituted by something of the form
ResourceToPkgDescrMapper DRL_TO_PKG_DESCR = ( kBuilder, resourceDescr ) -> new DrlProcessor(kBuilder.getBuilderConfiguration()).process(resourceDescr.resource);
(errors should be pulled and added to kbuilder though)
The goal of this task is to perform the refactoring of such methods from the `KnowledgeBuilderImpl` class to a series "Processors" similar to DrlProcessor described above, and update the lambdas in a similar way.
Attachments
Issue Links
- is related to
-
DROOLS-7010 Add PackageDescrBuilder: ResourceToPkgDescrMapper, refactor resource handling
- Resolved