Search in sources :

Example 86 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class MVELConsequenceBuilderTest method setupTest.

private void setupTest(String consequence, Map<String, Object> namedConsequences) {
    builder = new MVELConsequenceBuilder();
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools.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) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 87 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class ScenarioTest method createContext.

public BuildContext createContext() {
    RuleBaseConfiguration conf = new RuleBaseConfiguration();
    KnowledgeBaseImpl rbase = new KnowledgeBaseImpl("ID", conf);
    BuildContext buildContext = new BuildContext(rbase);
    RuleImpl rule = new RuleImpl("rule1").setPackage("org.pkg1");
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
    pkg.getDialectRuntimeRegistry().setDialectData("mvel", new MVELDialectRuntimeData());
    pkg.addRule(rule);
    buildContext.setRule(rule);
    return buildContext;
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 88 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class JavaAccumulateBuilderTest method testBuildRuleBuildContextBaseDescr.

@Test
public void testBuildRuleBuildContextBaseDescr() {
    // $total : Integer() from accumulate( Cheese( $price : price ) init( int x = 0; ) action( x += $price ) result( new Integer( x ) ) )
    AccumulateDescr accumDescr = new AccumulateDescr();
    BindingDescr price = new BindingDescr("$price", "price");
    PatternDescr cheeseDescr = new PatternDescr("org.drools.compiler.Cheese");
    cheeseDescr.addConstraint(price);
    accumDescr.setInputPattern(cheeseDescr);
    accumDescr.setInitCode("int x = 0; int y = 0;");
    accumDescr.setActionCode("x += $price;");
    accumDescr.setResultCode("new Integer( x )");
    // org.drools.core.rule.Package pkg = new org.kie.rule.Package( "org.kie" );
    final KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
    pkgBuilder.addPackage(new PackageDescr("org.drools"));
    final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
    PackageRegistry pkgReg = pkgBuilder.getPackageRegistry("org.drools");
    InternalKnowledgePackage pkg = pkgReg.getPackage();
    DialectCompiletimeRegistry dialectRegistry = pkgReg.getDialectCompiletimeRegistry();
    Dialect dialect = dialectRegistry.getDialect("java");
    RuleDescr ruleDescr = new RuleDescr("test rule");
    RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, dialect);
    Accumulate accumulate = (Accumulate) builder.build(context, accumDescr);
    String generatedCode = (String) context.getMethods().get(0);
    assertTrue(generatedCode.contains("private int x;"));
    assertTrue(generatedCode.contains("private int y;"));
    assertTrue(generatedCode.contains("x = 0;y = 0;"));
// System.out.println( context.getInvokers() );
// System.out.println( context.getMethods() );
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) Accumulate(org.drools.core.rule.Accumulate) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Dialect(org.drools.compiler.compiler.Dialect) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 89 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class JavaDialectBinaryEqualityTest method test1.

@Test
public void test1() {
    KiePackage pkg1 = getKnowledgePackage1();
    KiePackage pkg2 = getKnowledgePackage1();
    KiePackage pkg3 = getKnowledgePackage2();
    RuleImpl rule1 = ((InternalKnowledgePackage) pkg1).getRule("rule1");
    RuleImpl rule2 = ((InternalKnowledgePackage) pkg2).getRule("rule1");
    RuleImpl rule3 = ((InternalKnowledgePackage) pkg3).getRule("rule1");
    // test return value
    Pattern p1 = (Pattern) rule1.getLhs().getChildren().get(0);
    Constraint rvc1 = p1.getConstraints().get(0);
    Pattern p2 = (Pattern) rule2.getLhs().getChildren().get(0);
    Constraint rvc2 = p2.getConstraints().get(0);
    assertNotSame(rvc1, rvc2);
    assertEquals(rvc1, rvc2);
    Pattern p3 = (Pattern) rule3.getLhs().getChildren().get(0);
    Constraint rvc3 = p3.getConstraints().get(0);
    assertNotSame(rvc1, rvc3);
    assertThat(rvc1, not(equalTo(rvc3)));
    // test inline eval
    PredicateConstraint pc1 = getPredicateConstraint(p1);
    PredicateExpression pe1 = (PredicateExpression) pc1.getPredicateExpression();
    PredicateConstraint pc2 = getPredicateConstraint(p2);
    PredicateExpression pe2 = (PredicateExpression) pc2.getPredicateExpression();
    assertNotSame(pe1, pe2);
    assertEquals(pe1, pe2);
    PredicateConstraint pc3 = getPredicateConstraint(p3);
    PredicateExpression pe3 = (PredicateExpression) pc3.getPredicateExpression();
    assertNotSame(pe1, pe3);
    assertThat(pe1, not(equalTo(pe3)));
    // test eval
    EvalCondition ec1 = (EvalCondition) rule1.getLhs().getChildren().get(1);
    EvalExpression ee1 = (EvalExpression) ec1.getEvalExpression();
    EvalCondition ec2 = (EvalCondition) rule2.getLhs().getChildren().get(1);
    EvalExpression ee2 = (EvalExpression) ec2.getEvalExpression();
    assertNotSame(ee1, ee2);
    assertEquals(ee1, ee2);
    EvalCondition ec3 = (EvalCondition) rule3.getLhs().getChildren().get(1);
    EvalExpression ee3 = (EvalExpression) ec3.getEvalExpression();
    assertNotSame(ee1, ee3);
    assertThat(ee1, not(equalTo(ee3)));
    // test consequence
    assertNotSame(rule1.getConsequence(), rule2.getConsequence());
    assertEquals(rule1.getConsequence(), rule2.getConsequence());
    assertNotSame(rule1.getConsequence(), rule3.getConsequence());
    assertThat(rule1.getConsequence(), not(equalTo(rule3.getConsequence())));
    // check LHS equals
    assertNotSame(rule1.getLhs(), rule2.getLhs());
    assertEquals(rule1.getLhs(), rule2.getLhs());
    assertNotSame(rule1.getLhs(), rule3.getLhs());
    assertThat(rule1.getLhs(), not(equalTo(rule3.getLhs())));
}
Also used : EvalExpression(org.drools.core.spi.EvalExpression) Pattern(org.drools.core.rule.Pattern) KiePackage(org.kie.api.definition.KiePackage) Constraint(org.drools.core.spi.Constraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PredicateConstraint(org.drools.core.rule.PredicateConstraint) PredicateExpression(org.drools.core.spi.PredicateExpression) EvalCondition(org.drools.core.rule.EvalCondition) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 90 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class MVELAccumulator method init.

/* (non-Javadoc)
     * @see org.kie.spi.Accumulator#init(java.lang.Object, org.kie.spi.Tuple, org.kie.rule.Declaration[], org.kie.WorkingMemory)
     */
public void init(Object workingMemoryContext, Object context, Tuple tuple, Declaration[] declarations, WorkingMemory workingMemory) throws Exception {
    Object[] localVars = new Object[initUnit.getOtherIdentifiers().length];
    MVELAccumulatorFactoryContext factoryContext = (MVELAccumulatorFactoryContext) workingMemoryContext;
    VariableResolverFactory factory = factoryContext.getInitFactory();
    initUnit.updateFactory(null, tuple, localVars, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver(), factory);
    InternalKnowledgePackage pkg = workingMemory.getKnowledgeBase().getPackage("MAIN");
    if (pkg != null) {
        MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("mvel");
        factory.setNextFactory(data.getFunctionFactory());
    }
    MVELSafeHelper.getEvaluator().executeExpression(this.init, null, factory);
    DroolsVarFactory df = (DroolsVarFactory) factory.getNextFactory();
    if (localVars.length > 0) {
        for (int i = 0; i < df.getOtherVarsLength(); i++) {
            localVars[i] = factory.getIndexedVariableResolver(df.getOtherVarsPos() + i).getValue();
        }
    }
    ((MVELAccumulatorContext) context).setVariables(localVars);
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) DroolsVarFactory(org.drools.core.base.mvel.MVELCompilationUnit.DroolsVarFactory) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)97 Test (org.junit.Test)32 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)28 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)26 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)24 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)21 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)20 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)16 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)14 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 TypeDeclaration (org.drools.core.rule.TypeDeclaration)13 HashMap (java.util.HashMap)12 Pattern (org.drools.core.rule.Pattern)12 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)11 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)11 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)11 ClassObjectType (org.drools.core.base.ClassObjectType)10 ArrayList (java.util.ArrayList)9 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)9 ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)8