grule-rule-engine icon indicating copy to clipboard operation
grule-rule-engine copied to clipboard

Using maps like objects

Open lucasbbb opened this issue 3 years ago • 2 comments

Is your feature request related to a problem? Please describe. Noticed that I can visit a JSON Fact as a normal object or a map, but I can't visit a map like a normal object.

Describe the solution you'd like

    drls := `rule Demo "demo" salience 10 {
    when 
        myMap.name == "foo"
    then
        myMap.name == "bar";
	Retract("Demo");
    }`

    myMap := map[string]string{
        "name": "foo",
    }
    err := dataContext.AddMap("myMap", myMap)

Due to the restrictions of the Go, there is no way to set the value of a struct's non-exported filed. If this feature can be supported, I am able to use non-exported fields when defining my DSL.

Describe alternatives you've considered An alternative solution is define a tag, for example:

type User struct {
    Name string `grule:"name"`
}
    drls := `rule Demo "demo" salience 10 {
    when 
        user.name == "foo"
    then
        user.name == "bar";
	Retract("Demo");
    }`

    user := User{
        Name: "foo",
    }
    err := dataContext.Add("user", &user)

If the tag is specified, the grule engine can use it When calculating the rules.

Additional context

type User struct {
    Name string `json:"name"`
}
{
    "name": "foo"
}

Most APIs are defined in snake case. So I wish can define the rules in snake case.

lucasbbb avatar Jul 18 '22 10:07 lucasbbb

Also, the non-exported fields are readable but unmodifiable, which means they can be used in the When Statement but can't be used in the Then Statement.

lucasbbb avatar Jul 18 '22 10:07 lucasbbb

Hi @lucasbbb

Sorry for very very late response. Just want to confirm

when 
        myMap.name == "foo"
    then
        myMap.name == "bar";  <--- isn't this comparison expression, not assignment expression ?
	Retract("Demo");
    }`

newm4n avatar Nov 19 '22 09:11 newm4n