Search in sources :

Example 46 with InternalKnowledgePackage

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

the class KnowledgeBuilderImpl method compileRete.

protected void compileRete(PackageDescr packageDescr) {
    if (!hasErrors() && this.kBase != null) {
        Collection<RuleImpl> rulesToBeAdded = new ArrayList<>();
        for (RuleDescr ruleDescr : packageDescr.getRules()) {
            if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
                InternalKnowledgePackage pkg = pkgRegistryMap.get(ruleDescr.getNamespace()).getPackage();
                rulesToBeAdded.add(pkg.getRule(ruleDescr.getName()));
            }
        }
        if (!rulesToBeAdded.isEmpty()) {
            this.kBase.addRules(rulesToBeAdded);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 47 with InternalKnowledgePackage

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

the class DefaultTemplateRuleBaseTest method testSimpleTemplate.

@SuppressWarnings("unchecked")
@Test
public void testSimpleTemplate() throws Exception {
    TemplateContainer tc = new TemplateContainer() {

        private Column[] columns = new Column[] { new LongColumn("column1"), new LongColumn("column2"), new StringColumn("column3") };

        public Column[] getColumns() {
            return columns;
        }

        public String getHeader() {
            return null;
        }

        public Map<String, RuleTemplate> getTemplates() {
            Map<String, RuleTemplate> templates = new HashMap<String, RuleTemplate>();
            RuleTemplate ruleTemplate = new RuleTemplate("template1", this);
            ruleTemplate.addColumn("column1 == 10");
            ruleTemplate.addColumn("column2 < 5 || > 20");
            ruleTemplate.addColumn("column3 == \"xyz\"");
            templates.put("template1", ruleTemplate);
            return templates;
        }

        public Column getColumn(String name) {
            return columns[Integer.parseInt(name.substring(6)) - 1];
        }
    };
    DefaultTemplateRuleBase ruleBase = new DefaultTemplateRuleBase(tc);
    InternalKnowledgePackage[] packages = ((KnowledgeBaseImpl) ruleBase.newStatefulSession().getKieBase()).getPackages();
    assertEquals(1, packages.length);
    Map<String, String> globals = packages[0].getGlobals();
    assertEquals(DefaultGenerator.class.getName(), globals.get("generator"));
    Collection<org.kie.api.definition.rule.Rule> rules = packages[0].getRules();
    assertEquals(1, rules.size());
    assertEquals("template1", rules.iterator().next().getName());
    GroupElement lhs = ((RuleImpl) rules.iterator().next()).getLhs();
    // when
    // r : Row()
    // column1 : Column(name == "column1")
    // exists LongCell(row == r, column == column1, value == 10)
    // column2 : Column(name == "column2")
    // exists LongCell(row == r, column == column2, value < 5 | > 20)
    // column3 : Column(name == "column3")
    // exists StringCell(row == r, column == column3, value == "xyz")
    assertEquals(7, lhs.getChildren().size());
    org.drools.core.rule.Pattern pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(1);
    assertEquals(1, pattern.getConstraints().size());
    Constraint constraint = pattern.getConstraints().get(0);
    GroupElement exists = (GroupElement) lhs.getChildren().get(2);
    pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
    assertEquals(3, pattern.getConstraints().size());
    IndexableConstraint vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
    assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
    assertEquals("column1", vconstraint.getRequiredDeclarations()[0].getIdentifier());
    pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(3);
    assertEquals(1, pattern.getConstraints().size());
    constraint = pattern.getConstraints().get(0);
    exists = (GroupElement) lhs.getChildren().get(4);
    pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
    assertEquals(3, pattern.getConstraints().size());
    vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
    assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
    assertEquals("column2", vconstraint.getRequiredDeclarations()[0].getIdentifier());
    pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(5);
    assertEquals(1, pattern.getConstraints().size());
    constraint = pattern.getConstraints().get(0);
    exists = (GroupElement) lhs.getChildren().get(6);
    pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
    assertEquals(3, pattern.getConstraints().size());
    vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
    assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
    assertEquals("column3", vconstraint.getRequiredDeclarations()[0].getIdentifier());
}
Also used : HashMap(java.util.HashMap) Constraint(org.drools.core.spi.Constraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) IndexableConstraint(org.drools.core.rule.IndexableConstraint) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 48 with InternalKnowledgePackage

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

the class PatternBuilder method getTypeDeclaration.

private TypeDeclaration getTypeDeclaration(RuleBuildContext context, Class<?> userProvidedClass) {
    String packageName = ClassUtils.getPackage(userProvidedClass);
    KnowledgeBuilderImpl kbuilder = context.getKnowledgeBuilder();
    PackageRegistry pkgr = kbuilder.getPackageRegistry(packageName);
    TypeDeclaration typeDeclaration = pkgr != null ? pkgr.getPackage().getTypeDeclaration(userProvidedClass) : null;
    if (typeDeclaration == null && kbuilder.getKnowledgeBase() != null) {
        // check if the type declaration is contained only in the already existing kbase (possible during incremental compilation)
        InternalKnowledgePackage pkg = kbuilder.getKnowledgeBase().getPackage(packageName);
        typeDeclaration = pkg != null ? pkg.getTypeDeclaration(userProvidedClass) : null;
    }
    if (typeDeclaration == null) {
        typeDeclaration = context.getPkg().getTypeDeclaration(userProvidedClass);
    }
    return typeDeclaration;
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 49 with InternalKnowledgePackage

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

the class KnowledgeBuilderTest method testSerializable.

@Test
public void testSerializable() 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) );");
    // check that packageDescr is serializable
    final PackageDescr back = (PackageDescr) SerializationHelper.serializeObject(packageDescr);
    assertNotNull(back);
    assertEquals("p1", back.getName());
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    final RuleImpl rule = pkg.getRule("rule-1");
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgePackage newPkg = SerializationHelper.serializeObject(pkg);
    final RuleImpl newRule = newPkg.getRule("rule-1");
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    // It's been serialised so we have to simulate the re-wiring process
    newPkg.getDialectRuntimeRegistry().onAdd(kBase.getRootClassLoader());
    newPkg.getDialectRuntimeRegistry().onBeforeExecute();
    kBase.getGlobals().put("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 CompositeObjectSinkAdapterTest.MockBetaNode(), newRule, newRule.getLhs(), 0, new BuildContext(kBase)));
    final Activation activation = new MockActivation(newRule, 0, newRule.getLhs(), tuple);
    final DefaultKnowledgeHelper knowledgeHelper = new org.drools.core.base.DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
    knowledgeHelper.setActivation(activation);
    newRule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
    assertEquals(new Integer(1), map.get("value"));
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) HashMap(java.util.HashMap) AndDescr(org.drools.compiler.lang.descr.AndDescr) DefaultKnowledgeHelper(org.drools.core.base.DefaultKnowledgeHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Activation(org.drools.core.spi.Activation) BuildContext(org.drools.core.reteoo.builder.BuildContext) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 50 with InternalKnowledgePackage

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

the class KnowledgeBuilderTest method testTimeWindowBehavior.

@Test
public void testTimeWindowBehavior() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final TypeDeclarationDescr typeDeclDescr = new TypeDeclarationDescr(StockTick.class.getName());
    typeDeclDescr.addAnnotation("role", "event");
    packageDescr.addTypeDeclaration(typeDeclDescr);
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    final PatternDescr patternDescr = new PatternDescr(StockTick.class.getName(), "$tick");
    final BehaviorDescr windowDescr = new BehaviorDescr("window");
    windowDescr.setSubType("time");
    windowDescr.setParameters(Collections.singletonList("60000"));
    patternDescr.addBehavior(windowDescr);
    lhs.addDescr(patternDescr);
    ruleDescr.setConsequence("System.out.println( $tick );");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgePackage pkg = builder.getPackageRegistry().get("p1").getPackage();
    final RuleImpl rule = pkg.getRule("rule-1");
    assertNotNull(rule);
    final Pattern pattern = (Pattern) rule.getLhs().getChildren().get(0);
    assertEquals(StockTick.class.getName(), ((ClassObjectType) pattern.getObjectType()).getClassType().getName());
    final Behavior window = pattern.getBehaviors().get(0);
    assertEquals(Behavior.BehaviorType.TIME_WINDOW, window.getType());
    assertEquals(60000, ((SlidingTimeWindow) window).getSize());
}
Also used : Pattern(org.drools.core.rule.Pattern) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) BehaviorDescr(org.drools.compiler.lang.descr.BehaviorDescr) ClassObjectType(org.drools.core.base.ClassObjectType) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) StockTick(org.drools.compiler.StockTick) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Behavior(org.drools.core.rule.Behavior) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

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