microsoft / RulesEngine

A Json based Rules Engine with extensive Dynamic expression support
https://microsoft.github.io/RulesEngine/
MIT License
3.47k stars 530 forks source link

Getting exception by using NetTopologySuite objects inside rule expression #551

Closed goodstas closed 7 months ago

goodstas commented 7 months ago

Hello, i created simple Console application where i defined two geometries from NetTopologySuite nuget : ` var point = new Point(0.5, 0.5); var polygon = new Polygon(new LinearRing(new Coordinate[] { new Coordinate(0,0), new Coordinate(1,0), new Coordinate(1,1), new Coordinate(1,0), new Coordinate(0,0) }));

var rulesParameters = new List() { new RuleParameter("point1", point), new RuleParameter("polygon1", polygon) };

var workflow = new Workflow() { WorkflowName = "test workflow", GlobalParams = null, RuleExpressionType = RuleExpressionType.LambdaExpression, Rules = new List() { new Rule() { RuleName = "geo rule", SuccessEvent = "geo success", ErrorMessage = "geo error", Enabled = true, Expression = "polygon1.Contains(point1)" } } }; rulesEngine.AddWorkflow(workflow); var result = rulesEngine.ExecuteAllRulesAsync("test workflow", rulesParameters.ToArray()); Unfortunately i got a following exception message after running the workflow and inspecting the result object : Exception while parsing expressionpolygon1.Contains(point1)` - Methods on type 'Geometry' are not accessible

Polygon and Point classes inherit from Geometry base class. Geometry class has 'Contains' method with a following signature : public virtual bool Contains(Geometry g)

What can i do to bypass this exception or it is kind of bug of RulesEngine? Thank you.

@abbasc52

abbasc52 commented 7 months ago

Hi @goodstas, by default method calls on class is not allowed. To enable it, you need to register the type in ReSettings e.g.

var reSettingsWithCustomTypes = new ReSettings { CustomTypes = new Type[] { typeof(Geometry) } };
new RulesEngine.RulesEngine(workflowRules.ToArray(), reSettingsWithCustomTypes);
goodstas commented 7 months ago

@abbasc52 Thank you! I actually knew it and i added Polygon and Point types as a custom types. Now i understand that i need to add also a base classes.