Authoring a UDF in Code

  • Updated

Prerequisites: None
Namespaces: InRule.Repository, InRule.Repository.RuleElements
Classes: RuleApplicationDef, EntityDef, FieldDef, RuleSetDef, UdfLibraryDef, UdfDef, UdfArgumentDef, CalcDef, ExecuteMethodActionDef, ExecuteMethodActionParamDef
References: InRule.Repository.dll

The sample code snippet below illustrates authoring an UDF and using an ExecuteMethodActionDef to execute the UDF.

// Create a ruleapplication
RuleApplicationDef ruleAppDef = new RuleApplicationDef();
EntityDef entity1 = ruleAppDef.Entities.Add(new EntityDef("Entity1"));
entity1.Fields.Add(new FieldDef("InputText"));
entity1.Fields.Add(new FieldDef("Pattern"));

// Add a UDF library
UdfLibraryDef udfLibrary = new UdfLibraryDef("StringFunctionsLibrary");

// Define UDF to match a string for a given pattern
UdfDef udf = new UdfDef("IsPatternMatch");

// Input arguments
UdfArgumentDef arg1 = new UdfArgumentDe("InputText");
arg1.ArgumentTypeInfo.DataType = DataType.String;
udf.FunctionArguments.Add(arg1);
UdfArgumentDef arg2 = new UdfArgumentDef("Pattern");
arg2.ArgumentTypeInfo.DataType = DataType.String;
udf.FunctionArguments.Add(arg2);

// Return type
udf.ReturnTypeInfo.DataType = DataType.Boolean;

// Function declaration
CalcDef func = new CalcDef();
func.FormulaText = "return InputText.Contains(Pattern);";
udf.FunctionBody = func;

// Add UDF to the library
udfLibrary.UserDefinedFunctions.Add(udf);
ruleAppDef.UdfLibraries.Add(udfLibrary);

// Add an execute method action to execute UDF
RuleSetDef rs1 = new RuleSetDef("r1");
ExecuteMethodActionDef execUdf = new ExecuteMethodActionDef();
execUdf.AliasName = "StringFunctionsLibrary";
execUdf.MethodName = "IsPatternMatch";
ExecuteMethodActionParamDef p1 = new ExecuteMethodActionParamDef("abcdeedef", "InputText");
execUdf.ParameterValues.Add(p1);
ExecuteMethodActionParamDef p2 = new ExecuteMethodActionParamDef("deed", "Pattern");
execUdf.ParameterValues.Add(p2);

rs1.Rules.Add(execUdf);
entity1.RuleElements.Add(rs1);

Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.