use of org.drools.core.rule.JavaDialectRuntimeData in project drools by kiegroup.
the class DeclaredClassBuilder method generateBeanFromDefinition.
public void generateBeanFromDefinition(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, PackageRegistry pkgRegistry, ClassDefinition def) {
if (type.isNovel()) {
String fullName = typeDescr.getType().getFullName();
JavaDialectRuntimeData dialect = (JavaDialectRuntimeData) pkgRegistry.getDialectRuntimeRegistry().getDialectData("java");
if (ensureJavaTypeConsistency(typeDescr, def, pkgRegistry.getTypeResolver())) {
switch(type.getKind()) {
case TRAIT:
try {
buildClass(def, fullName, dialect, this.kbuilder.getBuilderConfiguration().getClassBuilderFactory().getTraitBuilder(), pkgRegistry);
} catch (Exception e) {
e.printStackTrace();
this.kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to compile declared trait " + fullName + ": " + e.getMessage() + ";"));
}
break;
case ENUM:
try {
buildClass(def, fullName, dialect, this.kbuilder.getBuilderConfiguration().getClassBuilderFactory().getEnumClassBuilder(), pkgRegistry);
} catch (Exception e) {
e.printStackTrace();
this.kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to compile declared enum " + fullName + ": " + e.getMessage() + ";"));
}
break;
case CLASS:
default:
try {
buildClass(def, fullName, dialect, this.kbuilder.getBuilderConfiguration().getClassBuilderFactory().getBeanClassBuilder(), pkgRegistry);
} catch (Exception e) {
e.printStackTrace();
this.kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to create a class for declared type " + fullName + ": " + e.getMessage() + ";"));
}
break;
}
}
}
}
use of org.drools.core.rule.JavaDialectRuntimeData in project drools by kiegroup.
the class JavaDialect method addRule.
/**
* This will add the rule for compiling later on.
* It will not actually call the compiler
*/
public void addRule(final RuleBuildContext context) {
final RuleImpl rule = context.getRule();
final RuleDescr ruleDescr = context.getRuleDescr();
RuleClassBuilder classBuilder = context.getDialect().getRuleClassBuilder();
String ruleClass = classBuilder.buildRule(context);
// return if there is no ruleclass name;
if (ruleClass == null) {
return;
}
// The compilation result is for the entire rule, so difficult to associate with any descr
addClassCompileTask(this.pkg.getName() + "." + ruleDescr.getClassName(), ruleDescr, ruleClass, this.src, new RuleErrorHandler(ruleDescr, rule, "Rule Compilation error"));
JavaDialectRuntimeData data = (JavaDialectRuntimeData) this.pkg.getDialectRuntimeRegistry().getDialectData(ID);
for (Map.Entry<String, String> invokers : context.getInvokers().entrySet()) {
final String className = invokers.getKey();
// Check if an invoker - returnvalue, predicate, eval or consequence has been associated
// If so we add it to the PackageCompilationData as it will get wired up on compilation
final Object invoker = context.getInvokerLookup(className);
if (invoker instanceof Wireable) {
data.putInvoker(className, (Wireable) invoker);
}
final String text = invokers.getValue();
final BaseDescr descr = context.getDescrLookup(className);
addClassCompileTask(className, descr, text, this.src, new RuleInvokerErrorHandler(descr, rule, "Unable to generate rule invoker."));
}
// setup the line mappins for this rule
final String name = this.pkg.getName() + "." + StringUtils.ucFirst(ruleDescr.getClassName());
final LineMappings mapping = new LineMappings(name);
mapping.setStartLine(ruleDescr.getConsequenceLine());
mapping.setOffset(ruleDescr.getConsequenceOffset());
this.pkg.getDialectRuntimeRegistry().getLineMappings().put(name, mapping);
}
use of org.drools.core.rule.JavaDialectRuntimeData in project drools by kiegroup.
the class DisposeCommandPublicAPITest method testDisposeCommand.
@Test
public void testDisposeCommand() {
InternalKnowledgeBase kBase;
RuleImpl rule;
InternalKnowledgePackage pkg;
kBase = KnowledgeBaseFactory.newKnowledgeBase();
pkg = new KnowledgePackageImpl("org.droos.test");
pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
JavaDialectRuntimeData data = new JavaDialectRuntimeData();
data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
pkg.getDialectRuntimeRegistry().setDialectData("java", data);
rule = new RuleImpl("Test");
rule.setDialect("java");
rule.setConsequence(new Consequence() {
public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
}
public String getName() {
return "default";
}
});
pkg.addRule(rule);
kBase.addPackage(pkg);
KieSession session = kBase.newKieSession();
Command dispose = KieServices.Factory.get().getCommands().newDispose();
session.insert("whatever");
session.fireAllRules();
session.execute(dispose);
try {
session.insert("whatever");
} catch (Exception e) {
Assert.assertEquals(e.getMessage(), "Illegal method call. This session was previously disposed.");
}
}
use of org.drools.core.rule.JavaDialectRuntimeData in project drools by kiegroup.
the class SegmentPropagationTest method createContext.
public BuildContext createContext() {
RuleBaseConfiguration conf = new RuleBaseConfiguration();
KnowledgeBaseImpl rbase = new KnowledgeBaseImpl("ID", conf);
BuildContext buildContext = new BuildContext(rbase, Collections.emptyList());
RuleImpl rule = new RuleImpl("rule1").setPackage("org.pkg1");
InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("org.pkg1");
pkg.getDialectRuntimeRegistry().setDialectData("java", new JavaDialectRuntimeData());
pkg.addRule(rule);
buildContext.setRule(rule);
return buildContext;
}
use of org.drools.core.rule.JavaDialectRuntimeData in project drools by kiegroup.
the class PhreakJoinNodeTest method createContext.
public BuildContext createContext() {
RuleBaseConfiguration conf = new RuleBaseConfiguration();
KnowledgeBaseImpl rbase = new KnowledgeBaseImpl("ID", conf);
BuildContext buildContext = new BuildContext(rbase, Collections.emptyList());
RuleImpl rule = new RuleImpl("rule1").setPackage("org.pkg1");
InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("org.pkg1");
pkg.getDialectRuntimeRegistry().setDialectData("java", new JavaDialectRuntimeData());
pkg.addRule(rule);
buildContext.setRule(rule);
return buildContext;
}
Aggregations