Search in sources :

Example 1 with JavaDialectRuntimeData

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

the class DeclaredClassBuilder method generateBeanFromDefinition.

public void generateBeanFromDefinition(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, PackageRegistry pkgRegistry, ClassDefinition def) {
    if (type.isNovel()) {
        String fullName = typeDescr.getType().getFullName();
        JavaDialectRuntimeData dialect = (JavaDialectRuntimeData) pkgRegistry.getDialectRuntimeRegistry().getDialectData("java");
        if (ensureJavaTypeConsistency(typeDescr, def, pkgRegistry.getTypeResolver())) {
            switch(type.getKind()) {
                case TRAIT:
                    try {
                        buildClass(def, fullName, dialect, this.kbuilder.getBuilderConfiguration().getClassBuilderFactory().getTraitBuilder(), pkgRegistry);
                    } catch (Exception e) {
                        e.printStackTrace();
                        this.kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to compile declared trait " + fullName + ": " + e.getMessage() + ";"));
                    }
                    break;
                case ENUM:
                    try {
                        buildClass(def, fullName, dialect, this.kbuilder.getBuilderConfiguration().getClassBuilderFactory().getEnumClassBuilder(), pkgRegistry);
                    } catch (Exception e) {
                        e.printStackTrace();
                        this.kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to compile declared enum " + fullName + ": " + e.getMessage() + ";"));
                    }
                    break;
                case CLASS:
                default:
                    try {
                        buildClass(def, fullName, dialect, this.kbuilder.getBuilderConfiguration().getClassBuilderFactory().getBeanClassBuilder(), pkgRegistry);
                    } catch (Exception e) {
                        e.printStackTrace();
                        this.kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to create a class for declared type " + fullName + ": " + e.getMessage() + ";"));
                    }
                    break;
            }
        }
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData)

Example 2 with JavaDialectRuntimeData

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);
}
Also used : RuleInvokerErrorHandler(org.drools.compiler.builder.impl.errors.RuleInvokerErrorHandler) Wireable(org.drools.core.spi.Wireable) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) RuleClassBuilder(org.drools.compiler.rule.builder.RuleClassBuilder) LineMappings(org.drools.core.rule.LineMappings) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) RuleErrorHandler(org.drools.compiler.builder.impl.errors.RuleErrorHandler) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 3 with JavaDialectRuntimeData

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 = new KnowledgePackageImpl("org.droos.test");
    pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(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, WorkingMemory workingMemory) 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.");
    }
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) Command(org.kie.api.command.Command) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) KieSession(org.kie.api.runtime.KieSession) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 4 with JavaDialectRuntimeData

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

the class SegmentPropagationTest 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;
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 5 with JavaDialectRuntimeData

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

the class PhreakJoinNodeTest 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;
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

JavaDialectRuntimeData (org.drools.core.rule.JavaDialectRuntimeData)28 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)13 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)11 HashMap (java.util.HashMap)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)6 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)5 Test (org.junit.Test)5 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)4 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)4 Consequence (org.drools.core.spi.Consequence)4 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)4 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)3 TypeDeclaration (org.drools.core.rule.TypeDeclaration)3 Wireable (org.drools.core.spi.Wireable)3 Process (org.kie.api.definition.process.Process)3 KieSession (org.kie.api.runtime.KieSession)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2