Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complex rule for the fact #328

Open
suresh-webonise opened this issue Mar 17, 2023 · 2 comments
Open

Complex rule for the fact #328

suresh-webonise opened this issue Mar 17, 2023 · 2 comments

Comments

@suresh-webonise
Copy link

Suppose the fact is
const fact={
id:1,
username:'sureshk',
student:[{name:'zbc',percentage:100},{name:'zb',percentage:10}],
role:['coach','mentor'],
sport:['hockey','base']
}
and the rule should be

rule criteria => (student.name ='zbc' AND student.percentage=100) AND id=1 AND username='sureshk' AND role = 'coach' AND sport='hockey'
what will the json rules for the above rule criteria and facts

@chris-pardy
Copy link
Collaborator

@suresh-webonise can you clarify the structure of the facts? Specifically the role, name, and sport.
If role is an array containing multiple roles you can do a contains check. If it's a single role you can use an equality check.

For example:

const facts = {
  id:1,
  username:'sureshk',
  student: {name:'zbc',percentage:100},
  role: 'coach',
  sport:'hockey'
}

const rule = {
   condition: {
      all: [
        {
           fact: 'id',
           operator: 'equal'
           value: 1
        },
       {
          fact: 'username',
          operator: 'equal',
          value: 'sureshk'
        },
        {
           fact: 'student',
           path: '$.name',
           operator: 'equal'
           value: 'zbc'
        },
        ...
      ]
    }
}

@augustosilas
Copy link

augustosilas commented Nov 13, 2023

const fact={
  id:1,
  username:'sureshk',
  student:[{name:'zbc',percentage:100},{name:'zb',percentage:10}],
  role:['coach','mentor'],
  sport:['hockey','base']
}

const rule = {
	event: {
		type: "",
		params: {}
	},
	conditions: {
		all: [
                        {
				fact: "student",
				operator: "customContains",
				value: [ 
                                 {
                                   "field": "name",
                                   "value": "zbc",
                                   "operator": "="
                                 },
                                 {
                                   "field": "percentage",
                                   "value": 100,
                                   "operator": "="
                                 }
                                ]
			},
			{
				fact: "id",
				operator: "equal",
				value: 1
			},
			{
				fact: "username",
				operator: "equal",
				value: "sureshk"
			},
			{
				fact: "role",
				operator: "contains",
				value: "coach"
			},
			{
				fact: "sport",
				operator: "contains",
				value: "hockey"
			}
		]
	}
}

I hadn't realized that students was an array of objects. In this case, I created a custom operator that searches for an object within an array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants