-
Hi, there. Firstly I want to said that I have read https://github.com/hyperjumptech/grule-rule-engine/blob/master/docs/GRL_JSON_en.md many times. But on my side it's not working as expected.
package main
import (
"fmt"
"github.com/hyperjumptech/grule-rule-engine/pkg"
)
// https://github.com/hyperjumptech/grule-rule-engine/blob/master/docs/GRL_JSON_en.md#expanded-representation
const jsonData = `{
"name": "SpeedUp",
"desc": "When testcar is speeding up we increase the speed.",
"salience": 10,
"when": {
"and": [
{"eq": ["TestCar.SpeedUp", true]},
{"lt": ["TestCar.Speed", "TestCar.MaxSpeed"]}
]
},
"then": [
{"set": ["TestCar.Speed", {"plus": ["TestCar.Speed", "TestCar.SpeedIncrement"]}]},
{"set": ["DistanceRecord.TotalDistance", {"plus": ["DistanceRecord.TotalDistance", "TestCar.Speed"]}]},
{"call": ["Log", {"const": "Speed increased"}]}
]
}`
func main() {
// https://github.com/hyperjumptech/grule-rule-engine/blob/master/docs/GRL_JSON_en.md#loading-json-rules
ruleset, err := pkg.ParseJSONRuleset([]byte(jsonData))
if err != nil {
panic(err)
}
fmt.Println(ruleset)
} Result:
What I missing? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The problem is that pkg.ParseJSONRuleset expects to be given a rule set rather than just a single rule. A rule set is just a JSON array of rules and it is perfectly fine to only have a single rule as long as it is wrapped in an array. The Load() function of the JSONResource handles both cases transparently, but if you want to manually load a single GRL JSON rule you can call the function pkg.ParseJSONRule() instead. |
Beta Was this translation helpful? Give feedback.
The problem is that pkg.ParseJSONRuleset expects to be given a rule set rather than just a single rule. A rule set is just a JSON array of rules and it is perfectly fine to only have a single rule as long as it is wrapped in an array.
The Load() function of the JSONResource handles both cases transparently, but if you want to manually load a single GRL JSON rule you can call the function pkg.ParseJSONRule() instead.