Search in sources :

Example 11 with GlobalDescr

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;
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) Iterator(java.util.Iterator)

Example 12 with GlobalDescr

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);
        }
    }
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 13 with GlobalDescr

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);
        }
    }
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 14 with GlobalDescr

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());
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr)

Example 15 with GlobalDescr

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"));
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) HashMap(java.util.HashMap) AndDescr(org.drools.drl.ast.descr.AndDescr) DefaultKnowledgeHelper(org.drools.kiesession.consequence.DefaultKnowledgeHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Activation(org.drools.core.spi.Activation) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) MockBetaNode(org.drools.mvel.MockBetaNode) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Aggregations

GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)24 PackageDescr (org.drools.drl.ast.descr.PackageDescr)14 RuleDescr (org.drools.drl.ast.descr.RuleDescr)13 Test (org.junit.Test)12 AndDescr (org.drools.drl.ast.descr.AndDescr)10 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 PatternDescr (org.drools.drl.ast.descr.PatternDescr)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)6 BindingDescr (org.drools.drl.ast.descr.BindingDescr)6 Cheese (org.drools.mvel.compiler.Cheese)6 List (java.util.List)5 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)5 FunctionDescr (org.drools.drl.ast.descr.FunctionDescr)5 InputStreamReader (java.io.InputStreamReader)4 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)4 ImportDescr (org.drools.drl.ast.descr.ImportDescr)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 Iterator (java.util.Iterator)2