User-Defined Functions
The examples in this topic demonstrate how to extend the possible expressions in StreamInsight LINQ operators by using method calls to existing .NET functions or user-defined functions (UDFs).
By using UDFs, expressions of any complexity are possible. They can be used wherever ordinary expressions occur: filter predicates, projections, join predicates, and so on. However, be aware that such a method call is evaluated for each event. A user-defined function must be compiled into an assembly that is accessible by the StreamInsight server in the same way that adapters are provided and used at run time.
The parameters and the return value of a UDF must be of one of the StreamInsight primitive types. Additionally, culture-related parameters are accepted for UDFs.
Use Cases
The following use cases illustrate ways that user-defined functions can be used to extend the functionality of StreamInsight queries.
User-defined Filters
You can create a user-defined filter operator that reasons about the incoming event and passes the event to the output or filters the event out. Such a user-defined filter may reason about user-defined data and may include complex or specialized logic in the process of event filtration.
User-defined Projects
Similar to user-defined filters, a user-defined project expression can produce a new payload value, whose computation is beyond the means of the provided expression functionality in StreamInsight.
Examples
The following example uses a user-defined calculation in a filter expression by applying the function MyFunc on the payload fields.
// Assuming the following input event type:
public class MyPayload
{
public int id;
public int a;
public int b;
public double value;
}
var filteredStream = from e in stream
where e.value > MyFunc(e.a * e.b)
select e;
The following example specifies the user-defined function MyFunctions.valThreshold in the filter predicate.
var filteredStream = from e in stream
where e.value < MyFunctions.valThreshold(e.id)
select e;
The following example uses the user-defined function valThreshold, which takes a single parameter and returns a value.
public class MyFunctions
{
public static int valThreshold(int id)
{
switch (id)
{
case 1:
return 15;
case 2:
return 11;
case 3:
return 18;
default:
return 0;
}
}
}
A user-defined project function can be used like any other expression in a projection, as shown in the following example.
var result = from e in stream
select new { location = MyFunctions.Lookup(e.id) };
In addition to taking built-in primitive types as parameters, user-defined functions can take culture-related information as parameters. The accepted types are:
CultureInfo
StringComparison
CompareOptions
The following example shows how to provide culture-specific information to a UDF.
// Assuming the following input event type:
public class MyPayload
{
public int id;
public string name;
}
var result = from e in stream
select new { location = MyFunctions.Lookup2(
e.name,
new CultureInfo("en-US"),
CompareOptions.IgnoreCase)
};
The culture information can also be taken from an event field in the input stream, using the helper function CepUtility.GetEventFieldCulture as shown in the following example.
var result = from e in stream
select new { location = MyFunctions.Lookup2(
e.name,
CepUtility.GetEventFieldCulture(e.name),
CompareOptions.IgnoreCase)
};