Function - aceryan-consulting/aceryansoft.codeflow GitHub Wiki

We have a clean set of customers and orders data, the next step for our artificial intelligence program is to create a function to predict customer behaviors.

We can start by computing for instance the SpendingThreshold indicator, Of course the main idea behind our project is to show the framework logic. Please feel free to implement the real code logic 😁 .

A function can be called as a delegate (ActivityFunctionDelegate) or a class implementing ICodeFlowActivityFunction. let's try to write our first function bellow.

public class SpendingThresholdFunction : ISpendingThresholdFunction
{
	private readonly IAwesomeSpendingThresholdComputer _awesomeSpendingThresholdComputer;

	// Country and  AdjustmentValue value are automatically filled when creating the function class
	public string Country { get; set; }  
	public decimal AdjustmentValue { get; set; } 

	public SpendingThresholdFunction(IAwesomeSpendingThresholdComputer awesomeSpendingThresholdComputer)
	{
		_awesomeSpendingThresholdComputer = awesomeSpendingThresholdComputer;
	}

	public object Execute(ICodeFlowContext context, params object[] inputs)
	{
		// Another key point here is that function parameters (Country=inputs[0], AdjustmentValue=inputs[1]) are also available in inputs array.  
		// we will talk about Foreach loop later, but for now remember that if our function is called in a foreach loop 
		// inputs[0 ... n] will contains loop values and  inputs[n+1 ... n+p] wil contains parameter values
		var customers = context.GetCollection<Customer>("Customers");
		var orders = context.GetCollection<Order>("Orders");
		var formated3Ddata = FormatInputsAs3DBeforeComputation(customers, orders);
		return _awesomeSpendingThresholdComputer.Compute(Country, AdjustmentValue,formated3Ddata);   
	}

	private object[,,] FormatInputsAs3DBeforeComputation(List<Customer> customers, List<Order> orders)
	{
		//todo write 3D formating logic 
		return new object[,,] { };
	}
}

public interface ISpendingThresholdFunction : ICodeFlowActivityFunction
{
}

public interface IAwesomeSpendingThresholdComputer
{
	decimal Compute(string country, decimal adjustmentValue, object[,,] dataMatrix);
}

public class AwesomeSpendingThresholdComputer : IAwesomeSpendingThresholdComputer
{
	public decimal Compute(string country, decimal adjustmentValue, object[,,] dataMatrix)
	{
		throw new NotImplementedException();
	}
}

Of course we need to register our new classes to the Dependency injection container.

kernel.Bind<IAwesomeSpendingThresholdComputer>().To<AwesomeSpendingThresholdComputer>();
kernel.Bind<ISpendingThresholdFunction>().To<SpendingThresholdFunction>();

Finally we can use our new function in the ai codeflow.

var aicodeFlow = new CodeFlow();
aicodeFlow.StartNew(cfg => {
	cfg.WithContext(() => new AiContext())
	.UseMiddleware<IExceptionHandlingMiddleware>()  
	.UseActivityFilter<ILogActivityFilter>()  
	.WithServiceResolver(serviceResolver);
})
.Do<ILoadCustomerActivity>()  
.Do<ILoadCustomerOrdersActivity>() 
.CallCodeFlow(FillAndCleanCustomerData)
.Function<ISpendingThresholdFunction>()
	.WithArg<string>("Country", (ctx, inputs) => "France")
	.WithArg<decimal>("AdjustmentValue", (ctx, inputs) => 1.85M)
	.WithResult<decimal>((ctx, inputs, res) => ctx.SetValue<decimal>("SpendingThreshold", res))
.Close()
.Close();

aicodeFlow.Execute();

Continue reading: ContainerFlow

⚠️ **GitHub.com Fallback** ⚠️