Search in sources :

Example 1 with ImportDeclaration

use of org.drools.core.rule.ImportDeclaration in project drools by kiegroup.

the class KnowledgeBaseImpl method mergePackage.

/**
 * Merge a new package with an existing package.
 * Most of the work is done by the concrete implementations,
 * but this class does some work (including combining imports, compilation data, globals,
 * and the actual Rule objects into the package).
 */
private void mergePackage(InternalKnowledgePackage pkg, InternalKnowledgePackage newPkg) {
    // Merge imports
    final Map<String, ImportDeclaration> imports = pkg.getImports();
    imports.putAll(newPkg.getImports());
    // Merge static imports
    for (String staticImport : newPkg.getStaticImports()) {
        pkg.addStaticImport(staticImport);
    }
    String lastIdent = null;
    String lastType = null;
    try {
        // merge globals
        if (newPkg.getGlobals() != null && newPkg.getGlobals() != Collections.EMPTY_MAP) {
            Map<String, String> globals = pkg.getGlobals();
            // Add globals
            for (final Map.Entry<String, String> entry : newPkg.getGlobals().entrySet()) {
                final String identifier = entry.getKey();
                final String type = entry.getValue();
                lastIdent = identifier;
                lastType = type;
                if (globals.containsKey(identifier) && !globals.get(identifier).equals(type)) {
                    throw new RuntimeException(pkg.getName() + " cannot be integrated");
                } else {
                    pkg.addGlobal(identifier, this.rootClassLoader.loadClass(type));
                    // this isn't a package merge, it's adding to the rulebase, but I've put it here for convienience
                    addGlobal(identifier, this.rootClassLoader.loadClass(type));
                }
            }
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Unable to resolve class '" + lastType + "' for global '" + lastIdent + "'");
    }
    // merge entry point declarations
    if (newPkg.getEntryPointIds() != null) {
        for (String ep : newPkg.getEntryPointIds()) {
            pkg.addEntryPointId(ep);
        }
    }
    // merge the type declarations
    if (newPkg.getTypeDeclarations() != null) {
        // add type declarations
        for (TypeDeclaration type : newPkg.getTypeDeclarations().values()) {
            // @TODO should we allow overrides? only if the class is not in use.
            if (!pkg.getTypeDeclarations().containsKey(type.getTypeName())) {
                // add to package list of type declarations
                pkg.addTypeDeclaration(type);
            }
        }
    }
    // merge window declarations
    if (newPkg.getWindowDeclarations() != null) {
        // add window declarations
        for (WindowDeclaration window : newPkg.getWindowDeclarations().values()) {
            if (!pkg.getWindowDeclarations().containsKey(window.getName()) || pkg.getWindowDeclarations().get(window.getName()).equals(window)) {
                pkg.addWindowDeclaration(window);
            } else {
                throw new RuntimeException("Unable to merge two conflicting window declarations for window named: " + window.getName());
            }
        }
    }
    // Merge rules into the RuleBase package
    // as this is needed for individual rule removal later on
    List<RuleImpl> rulesToBeRemoved = new ArrayList<RuleImpl>();
    for (Rule newRule : newPkg.getRules()) {
        // remove the rule if it already exists
        RuleImpl oldRule = pkg.getRule(newRule.getName());
        if (oldRule != null) {
            rulesToBeRemoved.add(oldRule);
        }
    }
    if (!rulesToBeRemoved.isEmpty()) {
        removeRules(rulesToBeRemoved);
    }
    for (Rule newRule : newPkg.getRules()) {
        pkg.addRule((RuleImpl) newRule);
    }
    // Merge The Rule Flows
    if (newPkg.getRuleFlows() != null) {
        for (Process flow : newPkg.getRuleFlows().values()) {
            pkg.addProcess(flow);
        }
    }
    if (!newPkg.getResourceTypePackages().isEmpty()) {
        for (ResourceTypePackage rtkKpg : newPkg.getResourceTypePackages().values()) {
            ResourceType rt = rtkKpg.getResourceType();
            KieWeavers weavers = ServiceRegistry.getInstance().get(KieWeavers.class);
            KieWeaverService weaver = weavers.getWeavers().get(rt);
            weaver.merge(this, pkg, rtkKpg);
        }
    }
}
Also used : KieWeaverService(org.kie.api.internal.weaver.KieWeaverService) ArrayList(java.util.ArrayList) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Process(org.kie.api.definition.process.Process) ResourceType(org.kie.api.io.ResourceType) KieWeavers(org.kie.api.internal.weaver.KieWeavers) WindowDeclaration(org.drools.core.rule.WindowDeclaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) Rule(org.kie.api.definition.rule.Rule) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 2 with ImportDeclaration

use of org.drools.core.rule.ImportDeclaration 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();
}
Also used : Pattern(org.drools.core.rule.Pattern) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) Person(org.drools.compiler.Person) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 3 with ImportDeclaration

use of org.drools.core.rule.ImportDeclaration 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();
}
Also used : Pattern(org.drools.core.rule.Pattern) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) Person(org.drools.compiler.Person) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 4 with ImportDeclaration

use of org.drools.core.rule.ImportDeclaration in project drools by kiegroup.

the class KnowledgeBaseImpl method mergePackage.

/**
 * Merge a new package with an existing package.
 * Most of the work is done by the concrete implementations,
 * but this class does some work (including combining imports, compilation data, globals,
 * and the actual Rule objects into the package).
 */
private void mergePackage(InternalKnowledgePackage pkg, InternalKnowledgePackage newPkg, Collection<InternalWorkingMemory> workingMemories) {
    // Merge imports
    final Map<String, ImportDeclaration> imports = pkg.getImports();
    imports.putAll(newPkg.getImports());
    // Merge static imports
    for (String staticImport : newPkg.getStaticImports()) {
        pkg.addStaticImport(staticImport);
    }
    // merge globals
    if (newPkg.getGlobals() != null && !newPkg.getGlobals().isEmpty()) {
        Map<String, Class<?>> pkgGlobals = pkg.getGlobals();
        // Add globals
        for (final Map.Entry<String, Class<?>> entry : newPkg.getGlobals().entrySet()) {
            final String identifier = entry.getKey();
            final Class<?> type = entry.getValue();
            if (pkgGlobals.containsKey(identifier) && !pkgGlobals.get(identifier).equals(type)) {
                throw new RuntimeException(pkg.getName() + " cannot be integrated");
            } else {
                pkg.addGlobal(identifier, type);
                // this isn't a package merge, it's adding to the rulebase, but I've put it here for convienience
                addGlobal(identifier, type);
            }
        }
    }
    // merge entry point declarations
    if (newPkg.getEntryPointIds() != null) {
        for (String ep : newPkg.getEntryPointIds()) {
            pkg.addEntryPointId(ep);
        }
    }
    // merge the type declarations
    if (newPkg.getTypeDeclarations() != null) {
        // add type declarations
        for (TypeDeclaration type : newPkg.getTypeDeclarations().values()) {
            // @TODO should we allow overrides? only if the class is not in use.
            if (!pkg.getTypeDeclarations().containsKey(type.getTypeName())) {
                // add to package list of type declarations
                pkg.addTypeDeclaration(type);
            }
        }
    }
    // merge window declarations
    if (newPkg.getWindowDeclarations() != null) {
        // add window declarations
        for (WindowDeclaration window : newPkg.getWindowDeclarations().values()) {
            if (!pkg.getWindowDeclarations().containsKey(window.getName()) || pkg.getWindowDeclarations().get(window.getName()).equals(window)) {
                pkg.addWindowDeclaration(window);
            } else {
                throw new RuntimeException("Unable to merge two conflicting window declarations for window named: " + window.getName());
            }
        }
    }
    // Merge rules into the RuleBase package
    // as this is needed for individual rule removal later on
    List<RuleImpl> rulesToBeRemoved = new ArrayList<>();
    for (Rule newRule : newPkg.getRules()) {
        // remove the rule if it already exists
        RuleImpl oldRule = pkg.getRule(newRule.getName());
        if (oldRule != null) {
            rulesToBeRemoved.add(oldRule);
        }
    }
    if (!rulesToBeRemoved.isEmpty()) {
        kBaseInternal_removeRules(rulesToBeRemoved, workingMemories);
    }
    for (Rule newRule : newPkg.getRules()) {
        pkg.addRule((RuleImpl) newRule);
    }
    // Merge The Rule Flows
    if (newPkg.getRuleFlows() != null) {
        for (Process flow : newPkg.getRuleFlows().values()) {
            pkg.addProcess(flow);
        }
    }
    if (!newPkg.getResourceTypePackages().isEmpty()) {
        KieWeavers weavers = KieService.load(KieWeavers.class);
        if (weavers == null) {
            throw new IllegalStateException("Unable to find KieWeavers implementation");
        }
        for (ResourceTypePackage rtkKpg : newPkg.getResourceTypePackages().values()) {
            weavers.merge(pkg, rtkKpg);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Process(org.kie.api.definition.process.Process) KieWeavers(org.kie.api.internal.weaver.KieWeavers) WindowDeclaration(org.drools.core.rule.WindowDeclaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) Rule(org.kie.api.definition.rule.Rule) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 5 with ImportDeclaration

use of org.drools.core.rule.ImportDeclaration in project drools by kiegroup.

the class MVELConsequenceBuilderTest method setupTest.

private void setupTest(String consequence, Map<String, Object> namedConsequences) {
    builder = new MVELConsequenceBuilder();
    InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("org.drools.mvel.compiler.test");
    pkg.addImport(new ImportDeclaration(Cheese.class.getCanonicalName()));
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg, conf);
    ruleDescr = new RuleDescr("test consequence builder");
    ruleDescr.setConsequence(consequence);
    ruleDescr.addAttribute(new AttributeDescr("dialect", "mvel"));
    for (Entry<String, Object> entry : namedConsequences.entrySet()) {
        ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
    }
    RuleImpl rule = new RuleImpl(ruleDescr.getName());
    rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
    rule.addPattern(new Pattern(0, new ClassObjectType(Map.class), "$map"));
    PackageRegistry pkgRegistry = pkgBuilder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry reg = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    context = new RuleBuildContext(pkgBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
    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());
    pkgBuilder.compileAll();
    pkgBuilder.reloadAll();
    if (pkgBuilder.hasErrors()) {
        fail(pkgBuilder.getErrors().toString());
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) MVELConsequenceBuilder(org.drools.mvel.builder.MVELConsequenceBuilder) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleDescr(org.drools.drl.ast.descr.RuleDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

ImportDeclaration (org.drools.core.rule.ImportDeclaration)14 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)12 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)6 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)6 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)6 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)6 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)6 ClassObjectType (org.drools.core.base.ClassObjectType)6 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6 Pattern (org.drools.core.rule.Pattern)6 WindowDeclaration (org.drools.core.rule.WindowDeclaration)5 Declaration (org.drools.core.rule.Declaration)4 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)3 DroolsObjectInputStream (org.drools.core.common.DroolsObjectInputStream)3 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)3