use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.
the class TypeDeclarationCache method buildTypeDeclarationInterfaces.
private boolean buildTypeDeclarationInterfaces(Class cls, Set<TypeDeclaration> tdecls) {
PackageRegistry pkgReg;
TypeDeclaration tdecl = this.cacheTypes.get((cls.getName()));
if (tdecl == null) {
pkgReg = kbuilder.getPackageRegistry(ClassUtils.getPackage(cls));
if (pkgReg != null) {
tdecl = pkgReg.getPackage().getTypeDeclaration(cls.getSimpleName());
}
}
if (tdecl != null) {
if (!tdecls.add(tdecl)) {
// the interface already exists, return to stop recursion
return false;
}
}
Class<?>[] intfs = cls.getInterfaces();
for (Class<?> intf : intfs) {
pkgReg = kbuilder.getPackageRegistry(ClassUtils.getPackage(intf));
if (pkgReg != null) {
tdecl = pkgReg.getPackage().getTypeDeclaration(intf.getSimpleName());
}
if (tdecl != null) {
tdecls.add(tdecl);
}
}
for (Class<?> intf : intfs) {
if (!buildTypeDeclarationInterfaces(intf, tdecls)) {
return false;
}
}
return true;
}
use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.
the class TypeDeclarationNameResolver method discoverHierarchyForRedeclarations.
private void discoverHierarchyForRedeclarations(TypeDeclarationDescr typeDescr, PackageDescr packageDescr, TypeResolver typeResolver) {
PackageRegistry pkReg = kbuilder.getPackageRegistry(packageDescr.getName());
Class typeClass = TypeDeclarationUtils.getExistingDeclarationClass(typeDescr, pkReg);
if (typeClass != null) {
if (typeDescr.isTrait()) {
fillStaticInterfaces(typeDescr, typeClass);
} else {
typeDescr.getSuperTypes().clear();
typeDescr.addSuperType(typeClass.isInterface() || typeClass == Object.class ? Object.class.getName() : typeClass.getSuperclass().getName());
}
} else {
// avoid to cache in the type resolver that this class doesn't exist
// since we may still look for it in the wrong package
typeResolver.registerClass(typeDescr.getFullTypeName(), null);
}
}
use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.
the class MVELConsequenceBuilderTest method testImperativeCodeError.
@Test
public void testImperativeCodeError() throws Exception {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
ruleDescr.setConsequence("if (cheese.price == 10) { cheese.price = 5; }");
Properties properties = new Properties();
properties.setProperty("drools.dialect.default", "mvel");
KnowledgeBuilderConfigurationImpl cfg1 = new KnowledgeBuilderConfigurationImpl(properties);
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg, cfg1);
PackageRegistry pkgRegistry = pkgBuilder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect(pkgRegistry.getDialect());
final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final ObjectType cheeseObjeectType = new ClassObjectType(Cheese.class);
final Pattern pattern = new Pattern(0, cheeseObjeectType);
final PatternExtractor extractor = new PatternExtractor(cheeseObjeectType);
final Declaration declaration = new Declaration("cheese", extractor, pattern);
final Map<String, Declaration> map = new HashMap<String, Declaration>();
map.put("cheese", declaration);
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
final MVELConsequenceBuilder builder = new MVELConsequenceBuilder();
builder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
final AgendaItem item = new AgendaItemImpl(0, tuple, 10, null, null, null);
final DefaultKnowledgeHelper kbHelper = new DefaultKnowledgeHelper(ksession);
kbHelper.setActivation(item);
try {
((MVELConsequence) context.getRule().getConsequence()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
context.getRule().getConsequence().evaluate(kbHelper, ksession);
fail("should throw an exception, as 'if' is not allowed");
} catch (Exception e) {
}
assertEquals(10, cheddar.getPrice());
}
use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.
the class JavaConsequenceBuilderPRAlwaysTest method setupTest.
private void setupTest(String consequence, Map<String, Object> namedConsequences) {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
pkg.addImport(new ImportDeclaration("org.drools.compiler.Cheese"));
KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
// Although it should be the default, for completeness we explicit this is the test cases how RHS should be rewritten as:
conf.setOption(PropertySpecificOption.ALWAYS);
KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl(pkg, conf);
ruleDescr = new RuleDescr("test consequence builder");
ruleDescr.setConsequence(consequence);
for (Entry<String, Object> entry : namedConsequences.entrySet()) {
ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
}
RuleImpl rule = ruleDescr.toRule();
PackageRegistry pkgRegistry = kBuilder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry reg = kBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
context = new RuleBuildContext(kBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
Pattern p = new Pattern(1, new ClassObjectType(Person.class), "$persone");
Declaration declr = p.addDeclaration("age");
final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, new BindingDescr("age", "age"), p, "age", declr, true);
rule.addPattern(p);
context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
for (String name : namedConsequences.keySet()) {
context.getDialect().getConsequenceBuilder().build(context, name);
}
context.getDialect().addRule(context);
pkgRegistry.getPackage().addRule(context.getRule());
kBuilder.compileAll();
kBuilder.reloadAll();
}
use of org.drools.compiler.compiler.PackageRegistry in project drools by kiegroup.
the class JavaConsequenceBuilderTest method setupTest.
private void setupTest(String consequence, Map<String, Object> namedConsequences) {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
pkg.addImport(new ImportDeclaration("org.drools.compiler.Cheese"));
KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
// this test was originally intended with PropertyReactive.ALLOWED:
conf.setOption(PropertySpecificOption.ALLOWED);
KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl(pkg, conf);
ruleDescr = new RuleDescr("test consequence builder");
ruleDescr.setConsequence(consequence);
for (Entry<String, Object> entry : namedConsequences.entrySet()) {
ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
}
RuleImpl rule = ruleDescr.toRule();
PackageRegistry pkgRegistry = kBuilder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry reg = kBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
context = new RuleBuildContext(kBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
Pattern p = new Pattern(1, new ClassObjectType(Person.class), "$persone");
Declaration declr = p.addDeclaration("age");
final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, new BindingDescr("age", "age"), p, "age", declr, true);
rule.addPattern(p);
context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
for (String name : namedConsequences.keySet()) {
context.getDialect().getConsequenceBuilder().build(context, name);
}
context.getDialect().addRule(context);
pkgRegistry.getPackage().addRule(context.getRule());
kBuilder.compileAll();
kBuilder.reloadAll();
}
Aggregations