Basic Example of Creating a Rule Application in Code

  • Updated

Prerequisites: None
Namespaces: InRule.Repository, InRule.Repository.RuleElements
Classes: RuleApplicationDef, EntityDef, FieldDef, RuleSetDef, SimpleRuleDef, FireNotificationActionDef
References: InRule.Repository.dll

The RuleApplicationDef object is used to dynamically create and modify rules and schema elements in code.  The example below creates the simple rectangle rule application.

// Create a new rule application named "RectangleApp"
RuleApplicationDef ruleAppDef = new RuleApplicationDef("GeneratedRectangleApp");

// Create a rectangle entity
EntityDef entityDef = new EntityDef("Rectangle");

// Add the rectangle entity the rule application's entity collection
ruleAppDef.Entities.Add(entityDef);

// Add numeric height field
entityDef.Fields.Add(new FieldDef("Height", DataType.Number));

// Set the height default value to 4
entityDef.Fields["Height"].DefaultValue = "4";

// Add numeric width field
entityDef.Fields.Add(new FieldDef("Width", DataType.Number));

// Set the width default value to 5
entityDef.Fields["Width"].DefaultValue = "5";

// Add numeric area calculation with syntax expression
entityDef.Fields.Add(new FieldDef("Area", "Height * Width", DataType.Number));

// Create a ruleset
RuleSetDef ruleSetDef = (RuleSetDef)entityDef.RuleElements.Add(new RuleSetDef("MeasurementRules"));

// Create a simple if then rule with condition expression
SimpleRuleDef ruleDef = new SimpleRuleDef("Height > 0 and Width > 0");
ruleSetDef.Rules.Add(ruleDef);

// Create a notification with tokenized message
FireNotificationActionDef notificationDef = new FireNotificationActionDef();

notificationDef.NotificationMessageText = "Generated RectangleApp: Height <%Height%> * Width <%Width%>  = Area <%Area%>";

// Add the notification as an action under the if then rule
ruleDef.SubRules.Add(notificationDef);

// Save the rule application to the file system
ruleAppDef.SaveToFile(@"c:\temp\Rectangle.ruleapp");

Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.