use of org.drools.core.definitions.impl.KnowledgePackageImpl 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.impl.KnowledgePackageImpl in project drools by kiegroup.
the class CrossProductTest method setUp.
@Before
public void setUp() throws Exception {
final ObjectType list1ObjectType = new ClassObjectType(String.class);
final ObjectType list2ObjectType = new ClassObjectType(String.class);
final RuleImpl rule = new RuleImpl("rule-1");
final Pattern list1Pattern = new Pattern(0, list1ObjectType, "s1");
final Pattern list2Pattern = new Pattern(1, list2ObjectType, "s2");
rule.addPattern(list1Pattern);
rule.addPattern(list2Pattern);
final Declaration s1Declaration = rule.getDeclaration("s1");
final Declaration s2Declaration = rule.getDeclaration("s2");
this.values = new ArrayList();
rule.setConsequence(new Consequence() {
private static final long serialVersionUID = 510l;
public void evaluate(final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {
final String s1 = (String) knowledgeHelper.get(s1Declaration);
final String s2 = (String) knowledgeHelper.get(s2Declaration);
CrossProductTest.this.values.add(new String[] { s1, s2 });
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
});
this.pkg = new KnowledgePackageImpl("org.drools");
this.pkg.addRule(rule);
}
use of org.drools.core.definitions.impl.KnowledgePackageImpl 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.impl.KnowledgePackageImpl in project drools by kiegroup.
the class KiePackagesBuilder method getClassObjectType.
private ObjectType getClassObjectType(Class<?> patternClass) {
return objectTypeCache.computeIfAbsent(patternClass.getCanonicalName(), name -> {
boolean isEvent = false;
if (!name.startsWith("java.") && !patternClass.isPrimitive()) {
KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(patternClass.getPackage().getName(), this::createKiePackage);
TypeDeclaration typeDeclaration = pkg.getTypeDeclaration(patternClass);
if (typeDeclaration == null) {
typeDeclaration = createTypeDeclaration(patternClass);
pkg.addTypeDeclaration(typeDeclaration);
}
isEvent = typeDeclaration.getRole() == Role.Type.EVENT;
}
return new ClassObjectType(patternClass, isEvent);
});
}
use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.
the class KnowledgeBuilderImpl method createPackageRegistry.
private PackageRegistry createPackageRegistry(PackageDescr packageDescr) {
initPackage(packageDescr);
InternalKnowledgePackage pkg;
if (this.kBase == null || (pkg = this.kBase.getPackage(packageDescr.getName())) == null) {
// there is no rulebase or it does not define this package so define it
pkg = new KnowledgePackageImpl(packageDescr.getName());
pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(this.rootClassLoader));
// if there is a rulebase then add the package.
if (this.kBase != null) {
try {
pkg = (InternalKnowledgePackage) this.kBase.addPackage(pkg).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
} else {
// the RuleBase will also initialise the
pkg.getDialectRuntimeRegistry().onAdd(this.rootClassLoader);
}
}
PackageRegistry pkgRegistry = new PackageRegistry(rootClassLoader, configuration, pkg);
// add default import for this namespace
pkgRegistry.addImport(new ImportDescr(packageDescr.getNamespace() + ".*"));
for (ImportDescr importDescr : packageDescr.getImports()) {
pkgRegistry.registerImport(importDescr.getTarget());
}
return pkgRegistry;
}
Aggregations