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 = CoreComponentFactory.get().createKnowledgePackage("org.droos.test");
pkg.setClassLoader(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, ReteEvaluator reteEvaluator) 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 ScenarioTest 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 PhreakNotNodeTest 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 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 MarshallingKieMetaInfoBuilder method generateKieModuleMetaInfo.
public KieModuleMetaInfo generateKieModuleMetaInfo(ResourceStore trgMfs) {
Map<String, TypeMetaInfo> typeInfos = new HashMap<>();
Map<String, Set<String>> rulesPerPackage = new HashMap<>();
KieModuleModel kieModuleModel = kModule.getKieModuleModel();
for (String kieBaseName : kieModuleModel.getKieBaseModels().keySet()) {
KnowledgeBuilder kBuilder = kModule.getKnowledgeBuilderForKieBase(kieBaseName);
KieModuleCache.KModuleCache.Builder _kmoduleCacheBuilder = createCacheBuilder();
KieModuleCache.CompilationData.Builder _compData = createCompilationData();
for (KiePackage kPkg : kBuilder.getKnowledgePackages()) {
PackageRegistry pkgRegistry = ((InternalKnowledgeBuilder) kBuilder).getPackageRegistry(kPkg.getName());
JavaDialectRuntimeData runtimeData = (JavaDialectRuntimeData) pkgRegistry.getDialectRuntimeRegistry().getDialectData("java");
List<String> types = new ArrayList<>();
for (FactType factType : kPkg.getFactTypes()) {
Class<?> typeClass = ((ClassDefinition) factType).getDefinedClass();
TypeDeclaration typeDeclaration = pkgRegistry.getPackage().getTypeDeclaration(typeClass);
if (typeDeclaration != null) {
typeInfos.put(typeClass.getName(), new TypeMetaInfo(typeDeclaration));
}
String className = factType.getName();
String internalName = className.replace('.', '/') + ".class";
if (trgMfs != null) {
byte[] bytes = runtimeData.getBytecode(internalName);
if (bytes != null) {
trgMfs.write(internalName, bytes, true);
}
}
types.add(internalName);
}
Set<String> rules = rulesPerPackage.get(kPkg.getName());
if (rules == null) {
rules = new HashSet<>();
}
for (Rule rule : kPkg.getRules()) {
rules.add(rule.getName());
}
if (!rules.isEmpty()) {
rulesPerPackage.put(kPkg.getName(), rules);
}
addToCompilationData(_compData, runtimeData, types);
}
_kmoduleCacheBuilder.addCompilationData(_compData.build());
if (trgMfs != null) {
writeCompilationDataToTrg(_kmoduleCacheBuilder.build(), kieBaseName, trgMfs);
}
}
return new KieModuleMetaInfo(typeInfos, rulesPerPackage);
}
Aggregations