use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class ModelBuilderImpl method compileKnowledgePackages.
@Override
protected void compileKnowledgePackages(PackageDescr packageDescr, PackageRegistry pkgRegistry) {
validateUniqueRuleNames(packageDescr);
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
String pkgName = pkg.getName();
PackageModel model = packageModels.computeIfAbsent(pkgName, s -> {
final DialectCompiletimeRegistry dialectCompiletimeRegistry = pkgRegistry.getDialectCompiletimeRegistry();
return new PackageModel(pkgName, this.getBuilderConfiguration(), isPattern, dialectCompiletimeRegistry);
});
generateModel(this, pkg, packageDescr, model, isPattern);
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class ModelBuilderImpl method buildRules.
protected void buildRules(Collection<CompositePackageDescr> packages) {
for (CompositePackageDescr packageDescr : packages) {
PackageRegistry pkgRegistry = getPackageRegistry(packageDescr.getNamespace());
generatePOJOs(packageDescr, pkgRegistry);
}
List<GeneratedClassWithPackage> allGeneratedPojos = packageModels.values().stream().flatMap(p -> p.getGeneratedPOJOsSource().stream().map(c -> new GeneratedClassWithPackage(c, p.getName(), p.getImports()))).collect(Collectors.toList());
// Every class gets compiled in each classloader, maybe they can be compiled only one time?
final Map<String, Class<?>> allCompiledClasses = new HashMap<>();
for (CompositePackageDescr packageDescr : packages) {
InternalKnowledgePackage pkg = getPackageRegistry(packageDescr.getNamespace()).getPackage();
allCompiledClasses.putAll(compileType(this, pkg.getPackageClassLoader(), allGeneratedPojos));
}
for (CompositePackageDescr packageDescr : packages) {
InternalKnowledgePackage pkg = getPackageRegistry(packageDescr.getNamespace()).getPackage();
allGeneratedPojos.forEach(c -> registerType(pkg.getTypeResolver(), allCompiledClasses));
}
if (hasErrors()) {
// if Error while generating pojo do not try compile rule as they very likely depends hence fail too.
return;
}
for (CompositePackageDescr packageDescr : packages) {
setAssetFilter(packageDescr.getFilter());
PackageRegistry pkgRegistry = getPackageRegistry(packageDescr.getNamespace());
compileKnowledgePackages(packageDescr, pkgRegistry);
setAssetFilter(null);
}
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class PatternTest method testDeclarationsFactTemplate.
@Test
public void testDeclarationsFactTemplate() throws Exception {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.store");
final FieldTemplate cheeseName = new FieldTemplateImpl("name", 0, String.class);
final FieldTemplate cheesePrice = new FieldTemplateImpl("price", 1, Integer.class);
final FieldTemplate[] fields = new FieldTemplate[] { cheeseName, cheesePrice };
final FactTemplate cheese = new FactTemplateImpl(pkg, "Cheese", fields);
final ObjectType type = new FactTemplateObjectType(cheese);
final Pattern col = new Pattern(0, type, "foo");
final Declaration dec = col.getDeclaration();
final InternalReadAccessor ext = dec.getExtractor();
assertEquals(Fact.class, ext.getExtractToClass());
final Fact stilton = cheese.createFact(10);
stilton.setFieldValue("name", "stilton");
stilton.setFieldValue("price", new Integer(200));
assertEquals(stilton, dec.getValue(null, stilton));
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class TypeDeclarationConfigurator method wireDurationAccessor.
private static void wireDurationAccessor(KnowledgeBuilderImpl kbuilder, Annotated annotated, TypeDeclaration type, PackageRegistry pkgRegistry) {
Duration duration = annotated.getTypedAnnotation(Duration.class);
if (duration != null) {
BaseDescr typeDescr = annotated instanceof BaseDescr ? ((BaseDescr) annotated) : new BaseDescr();
String durationField;
try {
durationField = duration.value();
} catch (Exception e) {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, e.getMessage()));
return;
}
type.setDurationAttribute(durationField);
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
MVELAnalysisResult results = getMvelAnalysisResult(kbuilder, typeDescr, type, pkgRegistry, durationField, pkg);
if (results != null) {
type.setDurationExtractor(getFieldExtractor(type, durationField, pkg, results));
} else {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Error processing @duration for TypeDeclaration '" + type.getFullName() + "': cannot access the field '" + durationField + "'"));
}
}
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class BayesAssemblerService method addResource.
@Override
public void addResource(Object kbuilder, Resource resource, ResourceType type, ResourceConfiguration configuration) throws Exception {
BayesNetwork network;
JunctionTreeBuilder builder;
KnowledgeBuilder kb = (KnowledgeBuilder) kbuilder;
Bif bif = XmlBifParser.loadBif(resource, kb.getErrors());
if (bif == null) {
return;
}
try {
network = XmlBifParser.buildBayesNetwork(bif);
} catch (Exception e) {
kb.getErrors().add(new BayesNetworkAssemblerError(resource, "Unable to parse opening Stream:\n" + e.toString()));
return;
}
try {
builder = new JunctionTreeBuilder(network);
} catch (Exception e) {
kb.getErrors().add(new BayesNetworkAssemblerError(resource, "Unable to build Junction Tree:\n" + e.toString()));
return;
}
KnowledgeBuilderImpl kbuilderImpl = (KnowledgeBuilderImpl) kbuilder;
PackageRegistry pkgReg = kbuilderImpl.getOrCreatePackageRegistry(new PackageDescr(network.getPackageName()));
InternalKnowledgePackage kpkgs = pkgReg.getPackage();
Map<ResourceType, ResourceTypePackage> rpkg = kpkgs.getResourceTypePackages();
BayesPackage bpkg = (BayesPackage) rpkg.get(ResourceType.BAYES);
if (bpkg == null) {
bpkg = new BayesPackage();
rpkg.put(ResourceType.BAYES, bpkg);
}
bpkg.addJunctionTree(network.getName(), builder.build());
}
Aggregations