use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.
the class KiePackagesBuilder method createKiePackage.
private KnowledgePackageImpl createKiePackage(String name) {
KnowledgePackageImpl kpkg = new KnowledgePackageImpl(name);
kpkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(configuration.getClassLoader()));
TypeResolver typeResolver = new ClassTypeResolver(new HashSet<>(kpkg.getImports().keySet()), configuration.getClassLoader(), name);
typeResolver.addImport(name + ".*");
kpkg.setTypeResolver(typeResolver);
return kpkg;
}
use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.
the class KiePackagesBuilder method build.
public CanonicalKiePackages build() {
for (Model model : models) {
for (TypeMetaData metaType : model.getTypeMetaDatas()) {
KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(metaType.getPackage(), this::createKiePackage);
pkg.addTypeDeclaration(createTypeDeclaration(pkg, metaType));
}
for (Global global : model.getGlobals()) {
KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(global.getPackage(), this::createKiePackage);
pkg.addGlobal(global.getName(), global.getType());
}
for (Query query : model.getQueries()) {
KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(query.getPackage(), this::createKiePackage);
pkg.addRule(compileQuery(pkg, query));
}
int ruleCounter = 0;
for (Rule rule : model.getRules()) {
KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(rule.getPackage(), this::createKiePackage);
RuleImpl ruleImpl = compileRule(pkg, rule);
ruleImpl.setLoadOrder(ruleCounter++);
pkg.addRule(ruleImpl);
}
}
return new CanonicalKiePackages(packages);
}
use of org.drools.core.definitions.impl.KnowledgePackageImpl 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.impl.KnowledgePackageImpl in project drools by kiegroup.
the class FactTemplateTest method testFacts.
@Test
public void testFacts() {
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[] fields1 = new FieldTemplate[] { cheeseName, cheesePrice };
final FactTemplate cheese1 = new FactTemplateImpl(pkg, "Cheese", fields1);
final Fact stilton1 = cheese1.createFact(10);
stilton1.setFieldValue("name", "stilton");
stilton1.setFieldValue("price", new Integer(200));
final Fact stilton2 = cheese1.createFact(11);
stilton2.setFieldValue(0, "stilton");
stilton2.setFieldValue(1, new Integer(200));
assertEquals(stilton1, stilton2);
assertEquals(stilton1.hashCode(), stilton2.hashCode());
final Fact brie = cheese1.createFact(12);
brie.setFieldValue("name", "brie");
brie.setFieldValue("price", new Integer(55));
assertFalse(stilton1.equals(brie));
assertFalse(stilton1.hashCode() == brie.hashCode());
}
use of org.drools.core.definitions.impl.KnowledgePackageImpl in project drools by kiegroup.
the class FactTemplateTest method testFieldsAndGetters.
@Test
public void testFieldsAndGetters() {
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);
assertEquals("org.store", cheese.getPackage().getName());
assertEquals("Cheese", cheese.getName());
assertEquals(2, cheese.getNumberOfFields());
assertSame(fields, cheese.getAllFieldTemplates());
assertSame(cheeseName, cheese.getFieldTemplate(0));
assertSame(cheesePrice, cheese.getFieldTemplate(1));
assertSame(cheeseName, cheese.getFieldTemplate("name"));
assertSame(cheesePrice, cheese.getFieldTemplate("price"));
assertEquals(0, cheese.getFieldTemplateIndex("name"));
assertEquals(1, cheese.getFieldTemplateIndex("price"));
}
Aggregations