Files
jube-fork/Jube.Sandbox/Example.cs
richard-churchman bc66cd80e3 Expanded Inline Script Invocation Coverage and Implemented Activation Rule Chaining
* Updated documentation to include Inline Script events and Activation Rule Chaining concepts.
* Included Activation Rules in Activation Completions to make them available.
* Modified activation rule signatures to include match keys for facilitation.
* Updated Gateway Rule Priority to support double type, including changes in migrations and POCOs.
* Updated tree to ensure entities are listed by Priority when Priority exists in the entity.
* Included Priority in Activation Rule user interface, POCOs, and migrations; ensured invocation and synchronization preserve order for evaluation sequence to support rule chaining.
* Updated completions to support Activation Rules and Activation Rule chaining.
* Improved delegate compilation to avoid reflection during invocation; delegates are now compiled during synchronization, eliminating reflection usage during invocation for real-time processing.
* Created event attributes to direct matching of Inline Scripts, including matching properties given event context (to restrict to a single Activation Rule, for example).
* Separated ExecuteAsync attributes from property attributes in the solution for better organization.
* Mapped Inline Script property and method attributes during synchronization to avoid reflection during invocation.
* Removed parallel for loops and locks in Activation Rule processing to support chaining.
* Modified reflection wrapper for Inline Scripts to eliminate reflection by compiling delegates during synchronization.
* Minor refactoring and variable renaming.
* Updated Inline Script interface to mandate bool return value in ExecuteAsync method.
* Updated rule parser to support evaluation of exists activation rule match for chaining support.
* Updated sandbox code for new Inline Script format.
* Corrected some spelling in documentation.
2026-02-12 12:51:52 +02:00

62 lines
3.5 KiB
C#

// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
//The following are the minimum .Net libraries to make this work, however, if using the Jube.Sandbox, this will be shown, as the project file is set to not default any assemblies and will be a like for like compile.
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
//All assemblies are passed on the basis of what the Jube.App context is using, so nearly all libraries are supported.
using Newtonsoft.Json.Linq;
//The following are Jube libraries that contain the context and Inline Script attributes.
using Jube.Engine.Attributes;
using Jube.Engine.Attributes.Events;
using Jube.Engine.Attributes.Properties;
using Jube.Engine.EntityAnalysisModelInvoke.Context;
using Jube.Engine.Interfaces;
public class Example : IInlineScript//Class entry point is available in table configuration.
{
//Attributes overlap with the same options available in Request XPath
[ReportTable] //ReportTable is evaluated on recall.
[ResponsePayload]//ResponsePayload is evaluated in model synchronization and used in model response preparation.
[Latitude] //Latitude is evaluated in model synchronization and is used during model activation watcher dispatch.
[Longitude] //Longitude is evaluated in model synchronization and is used during model activation watcher dispatch.
[SearchKey(SearchKeyTtlInterval = "h",
SearchKeyTtlIntervalValue = 1,
SearchKeyFetchLimit = 100,
SearchKeyCache = false,
SearchKeyCacheInterval = "d",
SearchKeyCacheValue = 1,
SearchKeyCacheSample = true,
SearchKeyCacheFetchLimit = 100,
SearchKeyCacheTtlInterval = "h",
SearchKeyCacheTtlValue = 1)] //SearchKey ensures that this is exposed in for aggregation in both the background engine.
public string UserAgent { get; set; } = string.Empty;//Public properties are available for processing, being analogous, when taken together with attributes, to a Request XPath entry
[ActivationRuleOverrideEvent(Guid="bc81ff60-3254-4f1a-9003-ecae5e114142", Priority = 1)]
[PayloadEvent]
public async Task<bool> ExecuteAsync(Context context)//Method entry point. The Context object gives access to all resources that would otherwise be available during invocation.
{
//Example HTTP Call with the fetching of data from the context.
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", "MyApp/1.0");
client.DefaultRequestHeaders.Add("IP", context.EntityAnalysisModelInstanceEntryPayload.Payload["IP"]);//Here we are looking at the context, and while we are only extracting data here, full access to all application resources are available in this context, such as logging.
var response = await client.GetStringAsync("https://postman-echo.com/get");//Get the data from remote using the standard HTTP client.
//Example Parse and transpose to payload.
var jObject = JObject.Parse(response);//The response stream is processed using Newtonsoft
#pragma warning disable CS8600// Converting null literal or possible null value to non-nullable type.
var userAgent = (string)jObject["headers"]?["user-agent"] ?? "Unknown";
#pragma warning restore CS8600// Converting null literal or possible null value to non-nullable type.
UserAgent = userAgent;
return true; //Behavior depends upon the event, but in all cases properties will only be extracted to payload on return true;
}
}