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

ActionContext is not able to get the actual value assigned from the rules #390

Open
vprabhu81 opened this issue Sep 16, 2022 · 6 comments
Open

Comments

@vprabhu81
Copy link

Hi,

Here is a snippet where we pass certain parameters on success and failure methods.
image

Now I was expecting the underlying value in my action context but instead I get the value in string like "Device.Id".
I tried a simple arithematic equation like "20*2" that too is being received as a string
image

@abbasc52
Copy link
Contributor

Hi @vprabhu81, this is intended behavior, context does not automatically get evaluated.

Can you provide the context on what you are trying to achieve?

@vprabhu81
Copy link
Author

I have these parameters passed into the rule engines, On success or failure I would need to retireve those data set to send notifications.

I am bit surprised though because I see that in the sample below we do pass the expressions as an input
image
https://microsoft.github.io/RulesEngine/#outputexpression

@vprabhu81
Copy link
Author

@abbasc52 : Just wanted to check with you if got a chance to look at the sample provided

@abbasc52
Copy link
Contributor

abbasc52 commented Sep 25, 2022

Hi @vprabhu81 , assuming you want to create a custom action, context is passed as is and needs to be parsed at the custom action code
e.g.

var expression = context.GetContext<string>("expression");
return new ValueTask<object>(_ruleExpressionParser.Evaluate<object>(expression, ruleParameters));

The ruleparser is a public class and can be used to evaluate expressions

If you just want to return an object , just use outputExpression and set the object there
e.g.

"OnSuccess": {
            "Name": "OutputExpression",  //Name of action you want to call
            "Context": {  //This is passed to the action as action context
               "Expression": "new (Device.Id as BodyId, Device.DateTimeUTC as EventUTCTime)"
            }
         }

@abbasc52 abbasc52 reopened this Sep 25, 2022
@vprabhu81
Copy link
Author

@abbasc52 ,

How do I Inject the RuleExpressionParser into my custom action? while registering the custom actions in RESettings, I will not have the rule engine instance yet, Is there an example I can refer to?

I tried the second approach as I just need the object in the custom action
"Expression": "new (Device.Id as BodyId, Device.DateTimeUTC as EventUTCTime)"

Unfortunately I still get it as string at the receiving end

@m4ss1m0g
Copy link

m4ss1m0g commented Nov 29, 2022

@abbasc52 ,

How do I Inject the RuleExpressionParser into my custom action? while registering the custom actions in RESettings, I will not have the rule engine instance yet, Is there an example I can refer to?

I tried the second approach as I just need the object in the custom action "Expression": "new (Device.Id as BodyId, Device.DateTimeUTC as EventUTCTime)"

Unfortunately I still get it as string at the receiving end

Hi @abbasc52

I found the answer on UnitTests

Name: "OutputExpression"
expression: "input1.country.ToString() + input2.totalOrders.ToString()",

"OnSuccess": {
            "Name": "OutputExpression",  
            "Context": {  //This is passed to the action as action context
               "expression": "input1.country.ToString() +  input2.totalOrders.ToString()"
            }
         }

See here

I hope this snippet can be added to the docs.

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