Skip to content

Commit

Permalink
Merge pull request #43 from balamg/issue42
Browse files Browse the repository at this point in the history
Fix for #42 : Bug in handling rule conditions with no identifiers
  • Loading branch information
balamg authored May 2, 2019
2 parents cc15574 + 825647e commit 9da2156
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rete/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (nw *reteNetworkImpl) buildNetwork(rule model.Rule, nodesOfRule *list.List,
}
//Yoohoo! We have a Rule!!
ruleNode := newRuleNode(rule)
newNodeLink(nw, node, ruleNode, false)
newNodeLink(nw, lastNode, ruleNode, false)
nodesOfRule.PushBack(ruleNode)
} else {
idrs := SecondMinusFirst(node.getIdentifiers(), rule.GetIdentifiers())
Expand Down
4 changes: 3 additions & 1 deletion ruleapi/tests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func createRuleSession() (model.RuleSession, error) {
func trueCondition(ruleName string, condName string, tuples map[model.TupleType]model.Tuple, ctx model.RuleContext) bool {
return true
}

func falseCondition(ruleName string, condName string, tuples map[model.TupleType]model.Tuple, ctx model.RuleContext) bool {
return false
}
func emptyAction(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
}

Expand Down
59 changes: 59 additions & 0 deletions ruleapi/tests/rtctxn_8_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package tests

import (
"github.com/project-flogo/rules/common/model"
"github.com/project-flogo/rules/ruleapi"

"context"
"testing"
)

//no-identifier condition
func Test_T8(t *testing.T) {

rs, _ := createRuleSession()

rule := ruleapi.NewRule("R1")
rule.AddCondition("R1_c1", []string{"t1.none"}, trueCondition, nil)
rule.AddCondition("R1_c2", []string{}, falseCondition, nil)
rule.SetAction(assertTuple)
rule.SetPriority(1)
rs.AddRule(rule)
t.Logf("Rule added: [%s]\n", rule.GetName())

rs.RegisterRtcTransactionHandler(t8Handler, t)
rs.Start(nil)

t1, _ := model.NewTupleWithKeyValues("t1", "t1")
rs.Assert(context.TODO(), t1)
rs.Unregister()

}


func assertTuple(ctx context.Context, rs model.RuleSession, ruleName string, tuples map[model.TupleType]model.Tuple, ruleCtx model.RuleContext) {
t, _:= model.NewTupleWithKeyValues("t1", "t2")
rs.Assert(ctx, t)
}

func t8Handler(ctx context.Context, rs model.RuleSession, rtxn model.RtcTxn, handlerCtx interface{}) {

t := handlerCtx.(*testing.T)
if m, found := rtxn.GetRtcAdded()["t1"]; found {
lA := len(m)
if lA != 1 {
t.Errorf("RtcAdded: Expected [%d], got [%d]\n", 1, lA)
printTuples(t,"Added", rtxn.GetRtcAdded())
}
}
lM := len(rtxn.GetRtcModified())
if lM != 0 {
t.Errorf("RtcModified: Expected [%d], got [%d]\n", 0, lM)
printModified(t, rtxn.GetRtcModified())
}
lD := len(rtxn.GetRtcDeleted())
if lD != 0 {
t.Errorf("RtcDeleted: Expected [%d], got [%d]\n", 0, lD)
printTuples(t,"Deleted", rtxn.GetRtcDeleted())
}
}

0 comments on commit 9da2156

Please sign in to comment.