diff --git a/State Machine/Function Definitions/GreetFunction.cs b/State Machine/Function Definitions/GreetFunction.cs index 63ec3cd..bdd56bc 100644 --- a/State Machine/Function Definitions/GreetFunction.cs +++ b/State Machine/Function Definitions/GreetFunction.cs @@ -21,9 +21,9 @@ protected override void DefineFunction() Name = "GreetUser"; - ExpectedParameters = new Dictionary() + ExpectedParameters = new Dictionary() { - { "name", new ReferenceTuple(StateMachineVariableType.Text, false) } + { "name", new Parameter(StateMachineVariableType.Text) } }; Func GreetUser = () => @@ -37,25 +37,28 @@ protected override void DefineFunction() Debug.WriteLine("What is your Name?"); result = "John Doe"; // Console.ReadLine(); - if (result == null || result == string.Empty) - { - Debug.WriteLine("That's not your name!"); - } + var nameV = Parameters["name"]; - if (result?.ToLower().Trim() == "exit") + if (nameV is VariableDefinition name) { - return -1; - } - // bool success = stateMachine.WriteValue(0, 0, result?.Replace(',', '\'') ?? ""); + if (result == null || result == string.Empty) + { + Debug.WriteLine("That's not your name!"); + } + + if (result?.ToLower().Trim() == "exit") + { + return -1; + } - Parameters["name"].SetValue(result?.Replace(',', '\'') ?? ""); + name.Value = result?.Replace(',', '\'') ?? ""; - // if (!success) - // { - // Debug.WriteLine("could not write to store"); - // return -1; - // } + } + else + { + return -1; + } } return 1; diff --git a/State Machine/Function Definitions/GuessFunction.cs b/State Machine/Function Definitions/GuessFunction.cs index 10d4203..2210492 100644 --- a/State Machine/Function Definitions/GuessFunction.cs +++ b/State Machine/Function Definitions/GuessFunction.cs @@ -25,10 +25,10 @@ protected override void DefineFunction() // StateMachineVariableType.Integer // guess // }; - ExpectedParameters = new Dictionary() + ExpectedParameters = new Dictionary() { - { "name", new ReferenceTuple(StateMachineVariableType.Text, false) }, - { "guess", new ReferenceTuple(StateMachineVariableType.Integer, false) } + { "name", new Parameter(StateMachineVariableType.Text) }, + { "guess", new Parameter(StateMachineVariableType.Integer) } }; Name = "Guess"; @@ -36,8 +36,14 @@ protected override void DefineFunction() Function = () => { - string name = Parameters["name"].GetText(); - int guessNumber = Parameters["guess"].GetInt(); + VariableDefinition? nameVarCheck = Parameters["name"] as VariableDefinition; + VariableDefinition? guessNumberVarCheck = Parameters["guess"] as VariableDefinition; + + if (nameVarCheck == null || guessNumberVarCheck == null) + return -1; + + var name = nameVarCheck.Value; + var guessNumber = guessNumberVarCheck.Value; Debug.WriteLine($"Okay {name}, Guess a number"); string? number = guessNumber.ToString(); // Console.ReadLine(); @@ -70,6 +76,7 @@ protected override void DefineFunction() Debug.WriteLine("Incorrect. Try Again"); return 0; } + }; } } diff --git a/State Machine/Function Definitions/Math/AddNumber.cs b/State Machine/Function Definitions/Math/AddNumber.cs index f3bd6e3..3ff3d9d 100644 --- a/State Machine/Function Definitions/Math/AddNumber.cs +++ b/State Machine/Function Definitions/Math/AddNumber.cs @@ -13,11 +13,11 @@ public AddNumber() void Setup() { - ExpectedParameters = new Dictionary() + ExpectedParameters = new Dictionary() { - { "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, - { "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, - { "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) } + { "a", new Parameter(StateMachineVariableType.Decimal) }, + { "b", new Parameter(StateMachineVariableType.Decimal) }, + { "out", new Parameter(StateMachineVariableType.Decimal, VariableIO.Out) } }; } @@ -26,14 +26,22 @@ protected override void DefineFunction() Name = nameof(AddNumber); Function = () => { - var a = Parameters["a"].GetDecimal(); - var b = Parameters["b"].GetDecimal(); + var x = Parameters["a"]; + var y = Parameters["b"]; - Debug.WriteLine($"{a} + {b} = {a + b}"); + var z = Parameters["out"]; - Parameters["out"].SetValue(a + b); + if (x is VariableDefinition a && y is VariableDefinition b && z is VariableDefinition result) + { - return 1; + Debug.WriteLine($"{a} + {b} = {a.Value + b.Value}"); + + result.Value = a.Value + b.Value; + + return 1; + } + + return -1; }; } diff --git a/State Machine/Function Definitions/Math/DivideNumber.cs b/State Machine/Function Definitions/Math/DivideNumber.cs index c7a9f9a..65bb23d 100644 --- a/State Machine/Function Definitions/Math/DivideNumber.cs +++ b/State Machine/Function Definitions/Math/DivideNumber.cs @@ -17,11 +17,11 @@ public DivideNumber() void Setup() { - ExpectedParameters = new Dictionary() + ExpectedParameters = new Dictionary() { - { "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, - { "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, - { "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) } + { "a", new Parameter(StateMachineVariableType.Decimal) }, + { "b", new Parameter(StateMachineVariableType.Decimal) }, + { "out", new Parameter(StateMachineVariableType.Decimal, VariableIO.Out) } }; } @@ -30,20 +30,30 @@ protected override void DefineFunction() Name = nameof(DivideNumber); Function = () => { - var a = Parameters["a"].GetDecimal(); - var b = Parameters["b"].GetDecimal(); + var x = Parameters["a"]; + var y = Parameters["b"]; - if (b == 0) + var z = Parameters["out"]; + + if (x is VariableDefinition aV && y is VariableDefinition bV && z is VariableDefinition resultV) { - Debug.WriteLine("Cannot divide by zero"); - return -1; - } + var b = bV.Value; + var a = aV.Value; + + if (b == 0) + { + Debug.WriteLine("Cannot divide by zero"); + return -1; + } - Debug.WriteLine($"{a} / {b} = {a / b}"); + Debug.WriteLine($"{a} / {b} = {a / b}"); - Parameters["out"].SetValue(a / b); + resultV.Value = a / b; + + return 1; + } - return 1; + return -1; }; } diff --git a/State Machine/Function Definitions/Math/MultiplyNumber.cs b/State Machine/Function Definitions/Math/MultiplyNumber.cs index 350fd19..45c494a 100644 --- a/State Machine/Function Definitions/Math/MultiplyNumber.cs +++ b/State Machine/Function Definitions/Math/MultiplyNumber.cs @@ -16,11 +16,11 @@ public MultiplyNumber() void Setup() { - ExpectedParameters = new Dictionary() + ExpectedParameters = new Dictionary() { - { "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, - { "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, - { "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) } + { "a", new Parameter(StateMachineVariableType.Decimal) }, + { "b", new Parameter(StateMachineVariableType.Decimal) }, + { "out", new Parameter(StateMachineVariableType.Decimal, VariableIO.Out) } }; } @@ -29,14 +29,24 @@ protected override void DefineFunction() Name = nameof(MultiplyNumber); Function = () => { - var a = Parameters["a"].GetDecimal(); - var b = Parameters["b"].GetDecimal(); + var x = Parameters["a"]; + var y = Parameters["b"]; - Debug.WriteLine($"{a} * {b} = {a * b}"); + var z = Parameters["out"]; - Parameters["out"].SetValue(a * b); + if (x is VariableDefinition aV && y is VariableDefinition bV && z is VariableDefinition resultV) + { + var b = bV.Value; + var a = aV.Value; - return 1; + Debug.WriteLine($"{a} * {b} = {a * b}"); + + resultV.Value = a * b; + + return 1; + } + + return -1; }; } } diff --git a/State Machine/Function Definitions/Math/Pow.cs b/State Machine/Function Definitions/Math/Pow.cs index 34bd3b8..b61ad9e 100644 --- a/State Machine/Function Definitions/Math/Pow.cs +++ b/State Machine/Function Definitions/Math/Pow.cs @@ -16,11 +16,11 @@ public PowNumber() void Setup() { - ExpectedParameters = new Dictionary() + ExpectedParameters = new Dictionary() { - { "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, - { "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, - { "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) } + { "a", new Parameter(StateMachineVariableType.Decimal) }, + { "b", new Parameter(StateMachineVariableType.Decimal) }, + { "out", new Parameter(StateMachineVariableType.Decimal, VariableIO.Out) } }; } @@ -29,16 +29,27 @@ protected override void DefineFunction() Name = nameof(PowNumber); Function = () => { - var a = Parameters["a"].GetDecimal(); - var b = Parameters["b"].GetDecimal(); + var x = Parameters["a"]; + var y = Parameters["b"]; - var pow = Math.Pow((double)a, (double)b); + var z = Parameters["out"]; - Debug.WriteLine($"{a} to the power of {b} = {pow}"); + if (x is VariableDefinition aV && y is VariableDefinition bV && z is VariableDefinition resultV) + { - Parameters["out"].SetValue((decimal)pow); + var b = bV.Value; + var a = aV.Value; + + var powResult = Math.Pow((double)a, (double)b); + + Debug.WriteLine($"{a} to the power of {b} = {powResult}"); + + resultV.Value = (decimal)powResult; + return 1; + } + + return -1; - return 1; }; } } diff --git a/State Machine/Function Definitions/Math/SubtractNumber.cs b/State Machine/Function Definitions/Math/SubtractNumber.cs index 06857af..9196015 100644 --- a/State Machine/Function Definitions/Math/SubtractNumber.cs +++ b/State Machine/Function Definitions/Math/SubtractNumber.cs @@ -16,11 +16,11 @@ public SubtractNumber() void Setup() { - ExpectedParameters = new Dictionary() + ExpectedParameters = new Dictionary() { - { "a", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, - { "b", new ReferenceTuple(StateMachineVariableType.Decimal, false) }, - { "out", new ReferenceTuple(StateMachineVariableType.Decimal, false, VariableIO.Out) } + { "a", new Parameter(StateMachineVariableType.Decimal) }, + { "b", new Parameter(StateMachineVariableType.Decimal) }, + { "out", new Parameter(StateMachineVariableType.Decimal, VariableIO.Out) } }; } @@ -29,14 +29,25 @@ protected override void DefineFunction() Name = nameof(SubtractNumber); Function = () => { - var a = Parameters["a"].GetDecimal(); - var b = Parameters["b"].GetDecimal(); + var x = Parameters["a"]; + var y = Parameters["b"]; - Debug.WriteLine($"{a} + {b} = {a - b}"); + var z = Parameters["out"]; - Parameters["out"].SetValue(a - b); + if (x is VariableDefinition aV && y is VariableDefinition bV && z is VariableDefinition resultV) + { + + var b = bV.Value; + var a = aV.Value; + + Debug.WriteLine($"{a} + {b} = {a - b}"); + + resultV.Value = a - b; + return 1; + } + + return -1; - return 1; }; } } diff --git a/State Machine/Function Definitions/SetRandomNumberFunction.cs b/State Machine/Function Definitions/SetRandomNumberFunction.cs index b19b984..d8c2522 100644 --- a/State Machine/Function Definitions/SetRandomNumberFunction.cs +++ b/State Machine/Function Definitions/SetRandomNumberFunction.cs @@ -6,43 +6,43 @@ namespace Avalon { + /// + /// Set a random number + /// public sealed class SetRandomNumberFunction : FunctionDefinition { public SetRandomNumberFunction() { + Setup(); DefineFunction(); } - protected override void DefineFunction() + void Setup() { - Name = "SetRandomNumber"; + Description = "Set a random number within the range of min and max"; - ExpectedParameters = new Dictionary() + ExpectedParameters = new Dictionary() { - { "valueToChange", new ReferenceTuple(StateMachineVariableType.Integer, false) }, - { "min", new ReferenceTuple(StateMachineVariableType.Integer, false) }, - { "max", new ReferenceTuple(StateMachineVariableType.Integer, false) } + { "valueToChange", new Parameter(StateMachineVariableType.Integer) }, + { "min", new Parameter(StateMachineVariableType.Integer) }, + { "max", new Parameter(StateMachineVariableType.Integer) } }; + } + + protected override void DefineFunction() + { Func func = () => { - int valueToChange = Parameters["valueToChange"].GetInt(); - - int min = Parameters["min"].GetInt(); - - int max = Parameters["max"].GetInt(); Random random = new Random(); + int randomNumber = random.Next(Get("min"), Get("max")); + bool result = TrySet("valueToChange", randomNumber); - int randomNumber = random.Next(min, max); - - // Console.WriteLine($"The random number is {randomNumber}"); - - Parameters["valueToChange"].SetValue(randomNumber.ToString()); - return 1; + return result == true ? 1 : -1; }; Function = func; diff --git a/State Machine/FunctionDefinition.cs b/State Machine/FunctionDefinition.cs index 6d3e333..554620e 100644 --- a/State Machine/FunctionDefinition.cs +++ b/State Machine/FunctionDefinition.cs @@ -11,6 +11,9 @@ namespace Avalon /// public abstract class FunctionDefinition { + /// + /// Function to run + /// public Func Function { get; set; } = () => { return 0; }; //default function /// @@ -34,7 +37,7 @@ public abstract class FunctionDefinition /// /// /// - public Dictionary ExpectedParameters { get; set; } = new Dictionary(); + public Dictionary ExpectedParameters { get; set; } = new Dictionary(); /// /// Get the list of expected parameters @@ -47,7 +50,7 @@ public ExpectedParameter[] ExpectedParametersList foreach (var x in ExpectedParameters) { - parameters.Add(new ExpectedParameter(x.Key, x.Value.type.ToString())); + parameters.Add(new ExpectedParameter(x.Key, x.Value.IO.ToString(), x.Value.Type.ToString())); } return parameters.ToArray(); @@ -58,7 +61,92 @@ public ExpectedParameter[] ExpectedParametersList /// The dictionary of parameters /// /// - protected Dictionary Parameters { get; set; } = new Dictionary(); + protected Dictionary Parameters { get; set; } = new Dictionary(); + + + + public VariableDefinition GetVariableDefinitionParameter(string name) + { + var param = Parameters[name] as VariableDefinition; + + if (param == null) + { + throw new Exception("Parameter " + name + " is not of type " + typeof(T).Name); + } + + return param; + } + + /// + /// Get the parameter + /// + /// + /// + /// + /// + public T Get(string name) + { + + var param = GetVariableDefinitionParameter(name); + + return param.Value; + } + + /// + /// Try get the value from the parameter + /// + /// + /// + /// + /// + /// + public bool TryGet(string name, out T? value) + { + try + { + var param = GetVariableDefinitionParameter(name); + value = param.Value; + return true; + } + catch + { + value = default(T); + return false; + } + } + + /// + /// set the parameter + /// + /// + /// + /// + public void Set(string name, T value) + { + var param = GetVariableDefinitionParameter(name); + param.Value = value; + } + + /// + /// set the parameter if it exists + /// + /// + /// + /// + /// + public bool TrySet(string name, T value) + { + try + { + var param = GetVariableDefinitionParameter(name); + param.Value = value; + return true; + } + catch + { + return false; + } + } /// /// Inject the parameters into the function definition so the function knows what to call @@ -66,7 +154,7 @@ public ExpectedParameter[] ExpectedParametersList /// the list of parameters /// the result of the injection, outputs error messages /// - public bool TryInjectParameters(Dictionary parameters, out string result) + public bool TryInjectParameters(Dictionary parameters, out string result) { if (parameters.Count == ExpectedParameters.Count) { @@ -75,14 +163,14 @@ public bool TryInjectParameters(Dictionary parameters { if (ExpectedParameters.ContainsKey(x.Key)) { - if (ExpectedParameters[x.Key].type == x.Value.Type) + if (ExpectedParameters[x.Key].Type == x.Value.Type) { Parameters.Add(x.Key, x.Value); - ExpectedParameters[x.Key] = new ReferenceTuple(ExpectedParameters[x.Key].type, true); + ExpectedParameters[x.Key] = new Parameter(ExpectedParameters[x.Key].Type, parameterInjectedSuccessfully: true); } else { - result = "Parameter type mismatch. The expected type of parameter " + x.Value.Key + " is " + ExpectedParameters[x.Key].type + " but " + x.Value.Type + " was provided for the function " + Name + "."; + result = "Parameter type mismatch. The expected type of parameter " + x.Value.Key + " is " + ExpectedParameters[x.Key].Type + " but " + x.Value.Type + " was provided for the function " + Name + "."; return false; } } @@ -93,7 +181,7 @@ public bool TryInjectParameters(Dictionary parameters List keys = new List(); foreach (var y in ExpectedParameters) { - if (y.Value.applied == false) + if (y.Value.InjectedSuccessfully == false) { keys.Add(y.Key); } @@ -119,7 +207,7 @@ public bool TryGetVariableType(string name, out StateMachineVariableType result) { if (ExpectedParameters.ContainsKey(name)) { - result = ExpectedParameters[name].type; + result = ExpectedParameters[name].Type; return true; } @@ -135,14 +223,15 @@ public struct ExpectedParameter { public string Name { get; init; } public string Type { get; init; } - public string Description { get; init; } + public string IO { get; init; } - public ExpectedParameter(string name, string type, string description = "") + public ExpectedParameter(string name, string type, string io, string description = "") { Name = name; Type = type; Description = description; + IO = io; } } } \ No newline at end of file diff --git a/State Machine/KeyTypeDefinition.cs b/State Machine/KeyTypeDefinition.cs index 9e8aed0..c1e06aa 100644 --- a/State Machine/KeyTypeDefinition.cs +++ b/State Machine/KeyTypeDefinition.cs @@ -4,12 +4,13 @@ using System.Threading.Tasks; + namespace Avalon { /// /// The Key Type Definition handles the type of the key, name, and the value of the key. /// - public class KeyTypeDefinition + public class KeyTypeDefinition : IVariableType { /// /// The name of the key. @@ -29,6 +30,10 @@ public class KeyTypeDefinition /// public string Value { get; private set; } = ""; + /// + /// Is the value an out value? + /// + public VariableIO IO { get; set; } = VariableIO.In; /// /// Default constructor. @@ -96,16 +101,28 @@ public void SetValue(decimal value) { this.Value = value.ToString(); } - } - /// - /// The type of the key. - /// - public enum StateMachineVariableType - { - Text, //text - Decimal, //decimal - Integer, //int - YesNo, //boolean + + public void SetValue(bool value) + { + if (value) + { + this.Value = "yes"; + } + else + { + this.Value = "no"; + } + } + + public void SetValue(int value) + { + this.Value = value.ToString(); + } + + public void SetValue(double value) + { + this.Value = value.ToString(); + } } } \ No newline at end of file diff --git a/State Machine/Parameter.cs b/State Machine/Parameter.cs new file mode 100644 index 0000000..495f5d7 --- /dev/null +++ b/State Machine/Parameter.cs @@ -0,0 +1,41 @@ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Avalon +{ + /// + /// This is so I can fix the bug where I cannot pass pointers to the correct state function. Just google: value vs reference + /// + public struct Parameter : IVariableType + { + public StateMachineVariableType Type { get; set; } + + /// + /// Has the parameter been injected successfully? + /// + public bool InjectedSuccessfully { get; private set; } + + public VariableIO IO { get; set; } + + /// + /// Key not used for this class. + /// + public string Key { get; set; } = ""; + + /// + /// the description of the parameter + /// + public string Description { get; set; } = ""; + + public Parameter(StateMachineVariableType type, VariableIO io = VariableIO.In, bool parameterInjectedSuccessfully = false, string description = "") + { + Type = type; + this.InjectedSuccessfully = parameterInjectedSuccessfully; + this.IO = io; + this.Description = description; + } + } +} \ No newline at end of file diff --git a/State Machine/ReferenceTuple.cs b/State Machine/ReferenceTuple.cs deleted file mode 100644 index cee4ff0..0000000 --- a/State Machine/ReferenceTuple.cs +++ /dev/null @@ -1,33 +0,0 @@ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Avalon -{ - /// - /// This is so I can fix the bug where I cannot pass pointers to the correct state function. Just google: value vs reference - /// - public class ReferenceTuple - { - public StateMachineVariableType type { get; set; } - public bool applied { get; set; } - - public VariableIO VariableIO { get; set; } - - public ReferenceTuple(StateMachineVariableType type, bool applied, VariableIO io = VariableIO.In, string description = "") - { - this.type = type; - this.applied = applied; - this.VariableIO = io; - } - } - - - public enum VariableIO - { - In, - Out - } -} \ No newline at end of file diff --git a/State Machine/StateMachine.cs b/State Machine/StateMachine.cs index ffc480d..8f55f11 100644 --- a/State Machine/StateMachine.cs +++ b/State Machine/StateMachine.cs @@ -21,7 +21,7 @@ public class StateMachine /// public List States = new List(); - public Dictionary Variables = new Dictionary(); + public Dictionary Variables = new Dictionary(); /// /// The current state of the machine. diff --git a/State Machine/StateMachineBuilder.cs b/State Machine/StateMachineBuilder.cs index dd92670..9072ef9 100644 --- a/State Machine/StateMachineBuilder.cs +++ b/State Machine/StateMachineBuilder.cs @@ -18,7 +18,7 @@ public class StateMachineBuilder private Dictionary functions; private Dictionary States; private Dictionary Transitions; - private Dictionary Variables; + private Dictionary Variables; private FunctionLibrary FunctionLibrary; StateMachine StateMachine; @@ -36,7 +36,7 @@ public StateMachineBuilder() functions = new Dictionary(); States = new Dictionary(); Transitions = new Dictionary(); - Variables = new Dictionary(); + Variables = new Dictionary(); } public Func GetDefaultFunction() @@ -205,7 +205,7 @@ private bool GetType(string type, out StateMachineVariableType result) } } - private void InjectParameters(FunctionDefinition function, Dictionary parameters, int lineNumber) + private void InjectParameters(FunctionDefinition function, Dictionary parameters, int lineNumber) { if (parameters.Count > 0 && function != null) { @@ -307,77 +307,33 @@ public StateMachine ParseInstructionsJSON(string json) Debug.WriteLine($"adding variable {name} ({variableType}) = {value}."); } - Variables.Add(name, new KeyTypeDefinition(name, variableType, value)); - - // if (foundProperty && visibility == "ref") //handle for other objects to reference this variable - // { - // switch (variableType) - // { - // case StateMachineVariableType.Text: - // if (stringRefDict.ContainsKey(name)) - // { - // var str = stringRefDict[name]; - // Variables.Add(name, new KeyTypeDefinition(name, variableType, str)); - // } - // break; - // case StateMachineVariableType.Single: - // if (floatRefDict.ContainsKey(name)) - // { - // var flt = floatRefDict[name]; - // Variables.Add(name, new KeyTypeDefinition(name, variableType, flt)); - // } - // break; - // case StateMachineVariableType.GameObject: - // if (GameObjectRefDict.ContainsKey(name)) - // { - // var obj = GameObjectRefDict[name]; - // Variables.Add(name, new KeyTypeDefinition(name, variableType, obj)); - // } - // break; - // } - // } - // else - // { - - // if (!Variables.ContainsKey(name)) - // if (variableType != StateMachineVariableType.Single) - // Variables.Add(name, new KeyTypeDefinition(name, variableType, value)); - // else - // Variables.Add(name, new KeyTypeDefinition(name, variableType, float.Parse(value))); - // } - - // foreach (var t in GameObjectRefDict.Keys) - // { - // if (Variables.ContainsKey(t)) continue; - - // var obj = GameObjectRefDict[t]; - // Variables.Add(t, new KeyTypeDefinition(t, StateMachineVariableType.GameObject, obj)); // <- error here - // } - - // foreach (var t in stringRefDict.Keys) - // { - - // if (Variables.ContainsKey(t)) continue; - - // var str = stringRefDict[t]; - // Variables.Add(t, new KeyTypeDefinition(t, StateMachineVariableType.Text, str)); - // } - - // foreach (var t in floatRefDict.Keys) - // { - - // if (Variables.ContainsKey(t)) continue; - - // var flt = floatRefDict[t]; - // Variables.Add(t, new KeyTypeDefinition(t, StateMachineVariableType.Single, flt)); - // } - + switch (variableType) + { + case StateMachineVariableType.Text: + Variables.Add(name, VariableDefinition.CreateString(name, value)); + break; + case StateMachineVariableType.Decimal: + Variables.Add(name, VariableDefinition.CreateDecimal(name, decimal.Parse(value))); + break; + + case StateMachineVariableType.Integer: + Variables.Add(name, VariableDefinition.CreateInt(name, int.Parse(value))); + break; + + case StateMachineVariableType.YesNo: + Variables.Add(name, VariableDefinition.CreateBool(name, bool.Parse(value))); + break; + + default: + Variables.Add(name, new KeyTypeDefinition(name, variableType, value)); + break; + } + //Variables.Add(name, new KeyTypeDefinition(name, variableType, value)); createdVariables = true; } - foreach (var x in functionsJson.EnumerateArray()) { var name = x.GetProperty("name").GetString(); @@ -387,9 +343,9 @@ public StateMachine ParseInstructionsJSON(string json) - Dictionary parameterList = new Dictionary(); + Dictionary parameterList = new Dictionary(); - Dictionary injectionVariables = new Dictionary(); + Dictionary injectionVariables = new Dictionary(); if (name == null) throw new Exception($"Invalid function definition. A valid name must be given after the definition."); @@ -465,7 +421,7 @@ public StateMachine ParseInstructionsJSON(string json) if (!result) throw new Exception($"Invalid function parameter definition. A valid type must be given after the name. ({paramName} : {paramType})"); - parameterList.Add(paramName, new ReferenceTuple(variableType, true)); + parameterList.Add(paramName, new Parameter(variableType, parameterInjectedSuccessfully: true)); if (!Variables.TryGetValue(varToConnectName, out var variable)) throw new Exception($"Invalid function parameter definition. The connection variable \"{varToConnectName}\" does not exist. ({name} - {paramName} : {paramType})"); @@ -649,7 +605,7 @@ public StateMachine ParseInstructionsJSON(string json) foreach (var y in x.ExpectedParameters) { - pmtrs += y.Key + ": " + y.Value.type + ", "; + pmtrs += y.Key + ": " + y.Value.Type + ", "; } pmtrs = pmtrs.Trim().TrimEnd(','); @@ -669,7 +625,7 @@ public StateMachine ParseInstructionsJSON(string json) Debug.WriteLine("\n\t>Program Loaded.\n"); - foreach(var x in functions.Values) + foreach (var x in functions.Values) { x.StateMachine = StateMachine; } @@ -696,7 +652,7 @@ public async Task ParseInstructions(string filePath) var importFunctions = false; FunctionDefinition? currentFunction = null; // default function - Dictionary parameters = new Dictionary(); + Dictionary parameters = new Dictionary(); var hasImportedFunctions = false; var defineTransitions = false; @@ -1097,7 +1053,7 @@ public async Task ParseInstructions(string filePath) foreach (var y in x.ExpectedParameters) { - pmtrs += y.Key + ": " + y.Value.type + ", "; + pmtrs += y.Key + ": " + y.Value.Type + ", "; } pmtrs = pmtrs.Trim().TrimEnd(','); diff --git a/State Machine/StateMachineVariableType.cs b/State Machine/StateMachineVariableType.cs new file mode 100644 index 0000000..e45d294 --- /dev/null +++ b/State Machine/StateMachineVariableType.cs @@ -0,0 +1,20 @@ +namespace Avalon +{ + /// + /// The type of the key. + /// + public enum StateMachineVariableType + { + Text, //text + Decimal, //decimal + Integer, //int + YesNo, //boolean + + ListText, //list of text + ListDecimal, //list of decimal + ListInteger, //list of int + ListYesNo, //list of boolean + + Object //object + } +} diff --git a/State Machine/VariableTypes/IVariableType.cs b/State Machine/VariableTypes/IVariableType.cs new file mode 100644 index 0000000..094a192 --- /dev/null +++ b/State Machine/VariableTypes/IVariableType.cs @@ -0,0 +1,14 @@ +using Avalon; + +namespace Avalon +{ + public interface IVariableType + { + + public StateMachineVariableType Type { get; set; } + + public VariableIO IO { get; set; } + + public string Key { get; set; } + } +} diff --git a/State Machine/VariableTypes/ObjectType.cs b/State Machine/VariableTypes/ObjectType.cs new file mode 100644 index 0000000..f91511b --- /dev/null +++ b/State Machine/VariableTypes/ObjectType.cs @@ -0,0 +1,48 @@ +using Avalon; + +namespace Avalon +{ + /// + /// A class representing an object type. + /// + public class ObjectType : IVariableType + { + /// + /// The variables of the object. + /// + Dictionary _variables { get; set; } = new Dictionary(); + + /// + /// Enumerate through the variables. + /// + public IEnumerable Variables => _variables.Values; + + public StateMachineVariableType Type { get; set; } = StateMachineVariableType.Object; + + public VariableIO IO { get; set; } + + public string Key { get; set; } + + public ObjectType(string key, StateMachineVariableType type, VariableIO objIo) + { + Key = key; + Type = type; + IO = objIo; + } + + public bool TryGetItem(string name, out T? res) where T : IVariableType + { + if (_variables.TryGetValue(name, out IVariableType? item)) + { + if (item is T) + { + res = (T)item; + return true; + } + } + + res = default(T); + return false; + } + } +} diff --git a/State Machine/VariableTypes/VariableDefinition.cs b/State Machine/VariableTypes/VariableDefinition.cs new file mode 100644 index 0000000..37e62d7 --- /dev/null +++ b/State Machine/VariableTypes/VariableDefinition.cs @@ -0,0 +1,93 @@ + +namespace Avalon +{ + /// + /// Set a mutable variable definition. + /// + /// the type of the variable + public class VariableDefinition : IVariableType + { + public T Value { get; set; } + + public StateMachineVariableType Type { get; set; } + + public VariableIO IO { get; set; } + + public string Key { get; set; } + + /// + /// The default constructor. + /// + /// The key of the variable. + /// The value of the variable. + /// The type of the variable. + /// Whether or not the variable is an out value. + public VariableDefinition(string key, T value, StateMachineVariableType type, VariableIO variableIo = VariableIO.In) + { + Key = key; + Value = value; + Type = type; + IO = variableIo; + } + + /// + /// Check if two variable definitions are the same type. + /// + /// The first variable definition. + /// The second variable definition. + /// + public static bool IsSameType(VariableDefinition a, VariableDefinition b) + { + return a.Type == b.Type; + } + + #region create primitive types + + public static VariableDefinition CreateString(string key, string value, VariableIO varIo = VariableIO.In) + { + return new VariableDefinition(key, value, StateMachineVariableType.Text, varIo); + } + + public static VariableDefinition CreateInt(string key, int value, VariableIO varIo = VariableIO.In) + { + return new VariableDefinition(key, value, StateMachineVariableType.Integer, varIo); + } + + public static VariableDefinition CreateDecimal(string key, decimal value, VariableIO varIo = VariableIO.In) + { + return new VariableDefinition(key, value, StateMachineVariableType.Decimal, varIo); + } + + public static VariableDefinition CreateBool(string key, bool value, VariableIO varIo = VariableIO.In) + { + return new VariableDefinition(key, value, StateMachineVariableType.YesNo, varIo); + } + + #endregion + + + #region create list types + + public static VariableDefinition> CreateListString(string key, List value, VariableIO varIo = VariableIO.In) + { + return new VariableDefinition>(key, value, StateMachineVariableType.Text, varIo); + } + + public static VariableDefinition> CreateListInt(string key, List value, VariableIO varIo = VariableIO.In) + { + return new VariableDefinition>(key, value, StateMachineVariableType.Text, varIo); + } + + public static VariableDefinition> CreateListString(string key, List value, VariableIO varIo = VariableIO.In) + { + return new VariableDefinition>(key, value, StateMachineVariableType.Text, varIo); + } + + public static VariableDefinition> CreateListYesNo(string key, List value, VariableIO varIo = VariableIO.In) + { + return new VariableDefinition>(key, value, StateMachineVariableType.Text, varIo); + } + + #endregion + } +} diff --git a/State Machine/VariableTypes/VariableIO.cs b/State Machine/VariableTypes/VariableIO.cs new file mode 100644 index 0000000..ce44f61 --- /dev/null +++ b/State Machine/VariableTypes/VariableIO.cs @@ -0,0 +1,8 @@ +namespace Avalon +{ + public enum VariableIO + { + In, + Out, + } +}