API
- Simpleflow Execution
- Flowoutput
- Register Custom Functions
- Extensibility
- Compile Script
- Function Permissions
- Register Functions at Context Level
- Cache Options
- Handle Parser and Runtime Errors
- Get Syntax Tree
Simpleflow Execution
Sample code to create, build and run pipeline
// Build Pipeline to get full control over execution
ISimpleflowPipelineBuilder engine
= new SimpleflowPipelineBuilder()
// Add middleware services
.AddPipelineServices(
new MyPreprocessor(),
new MyLoggingService());
// Add core services - Cache, Compiler, Execution
.AddCorePipelineServices(FunctionRegister.Default) // or
/*
AddPipelineServices(
new CacheService(), //Cache uses Sliding window cache policy
new CompilerService(ActivityRegister.Default),
new ExecutionService());
*/
// Build
ISimpleflow flow = engine.Build();
// Run
FlowOutput result = flow.Run(script, new Member { Id = id});
FlowOutput
Emitters (message, error, output
) produce output from script that will be available in FlowOutput object.
Register Custom Functions
FunctionRegister.Default
.Add("DerivativeOfXPowN", (Func<int, int, int>)CalcDerivativeOfXPowN)
static int CalcDerivativeOfXPowN(int x, int n)
{
return n * Math.Pow(x, n-1); //
}
Extensibility
Create middleware and add it to pipeline.
public class LoggingService : IFlowPipelineService
{
public void Run<TArg>(FlowContext<TArg> context, NextPipelineService<TArg> next)
{
// log here
next?.Invoke(context);
}
}
Compile Script
By adding only CompilerService to build a pipeline, script can be compiled and reported if there are any errors.
var engine
= new SimpleflowPipelineBuilder()
.AddPipelineServices(new Services.CompilerService(FunctionRegister.Default));
var simpleflow = engine.Build();
try
{
simpleflow.Run(script, context);
}
catch(SimpleflowException exception)
{
//Handle script errors
}
Function Permissions
By setting FlowContextOptions, you can permit specific functions to be executed.
string script = @"
let d = $GetCurrentDateTime()
message d
";
var output = SimpleflowEngine.Run( script,
new object(),
new FlowContextOptions
{
AllowFunctions = new string[] {
"GetCurrentDateTime"
}
}
);
Register Functions at Context Level
string id = "7bfd56c8ca354307b6cb9e0805a7ae4c";
var options = new FlowContextOptions {
Id = id,
};
// Register additional function to this context only.
var funcRegister = new FunctionRegister();
funcRegister.Add("GetSysDate", (Func<string>)GetDate);
.Add("GetCurrentDate", (Func<string>)GetDate); // Override Default Function
FlowOutput result = new SimpleflowEngine.Run(script,
new object(),
options,
funcRegister);
Cache Options
Specify cache options based on frequency of executing Simpleflow code. Cache options can be specified at Simpleflow builder level as well as running-context level. Recommend to set ID at FlowContextOptions level in order to avoid hashing.
string id = "7bfd56c8ca354307b6cb9e0805a7ae4c";
var options = new FlowContextOptions {
// This will be used as cache key, if not specified then
// it creates using hashing algorithm
Id = id,
// This allows to reset the cache entry if script needs to recompile
// ResetCache = isModified
CacheOptions = new CacheOptions {
AbsoluteExpiration = System.DateTimeOffset.Now.AddHours(1),
SlidingExpiration = System.TimeSpan.FromMinutes(3),
// Default it uses MD5
HashingAlgToIdentifyScriptUniquely = "SHA256"
}
};
FlowOutput result = new SimpleflowEngine.Run(script, new object(), options);
// isModified = false
Handle Parser and Runtime Errors
Handle parser/compilation/execution errors
try
{
var result = new SimpleflowEngine.Run(script, ...);
}
catch(SyntaxException syntaxException)
{
// syntaxException.Errors
}
catch(SimpleflowRuntimeException runtimeException)
{
// runtime errors
// runtimeException.LineNumber
// runtimeException.Statement
}
catch(SimpleflowException exception)
{
//any other compile time exception
}
Get Syntax Tree
v1.0.3
SyntaxTree st = Simpleflow.Ast
.SimpleflowScript
.GetAbstractSyntaxTree("<your Simpleflow script here>")
This API provides abstract syntax tree data structure of Simpleflow script. If there are any syntax errors, this method reports that error using SyntaxTree.Errors property.