Skip to content

Commit

Permalink
Merge pull request #46 from project-flogo/action-input-mapping
Browse files Browse the repository at this point in the history
ruleaction input mapping
  • Loading branch information
jpark800 authored Apr 22, 2019
2 parents 8c0510f + b61095b commit 93c6ed6
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 77 deletions.
15 changes: 15 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ import (
"encoding/json"
"strconv"

"github.com/project-flogo/core/data/metadata"
"github.com/project-flogo/rules/common/model"
)

// RuleSessionDescriptor is a collection of rules to be loaded

type RuleActionDescriptor struct {
Name string `json:"name"`
IOMetadata *metadata.IOMetadata `json:"metadata"`
Rules []*RuleDescriptor `json:"rules"`
}

type RuleSessionDescriptor struct {
Rules []*RuleDescriptor `json:"rules"`
}
Expand Down Expand Up @@ -102,3 +110,10 @@ func (c *ConditionDescriptor) MarshalJSON() ([]byte, error) {

return buffer.Bytes(), nil
}

//metadata support
type DefinitionConfig struct {
Name string `json:"name"`
Metadata *metadata.IOMetadata `json:"metadata"`
Rules []*RuleDescriptor `json:"rules"`
}
38 changes: 35 additions & 3 deletions config/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ const (
)

type ResourceManager struct {
configs map[string]*RuleSessionDescriptor
configs map[string]*RuleActionDescriptor
}

func NewResourceManager() *ResourceManager {
manager := &ResourceManager{}
manager.configs = make(map[string]*RuleSessionDescriptor)
manager.configs = make(map[string]*RuleActionDescriptor)

return manager
}

func (m *ResourceManager) LoadResource(resConfig *resource.Config) (*resource.Resource, error) {
var rsConfig *RuleSessionDescriptor
var rsConfig *RuleActionDescriptor
err := json.Unmarshal(resConfig.Data, &rsConfig)
if err != nil {
return nil, fmt.Errorf("error unmarshalling rulesession resource with id '%s', %s", resConfig.ID, err.Error())
Expand All @@ -42,9 +42,41 @@ func (m *ResourceManager) GetResource(id string) interface{} {

func (m *ResourceManager) GetRuleSessionDescriptor(uri string) (*RuleSessionDescriptor, error) {

if strings.HasPrefix(uri, uriSchemeRes) {
return &RuleSessionDescriptor{m.configs[uri[len(uriSchemeRes):]].Rules}, nil
}

return nil, errors.New("cannot find RuleSession: " + uri)
}

func (m *ResourceManager) GetRuleActionDescriptor(uri string) (*RuleActionDescriptor, error) {

if strings.HasPrefix(uri, uriSchemeRes) {
return m.configs[uri[len(uriSchemeRes):]], nil
}

return nil, errors.New("cannot find RuleSession: " + uri)
}

//ioMetadata support
/*
type ActionResource struct {
IOMetadata *metadata.IOMetadata `json:"metadata"`
}
type ResManager struct {
IOMetadata *metadata.IOMetadata
}
func (m *ResManager) LoadResource(resConfig *resource.Config) (*resource.Resource, error) {
var res *ActionResource
err := json.Unmarshal(resConfig.Data, &res)
if err != nil {
return nil, fmt.Errorf("error unmarshalling metadata resource with id '%s', %s", resConfig.ID, err.Error())
}
m.IOMetadata = res.IOMetadata
return resource.New("ruleaction", m.IOMetadata), nil
}
*/
59 changes: 32 additions & 27 deletions examples/flogo/simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,32 @@ Below is the `flogo.json` file used in this example application. We will use thi
"port": "7777"
},
"handlers": [
{
"name": "n1",
{
"settings": {
"method": "GET",
"path": "/test/n1"
},
"actions": [
{
"id": "simple_rule",
"mappings": {
"input": [
{
"mapTo": "values",
"type": "assign",
"value": "$.queryParams"
}
]
"input": {
"tupletype": "n1",
"values": "=$.queryParams"
}
}
]
},
{
"name": "n2",
"settings": {
"method": "GET",
"path": "/test/n2"
},
"actions": [
{
"id": "simple_rule",
"mappings": {
"input": [
{
"mapTo": "values",
"type": "assign",
"value": "$.queryParams"
}
]
"input": {
"tupletype": "n2",
"values": "=$.queryParams"
}
}
]
Expand Down Expand Up @@ -110,6 +98,24 @@ Below is the `flogo.json` file used in this example application. We will use thi
{
"id": "rulesession:simple",
"data": {
"metadata": {
"input": [
{
"name": "values",
"type": "string"
},
{
"name": "tupletype",
"type": "string"
}
],
"output": [
{
"name": "outputData",
"type": "any"
}
]
},
"rules": [
{
"name": "n1.name == Bob",
Expand Down Expand Up @@ -177,18 +183,17 @@ The `actionFunction` is your rule's action. It means that when the `evaluator` c
Again, this is a unique string whose value binds to a Go function at runtime (explained later)

## Configure the trigger handler
Flogo users are perhaps already familiar with the trigger configurations. For rules, you have to additionally configure the `name` and
`action` of the handler
In this example, we configured two handlers. In the first, we have `handler`/`name` as `n1` and `path` as`/test/n1`
In the second, we have we have `handler`/`name` as `n2` and `path` as`/test/n2`
Flogo users are perhaps already familiar with the trigger configurations.
In this example, we configured two handlers. In the first, we have `tupletype` as `n1` and `path` as`/test/n1`
In the second, we have we have `tupletype` as `n2` and `path` as`/test/n2`
What this means is that when data arrives on URI `test/n1` we map its data to tuple type `n1` and
when it arrives on `/test/n2` we map its data to tuple type `n2` Note that the handler names should be one of the tuple type names as defined in the `tds` section
when it arrives on `/test/n2` we map its data to tuple type `n2`. Note that the `tupletype` should be one of the tuple type names defined in the `tds` section

##Mapping data from the handler to tuples
To do that, we simply configure the `actions/mappings/input/values` to `$.queryParams`
To do that, we simply configure the `actions/input/values` to `$.queryParams`

##How it all comes together
When data arrives on a trigger/handler, a new `Tuple` of type=handler's name is created
When data arrives on a trigger/handler, a new `Tuple` of `handlers/actions/input/tupletype` is created
The tuple values are initialized with the HTTP query parameters and the tuple is asserted to the rules session

##Binding Go functions for actions and conditions to the string tokens defined in the descriptor
Expand Down Expand Up @@ -252,7 +257,7 @@ First, inspect the `flogo.json` and the `functions.go` to understand the rules/c
For our test app `simplerules`, from the command line,
`simplerules/bin/simplerules`
Then from another command line, send a curl request
`curl localhost:7777/test/n2?name=Bob`
`curl localhost:7777/test/n1?name=Bob`
You should see this o/p on the console
```
Rule fired: [n1.name == Bob]
Expand Down
44 changes: 25 additions & 19 deletions examples/flogo/simple/flogo.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,32 @@
"port": "7777"
},
"handlers": [
{
"name": "n1",
{
"settings": {
"method": "GET",
"path": "/test/n1"
},
"actions": [
{
"id": "simple_rule",
"mappings": {
"input": [
{
"mapTo": "values",
"type": "assign",
"value": "$.queryParams"
}
]
"input": {
"tupletype": "n1",
"values": "=$.queryParams"
}
}
]
},
{
"name": "n2",
"settings": {
"method": "GET",
"path": "/test/n2"
},
"actions": [
{
"id": "simple_rule",
"mappings": {
"input": [
{
"mapTo": "values",
"type": "assign",
"value": "$.queryParams"
}
]
"input": {
"tupletype": "n2",
"values": "=$.queryParams"
}
}
]
Expand Down Expand Up @@ -92,6 +80,24 @@
{
"id": "rulesession:simple",
"data": {
"metadata": {
"input": [
{
"name": "values",
"type": "string"
},
{
"name": "tupletype",
"type": "string"
}
],
"output": [
{
"name": "outputData",
"type": "any"
}
]
},
"rules": [
{
"name": "n1.name == Bob",
Expand Down
Loading

0 comments on commit 93c6ed6

Please sign in to comment.