use of org.drools.drl.ast.descr.GlobalDescr in project drools by kiegroup.
the class XmlDumper method processGlobalsList.
private String processGlobalsList(final List globals) {
String globalList = "";
for (final Iterator iterator = globals.iterator(); iterator.hasNext(); ) {
final GlobalDescr global = (GlobalDescr) iterator.next();
final String identifier = global.getIdentifier();
final String type = global.getType();
final String globalTemplate = "<global identifier=\"" + identifier + "\" type=\"" + type + "\" />" + XmlDumper.eol;
globalList += globalTemplate;
}
return globalList + XmlDumper.eol;
}
use of org.drools.drl.ast.descr.GlobalDescr in project drools by kiegroup.
the class ModelBuilderImpl method addPackage.
@Override
public void addPackage(final PackageDescr packageDescr) {
if (compositePackagesMap == null) {
compositePackagesMap = new HashMap<>();
}
CompositePackageDescr pkgDescr = compositePackagesMap.get(packageDescr.getNamespace());
if (pkgDescr == null) {
compositePackagesMap.put(packageDescr.getNamespace(), new CompositePackageDescr(packageDescr.getResource(), packageDescr));
} else {
pkgDescr.addPackageDescr(packageDescr.getResource(), packageDescr);
}
PackageRegistry pkgRegistry = getOrCreatePackageRegistry(packageDescr);
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
for (final ImportDescr importDescr : packageDescr.getImports()) {
pkgRegistry.addImport(importDescr);
}
for (GlobalDescr globalDescr : packageDescr.getGlobals()) {
try {
Class<?> globalType = pkg.getTypeResolver().resolveType(globalDescr.getType());
addGlobal(globalDescr.getIdentifier(), globalType);
pkg.addGlobal(globalDescr.getIdentifier(), globalType);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
use of org.drools.drl.ast.descr.GlobalDescr in project drools by kiegroup.
the class ImpactModelBuilderImpl method addPackage.
@Override
public void addPackage(final PackageDescr packageDescr) {
if (compositePackagesMap == null) {
compositePackagesMap = new HashMap<>();
if (compositePackages != null) {
for (CompositePackageDescr pkg : compositePackages) {
compositePackagesMap.put(pkg.getNamespace(), pkg);
}
} else {
compositePackagesMap.put(packageDescr.getNamespace(), new CompositePackageDescr(packageDescr.getResource(), packageDescr));
}
compositePackages = null;
}
CompositePackageDescr pkgDescr = compositePackagesMap.get(packageDescr.getNamespace());
if (pkgDescr == null) {
compositePackagesMap.put(packageDescr.getNamespace(), new CompositePackageDescr(packageDescr.getResource(), packageDescr));
} else {
pkgDescr.addPackageDescr(packageDescr.getResource(), packageDescr);
}
PackageRegistry pkgRegistry = getOrCreatePackageRegistry(packageDescr);
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
for (final ImportDescr importDescr : packageDescr.getImports()) {
pkgRegistry.addImport(importDescr);
}
for (GlobalDescr globalDescr : packageDescr.getGlobals()) {
try {
Class<?> globalType = pkg.getTypeResolver().resolveType(globalDescr.getType());
addGlobal(globalDescr.getIdentifier(), globalType);
pkg.addGlobal(globalDescr.getIdentifier(), globalType);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
use of org.drools.drl.ast.descr.GlobalDescr in project drools by kiegroup.
the class KiePMMLDescrFactoryTest method checkGlobals.
private void checkGlobals(List<GlobalDescr> toCheck) {
assertEquals(2, toCheck.size());
GlobalDescr retrieved = toCheck.get(0);
assertEquals(PMML4_RESULT_IDENTIFIER, retrieved.getIdentifier());
assertEquals(PMML4_RESULT, retrieved.getType());
retrieved = toCheck.get(1);
assertEquals(OUTPUTFIELDS_MAP_IDENTIFIER, retrieved.getIdentifier());
assertEquals(OUTPUTFIELDS_MAP, retrieved.getType());
}
use of org.drools.drl.ast.descr.GlobalDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testReload.
@Test
public void testReload() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
ruleDescr.setConsequence("map.put(\"value\", new Integer(1) );");
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
RuleImpl rule = pkg.getRule("rule-1");
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addGlobal("map", Map.class);
final KieSession workingMemory = kBase.newKieSession();
final HashMap map = new HashMap();
workingMemory.setGlobal("map", map);
final LeftTupleImpl tuple = new MockTuple(new HashMap());
tuple.setLeftTupleSink(new RuleTerminalNode(1, new MockBetaNode(), rule, rule.getLhs(), 0, new BuildContext(kBase, Collections.emptyList())));
final Activation activation = new MockActivation(rule, 0, rule.getLhs(), tuple);
DefaultKnowledgeHelper knowledgeHelper = new DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
knowledgeHelper.setActivation(activation);
rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
assertEquals(new Integer(1), map.get("value"));
ruleDescr.setConsequence("map.put(\"value\", new Integer(2) );");
pkg.removeRule(rule);
// Make sure the compiled classes are also removed
assertEquals(0, ((JavaDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("java")).getStore().size());
builder.addPackage(packageDescr);
pkg = builder.getPackage(packageDescr.getName());
rule = pkg.getRule("rule-1");
knowledgeHelper = new DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
knowledgeHelper.setActivation(activation);
rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
assertEquals(new Integer(2), map.get("value"));
}
Aggregations