Search in sources :

Example 11 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 = CoreComponentFactory.get().createKnowledgePackage("org.drools");
    pkg.addImport(new ImportDeclaration("org.drools.mvel.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 = descrToRule(ruleDescr);
    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.drl.ast.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.drl.ast.descr.RuleDescr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) Person(org.drools.mvel.compiler.Person) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 12 with ImportDeclaration

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

the class KnowledgePackageImpl method readExternal.

/**
 * Handles the read serialization of the Package. Patterns in Rules may
 * reference generated data which cannot be serialized by default methods.
 * The Package uses PackageCompilationData to hold a reference to the
 * generated bytecode; which must be restored before any Rules. A custom
 * ObjectInputStream, able to resolve classes against the bytecode in the
 * PackageCompilationData, is used to restore the Rules.
 *
 * @param stream, the stream to read data from in order to restore the object;
 *                should be an instance of DroolsObjectInputStream or
 *                InputStream
 */
public void readExternal(ObjectInput stream) throws IOException, ClassNotFoundException {
    boolean isDroolsStream = stream instanceof DroolsObjectInputStream;
    DroolsObjectInputStream in = isDroolsStream ? (DroolsObjectInputStream) stream : new DroolsObjectInputStream(new ByteArrayInputStream((byte[]) stream.readObject()));
    this.name = (String) in.readObject();
    this.dialectRuntimeRegistry = (DialectRuntimeRegistry) in.readObject();
    this.typeDeclarations = (Map) in.readObject();
    this.imports = (Map<String, ImportDeclaration>) in.readObject();
    this.staticImports = (Set) in.readObject();
    this.functions = (Map<String, Function>) in.readObject();
    this.accumulateFunctions = (Map<String, AccumulateFunction>) in.readObject();
    this.factTemplates = (Map) in.readObject();
    this.globals = (Map<String, Class<?>>) in.readObject();
    this.valid = in.readBoolean();
    this.needStreamMode = in.readBoolean();
    this.rules = (Map<String, RuleImpl>) in.readObject();
    this.entryPointsIds = (Set<String>) in.readObject();
    this.windowDeclarations = (Map<String, WindowDeclaration>) in.readObject();
    this.resourceTypePackages = (ResourceTypePackageRegistry) in.readObject();
    if (!isDroolsStream) {
        in.close();
    }
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Function(org.drools.core.rule.Function) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) ByteArrayInputStream(java.io.ByteArrayInputStream) WindowDeclaration(org.drools.core.rule.WindowDeclaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction)

Example 13 with ImportDeclaration

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

the class TraitKnowledgePackageImpl method readExternal.

@Override
public void readExternal(ObjectInput stream) throws IOException, ClassNotFoundException {
    boolean isDroolsStream = stream instanceof DroolsObjectInputStream;
    DroolsObjectInputStream in = isDroolsStream ? (DroolsObjectInputStream) stream : new DroolsObjectInputStream(new ByteArrayInputStream((byte[]) stream.readObject()));
    this.name = (String) in.readObject();
    this.classFieldAccessorStore = (ClassFieldAccessorStore) in.readObject();
    in.setStore(this.classFieldAccessorStore);
    this.dialectRuntimeRegistry = (DialectRuntimeRegistry) in.readObject();
    this.typeDeclarations = (Map) in.readObject();
    this.imports = (Map<String, ImportDeclaration>) in.readObject();
    this.staticImports = (Set) in.readObject();
    this.functions = (Map<String, Function>) in.readObject();
    this.accumulateFunctions = (Map<String, AccumulateFunction>) in.readObject();
    this.factTemplates = (Map) in.readObject();
    this.globals = (Map<String, Class<?>>) in.readObject();
    this.valid = in.readBoolean();
    this.needStreamMode = in.readBoolean();
    this.rules = (Map<String, RuleImpl>) in.readObject();
    this.entryPointsIds = (Set<String>) in.readObject();
    this.windowDeclarations = (Map<String, WindowDeclaration>) in.readObject();
    this.traitRegistry = (TraitRegistryImpl) in.readObject();
    this.resourceTypePackages = (ResourceTypePackageRegistry) in.readObject();
    in.setStore(null);
    if (!isDroolsStream) {
        in.close();
    }
}
Also used : DroolsObjectInputStream(org.drools.core.common.DroolsObjectInputStream) Function(org.drools.core.rule.Function) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) ByteArrayInputStream(java.io.ByteArrayInputStream) WindowDeclaration(org.drools.core.rule.WindowDeclaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction)

Example 14 with ImportDeclaration

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

the class JavaProcessClassBuilder method buildRule.

/* (non-Javadoc)
     * @see org.drools.core.rule.builder.dialect.java.RuleClassBuilder#buildRule(org.drools.core.rule.builder.BuildContext, org.drools.core.rule.builder.dialect.java.BuildUtils, RuleDescr)
     */
public String buildRule(final ProcessBuildContext context) {
    // If there is no compiled code, return
    if (context.getMethods().isEmpty()) {
        return null;
    }
    final String lineSeparator = System.getProperty("line.separator");
    final StringBuilder buffer = new StringBuilder();
    buffer.append("package " + context.getPkg().getName() + ";" + lineSeparator);
    for (ImportDeclaration decl : context.getPkg().getImports().values()) {
        buffer.append("import " + decl.getTarget() + ";" + lineSeparator);
    }
    for (String systemImport : systemImports) {
        if (!context.getPkg().getImports().containsKey(systemImport)) {
            buffer.append("import ").append(systemImport).append(";").append(lineSeparator);
        }
    }
    for (final Iterator it = context.getPkg().getStaticImports().iterator(); it.hasNext(); ) {
        buffer.append("import static " + it.next() + ";" + lineSeparator);
    }
    final ProcessDescr processDescr = context.getProcessDescr();
    buffer.append("public class " + StringUtils.ucFirst(processDescr.getClassName()) + " {" + lineSeparator);
    buffer.append("    private static final long serialVersionUID = 510l;" + lineSeparator);
    // @TODO record line numbers for each Action method
    for (int i = 0, size = context.getMethods().size(); i < size; i++) {
        buffer.append(context.getMethods().get(i) + lineSeparator);
    }
    final String[] lines = buffer.toString().split(lineSeparator);
    buffer.append("}");
    return buffer.toString();
}
Also used : ImportDeclaration(org.drools.core.rule.ImportDeclaration) Iterator(java.util.Iterator) ProcessDescr(org.drools.compiler.lang.descr.ProcessDescr)

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