Search in sources :

Example 11 with GroupElement

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

the class RuleUnlinkingWithSegmentMemoryTest method createNetworkNode.

private NetworkNode createNetworkNode(int id, int type, LeftTupleSource leftTupleSource, RuleImpl rule) {
    MockObjectSource mockObjectSource = new MockObjectSource(8);
    LeftTupleSink networkNode = null;
    switch(type) {
        case JOIN_NODE:
            {
                networkNode = new JoinNode(id, leftTupleSource, mockObjectSource, new EmptyBetaConstraints(), buildContext);
                break;
            }
        case EXISTS_NODE:
            {
                networkNode = new ExistsNode(id, leftTupleSource, mockObjectSource, new EmptyBetaConstraints(), buildContext);
                break;
            }
        case NOT_NODE:
            {
                networkNode = new NotNode(id, leftTupleSource, mockObjectSource, new EmptyBetaConstraints(), buildContext);
                break;
            }
        case RULE_TERMINAL_NODE:
            {
                networkNode = new RuleTerminalNode(id, leftTupleSource, rule, new GroupElement(Type.AND), 0, buildContext);
                break;
            }
    }
    mockObjectSource.attach();
    if (NodeTypeEnums.isLeftTupleSource(networkNode)) {
        ((LeftTupleSource) networkNode).attach(buildContext);
    } else {
        ((RuleTerminalNode) networkNode).attach(buildContext);
    }
    return networkNode;
}
Also used : EmptyBetaConstraints(org.drools.core.common.EmptyBetaConstraints) GroupElement(org.drools.core.rule.GroupElement)

Example 12 with GroupElement

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

the class ReteooRuleBuilderTest method testAddRuleWithPatterns.

@Test
public void testAddRuleWithPatterns() {
    final RuleImpl rule = new RuleImpl("only patterns");
    final Pattern c1 = new Pattern(0, new ClassObjectType(String.class));
    final Pattern c2 = new Pattern(1, new ClassObjectType(String.class));
    final Pattern c3 = new Pattern(2, new ClassObjectType(String.class));
    final GroupElement lhsroot = GroupElementFactory.newAndInstance();
    lhsroot.addChild(c1);
    lhsroot.addChild(c2);
    lhsroot.addChild(c3);
    rule.setLhs(lhsroot);
    final Consequence consequence = new Consequence() {

        public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
            System.out.println("Consequence!");
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    };
    rule.setConsequence(consequence);
    final List terminals = this.builder.addRule(rule, this.rulebase);
    assertEquals("Rule must have a single terminal node", 1, terminals.size());
    final RuleTerminalNode terminal = (RuleTerminalNode) terminals.get(0);
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) GroupElement(org.drools.core.rule.GroupElement) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) List(java.util.List) ObjectInput(java.io.ObjectInput) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 13 with GroupElement

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

the class NamedConsequenceBuilder method buildTerminalNodeForNamedConsequence.

static RuleTerminalNode buildTerminalNodeForNamedConsequence(BuildContext context, NamedConsequence namedConsequence) {
    RuleImpl rule = context.getRule();
    GroupElement subrule = (GroupElement) context.peek();
    ActivationListenerFactory factory = context.getKnowledgeBase().getConfiguration().getActivationListenerFactory(rule.getActivationListener());
    context.setConsequenceName(namedConsequence.getConsequenceName());
    TerminalNode terminal = factory.createActivationListener(context.getNextId(), context.getTupleSource(), rule, subrule, // subruleIndex,
    0, context);
    context.setConsequenceName(null);
    return (RuleTerminalNode) terminal;
}
Also used : GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ActivationListenerFactory(org.drools.core.ActivationListenerFactory) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) TerminalNode(org.drools.core.reteoo.TerminalNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Example 14 with GroupElement

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

the class ReteooRuleBuilder method addRule.

/**
 * Creates the corresponting Rete network for the given <code>Rule</code> and adds it to
 * the given rule base.
 *
 * @param rule
 *            The rule to add.
 * @param kBase
 *            The rulebase to add the rule to.
 *
 * @return a List<BaseNode> of terminal nodes for the rule
 * @throws InvalidPatternException
 */
public List<TerminalNode> addRule(final RuleImpl rule, final InternalKnowledgeBase kBase) throws InvalidPatternException {
    // the list of terminal nodes
    final List<TerminalNode> nodes = new ArrayList<TerminalNode>();
    // transform rule and gets the array of subrules
    final GroupElement[] subrules = rule.getTransformedLhs(kBase.getConfiguration().getComponentFactory().getLogicTransformerFactory().getLogicTransformer(), kBase.getGlobals());
    for (int i = 0; i < subrules.length; i++) {
        // creates a clean build context for each subrule
        final BuildContext context = new BuildContext(kBase);
        context.setRule(rule);
        // if running in STREAM mode, calculate temporal distance for events
        if (EventProcessingOption.STREAM.equals(kBase.getConfiguration().getEventProcessingMode())) {
            TemporalDependencyMatrix temporal = this.utils.calculateTemporalDistance(subrules[i]);
            context.setTemporalDistance(temporal);
        }
        if (kBase.getConfiguration().isSequential()) {
            context.setTupleMemoryEnabled(false);
            context.setObjectTypeNodeMemoryEnabled(false);
        } else {
            context.setTupleMemoryEnabled(true);
            context.setObjectTypeNodeMemoryEnabled(true);
        }
        // adds subrule
        final TerminalNode node = this.addSubRule(context, subrules[i], i, rule);
        // adds the terminal node to the list of terminal nodes
        nodes.add(node);
    }
    return nodes;
}
Also used : TemporalDependencyMatrix(org.drools.core.time.TemporalDependencyMatrix) GroupElement(org.drools.core.rule.GroupElement) ArrayList(java.util.ArrayList) TerminalNode(org.drools.core.reteoo.TerminalNode) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint)

Example 15 with GroupElement

use of org.drools.core.rule.GroupElement 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)

Aggregations

GroupElement (org.drools.core.rule.GroupElement)29 Pattern (org.drools.core.rule.Pattern)16 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)14 Test (org.junit.Test)13 Declaration (org.drools.core.rule.Declaration)6 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)5 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)5 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)5 RuleConditionElement (org.drools.core.rule.RuleConditionElement)4 ObjectInput (java.io.ObjectInput)3 ObjectOutput (java.io.ObjectOutput)3 HashMap (java.util.HashMap)3 AndDescr (org.drools.compiler.lang.descr.AndDescr)3 WorkingMemory (org.drools.core.WorkingMemory)3 ClassObjectType (org.drools.core.base.ClassObjectType)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)3 IntrospectionException (java.beans.IntrospectionException)2 IOException (java.io.IOException)2