use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderTest method testTypeDeclarationNewBean.
@Test
public void testTypeDeclarationNewBean() throws Exception {
PackageDescr pkgDescr = new PackageDescr("org.drools.compiler.test");
TypeDeclarationDescr typeDescr = new TypeDeclarationDescr("NewBean");
TypeFieldDescr f1 = new TypeFieldDescr("name", new PatternDescr("String"));
TypeFieldDescr f2 = new TypeFieldDescr("age", new PatternDescr("int"));
typeDescr.addField(f1);
typeDescr.addField(f2);
pkgDescr.addTypeDeclaration(typeDescr);
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
builder.addPackage(pkgDescr);
InternalKnowledgePackage pkg = builder.getPackage(pkgDescr.getName());
assertEquals(1, pkg.getTypeDeclarations().size());
TypeDeclaration type = pkg.getTypeDeclaration("NewBean");
assertEquals("NewBean", type.getTypeName());
assertEquals(Role.Type.FACT, type.getRole());
assertEquals("org.drools.compiler.test.NewBean", type.getTypeClass().getName());
assertFalse(builder.hasErrors());
InternalKnowledgePackage bp = builder.getPackage(pkgDescr.getName());
Class newBean = bp.getPackageClassLoader().loadClass("org.drools.compiler.test.NewBean");
assertNotNull(newBean);
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class FactTemplateTest method testEqualsAndHashCode.
@Test
public void testEqualsAndHashCode() {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.store");
// Create cheese1 with name and price fields
final FieldTemplate cheeseName = new FieldTemplateImpl("name", 0, String.class);
final FieldTemplate cheesePrice = new FieldTemplateImpl("price", 1, Integer.class);
final FieldTemplate[] fields1 = new FieldTemplate[] { cheeseName, cheesePrice };
final FactTemplate cheese1 = new FactTemplateImpl(pkg, "Cheese", fields1);
// Create cheese2 with type and price fields
final FieldTemplate cheeseType = new FieldTemplateImpl("type", 0, String.class);
final FieldTemplate[] fields2 = new FieldTemplate[] { cheeseType, cheesePrice };
final FactTemplate cheese2 = new FactTemplateImpl(pkg, "Cheese", fields2);
assertNotSame(cheese1, cheese2);
assertFalse(cheese1.equals(cheese2));
assertFalse(cheese1.hashCode() == cheese2.hashCode());
// create cheese3 with name and price fields, using new instances
final FieldTemplate cheeseName2 = new FieldTemplateImpl("name", 0, String.class);
final FieldTemplate cheesePrice2 = new FieldTemplateImpl("price", 1, Integer.class);
final FieldTemplate[] fields3 = new FieldTemplate[] { cheeseName2, cheesePrice2 };
final FactTemplate cheese3 = new FactTemplateImpl(pkg, "Cheese", fields3);
assertNotSame(cheese1, cheese3);
assertNotSame(cheese1.getAllFieldTemplates(), cheese3.getAllFieldTemplates());
assertEquals(cheese1, cheese3);
assertEquals(cheese1.hashCode(), cheese3.hashCode());
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class PackageCompilationDataTest method testCodeSourceUrl.
@Test
public void testCodeSourceUrl() throws Exception {
final String className = TestEvalExpression.class.getName();
KnowledgeBaseImpl kBase = new KnowledgeBaseImpl("xxx", null);
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
JavaDialectRuntimeData data = new JavaDialectRuntimeData();
data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
pkg.getDialectRuntimeRegistry().setDialectData("java", data);
kBase.addPackage(pkg);
final JavaDialectRuntimeData pcData = (JavaDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("java");
final EvalCondition invoker = new EvalCondition(null);
pcData.putInvoker(className, invoker);
final InputStream is = getClass().getClassLoader().getResourceAsStream(className.replace('.', '/') + ".class");
try {
pcData.write(className.replace('.', '/') + ".class", read(is));
} finally {
is.close();
}
pcData.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
pcData.onBeforeExecute();
Class cls = kBase.getRootClassLoader().loadClass("org.drools.core.rule.PackageCompilationDataTest$TestEvalExpression");
final CodeSource codeSource = invoker.getEvalExpression().getClass().getProtectionDomain().getCodeSource();
assertNotNull(codeSource.getLocation());
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class DefaultTemplateRuleBase method readKnowledgeBase.
private InternalKnowledgeBase readKnowledgeBase(String drl) {
// read in the source
try (Reader source = new StringReader(drl)) {
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
builder.addPackageFromDrl(source);
InternalKnowledgePackage pkg = builder.getPackage(DefaultTemplateRuleBase.class.getPackage().getName());
// add the package to a rulebase (deploy the rule package).
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
kBase.addPackage(pkg);
return kBase;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class DMNAssemblerService method compileResourceToModel.
private DMNModel compileResourceToModel(KnowledgeBuilderImpl kbuilderImpl, DMNCompiler dmnCompiler, Resource resource, Collection<DMNModel> dmnModels) {
DMNModel model = dmnCompiler.compile(resource, dmnModels);
if (model != null) {
String namespace = model.getNamespace();
PackageRegistry pkgReg = kbuilderImpl.getOrCreatePackageRegistry(new PackageDescr(namespace));
InternalKnowledgePackage kpkgs = pkgReg.getPackage();
kpkgs.addCloningResource(DMN_COMPILER_CACHE_KEY, dmnCompiler);
Map<ResourceType, ResourceTypePackage> rpkg = kpkgs.getResourceTypePackages();
DMNPackageImpl dmnpkg = (DMNPackageImpl) rpkg.get(ResourceType.DMN);
if (dmnpkg == null) {
dmnpkg = new DMNPackageImpl(namespace);
rpkg.put(ResourceType.DMN, dmnpkg);
} else {
if (dmnpkg.getModel(model.getName()) != null) {
kbuilderImpl.addBuilderResult(new DMNKnowledgeBuilderError(ResultSeverity.ERROR, resource, namespace, "Duplicate model name " + model.getName() + " in namespace " + namespace));
logger.error("Duplicate model name {} in namespace {}", model.getName(), namespace);
}
}
dmnpkg.addModel(model.getName(), model);
dmnpkg.addProfiles(kbuilderImpl.getCachedOrCreate(DMN_PROFILES_CACHE_KEY, () -> getDMNProfiles(kbuilderImpl)));
} else {
kbuilderImpl.addBuilderResult(new DMNKnowledgeBuilderError(ResultSeverity.ERROR, resource, "Unable to compile DMN model for the resource"));
logger.error("Unable to compile DMN model for resource {}", resource.getSourcePath());
}
return model;
}
Aggregations