From 40152924d32d309f41acb27d9ec897bc550538dc Mon Sep 17 00:00:00 2001 From: Jon Park Date: Thu, 2 May 2019 08:34:08 -0700 Subject: [PATCH 1/5] added a test case --- ruleapi/tests/rtctxn_8_test.go | 53 ++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/ruleapi/tests/rtctxn_8_test.go b/ruleapi/tests/rtctxn_8_test.go index 350929c..6d0a34c 100644 --- a/ruleapi/tests/rtctxn_8_test.go +++ b/ruleapi/tests/rtctxn_8_test.go @@ -9,7 +9,7 @@ import ( ) //no-identifier condition -func Test_T8(t *testing.T) { +func Test_T8_1(t *testing.T) { rs, _ := createRuleSession() @@ -21,7 +21,7 @@ func Test_T8(t *testing.T) { rs.AddRule(rule) t.Logf("Rule added: [%s]\n", rule.GetName()) - rs.RegisterRtcTransactionHandler(t8Handler, t) + rs.RegisterRtcTransactionHandler(t8Handler_1, t) rs.Start(nil) t1, _ := model.NewTupleWithKeyValues("t1", "t1") @@ -29,21 +29,62 @@ func Test_T8(t *testing.T) { rs.Unregister() } +func Test_T8_2(t *testing.T) { + rs, _ := createRuleSession() + + rule := ruleapi.NewRule("R1") + rule.AddCondition("R1_c1", []string{"t1.none"}, trueCondition, nil) + rule.AddCondition("R1_c2", []string{}, trueCondition, nil) + rule.SetAction(assertTuple) + rule.SetPriority(1) + rs.AddRule(rule) + t.Logf("Rule added: [%s]\n", rule.GetName()) + + rs.RegisterRtcTransactionHandler(t8Handler_2, 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") + t, _ := model.NewTupleWithKeyValues("t1", "t2") rs.Assert(ctx, t) } -func t8Handler(ctx context.Context, rs model.RuleSession, rtxn model.RtcTxn, handlerCtx interface{}) { +func t8Handler_1(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()) + 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()) + } +} + +func t8Handler_2(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 != 2 { + t.Errorf("RtcAdded: Expected [%d], got [%d]\n", 1, lA) + printTuples(t, "Added", rtxn.GetRtcAdded()) } } lM := len(rtxn.GetRtcModified()) @@ -54,6 +95,6 @@ func t8Handler(ctx context.Context, rs model.RuleSession, rtxn model.RtcTxn, han lD := len(rtxn.GetRtcDeleted()) if lD != 0 { t.Errorf("RtcDeleted: Expected [%d], got [%d]\n", 0, lD) - printTuples(t,"Deleted", rtxn.GetRtcDeleted()) + printTuples(t, "Deleted", rtxn.GetRtcDeleted()) } } From 99abf59dbde0d34afa2abf67d5dfbc35b32e8503 Mon Sep 17 00:00:00 2001 From: Jon Park Date: Thu, 2 May 2019 08:43:30 -0700 Subject: [PATCH 2/5] added comment --- ruleapi/tests/rtctxn_8_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ruleapi/tests/rtctxn_8_test.go b/ruleapi/tests/rtctxn_8_test.go index 6d0a34c..27d9fe1 100644 --- a/ruleapi/tests/rtctxn_8_test.go +++ b/ruleapi/tests/rtctxn_8_test.go @@ -8,7 +8,7 @@ import ( "testing" ) -//no-identifier condition +//no-identifier condition with single-assertion func Test_T8_1(t *testing.T) { rs, _ := createRuleSession() @@ -29,6 +29,8 @@ func Test_T8_1(t *testing.T) { rs.Unregister() } + +//no-identifier condition with multi-assertion func Test_T8_2(t *testing.T) { rs, _ := createRuleSession() From a917bdc51e9fb345cbc56f71f1d332000870f4af Mon Sep 17 00:00:00 2001 From: Jon Park Date: Thu, 2 May 2019 19:46:30 -0700 Subject: [PATCH 3/5] Added RuleAction README --- ruleaction/README.md | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 ruleaction/README.md diff --git a/ruleaction/README.md b/ruleaction/README.md new file mode 100644 index 0000000..1b60855 --- /dev/null +++ b/ruleaction/README.md @@ -0,0 +1,77 @@ +## RuleAction +RuleAction is a flogo action which allows events to be injected into the rules engine via a flogo trigger. + +### Configuration +RuleAction configuration contains 2 parts, settings and resources. + +#### settings + +| Name | Type | Description | +|:-----------|:--------|:--------------| +| id | string | id is referenced by an element in another section of flogo configuration such as trigger handler action's id | +| rulesessionURI | uri | Uri that starts with 'res://rulesession:'. It's referenced in the resources section | +| tds | array | Tuple definitions | + + +#### tds +| Name | Type | Description | +|:-----------|:--------|:--------------| +| name | string | Tuple type name | +| properties | array | Properties of the tuple | + + +#### properties +| Name | Type | Description | +|:-----------|:--------|:--------------| +| name | string | Property name | +| type | string | Data type of the property | +| pk-index | int | Tuple key order. If -1, not used as part of tuple key | + +#### resources + +| Name | Type | Description | +|:-----------|:--------|:--------------| +| id | string | id is referenced by an element in another section of flogo configuration such as action settings's rulesessionURI | +| data | object | metadata and rule defintions | + +#### metadata +It contains an array of input element comprised of 2 parameters, values and tupletype. + +| Name | Type | Description | +|:-----------|:--------|:--------------| +| values | string | Tuple values | +| tupletype | string | Tupple type that the values parameter adheres to | + +#### rules +| Name | Type | Description | +|:-----------|:--------|:--------------| +| name | string | Name of the rule | +| conditions | array | Conditions that the rule evaluates given input | +| actionFunction | string | Rule action function to be fired when conditions are true. The function must exist in functions.go | + +#### conditions + +| Name | Type | Description | +|:-----------|:--------|:--------------| +| name | string | Name of the condition | +| identifiers | array | Tuple types the condition evaluates upon | +| evaluator | string | Function that envaluates the condition. The function must exist in functions.go | + + +### Usage + +Upon a configuration of flogo.json containing ruleaction configuration and creation of functions.go which contains the evaluator and actionFunction implementations, place flogo.json in a folder of your choice to run +``` +flogo create -f flogo.json myrules +cp functions.go myrules/src +cd myrules/src +flogo build +``` +To run the flogo rules binary, +``` +../bin/myrules +``` + + +### Examples +For examples, see [rules flogo examples](https://github.com/project-flogo/rules/tree/master/examples/flogo). \ No newline at end of file From 2680a0370614d4d0f55fa57b6e99e3790a06b889 Mon Sep 17 00:00:00 2001 From: Jon Park Date: Thu, 2 May 2019 19:57:36 -0700 Subject: [PATCH 4/5] corrected file paths --- ruleaction/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ruleaction/README.md b/ruleaction/README.md index 1b60855..6e9f48d 100644 --- a/ruleaction/README.md +++ b/ruleaction/README.md @@ -64,12 +64,12 @@ Upon a configuration of flogo.json containing ruleaction configuration and creat ``` flogo create -f flogo.json myrules cp functions.go myrules/src -cd myrules/src +cd myrules flogo build ``` To run the flogo rules binary, ``` -../bin/myrules +bin/myrules ``` From 6ceb5301da0be9c5f2d56ec385965b9ec16643c6 Mon Sep 17 00:00:00 2001 From: Jon Park Date: Thu, 2 May 2019 20:06:57 -0700 Subject: [PATCH 5/5] reverting unwanted changes --- ruleapi/tests/rtctxn_8_test.go | 57 +++++----------------------------- 1 file changed, 7 insertions(+), 50 deletions(-) diff --git a/ruleapi/tests/rtctxn_8_test.go b/ruleapi/tests/rtctxn_8_test.go index 27d9fe1..350929c 100644 --- a/ruleapi/tests/rtctxn_8_test.go +++ b/ruleapi/tests/rtctxn_8_test.go @@ -8,8 +8,8 @@ import ( "testing" ) -//no-identifier condition with single-assertion -func Test_T8_1(t *testing.T) { +//no-identifier condition +func Test_T8(t *testing.T) { rs, _ := createRuleSession() @@ -21,7 +21,7 @@ func Test_T8_1(t *testing.T) { rs.AddRule(rule) t.Logf("Rule added: [%s]\n", rule.GetName()) - rs.RegisterRtcTransactionHandler(t8Handler_1, t) + rs.RegisterRtcTransactionHandler(t8Handler, t) rs.Start(nil) t1, _ := model.NewTupleWithKeyValues("t1", "t1") @@ -30,63 +30,20 @@ func Test_T8_1(t *testing.T) { } -//no-identifier condition with multi-assertion -func Test_T8_2(t *testing.T) { - - rs, _ := createRuleSession() - - rule := ruleapi.NewRule("R1") - rule.AddCondition("R1_c1", []string{"t1.none"}, trueCondition, nil) - rule.AddCondition("R1_c2", []string{}, trueCondition, nil) - rule.SetAction(assertTuple) - rule.SetPriority(1) - rs.AddRule(rule) - t.Logf("Rule added: [%s]\n", rule.GetName()) - - rs.RegisterRtcTransactionHandler(t8Handler_2, 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") + t, _:= model.NewTupleWithKeyValues("t1", "t2") rs.Assert(ctx, t) } -func t8Handler_1(ctx context.Context, rs model.RuleSession, rtxn model.RtcTxn, handlerCtx interface{}) { +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()) - } -} - -func t8Handler_2(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 != 2 { - t.Errorf("RtcAdded: Expected [%d], got [%d]\n", 1, lA) - printTuples(t, "Added", rtxn.GetRtcAdded()) + printTuples(t,"Added", rtxn.GetRtcAdded()) } } lM := len(rtxn.GetRtcModified()) @@ -97,6 +54,6 @@ func t8Handler_2(ctx context.Context, rs model.RuleSession, rtxn model.RtcTxn, h lD := len(rtxn.GetRtcDeleted()) if lD != 0 { t.Errorf("RtcDeleted: Expected [%d], got [%d]\n", 0, lD) - printTuples(t, "Deleted", rtxn.GetRtcDeleted()) + printTuples(t,"Deleted", rtxn.GetRtcDeleted()) } }