Custom Match Algorithms
Adding custom algorithms or enhanced comparison between values is quite simple using the default match engine in SanteDB. If extending the blocking algorithm, you should implement:
  1. 1.
    IQueryFilterExtension - Which registers your extended algorithm on the HDSI query interface
  2. 2.
    IIDbFilterFunction - Which converts your extended HDSI query operation to SQL
If exposing a custom algorithm for scoring, you must implement either:
  1. 1.
    IUnaryTransform - Which transforms a single operand into another value for subsequent analysis, or
  2. 2.
    IBinaryTransform - Which transforms two operands into a single result

Implementing Custom IQueryFilterExtension

An IQueryFilter extension is responsible for exposing your filter algorithm on the HDSI query interface. For example, if we wanted to expose a custom query extension for the length of a string.
1
public class StringLengthFilter : IQueryFilterExtension
2
{
3
/// <summary>
4
/// Gets the name
5
/// </summary>
6
public string Name => "length_difference";
7
8
/// <summary>
9
/// The extension method which is used by Linq
10
/// </summary>
11
public MethodInfo ExtensionMethod => typeof(ExtensionMethods).GetMethod(nameof(ExtensionMethods.LengthDifference));
12
13
/// <summary>
14
/// Compose the expression in LINQ
15
/// </summary>
16
public BinaryExpression Compose(Expression scope, ExpressionType comparison, Expression valueExpression, Expression[] parms)
17
{
18
if (parms.Length != 1) throw new ArgumentOutOfRangeException("difference requires one parameter : value=:(length_difference|other)comparator");
19
return Expression.MakeBinary(comparison,
20
Expression.Call(this.ExtensionMethod, scope, parms[0]),
21
valueExpression);
22
}
23
}
Copied!
The primary responsibilities of an IQueryFilterExpression are:
  • Reserve a name (in this case length_difference) for use on the HDSI interfaces
  • Compose the parsed expression into a method call and comparison in a LINQ expression tree
  • Expose the method information to the LINQ composer so it may transform to/from HDSI / LINQ.
This particular filter expression uses an extension method to compose on LINQ, in this case our extension method is defined as:
1
public static class ExtensionMethods
2
{
3
/// <summary>
4
/// Return difference in length
5
/// </summary>
6
public static int LengthDifference(this String a, String b)
7
{
8
return Math.Abs(a.Length - b.Length);
9
}
10
}
Copied!
When the HDSI query engine encounters:
1
/Patient?name.component.value=:(length_difference|Steve)<2
Copied!
Your filter expression composition method will be invoked and should result in a LINQ expression of:
1
patient => patient.Names.Any(name => name.Component.Any(component => component.Value.LengthDifference("steve") < 2));
Copied!

Implementing Custom IDbFilterFunction

The IQueryFilterExpression is responsible for taking wire-level HTTP queries and constructing LINQ expressions. This is useful for in-memory objects, however to expose the filter expression to a database, a IDbFilterFunction from the ORM Lite library needs to be implemented.
Continuing our example above, a PostgreSQL implementation of length_difference would be:
1
public class PostgreStringLengthFilter : IDbFilterFunction
2
{
3
/// <summary>
4
/// Gets the provider name, in our case PostgreSQL
5
/// </summary>
6
public string Provider => "npgsql";
7
8
/// <summary>
9
/// Gets the name of the transform
10
/// </summary>
11
public string Name => "length_difference";
12
13
/// <summary>
14
/// Create a SQL statement
15
/// </summary>
16
public SqlStatement CreateSqlStatement(SqlStatement current, string filterColumn, string[] parms, string operand, Type operandType)
17
{
18
// Match an operand to support less than or equal to semantics
19
var match = new Regex(@"^([<>]?=?)(.*?)quot;).Match(operand);
20
String op = match.Groups[1].Value, value = match.Groups[2].Value;
21
if (String.IsNullOrEmpty(op)) op = "=";
22
23
return current.Append(quot;ABS(LENGTH({filterColumn}) - LENGTH(?)) {op} ?", QueryBuilder.CreateParameterValue(parms[0], operandType), QueryBuilder.CreateParameterValue(value, typeof(Int32)));
24
}
25
}
Copied!

Implementing Custom IDataTransform for Scoring

If we wanted to use or expose our extended algorithm to the scoring engine as a string transform, we can implement an IDataTransform interface. For example, if we wanted to allow users to configure a scoring attribute:
1
<attribute property="name.component.value" u="0.019" m="0.85" whenNull="nonmatch" required="true" >
2
<assert op="le" value="2">
3
<transform name="length_difference"/>
4
</assert>
5
</attribute>
Copied!
We would implement an IBinaryDataTransformer as :
1
public class StringLengthTransform : IBinaryDataTransformer
2
{
3
/// <summary>
4
/// Gets the name of the transform
5
/// </summary>
6
public string Name => "length_difference";
7
8
/// <summary>
9
/// Apply the transform to a and b
10
/// </summary>
11
public object Apply(object a, object b, params object[] parms)
12
{
13
if (a is String aString && b is String bString)
14
return ExtensionMethods.LengthDifference(aString, bString);
15
else
16
throw new ArgumentOutOfRangeException("A and B must be strings");
17
}
18
}
Copied!
In the configuration above, the matching engine would:
  1. 1.
    Extract the name.component.value of record A and record B
  2. 2.
    Resolve length_difference to our implementation of IBinaryDataTransformer
  3. 3.
    Call the Apply() method of our transformer
  4. 4.
    Pass the result to the next transform or compare it with the assertion provided (in this case the result is less than 2).
Last modified 11d ago