Skip to content

Commit

Permalink
updated functions, upgraded state machine, and fixed various problems…
Browse files Browse the repository at this point in the history
… to relax the ruleset from a FSM system to a visual scripting lite solution
  • Loading branch information
EarliestFall988 committed Dec 11, 2023
1 parent 07c0518 commit 713a4b6
Show file tree
Hide file tree
Showing 20 changed files with 226 additions and 111 deletions.
19 changes: 13 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
var apiKey = env["API_KEY"];
var valkApiKey = "some key";

long minuteCountWaitTime = 5;
long minuteCountWaitTime = 60000;
StateMachinesController? stateMachinesController = new StateMachinesController(minuteCountWaitTime);


Expand Down Expand Up @@ -60,7 +60,7 @@
app.MapGet("/api/v1/sync", async (HttpContext ctx) =>
{

Debug.WriteLine("test 0");
//Debug.WriteLine("test 0");

string instructionId = ctx.Request.Headers["id"];
string key = ctx.Request.Headers["apikey"];
Expand Down Expand Up @@ -135,6 +135,8 @@
return "No instruction ID provided";
}

Debug.WriteLine("check1");

var ValkyireServerController = new ValkyrieServerController()
{
ValkyrieAPIKey = valkApiKey
Expand All @@ -152,23 +154,28 @@

string id = Guid.NewGuid().ToString();

stateMachinesController.AddMachine(id, response.content);
var machine = stateMachinesController.AddMachine(id, response.content);

(bool complete, string result) status = (false, "Not completed - state machine took too long.");

long tick = 0;


var totalTime = (60000 / 2) * minuteCountWaitTime;
var totalTime = (60000 / 20) * minuteCountWaitTime;

while (!status.complete && tick < totalTime && !context.RequestAborted.IsCancellationRequested)
{

Debug.WriteLine("status: " + status.complete + " " + tick);
//Debug.WriteLine(tick);

if (machine != null)
{
Debug.WriteLine(machine.Variables["total labor"].ToJSON());
}

status = stateMachinesController.HandleStatus(id);

Thread.Sleep(0); // wait for the machine to complete
Thread.Sleep(1); // wait for the machine to complete
tick++;
}

Expand Down
17 changes: 6 additions & 11 deletions State Machine/Function Definitions/Math/AddNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void Setup()
{
{ "a", new Parameter(StateMachineVariableType.Decimal.ToString().ToLower()) },
{ "b", new Parameter(StateMachineVariableType.Decimal.ToString().ToLower()) },
{ "out", new Parameter(StateMachineVariableType.Decimal.ToString().ToLower(), VariableIO.Out) }
{ "out", new Parameter(StateMachineVariableType.Decimal.ToString().ToLower(), io:VariableIO.Out) }
};
}

Expand All @@ -26,22 +26,17 @@ protected override void DefineFunction()
Name = nameof(AddNumber);
Function = () =>
{
var x = Parameters["a"];
var y = Parameters["b"];
var a = Get<float>("a");
var b = Get<float>("b");

var z = Parameters["out"];

if (x is VariableDefinition<decimal> a && y is VariableDefinition<decimal> b && z is VariableDefinition<decimal> result)
{
Debug.WriteLine("\t adding " + a + " + " + b + " " + (a + b).ToString());

Debug.WriteLine($"{a} + {b} = {a.Value + b.Value}");
Set("out", a + b);

result.Value = a.Value + b.Value;
return 1;

return 1;
}

return -1;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ void Setup()
Description = "Convert a decimal type variable to integer type variable. Rounds up if the value is 0.5 or greater.";
ExpectedParameters = new Dictionary<string, Parameter>()
{
{ "decimal", new Parameter("decimal", VariableIO.In) },
{ "integer", new Parameter("integer", VariableIO.Out) },
{ "decimal", new Parameter("decimal", io:VariableIO.In) },
{ "integer", new Parameter("integer", io:VariableIO.Out) },
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ void Setup()
Description = "Convert an integer type variable to decimal type variable";
ExpectedParameters = new Dictionary<string, Parameter>()
{
{ "integer", new Parameter("integer", VariableIO.In) },
{ "decimal", new Parameter("decimal", VariableIO.Out) }
{ "integer", new Parameter("integer", io:VariableIO.In) },
{ "decimal", new Parameter("decimal", io:VariableIO.Out) }
};
}

Expand Down
25 changes: 5 additions & 20 deletions State Machine/Function Definitions/Math/DivideNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,15 @@ protected override void DefineFunction()
Name = nameof(DivideNumber);
Function = () =>
{
var x = Parameters["a"];
var y = Parameters["b"];
var a = Get<float>("a");
var b = Get<float>("b");

var z = Parameters["out"];

if (x is VariableDefinition<decimal> aV && y is VariableDefinition<decimal> bV && z is VariableDefinition<decimal> resultV)
{
var b = bV.Value;
var a = aV.Value;
Debug.WriteLine("\t dividing " + a + " / " + b + " " + (a / b).ToString());

if (b == 0)
{
Debug.WriteLine("Cannot divide by zero");
return -1;
}
Set("out", a / b);

Debug.WriteLine($"{a} / {b} = {a / b}");

resultV.Value = a / b;

return 1;
}

return -1;
return 1;
};
}

Expand Down
18 changes: 15 additions & 3 deletions State Machine/Function Definitions/Respond.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using ValkyrieFSMCore;
using System.Diagnostics;
using System.Text.Json;

using ValkyrieFSMCore;

namespace Valkyrie_Server.State_Machine.Function_Definitions
{
Expand Down Expand Up @@ -27,8 +30,17 @@ protected override void DefineFunction()

if (StateMachine != null)
{
StateMachine.Result = Get<object>("data").ToString() ?? "nothing to respond";
StateMachine.FallbackState = StateMachine.States.Find(x => x.Function.GetHashCode() == this.Function.GetHashCode()); //hijack the fallback state so the sm stops normally after this state

var res = Parameters["data"];


Debug.WriteLine($"Responding with {res.ToJSON()}");

StateMachine.Result = res.ToJSON();

var state = StateMachine.States.Find(x => x.Function.GetHashCode() == this.Function.GetHashCode());
if (state != null)
state.FallbackState = true; //hijack the fallback state prop so the sm stops normally after this state
return 1;
}

Expand Down
7 changes: 5 additions & 2 deletions State Machine/Function Definitions/WM/EachProject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ValkyrieFSMCore
using System.Diagnostics;

namespace ValkyrieFSMCore
{
public class EachProject : FunctionDefinition
{
Expand All @@ -25,13 +27,14 @@ protected override void DefineFunction()
{
Function = () =>
{
var proj = Get<List<Project>>("project");
var proj = Get<List<Project>>("projects");

if (proj is List<Project> projects)
{
if (iterator < projects.Count)
{
Set("project", projects[iterator]);
Debug.WriteLine($"\t\t {iterator} -> {projects[iterator].Name}");
iterator++;
return 0;
}
Expand Down
5 changes: 5 additions & 0 deletions State Machine/Function Definitions/WM/GetProjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ protected override void DefineFunction()
if (projects == null)
projects = new ProjectsListResult();

foreach(var x in projects.projects)
{
Debug.WriteLine(x.Name);
}

Set("result", projects.projects);
return 1;
}
Expand Down
16 changes: 8 additions & 8 deletions State Machine/Function Definitions/WM/SplitProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ void Setup()
ExpectedParameters = new Dictionary<string, Parameter>()
{
{ "project", new Parameter("project") },
{ "name", new Parameter("string", VariableIO.Out) },
{ "id", new Parameter("string", VariableIO.Out) },
{ "TotalManHours", new Parameter("decimal", VariableIO.Out) },
{ "laborCost", new Parameter("decimal", VariableIO.Out) },
{ "name", new Parameter("string", required: false, io:VariableIO.Out) },
{ "id", new Parameter("string", required: false, io:VariableIO.Out) },
{ "TotalManHours", new Parameter("decimal", required: false, io:VariableIO.Out) },
{ "laborCost", new Parameter("decimal", required: false, io:VariableIO.Out) },
};
}

Expand All @@ -31,10 +31,10 @@ protected override void DefineFunction()

var project = Get<Project>("project");

Set("name", project.Name);
Set("id", project.Id);
Set("TotalManHours", project.TotalManHours);
Set("laborCost", project.LaborCost);
TrySet("name", project.Name);
TrySet("id", project.Id);
TrySet("TotalManHours", project.TotalManHours);
TrySet("laborCost", project.LaborCost);

return 1;
};
Expand Down
Loading

0 comments on commit 713a4b6

Please sign in to comment.