using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Xml; using System.IO; namespace Fusion_Control_Centre_UberMDX { public partial class MainMDX : Form { //private bool OperationalLogicIsSetup = false; public static List FunctionOperationLookUpList = new List(); public class Operation { public Opcode _OpCode; public List _Operands; public Operation(Opcode input_OpCode, List input_Operands) { _OpCode = input_OpCode; _Operands = input_Operands; } } #region Opcodes public enum Opcode { /// /// TEMPORARY PLACEHOLDER IN FUNCTIONS /// NULL = -1, /// /// BEGINNING /// OpCode List Details: /// [0] Fire-On Timer /// [1] Index of next If/Then Node /// START = 0, /// /// RETURN ONLY /// OpCode List Details: /// [0] Index of Variable in AllVariables /// VARIABLE_GET, /// /// DIGGER /// OpCode List Details: /// [0] Index of Variable in AllVariables /// VARIABLE_SET, /// /// RETURN ONLY /// OpCode List Details: /// [0] Value of Number /// BASIC_NUMBER, /// /// RETURN ONLY /// OpCode List Details: /// [0] Value of Boolean /// BASIC_BOOLEAN, /// /// RETURN ONLY /// OpCode List Details: /// [0] Index of Analogue Input in AllAnalogueInputs /// PORT_ANALOGUE_INPUT, /// /// RETURN ONLY /// OpCode List Details: /// [0] Index of Analogue Input in AllAnalogueInputs /// [1] Index in History List (Previous Value) /// PORT_ANALOGUE_INPUT_TIME_DEPENDANT, /// /// RETURN ONLY /// OpCode List Details: /// [0] Index of Digital Output in AllDigitalOutputs /// PORT_DIGITAL_OUTPUT, /// /// RETURN ONLY /// OpCode List Details: /// [0] Index of Digital Input in AllDigitalInputs /// PORT_DIGITAL_INPUT, /// /// RETURN ONLY /// OpCode List Details: /// [0] Number 1 /// [1] Number 2 /// OPERATION_ADDITION, /// /// RETURN ONLY /// OpCode List Details: /// [0] Number 1 /// [1] Number 2 /// OPERATION_SUBTRACTION, /// /// RETURN ONLY /// OpCode List Details: /// [0] Number 1 /// [1] Number 2 /// OPERATION_DIVISION, /// /// RETURN ONLY /// OpCode List Details: /// [0] Number 1 /// [1] Number 2 /// OPERATION_MULTIPLICATION, /// /// RETURN ONLY /// OpCode List Details: /// [0] Number 1 /// [1] Number 2 /// [2] Number 3 /// COMPARISON_BETWEEN_EE, /// /// RETURN ONLY /// OpCode List Details: /// [0] Number 1 /// [1] Number 2 /// [2] Number 3 /// COMPARISON_BETWEEN_EI, /// /// RETURN ONLY /// OpCode List Details: /// [0] Number 1 /// [1] Number 2 /// [2] Number 3 /// COMPARISON_BETWEEN_IE, /// /// RETURN ONLY /// OpCode List Details: /// [0] Number 1 /// [1] Number 2 /// [2] Number 3 /// COMPARISON_BETWEEN_II, /// /// RETURN ONLY /// OpCode List Details: /// [0] Number 1 /// [1] Number 2 /// COMPARISON_GREATER_THAN, /// /// RETURN ONLY /// OpCode List Details: /// [0] Number 1 /// [1] Number 2 /// COMPARISON_GREATER_THAN_OR_EQUAL_TO, /// /// RETURN ONLY /// OpCode List Details: /// [0] Number 1 /// [1] Number 2 /// COMPARISON_LESS_THAN, /// /// RETURN ONLY /// OpCode List Details: /// [0] Number 1 /// [1] Number 2 /// COMPARISON_LESS_THAN_OR_EQUAL_TO, /// /// RETURN ONLY /// OpCode List Details: /// [0] Number 1 /// [1] Number 2 /// COMPARISON_EQUAL_TO, /// /// RETURN ONLY /// OpCode List Details: /// [0] Number 1 /// [1] Number 2 /// COMPARISON_NOT_EQUAL_TO, /// /// RETURN ONLY /// OpCode List Details: /// [0] Input A /// [1] Input B /// COMPARATIVE_LOGIC_AND, /// /// RETURN ONLY /// OpCode List Details: /// [0] Input A /// [1] Input B /// COMPARATIVE_LOGIC_OR, /// /// RETURN ONLY /// OpCode List Details: /// [0] Input A /// [1] Input B /// COMPARATIVE_LOGIC_NOT, /// /// RETURN ONLY /// OpCode List Details: /// [0] Input A /// COMPARATIVE_LOGIC_NAND, /// /// RETURN ONLY /// OpCode List Details: /// [0] Input A /// [1] Input B /// COMPARATIVE_LOGIC_NOR, /// /// RETURN ONLY /// OpCode List Details: /// [0] Input A /// [1] Input B /// COMPARATIVE_LOGIC_XOR, /// /// EXECUTE ONLY /// OpCode List Details: /// [0] Xml Do Node /// [1] DateTime: Last Vote Time /// [2] Minimum Delta Vote Time /// DO_VOTE, /// /// ROOT DIGGER /// OpCode List Details: /// [0] Evaluate Switch Off of outcome of this /// [1] List of Cases /// [0-x] Individual Cases /// [0] Check OpCode /// [1] Parameter 1 /// [2] Parameter 3 /// [3] List of Then Do's /// [0-x] Individual Then Do's /// SWITCH_MAIN, /// /// END OF A LOGIC SECTION /// OpCode List Details: /// [0] Then Do List /// [0-x] XML Nodes for Then Do List /// [1] Anti-Then Do List /// [0-x] XML Nodes for Anti-Then Do List /// [2] Last Vote Time Then List /// [3] Last Vote Time Anti-Then List /// [4] Minimum Delta Vote Time /// FIN } #endregion #region Replace Variables in Logic Node public class ReplaceVariablesInFunctionNode { public XmlNode _replacementNode = null; public string _name = null; public ReplaceVariablesInFunctionNode(XmlNode inputReplacementNode, string inputName) { _replacementNode = inputReplacementNode; _name = inputName; } } public XmlNode ReplaceVariablesInNode(XmlNode InputFunctionXML, List AllTheVariablesToFind) { for (int i = 0; i < InputFunctionXML.ChildNodes.Count; i++) { if (InputFunctionXML.ChildNodes[i].Name.ToLower() == "variable") { bool isGetNotSet = true; foreach (XmlAttribute isItGetOrSet in InputFunctionXML.ChildNodes[i].Attributes) { if (isItGetOrSet.Name.ToLower() == "do") { if (isItGetOrSet.Value.ToLower() == "get") { isGetNotSet = true; } else if (isItGetOrSet.Value.ToLower() == "set") { isGetNotSet = false; } break; } } if (isGetNotSet) { string varialbetoFind = InputFunctionXML.ChildNodes[i].FirstChild.Value; //object replacementvalue = null; foreach (ReplaceVariablesInFunctionNode testVariable in AllTheVariablesToFind) { if (testVariable._name.ToLower() == varialbetoFind.ToLower()) { InputFunctionXML.InnerXml = testVariable._replacementNode.OuterXml; break; } } /* if (replacementvalue == null) { replacementvalue = (object)0.0; } */ //InputFunctionXML.InnerXml = "" + replacementvalue + ""; } } else if (InputFunctionXML.ChildNodes[i].HasChildNodes) { InputFunctionXML.ReplaceChild(ReplaceVariablesInNode(InputFunctionXML.ChildNodes[i], AllTheVariablesToFind), InputFunctionXML.ChildNodes[i]); } } return InputFunctionXML; } #endregion public static int _Stack = 0; public static object IfThenEvaluatorOperationalLookupFunction(int index, string current_timer) { //Operation_Index--; int local_Operation_Index = index; _Stack++; if (_Stack > (FunctionOperationLookUpList.Count * 2)) { return (object)0; } while (local_Operation_Index >= 0 && local_Operation_Index < FunctionOperationLookUpList.Count) { //double d1 = CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)); //double d2 = CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer)); //double d3 = CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[2], current_timer)); switch (FunctionOperationLookUpList[local_Operation_Index]._OpCode) { case Opcode.START: { int new_index = -1; if (current_timer.ToLower() == FunctionOperationLookUpList[local_Operation_Index]._Operands[0].ToString().ToLower()) { new_index = (int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1]; } else { new_index = (int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1] + 1; local_Operation_Index = new_index; continue; } //Operation_Index = new_index; object r = IfThenEvaluatorOperationalLookupFunction(new_index, current_timer); if (r != null) { if (r.Equals(true)) { TimeSpan _timeDifference = DateTime.Now.Subtract((DateTime)FunctionOperationLookUpList[new_index]._Operands[2]); if (_timeDifference.TotalMilliseconds >= (int)FunctionOperationLookUpList[new_index]._Operands[4]) { List doNodes = (List)FunctionOperationLookUpList[(int)FunctionOperationLookUpList[(int)local_Operation_Index]._Operands[1]]._Operands[0]; foreach (XmlNode ThenDoNode in doNodes) { //FunctionEvaluator_EvaluateDoThenNode(ThenDoNode, -1); EvaluateDoThenNode(ThenDoNode, (DateTime)FunctionOperationLookUpList[new_index]._Operands[2], (int)FunctionOperationLookUpList[new_index]._Operands[4]); } FunctionOperationLookUpList[new_index]._Operands[2] = DateTime.Now; } } else if (r.Equals(false)) { TimeSpan _timeDifference = DateTime.Now.Subtract((DateTime)FunctionOperationLookUpList[new_index]._Operands[3]); if (_timeDifference.TotalMilliseconds >= (int)FunctionOperationLookUpList[new_index]._Operands[4]) { List doNodes = (List)FunctionOperationLookUpList[(int)FunctionOperationLookUpList[(int)local_Operation_Index]._Operands[1]]._Operands[1]; foreach (XmlNode AntiThenDoNode in doNodes) { //FunctionEvaluator_EvaluateDoThenNode(AntiThenDoNode, -1); EvaluateDoThenNode(AntiThenDoNode, (DateTime)FunctionOperationLookUpList[new_index]._Operands[3], (int)FunctionOperationLookUpList[new_index]._Operands[4]); } FunctionOperationLookUpList[new_index]._Operands[3] = DateTime.Now; } } } new_index = (int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1] + 1; local_Operation_Index = new_index; } break; case Opcode.FIN: { object r = IfThenEvaluatorOperationalLookupFunction(local_Operation_Index - 1, current_timer); return r; } case Opcode.DO_VOTE: { if (EvaluateDoThenNode((XmlNode)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], (DateTime)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], (int)FunctionOperationLookUpList[local_Operation_Index]._Operands[2])) { FunctionOperationLookUpList[local_Operation_Index]._Operands[1] = DateTime.Now; } return true; } case Opcode.SWITCH_MAIN: { object baseSelection = IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer); List> switch_cases = ((List>)FunctionOperationLookUpList[local_Operation_Index]._Operands[1]); for(int switch_i = 0; switch_i < switch_cases.Count; switch_i ++) { List paramList = ((List)FunctionOperationLookUpList[(int)switch_cases[switch_i][0]]._Operands); FunctionOperationLookUpList[(int)paramList[paramList.Count - 2]]._Operands[0] = baseSelection; object r = IfThenEvaluatorOperationalLookupFunction((int)switch_cases[switch_i][0], current_timer); if ((bool)r) { List case_do_list = ((List)switch_cases[switch_i][1]); for(int do_i = 0; do_i < case_do_list.Count; do_i++) { IfThenEvaluatorOperationalLookupFunction(case_do_list[do_i], current_timer); } break; } } return true; } case Opcode.VARIABLE_GET: { return AllVariables[(int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0]].variableValue; } case Opcode.VARIABLE_SET: { AllVariables[(int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0]].variableValue = IfThenEvaluatorOperationalLookupFunction(local_Operation_Index - 1, current_timer); return AllVariables[(int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0]].variableValue; } case Opcode.BASIC_NUMBER: { return FunctionOperationLookUpList[local_Operation_Index]._Operands[0]; } case Opcode.BASIC_BOOLEAN: { return FunctionOperationLookUpList[local_Operation_Index]._Operands[0]; } case Opcode.PORT_ANALOGUE_INPUT: { return AllAnalogueInputs[(int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0]].CurrentValue; } case Opcode.PORT_ANALOGUE_INPUT_TIME_DEPENDANT: { return AllAnalogueInputs[(int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0]].HistoryValues[(int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1]]; } case Opcode.PORT_DIGITAL_OUTPUT: { return AllDigitalOutputs[(int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0]].CurrentState; } case Opcode.PORT_DIGITAL_INPUT: { return AllDigitalInputs[(int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0]].CurrentState; } case Opcode.OPERATION_ADDITION: { return CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) + CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer)); } case Opcode.OPERATION_SUBTRACTION: { return CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) - CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer)); } case Opcode.OPERATION_MULTIPLICATION: { return CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) * CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer)); } case Opcode.OPERATION_DIVISION: { return CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) / CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer)); } case Opcode.COMPARISON_BETWEEN_EE: { return (CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) < CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))) && (CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer)) < CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[2], current_timer))); } case Opcode.COMPARISON_BETWEEN_EI: { return (CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) < CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))) && (CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer)) <= CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[2], current_timer))); } case Opcode.COMPARISON_BETWEEN_IE: { return (CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) <= CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))) && (CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer)) < CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[2], current_timer))); } case Opcode.COMPARISON_BETWEEN_II: { return (CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) <= CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))) && (CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer)) <= CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[2], current_timer))); } case Opcode.COMPARISON_EQUAL_TO: { return (CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) == CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))); } case Opcode.COMPARISON_NOT_EQUAL_TO: { return (CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) != CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))); } case Opcode.COMPARISON_GREATER_THAN: { return (CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) > CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))); } case Opcode.COMPARISON_GREATER_THAN_OR_EQUAL_TO: { return (CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) >= CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))); } case Opcode.COMPARISON_LESS_THAN: { return (CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) < CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))); } case Opcode.COMPARISON_LESS_THAN_OR_EQUAL_TO: { return (CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) <= CastToDouble(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))); } case Opcode.COMPARATIVE_LOGIC_AND: { return (CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) && CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))); } case Opcode.COMPARATIVE_LOGIC_NAND: { return !(CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) && CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))); } case Opcode.COMPARATIVE_LOGIC_OR: { return (CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) || CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))); } case Opcode.COMPARATIVE_LOGIC_NOR: { return !(CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) || CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))); } case Opcode.COMPARATIVE_LOGIC_NOT: { return !(CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer))); } case Opcode.COMPARATIVE_LOGIC_XOR: { return (CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[0], current_timer)) != CastToBool(IfThenEvaluatorOperationalLookupFunction((int)FunctionOperationLookUpList[local_Operation_Index]._Operands[1], current_timer))); } case Opcode.NULL: { break; } default: break; } } return true; } #region Cast to Double private static double CastToDouble(object toCast) { double d = 0.0; try { d = (double)toCast; } catch (InvalidCastException) { try { bool b = (bool)toCast; if (b) { d = 1.0; } else { d = 0.0; } } catch (InvalidCastException) { if (!double.TryParse(toCast.ToString(), out d)) { d = 0.0; } } } return d; } #endregion #region Cast to Boolean private static bool CastToBool(object toCast) { bool b = false; try { b = (bool)toCast; } catch (InvalidCastException) { try { double d = (double)toCast; if (d < 1) { b = false; } else { b = true; } } catch (InvalidCastException) { double d = 0.0; if (double.TryParse(toCast.ToString(), out d)) { if (d < 1) { b = false; } else { b = true; } } else { b = false; } } } return b; } #endregion #region Main If/Then Evaluation Loop ** Recursive ** public object IfThenEvaluator(XmlNode input, string typeinput) { return IfThenEvaluator(input, typeinput, true); } public object IfThenEvaluator(XmlNode input, string typeinput, bool setupList) { #region Evaluate Function if (input.Name.ToLower() == "evaluate") { foreach (XmlAttribute evalAttribute in input.Attributes) { if (evalAttribute.Name.ToLower() == "function") { XmlNode theFunctionNode = null; for (int i = 0; i < AllFunctions.Count; i++) { if (AllFunctions[i].functionName == evalAttribute.Value.ToLower()) { theFunctionNode = AllFunctions[i].rawFunction.CloneNode(true); break; } } if (theFunctionNode == null) { DisplayMessageBox("Undefined Function \"" + evalAttribute.Value.ToLower() + "\"", true); } List thisFunctionsVariables = new List(); foreach (XmlNode findInputs in theFunctionNode.ChildNodes) { if (findInputs.Name.ToLower() == "function") { foreach (XmlNode findInputsLower1 in findInputs.ChildNodes) { if (findInputsLower1.Name.ToLower() == "input") { foreach (XmlNode orderOfInput in input.ChildNodes) { if (orderOfInput.Name.ToLower() == "input") { //object newinputValue = 0.00; int newinputOrder = -1; string newinputName = findInputsLower1.ChildNodes[0].Value; foreach (XmlAttribute inputAtt in orderOfInput.Attributes) { if (inputAtt.Name.ToLower() == "order") { int.TryParse(inputAtt.Value, out newinputOrder); } } //object inputObjectReturned = IfThenEvaluator(orderOfInput, ""); //newinputValue = inputObjectReturned; thisFunctionsVariables.Add(new ReplaceVariablesInFunctionNode(orderOfInput.ChildNodes[0], newinputName)); } } } } //The internals of the function, and the inputs are already setup into variables... theFunctionNode = ReplaceVariablesInNode(theFunctionNode, thisFunctionsVariables); //Replaced variables with numbers... foreach (XmlNode actualFunctionNode in findInputs.ChildNodes) { if (actualFunctionNode.Name.ToLower() == "return") { //string functionNode_return = ""; //object functionNode_returned_Data; //foreach (XmlAttribute functionReturnType in actualFunctionNode.Attributes) //{ // if (functionReturnType.Name.ToLower() == "type") // { // functionNode_return = functionReturnType.Value.ToLower(); // } //} object functionNode_returned_Data = IfThenEvaluator(actualFunctionNode, ""); return functionNode_returned_Data; } } } } } } } #endregion #region Evaluate Variable else if (input.Name.ToLower() == "variable") { foreach (XmlAttribute inputAttribute in input.Attributes) { if (inputAttribute.Name.ToLower() == "do") { if (inputAttribute.Value.ToLower() == "set") { object newVariableValue = null; string newVariableName = ""; foreach (XmlAttribute sendInVariableAttribute in input.Attributes) { if (sendInVariableAttribute.Name.ToLower() == "name") { newVariableName = sendInVariableAttribute.Value.ToLower(); break; } } newVariableValue = IfThenEvaluator(input.FirstChild, ""); for (int newVariable_i = 0; newVariable_i < AllVariables.Count; newVariable_i++) { if (AllVariables[newVariable_i].variableReferenceName == newVariableName) { AllVariables[newVariable_i].variableValue = newVariableValue; if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.VARIABLE_SET, new List(new object[] { newVariable_i }))); } return newVariableValue; } } AllVariables.Add(new Variable(newVariableName, newVariableValue)); if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.VARIABLE_SET, new List(new object[] { (AllVariables.Count - 1) }))); } return newVariableValue; } else if (inputAttribute.Value.ToLower() == "get") { string doGetVariableNameToFind = ""; if (input.FirstChild != null) { doGetVariableNameToFind = input.FirstChild.Value.ToLower(); } else { foreach (XmlAttribute doGetVariableNameAttribute in input.Attributes) { if (doGetVariableNameAttribute.Name.ToLower() == "name") { doGetVariableNameToFind = doGetVariableNameAttribute.Value.ToLower(); break; } } } object replacementvalue = null; foreach (Variable testVariable in AllVariables) { if (testVariable.variableReferenceName == doGetVariableNameToFind) { replacementvalue = testVariable.variableValue; if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.VARIABLE_GET, new List(new object[] { AllVariables.IndexOf(testVariable) }))); } return replacementvalue; } } return null; } break; } } } #endregion #region If Block else if (input.Name.ToLower() == "if") { } #endregion #region Switch Statement else if (input.Name.ToLower() == "switch") { Operation _newSwitchOperation = new Operation(Opcode.SWITCH_MAIN, new List()); int index_of_switch_object = -1; object _switchOffOf = null; XmlNode _casesRoot = null; foreach (XmlNode _child in input.ChildNodes) { if (_child.Name.ToLower() == "object") { _switchOffOf = IfThenEvaluator(_child, ""); index_of_switch_object = FunctionOperationLookUpList.Count - 1; break; } } if (_switchOffOf == null) { return false; } _newSwitchOperation._Operands.Add(index_of_switch_object); List> cases = new List>(); _newSwitchOperation._Operands.Add(cases); object toReturn = null; foreach (XmlNode _child in input.ChildNodes) { if (_child.Name.ToLower() == "cases") { _casesRoot = _child; break; } } if (_casesRoot == null) { return false; } foreach (XmlNode _case in _casesRoot) { List doThenNodes = new List(); Opcode checkCode = Opcode.NULL; object operation_parameter1 = null, operation_parameter2 = null; int numberOfAdditionalParameters = 1; string _check = ""; bool _result = false; foreach (XmlAttribute _caseAttribute in _case.Attributes) { if (_caseAttribute.Name.ToLower() == "parameter1") { double f = 0.0f; bool b = false; if (double.TryParse(_caseAttribute.Value, out f)) { operation_parameter1 = f; } else if (bool.TryParse(_caseAttribute.Value, out b)) { operation_parameter1 = b; } } else if (_caseAttribute.Name.ToLower() == "parameter2") { double f = 0.0f; bool b = false; if (double.TryParse(_caseAttribute.Value, out f)) { operation_parameter2 = f; } else if (bool.TryParse(_caseAttribute.Value, out b)) { operation_parameter2 = b; } } else if (_caseAttribute.Name.ToLower() == "check") { _check = _caseAttribute.Value.ToLower(); } } if (_check != "") { //try //{ switch (_check.ToLower()) { case "between": case "between exclusive": _result = (bool)(((double)operation_parameter1 < (double)_switchOffOf) && ((double)_switchOffOf < (double)operation_parameter2)); checkCode = Opcode.COMPARISON_BETWEEN_EE; numberOfAdditionalParameters = 2; break; case "between inclusive": _result = (bool)(((double)operation_parameter1 <= (double)_switchOffOf) && ((double)_switchOffOf <= (double)operation_parameter2)); checkCode = Opcode.COMPARISON_BETWEEN_II; numberOfAdditionalParameters = 2; break; case "between inclusive-exclusive": _result = (bool)(((double)operation_parameter1 <= (double)_switchOffOf) && ((double)_switchOffOf < (double)operation_parameter2)); checkCode = Opcode.COMPARISON_BETWEEN_IE; numberOfAdditionalParameters = 2; break; case "between exclusive-inclusive": _result = (bool)(((double)operation_parameter1 < (double)_switchOffOf) && ((double)_switchOffOf <= (double)operation_parameter2)); checkCode = Opcode.COMPARISON_BETWEEN_EI; numberOfAdditionalParameters = 2; break; case "greater than": _result = (bool)((double)operation_parameter1 > (double)_switchOffOf); checkCode = Opcode.COMPARISON_GREATER_THAN; break; case "less than": _result = (bool)((double)operation_parameter1 < (double)_switchOffOf); checkCode = Opcode.COMPARISON_LESS_THAN; break; case "greater than or equal to": _result = (bool)((double)operation_parameter1 >= (double)_switchOffOf); checkCode = Opcode.COMPARISON_GREATER_THAN_OR_EQUAL_TO; break; case "less than or equal to": _result = (bool)((double)operation_parameter1 <= (double)_switchOffOf); checkCode = Opcode.COMPARISON_LESS_THAN_OR_EQUAL_TO; break; default: break; } //} //catch (InvalidCastException e) //{ // Console.WriteLine(e.ToString()); // //Error // return 0.0; //} if (_result || setupList) { object abba = null; foreach (XmlNode _caseChild in _case) { //int caseChildIndex = FunctionOperationLookUpList.Count - 1; abba = IfThenEvaluator(_caseChild, ""); doThenNodes.Add(FunctionOperationLookUpList.Count - 1); } //object abba = IfThenEvaluator(_case, ""); if (_result) { toReturn = abba; } } int index_of_parameter1 = -1, index_of_parameter2 = -1, index_of_parameter3 = -1; FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_NUMBER, new List(new object[] { operation_parameter1 }))); index_of_parameter1 = FunctionOperationLookUpList.Count - 1; FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_NUMBER, new List(new object[] { 0.0 }))); index_of_parameter2 = FunctionOperationLookUpList.Count - 1; if (numberOfAdditionalParameters == 2) { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_NUMBER, new List(new object[] { operation_parameter2 }))); index_of_parameter3 = FunctionOperationLookUpList.Count - 1; } List myParams = new List(); if (numberOfAdditionalParameters == 2) { myParams = new List(new object[] { index_of_parameter1, index_of_parameter2, index_of_parameter3 }); } else if (numberOfAdditionalParameters == 1) { myParams = new List(new object[] { index_of_parameter2, index_of_parameter1 }); } else { //quoi??? } FunctionOperationLookUpList.Add(new Operation(checkCode, myParams)); cases.Add(new List(new object[] { FunctionOperationLookUpList.Count - 1, doThenNodes })); //cases.Add(new List(new object[] { checkCode, operation_parameter1, operation_parameter2, doThenNodes })); } } FunctionOperationLookUpList.Add(_newSwitchOperation); return toReturn; } #endregion #region Do Statement else if (input.Name.ToLower() == "do") { if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.DO_VOTE, new List(new object[] { input, DateTime.MinValue, _currentLogicMinimumVoteTime }))); } //FunctionEvaluator_EvaluateDoThenNode(input, -1); } #endregion else { int _index = -1; switch (input.Name.ToLower()) { case "number": double returnValuezDouble = 0.00f; double.TryParse(input.FirstChild.Value, out returnValuezDouble); if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_NUMBER, new List(new object[] { returnValuezDouble }))); } return returnValuezDouble; case "boolean": bool reternValuezBool = false; bool.TryParse(input.FirstChild.Value, out reternValuezBool); if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_BOOLEAN, new List(new object[] { reternValuezBool }))); } return reternValuezBool; case "output": bool retrievedDO = false; foreach (XmlAttribute digitalOutputAttribute in input.Attributes) { switch (digitalOutputAttribute.Name.ToLower()) { case "id": foreach (DigitalOutputChannel digitalOutput_toFind in AllDigitalOutputs) { if (digitalOutput_toFind.id == digitalOutputAttribute.Value.ToLower()) { retrievedDO = digitalOutput_toFind.CurrentState; _index = AllDigitalOutputs.IndexOf(digitalOutput_toFind); break; } } break; default: break; } } if (setupList) { if (_index >= 0) { FunctionOperationLookUpList.Add(new Operation(Opcode.PORT_DIGITAL_OUTPUT, new List(new object[] { _index }))); } else { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_BOOLEAN, new List(new object[] { false }))); } } return retrievedDO; case "analogue": //Retrieve Analogue value of input string analogue_id_string = ""; int analogue_time_int = 0; foreach (XmlAttribute analogueAttribute in input.Attributes) { switch (analogueAttribute.Name.ToLower()) { case "id": analogue_id_string = analogueAttribute.Value.ToLower(); break; case "time": int.TryParse(analogueAttribute.Value, out analogue_time_int); break; default: //Unknown or Error break; } } foreach (AnalogueInputChannel analogue_toFind in AllAnalogueInputs) { if (analogue_toFind.id == analogue_id_string) { _index = AllAnalogueInputs.IndexOf(analogue_toFind); if ((setupList && analogue_time_int != 0) || (analogue_toFind.HistoryValues.Count > 0 && analogue_time_int != 0)) { if (setupList) { if (_index >= 0) { FunctionOperationLookUpList.Add(new Operation(Opcode.PORT_ANALOGUE_INPUT_TIME_DEPENDANT, new List(new object[] { _index, (analogue_toFind.HistoryValues.Count - 1 - analogue_time_int) }))); } else { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_NUMBER, new List(new object[] { 0.0f }))); } } return (object)analogue_toFind.HistoryValues[analogue_toFind.HistoryValues.Count - 1 - analogue_time_int]; } else { if (setupList) { if (_index >= 0) { FunctionOperationLookUpList.Add(new Operation(Opcode.PORT_ANALOGUE_INPUT, new List(new object[] { _index }))); } else { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_NUMBER, new List(new object[] { 0.0f }))); } } return (object)analogue_toFind.CurrentValue; } } } return (object)0.0f; case "digital_input": bool retrievedDI = false; foreach (XmlAttribute digitalInputAttribute in input.Attributes) { switch (digitalInputAttribute.Name.ToLower()) { case "id": foreach (DigitalInputChannel digitalInput_toFind in AllDigitalInputs) { if (digitalInput_toFind.id == digitalInputAttribute.Value.ToLower()) { retrievedDI = digitalInput_toFind.CurrentState; _index = AllDigitalInputs.IndexOf(digitalInput_toFind); break; } } break; default: break; } } if (setupList) { if (_index >= 0) { FunctionOperationLookUpList.Add(new Operation(Opcode.PORT_DIGITAL_INPUT, new List(new object[] { _index }))); } else { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_BOOLEAN, new List(new object[] { false }))); } } return retrievedDI; default: break; } } object ParameterNumero1 = null; object ParameterNumero2 = null; object ParameterNumero3 = null; int ParameterNumero1_index = -1; int ParameterNumero2_index = -1; int ParameterNumero3_index = -1; int ParameterNumero_temp_index = -1; foreach (XmlNode inputrecursive in input.ChildNodes) { int _index = -1; switch (inputrecursive.Name.ToLower()) { case "parameter1": case "parameter2": case "parameter3": object deReturnedObjecta = null; foreach (XmlNode inP in inputrecursive.ChildNodes) { switch (inP.Name.ToLower()) { case "number": deReturnedObjecta = IfThenEvaluator(inputrecursive, "numeric"); break; case "boolean": deReturnedObjecta = IfThenEvaluator(inputrecursive, "boolean"); break; case "if": deReturnedObjecta = IfThenEvaluator(inP, ""); break; default: //deReturnedObjecta = IfThenEvaluator(inP, ""); deReturnedObjecta = IfThenEvaluator(inputrecursive, ""); break; } } ParameterNumero_temp_index = FunctionOperationLookUpList.Count - 1; if (inputrecursive.Name.ToLower() == "parameter1") { ParameterNumero1_index = ParameterNumero_temp_index; ParameterNumero1 = deReturnedObjecta; } else if (inputrecursive.Name.ToLower() == "parameter2") { ParameterNumero2_index = ParameterNumero_temp_index; ParameterNumero2 = deReturnedObjecta; } else if (inputrecursive.Name.ToLower() == "parameter3") { ParameterNumero3_index = ParameterNumero_temp_index; ParameterNumero3 = deReturnedObjecta; } else { //How the f*** you get ici?!? } break; case "number": double returnValuezDouble = 0.00f; double.TryParse(inputrecursive.FirstChild.Value, out returnValuezDouble); if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_NUMBER, new List(new object[] { returnValuezDouble }))); } return returnValuezDouble; case "boolean": bool reternValuezBool = false; bool.TryParse(inputrecursive.FirstChild.Value, out reternValuezBool); if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_BOOLEAN, new List(new object[] { reternValuezBool }))); } return reternValuezBool; case "output": bool retrievedDO = false; foreach (XmlAttribute digitalOutputAttribute in inputrecursive.Attributes) { switch (digitalOutputAttribute.Name.ToLower()) { case "id": foreach (DigitalOutputChannel digitalOutput_toFind in AllDigitalOutputs) { if (digitalOutput_toFind.id == digitalOutputAttribute.Value.ToLower()) { retrievedDO = digitalOutput_toFind.CurrentState; _index = AllDigitalOutputs.IndexOf(digitalOutput_toFind); break; } } break; default: break; } } if (setupList) { if (_index >= 0) { FunctionOperationLookUpList.Add(new Operation(Opcode.PORT_DIGITAL_OUTPUT, new List(new object[] { _index }))); } else { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_BOOLEAN, new List(new object[] { false }))); } } return retrievedDO; case "analogue": //Retrieve Analogue value of input string analogue_id_string = ""; int analogue_time_int = 0; foreach (XmlAttribute analogueAttribute in inputrecursive.Attributes) { switch (analogueAttribute.Name.ToLower()) { case "id": analogue_id_string = analogueAttribute.Value.ToLower(); break; case "time": int.TryParse(analogueAttribute.Value, out analogue_time_int); break; default: //Unknown or Error break; } } foreach (AnalogueInputChannel analogue_toFind in AllAnalogueInputs) {if (analogue_toFind.id == analogue_id_string) { _index = AllAnalogueInputs.IndexOf(analogue_toFind); if ((setupList && analogue_time_int != 0) || (analogue_toFind.HistoryValues.Count > 0 && analogue_time_int != 0)) { if (setupList) { if (_index >= 0) { FunctionOperationLookUpList.Add(new Operation(Opcode.PORT_ANALOGUE_INPUT_TIME_DEPENDANT, new List(new object[] { _index, (analogue_toFind.HistoryValues.Count - 1 - analogue_time_int) }))); } else { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_NUMBER, new List(new object[] { 0.0f }))); } } return (object)analogue_toFind.HistoryValues[analogue_toFind.HistoryValues.Count - 1 - analogue_time_int]; } else { if (setupList) { if (_index >= 0) { FunctionOperationLookUpList.Add(new Operation(Opcode.PORT_ANALOGUE_INPUT, new List(new object[] { _index }))); } else { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_NUMBER, new List(new object[] { 0.0f }))); } } return (object)analogue_toFind.CurrentValue; } } } return (object)0.0f; case "digital_input": bool retrievedDI = false; foreach (XmlAttribute digitalInputAttribute in input.Attributes) { switch (digitalInputAttribute.Name.ToLower()) { case "id": foreach (DigitalInputChannel digitalInput_toFind in AllDigitalInputs) { if (digitalInput_toFind.id == digitalInputAttribute.Value.ToLower()) { retrievedDI = digitalInput_toFind.CurrentState; _index = AllDigitalInputs.IndexOf(digitalInput_toFind); break; } } break; default: break; } } if (setupList) { if (_index >= 0) { FunctionOperationLookUpList.Add(new Operation(Opcode.PORT_DIGITAL_INPUT, new List(new object[] { _index }))); } else { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_BOOLEAN, new List(new object[] { false }))); } } return retrievedDI; case "variable": /* * GET OR SET VARIABLE */ return IfThenEvaluator(inputrecursive, ""); case "evaluate": return IfThenEvaluator(inputrecursive, ""); #region Do Statement case "do": { if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.DO_VOTE, new List(new object[] { inputrecursive, DateTime.MinValue, _currentLogicMinimumVoteTime }))); } //FunctionEvaluator_EvaluateDoThenNode(input, -1); } return true; #endregion #region Boolean and Numeric Operation case "operation": case "boolean_operation": object operation_parameter1 = null; object operation_parameter2 = null; object operation_parameter3 = null; int operation_parameter1_index = -1; int operation_parameter2_index = -1; int operation_parameter3_index = -1; foreach (XmlNode funcParamNodes in inputrecursive.ChildNodes) { if (funcParamNodes.Name.ToLower() == "parameter1") { operation_parameter1 = IfThenEvaluator(funcParamNodes, "numeric"); operation_parameter1_index = FunctionOperationLookUpList.Count - 1; } else if (funcParamNodes.Name.ToLower() == "parameter2") { operation_parameter2 = IfThenEvaluator(funcParamNodes, "numeric"); operation_parameter2_index = FunctionOperationLookUpList.Count - 1; } else if (funcParamNodes.Name.ToLower() == "parameter3") { operation_parameter3 = IfThenEvaluator(funcParamNodes, "numeric"); operation_parameter3_index = FunctionOperationLookUpList.Count - 1; } } foreach (XmlAttribute optypeAttribute in inputrecursive.Attributes) { if (optypeAttribute.Name.ToLower() == "type") { try { switch (optypeAttribute.Value.ToLower()) { case "divide": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.OPERATION_DIVISION, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)((double)operation_parameter1 / (double)operation_parameter2); case "multiply": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.OPERATION_MULTIPLICATION, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)((double)operation_parameter1 * (double)operation_parameter2); case "add": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.OPERATION_ADDITION, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)((double)operation_parameter1 + (double)operation_parameter2); case "subtract": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.OPERATION_SUBTRACTION, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)((double)operation_parameter1 - (double)operation_parameter2); case "between": case "between ee": case "between exclusive": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_BETWEEN_EE, new List(new object[] { operation_parameter1_index, operation_parameter2_index, operation_parameter3_index }))); } return (bool)(((double)operation_parameter1 < (double)operation_parameter2) && ((double)operation_parameter2 < (double)operation_parameter3)); case "between inclusive": case "between ii": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_BETWEEN_II, new List(new object[] { operation_parameter1_index, operation_parameter2_index, operation_parameter3_index }))); } return (bool)(((double)operation_parameter1 <= (double)operation_parameter2) && ((double)operation_parameter2 <= (double)operation_parameter3)); case "between inclusive-exclusive": case "between ie": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_BETWEEN_IE, new List(new object[] { operation_parameter1_index, operation_parameter2_index, operation_parameter3_index }))); } return (bool)(((double)operation_parameter1 <= (double)operation_parameter2) && ((double)operation_parameter2 < (double)operation_parameter3)); case "between exclusive-inclusive": case "between ei": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_BETWEEN_EI, new List(new object[] { operation_parameter1_index, operation_parameter2_index, operation_parameter3_index }))); } return (bool)(((double)operation_parameter1 < (double)operation_parameter2) && ((double)operation_parameter2 <= (double)operation_parameter3)); case "greater than": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_GREATER_THAN, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)((double)operation_parameter1 > (double)operation_parameter2); case "less than": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_LESS_THAN, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)((double)operation_parameter1 < (double)operation_parameter2); case "greater than or equal to": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_GREATER_THAN_OR_EQUAL_TO, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)((double)operation_parameter1 >= (double)operation_parameter2); case "less than or equal to": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_LESS_THAN_OR_EQUAL_TO, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)((double)operation_parameter1 <= (double)operation_parameter2); case "equal to": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_EQUAL_TO, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)((double)operation_parameter1 == (double)operation_parameter2); case "not equal to": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_NOT_EQUAL_TO, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)((double)operation_parameter1 != (double)operation_parameter2); case "and": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARATIVE_LOGIC_AND, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)((bool)operation_parameter1 && (bool)operation_parameter2); case "or": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARATIVE_LOGIC_OR, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)((bool)operation_parameter1 || (bool)operation_parameter2); case "not": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARATIVE_LOGIC_NOT, new List(new object[] { operation_parameter1_index }))); } return (object)!(bool)operation_parameter1; case "nand": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARATIVE_LOGIC_NAND, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)!((bool)operation_parameter1 && (bool)operation_parameter2); case "nor": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARATIVE_LOGIC_NOR, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)!((bool)operation_parameter1 || (bool)operation_parameter2); case "xor": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARATIVE_LOGIC_XOR, new List(new object[] { operation_parameter1_index, operation_parameter2_index }))); } return (object)((bool)operation_parameter1 != (bool)operation_parameter2); default: break; } } //catch (InvalidCastException e) //{ // Console.WriteLine(e.ToString()); // //Error // if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_NUMBER, new List(new object[] { 0.0 }))); } // return 0.0; //} catch (NullReferenceException e) { Console.WriteLine(e.ToString()); if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.BASIC_BOOLEAN, new List(new object[] { false }))); } return false; } } } break; #endregion #region Ignore in this section case "test": case "then": //Ignore these one break; #endregion #region Unknown or Error default: break; #endregion } } /* * PARSER ABOVE * * * * * * * * * EVALUATOR BELOW */ //SHOULD BE FULLY EVALUATED... #region Find what to check Parameter1 and Parameter2 with string test_check_string = ""; foreach (XmlNode TestXMLNode in input.ChildNodes) { if (TestXMLNode.Name.ToLower() == "test") { foreach (XmlAttribute TestAttribute in TestXMLNode.Attributes) { if (TestAttribute.Name.ToLower() == "check") { test_check_string = TestAttribute.Value.ToLower(); break; } } break; } } if (test_check_string == "") { //No Test child node found so search attributes for Type (Boolean Operation most likely)... foreach (XmlAttribute TypeXMLAttribute in input.Attributes) { if (TypeXMLAttribute.Name.ToLower() == "type") { test_check_string = TypeXMLAttribute.Value.ToLower(); ; break; } } } if (test_check_string == "") { //Still not found, its an error. Return false return false; } #endregion object DeResultOfDoom = null; #region Test Parameter1 versus Parameter2 switch (test_check_string) { case "between": case "between ee": case "between exclusive": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_BETWEEN_EE, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index, ParameterNumero3_index }))); } DeResultOfDoom = (bool)(((double)ParameterNumero1 < (double)ParameterNumero2) && ((double)ParameterNumero2 < (double)ParameterNumero3)); break; case "between ii": case "between inclusive": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_BETWEEN_II, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index, ParameterNumero3_index }))); } DeResultOfDoom = (bool)(((double)ParameterNumero1 <= (double)ParameterNumero2) && ((double)ParameterNumero2 <= (double)ParameterNumero3)); break; case "between ie": case "between inclusive-exclusive": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_BETWEEN_IE, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index, ParameterNumero3_index }))); } DeResultOfDoom = (bool)(((double)ParameterNumero1 <= (double)ParameterNumero2) && ((double)ParameterNumero2 < (double)ParameterNumero3)); break; case "between ei": case "between exclusive-inclusive": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_BETWEEN_EI, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index, ParameterNumero3_index }))); } DeResultOfDoom = (bool)(((double)ParameterNumero1 < (double)ParameterNumero2) && ((double)ParameterNumero2 <= (double)ParameterNumero3)); break; case "greater than": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_GREATER_THAN, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = ((double)ParameterNumero1 > (double)ParameterNumero2); break; case "less than": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_LESS_THAN, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = ((double)ParameterNumero1 < (double)ParameterNumero2); break; case "greater than or equal to": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_GREATER_THAN_OR_EQUAL_TO, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = ((double)ParameterNumero1 >= (double)ParameterNumero2); break; case "less than or equal to": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_LESS_THAN_OR_EQUAL_TO, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = ((double)ParameterNumero1 <= (double)ParameterNumero2); break; case "equal to": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_EQUAL_TO, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = ((double)ParameterNumero1 == (double)ParameterNumero2); break; case "not equal to": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARISON_NOT_EQUAL_TO, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = ((double)ParameterNumero1 != (double)ParameterNumero2); break; case "and": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARATIVE_LOGIC_AND, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = ((bool)ParameterNumero1 && (bool)ParameterNumero2); break; case "or": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARATIVE_LOGIC_OR, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = ((bool)ParameterNumero1 || (bool)ParameterNumero2); break; case "not": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARATIVE_LOGIC_NOT, new List(new object[] { ParameterNumero1_index }))); } DeResultOfDoom = !(bool)ParameterNumero1; break; case "nand": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARATIVE_LOGIC_NAND, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = !((bool)ParameterNumero1 && (bool)ParameterNumero2); break; case "nor": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARATIVE_LOGIC_NOR, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = !((bool)ParameterNumero1 || (bool)ParameterNumero2); break; case "xor": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.COMPARATIVE_LOGIC_XOR, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = ((bool)ParameterNumero1 != (bool)ParameterNumero2); break; case "divide": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.OPERATION_DIVISION, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = (object)((double)ParameterNumero1 / (double)ParameterNumero2); break; case "multiply": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.OPERATION_MULTIPLICATION, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = (object)((double)ParameterNumero1 * (double)ParameterNumero2); break; case "add": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.OPERATION_ADDITION, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = (object)((double)ParameterNumero1 + (double)ParameterNumero2); break; case "subtract": if (setupList) { FunctionOperationLookUpList.Add(new Operation(Opcode.OPERATION_SUBTRACTION, new List(new object[] { ParameterNumero1_index, ParameterNumero2_index }))); } DeResultOfDoom = (object)((double)ParameterNumero1 - (double)ParameterNumero2); break; default: break; } #endregion return DeResultOfDoom; } #endregion #region Update Variable public void UpdateVariables() { foreach(Variable CurrentVariable in AllVariables) { if (CurrentVariable.WhatShouldMyValueBe.Count == 0) { continue; } else if (CurrentVariable.WhatShouldMyValueBe.Count == 2) { } object[] newValue = { 0.0, 0.0, 0.0 }; int[] votesCast = new int[3]; bool isBooleanNotDouble = true; foreach (LogicalVote LV in CurrentVariable.WhatShouldMyValueBe) { int LV_Priority_int = Vote_Priority_To_Integer(LV.MyLogicalPriority); try { bool _parseTry = false; if (bool.TryParse(LV.toWhat.ToString(), out _parseTry)) { if (_parseTry) { newValue[LV_Priority_int] = (object)((int)newValue[LV_Priority_int] + 1); } else { newValue[LV_Priority_int] = (object)((int)newValue[LV_Priority_int] - 1); } } else { isBooleanNotDouble = false; try { newValue[LV_Priority_int] = (object)(((double)newValue[LV_Priority_int] + (double)LV.toWhat)); votesCast[LV_Priority_int]++; } catch (InvalidCastException) { break; } } } catch (InvalidCastException) { isBooleanNotDouble = false; try { newValue[LV_Priority_int] = (object)(((double)newValue[LV_Priority_int] + (double)LV.toWhat)); votesCast[LV_Priority_int]++; } catch (InvalidCastException) { break; } } } if (isBooleanNotDouble) { if (newValue[0] != null && (int)newValue[0] > 0) { CurrentVariable.variableValue = true; } else if (newValue[0] != null && (int)newValue[0] < 0) { CurrentVariable.variableValue = false; } else if (newValue[0] != null && (int)newValue[1] > 0) { CurrentVariable.variableValue = true; } else if (newValue[0] != null && (int)newValue[1] < 0) { CurrentVariable.variableValue = false; } else if (newValue[0] != null && (int)newValue[2] > 0) { CurrentVariable.variableValue = true; } else if (newValue[0] != null && (int)newValue[2] < 0) { CurrentVariable.variableValue = false; } } else { if (votesCast[2] > 0) { double newValueToSet = (double)((double)newValue[2] / (double)votesCast[2]); CurrentVariable.variableValue = (object)newValueToSet; } else if (votesCast[1] > 0) { double newValueToSet = (double)((double)newValue[1] / (double)votesCast[1]); CurrentVariable.variableValue = (object)newValueToSet; } else if (votesCast[0] > 0) { double newValueToSet = (double)((double)newValue[0] / (double)votesCast[0]); CurrentVariable.variableValue = (object)newValueToSet; } } CurrentVariable.WhatShouldMyValueBe.Clear(); } if (Debug_VariableList != null) { try { this.BeginInvoke(Debug_VariableList, new object[] { AllVariables }); } catch (Exception) { } } } #endregion #region Update Timers public void UpdateTimers() { int[] votesLogic = new int[3]; int[] votesGUI = new int[3]; int[] votesInput = new int[3]; int[] votesOutput = new int[3]; int[] votesLogicTally = new int[3]; int[] votesGUITally = new int[3]; int[] votesInputTally = new int[3]; int[] votesOutputTally = new int[3]; #region Logic Timer foreach (LogicalVote LV_Logic in CanDo_ShouldIChangeLogicTimer) { votesLogic[Vote_Priority_To_Integer(LV_Logic.MyLogicalPriority)]++; votesLogicTally[Vote_Priority_To_Integer(LV_Logic.MyLogicalPriority)] += (int)LV_Logic.toWhat; } if (votesLogic[2] > 0) { int newTimerValue = (int)(votesLogicTally[2] / votesLogic[2]); LogicTimer.Interval = newTimerValue; THREAD_LOGIC_SLEEP_INTERVAL = newTimerValue; } else if (votesLogic[1] > 0) { int newTimerValue = (int)(votesLogicTally[1] / votesLogic[1]); LogicTimer.Interval = newTimerValue; THREAD_LOGIC_SLEEP_INTERVAL = newTimerValue; } else if (votesLogic[0] > 0) { int newTimerValue = (int)(votesLogicTally[0] / votesLogic[0]); LogicTimer.Interval = newTimerValue; THREAD_LOGIC_SLEEP_INTERVAL = newTimerValue; } #endregion #region GUI Timer foreach (LogicalVote LV_GUI in CanDo_ShouldIChangeGUITimer) { votesGUI[Vote_Priority_To_Integer(LV_GUI.MyLogicalPriority)]++; votesGUITally[Vote_Priority_To_Integer(LV_GUI.MyLogicalPriority)] += (int)LV_GUI.toWhat; } if (votesGUI[2] > 0) { int newTimerValue = (int)(votesGUITally[2] / votesGUI[2]); GUI_Timer.Interval = newTimerValue; } else if (votesGUI[1] > 0) { int newTimerValue = (int)(votesGUITally[1] / votesGUI[1]); GUI_Timer.Interval = newTimerValue; } else if (votesGUI[0] > 0) { int newTimerValue = (int)(votesGUITally[0] / votesGUI[0]); GUI_Timer.Interval = newTimerValue; } #endregion #region IO Timer foreach (LogicalVote LV_IO in CanDo_ShouldIChangeIOTimer) { votesInput[Vote_Priority_To_Integer(LV_IO.MyLogicalPriority)]++; votesInputTally[Vote_Priority_To_Integer(LV_IO.MyLogicalPriority)] += (int)LV_IO.toWhat; } if (votesInput[2] > 0) { int newTimerValue = (int)(votesInputTally[2] / votesInput[2]); IO_Timer.Interval = newTimerValue; THREAD_IO_SLEEP_INTERVAL = newTimerValue; } else if (votesInput[1] > 0) { int newTimerValue = (int)(votesInputTally[1] / votesInput[1]); IO_Timer.Interval = newTimerValue; THREAD_IO_SLEEP_INTERVAL = newTimerValue; } else if (votesInput[0] > 0) { int newTimerValue = (int)(votesInputTally[0] / votesInput[0]); IO_Timer.Interval = newTimerValue; THREAD_IO_SLEEP_INTERVAL = newTimerValue; } #endregion } #endregion #region Close All Open Streams public void CloseAllOpenLogStreams() { UpdateLogs("DUMP"); foreach (LogFileStream LFS in AllLogFileStreams) { LFS.MyFileStream.Close(); } } #endregion #region Update Log Files public void UpdateLogs(string targetFireOn) { foreach (LogClass CurrentLog in AllLogClasses) { #region Check if the Write Time is Correct if (targetFireOn == "DUMP") { CurrentLog.NumberToLogBeforeDumping = -1; } else { if (CurrentLog.fireOnString != targetFireOn && CurrentLog.fireOnString != targetFireOn + " timer") { continue; } } #endregion #region Tally Votes on whether or not to Turn On/Off int[] MyVotes = new int[3]; int[] MyIdea = new int[3]; foreach (LogicalVote LV in CurrentLog.ShouldIBeLogging) { MyVotes[Vote_Priority_To_Integer(LV.MyLogicalPriority)]++; MyIdea[Vote_Priority_To_Integer(LV.MyLogicalPriority)] += Vote_Opinion_To_Integer(LV.MyLogicalOpinion); } CurrentLog.ShouldIBeLogging.Clear(); if (MyVotes[0] > 0) { if (MyIdea[0] > 0) { CurrentLog.monitorState = true; } else if (MyIdea[0] < 0) { CurrentLog.monitorState = false; } } else if (MyVotes[1] > 0) { if (MyIdea[1] > 0) { CurrentLog.monitorState = true; } else if (MyIdea[1] < 0) { CurrentLog.monitorState = false; } } else if (MyVotes[2] > 0) { if (MyIdea[2] > 0) { CurrentLog.monitorState = true; } else if (MyIdea[2] < 0) { CurrentLog.monitorState = false; } } #endregion if (CurrentLog.monitorState || targetFireOn == "DUMP") { object dataToLog = null; string LogXMLString = "\r\n\r\n"; #region Get Value of Data To Log switch (CurrentLog.toMonitorType) { case "digital output": foreach(DigitalOutputChannel DOC in AllDigitalOutputs) { if (DOC.id == CurrentLog.toMonitorName) { dataToLog = (object)DOC.CurrentState; break; } } break; case "analogue input": foreach (AnalogueInputChannel AIC in AllAnalogueInputs) { if (AIC.id == CurrentLog.toMonitorName) { dataToLog = (object)AIC.CurrentValue; break; } } break; case "digital input": foreach (DigitalInputChannel DIC in AllDigitalInputs) { if (DIC.id == CurrentLog.toMonitorName) { dataToLog = (object)DIC.CurrentState; break; } } break; case "variable": foreach (Variable VAR in AllVariables) { if (VAR.variableReferenceName == CurrentLog.toMonitorName) { dataToLog = VAR.variableValue; break; } } break; default: //Error or Unknown break; } #endregion #region Check that Something was Found if (dataToLog == null) { //Nothing was found, skip continue; } #endregion #region Decide to Either Dump or Store the Data if (CurrentLog.monitorState) { string newLogName = CurrentLog.dataName; if (newLogName == "") { newLogName = CurrentLog.toMonitorType + ": " + CurrentLog.toMonitorName; } CurrentLog.MyDataPoints.Add(new LogDataPoint(dataToLog, newLogName)); } if (targetFireOn != "DUMP") { if (CurrentLog.MyDataPoints.Count <= CurrentLog.NumberToLogBeforeDumping) { //Can still hold more points continue; } if (CurrentLog.MyDataPoints.Count == 0) { // Nothing here... continue; } } #endregion #region Check for and Open or Create if (!CurrentLog.HoldingAStreamOpen) { try { if (File.Exists(CurrentLog.saveToFile + ".xml")) { FileStream logFileStream = new FileStream(CurrentLog.saveToFile + ".xml", FileMode.Open, FileAccess.ReadWrite); StreamReader logStreamReader = new StreamReader(logFileStream); LogXMLString = logStreamReader.ReadToEnd(); logStreamReader.Close(); logFileStream.Close(); if (!LogXMLString.StartsWith("\r\n")) { LogXMLString = "\r\n\r\n"; } } else { if (CurrentLog.saveToFile.Contains("\\")) { string temp_Directory = CurrentLog.saveToFile.Substring(0, CurrentLog.saveToFile.LastIndexOf('\\')); Directory.CreateDirectory(temp_Directory); } File.Create(CurrentLog.saveToFile + ".xml").Close(); } } catch (Exception) { continue; } } else { LogXMLString = AllLogFileStreams[CurrentLog.StreamIndex].CurrentData; } #endregion string All_newLogNodes = ""; foreach (LogDataPoint LDP in CurrentLog.MyDataPoints) { All_newLogNodes += LDP.GenerateXMLNode(); } CurrentLog.MyDataPoints.Clear(); LogXMLString = LogXMLString.Replace("", All_newLogNodes + ""); try { if (!CurrentLog.HoldingAStreamOpen) { FileStream log_write_FileStream = new FileStream(CurrentLog.saveToFile + ".xml", FileMode.Open, FileAccess.ReadWrite); StreamWriter log_write_StreamWriter = new StreamWriter(log_write_FileStream); log_write_StreamWriter.Write(LogXMLString); log_write_StreamWriter.Close(); log_write_FileStream.Close(); } else { AllLogFileStreams[CurrentLog.StreamIndex].CurrentData = LogXMLString; } if (targetFireOn == "DUMP") { if (CurrentLog.StreamIndex >= 0 && AllLogFileStreams[CurrentLog.StreamIndex].MyStreamWriter.BaseStream.CanWrite) { AllLogFileStreams[CurrentLog.StreamIndex].MyStreamWriter.Write(AllLogFileStreams[CurrentLog.StreamIndex].CurrentData); } else { FileStream log_write_FileStream = new FileStream(CurrentLog.saveToFile + ".xml", FileMode.Open, FileAccess.ReadWrite); StreamWriter log_write_StreamWriter = new StreamWriter(log_write_FileStream); log_write_StreamWriter.Write(LogXMLString); log_write_StreamWriter.Close(); log_write_FileStream.Close(); } } } catch (Exception) { } } } } #endregion #region Update Graphs private float DEBUG_SIN_PLUSPLUS = 0.0f; public void UpdateGraphs(string input_timer) { for (int i = 0; i < AllGraphs.Count; i++) { if (!AllGraphs[i].timer.ToLower().StartsWith(input_timer.ToCharArray()[0].ToString(), StringComparison.OrdinalIgnoreCase)) { continue; } object SelectionBasis = BaseSelectionOffOf_Function(AllGraphs[i].target, AllGraphs[i].targetID); if (SelectionBasis == null) { /* for (int rSeed = 0; rSeed < MDX_Random.Next(1000); rSeed++) { MDX_Random.NextDouble(); } SelectionBasis = (object)((float)MDX_Random.NextDouble() * (AllGraphs[i].value_maximum - AllGraphs[i].value_minimum + 50)); */ //SelectionBasis = (object)0.0f; SelectionBasis = (object)(((float)Math.Sin((double)DEBUG_SIN_PLUSPLUS) * ((AllGraphs[i].value_maximum - AllGraphs[i].value_minimum) / 2)) + ((AllGraphs[i].value_maximum - AllGraphs[i].value_minimum) / 2)); DEBUG_SIN_PLUSPLUS += 0.1f; if (DEBUG_SIN_PLUSPLUS > (2 * Math.PI)) { DEBUG_SIN_PLUSPLUS = 0.0f; } //Object to base image selection on was not found so skip item... //return; } try { float f = 0.0f; //try //{ // f = (float)SelectionBasis; //} //catch (InvalidCastException invCE) //{ float.TryParse(SelectionBasis.ToString(), out f); //} if (f > AllGraphs[i].value_maximum) { f = AllGraphs[i].value_maximum; } else if (f < AllGraphs[i].value_minimum) { f = AllGraphs[i].value_minimum; } bool smooth = AllGraphs[i].smooth; AllGraphs[i].DataPoints.Add(f); if (AllGraphs[i].DataPoints.Count > AllGraphs[i].time_maximum) { AllGraphs[i].DataPoints.RemoveAt(0); } if (smooth && (AllGraphs[i].DataPoints.Count >= 2)) { bool goFWD = AllGraphs[i].forwardBiasNotReverseBias; int numberToAverage = AllGraphs[i].smoothNumbers; float dWeight = 1.0f / (float)numberToAverage; float totalWeight = 0.0f; if (numberToAverage > AllGraphs[i].DataPoints.Count - 1) { numberToAverage = AllGraphs[i].DataPoints.Count - 1; } if (goFWD) { for (int j = numberToAverage; j > 0; j--) { totalWeight += AllGraphs[i].DataPoints[AllGraphs[i].DataPoints.Count - j] * (dWeight * (float)j); } } else { for (int j = 1; j <= numberToAverage; j++) { totalWeight += AllGraphs[i].DataPoints[AllGraphs[i].DataPoints.Count - j] * (dWeight * (float)j); } } f = totalWeight / (float)numberToAverage; if (f == float.NaN) { f = 0.0f; } AllGraphs[i].DataPoints[AllGraphs[i].DataPoints.Count - 1] = f; } } catch (Exception) { } } } #endregion private List _logicBooleanList = new List(); private static int _logicIteration = -1; private static int _currentLogicMinimumVoteTime = 0; private bool OperationalLogicIsSetup = false; public bool PerformLogicCommands(string which_timer) { _currentLogicMinimumVoteTime = 0; _Stack = 0; if (which_timer.ToLower() == "logic") { UpdateVariables(); UpdateTimers(); } else if (which_timer.ToLower() == "gui") { foreach (BrainID CurrentBrain in AllBrains) { if (VirtualBrainClass.isAVirtualBrain(CurrentBrain.humanName)) { try { if (Debug_VirtualBrain_03 != null) { try { this.BeginInvoke(Debug_VirtualBrain_03, new object[] { CurrentBrain.humanName }); } catch (Exception) { } } if (Debug_VirtualBrain_04 != null) { try { this.BeginInvoke(Debug_VirtualBrain_04, new object[] { CurrentBrain }); } catch (Exception) { } } } catch (Exception) { } } } } UpdateGraphs(which_timer); if (AllLogicXMlNodes.Count == 0) { return false; } _logicIteration = -1; bool _tempISDEBUGGING = false; if (OperationalLogicIsSetup) { if (_tempISDEBUGGING) { IfThenEvaluatorOperationalLookupFunction(0, which_timer); } else { try { IfThenEvaluatorOperationalLookupFunction(0, which_timer); } catch (Exception e) { } } } else { OperationalLogicIsSetup = true; _logicBooleanList = new List(); _logicIteration = -1; for (int logicI = 0; logicI < AllLogicXMlNodes.Count; logicI++) { XmlNode IfNode = AllLogicXMlNodes[logicI]._logicNode; _currentLogicMinimumVoteTime = 0; if (IfNode.Name.ToLower() == "if" || IfNode.Name.ToLower() == "switch") { _logicIteration++; LogicalVote thisVote; thisVote.MyLogicalPriority = Vote_Priority.MEDIUM_PRIORITY; string fireon = "logic"; int minimum_vote_time = 0; foreach (XmlAttribute IfAttribute in IfNode.Attributes) { if (IfAttribute.Name.ToLower() == "fire_on") { fireon = IfAttribute.Value.ToLower(); } else if (IfAttribute.Name.ToLower() == "minimum_delta_vote_time") { int.TryParse(IfAttribute.Value, out minimum_vote_time); _currentLogicMinimumVoteTime = minimum_vote_time; } } if (!OperationalLogicIsSetup) { if (which_timer != fireon) { continue; } } Operation _StartOperation = new Operation(Opcode.START, new List(new object[] { fireon })); FunctionOperationLookUpList.Add(_StartOperation); object outcomeIfThen = null; if (!_tempISDEBUGGING) { try { outcomeIfThen = IfThenEvaluator(IfNode, "boolean"); } catch (Exception exception_e) { Console.WriteLine(exception_e.ToString()); } } else { outcomeIfThen = IfThenEvaluator(IfNode, "boolean"); } Operation _FinOperation = new Operation(Opcode.FIN, new List()); FunctionOperationLookUpList.Add(_FinOperation); _StartOperation._Operands.Add((FunctionOperationLookUpList.Count - 1)); if (!OperationalLogicIsSetup) { if (outcomeIfThen == null) { continue; } } bool outcomeIfThen_boolean = false; if (!bool.TryParse(outcomeIfThen.ToString(), out outcomeIfThen_boolean)) { } _logicBooleanList.Add(outcomeIfThen_boolean); List ThenNodeList = new List(); List AntiThenNodeList = new List(); if (OperationalLogicIsSetup) { foreach (XmlNode findThenNode in IfNode.ChildNodes) { if (findThenNode.Name.ToLower() == "then") { foreach (XmlNode findThenDoNode in findThenNode.ChildNodes) { if (findThenDoNode.Name.ToLower() == "do") { ThenNodeList.Add(findThenDoNode); } } } else if (findThenNode.Name.ToLower() == "anti-then") { foreach (XmlNode findThenDoNode in findThenNode.ChildNodes) { if (findThenDoNode.Name.ToLower() == "do") { AntiThenNodeList.Add(findThenDoNode); } } } } } _FinOperation._Operands.AddRange(new object[] { ThenNodeList, AntiThenNodeList, DateTime.MinValue, DateTime.MinValue, minimum_vote_time}); if (outcomeIfThen_boolean) { //If resolved to true, then do some stuff... foreach (XmlNode findThenNode in IfNode.ChildNodes) { if (findThenNode.Name.ToLower() == "then") { foreach (XmlNode findThenDoNode in findThenNode.ChildNodes) { if (findThenDoNode.Name.ToLower() == "do") { //FunctionEvaluator_EvaluateDoThenNode(findThenDoNode, logicI); EvaluateDoThenNode(findThenDoNode, DateTime.MinValue, minimum_vote_time); } else { //Error or Unknown } } } } } else { //If resolved to false, then do some stuff... foreach (XmlNode findThenNode in IfNode.ChildNodes) { if (findThenNode.Name.ToLower() == "anti-then") { foreach (XmlNode findThenDoNode in findThenNode.ChildNodes) { if (findThenDoNode.Name.ToLower() == "do") { //FunctionEvaluator_EvaluateDoThenNode(findThenDoNode, logicI); EvaluateDoThenNode(findThenDoNode, DateTime.MinValue, minimum_vote_time); } else { //Error or Unknown } } } } } } } } if (Debug_IfThenList != null && _logicBooleanList.Count > 0) { try { this.BeginInvoke(Debug_IfThenList, new object[] { _logicBooleanList }); } catch (Exception) { } } return true; } public static bool EvaluateDoThenNode(XmlNode input_ThenDoNode, DateTime input_lastVoteTime, int input_minimumVoteTime) { string find_Function = ""; string find_FunctionTarget = ""; string find_VoteOpinion = ""; Vote_Priority find_VotePriority = Vote_Priority.MEDIUM_PRIORITY; string find_VoteType = ""; foreach (XmlAttribute findThenDoNodeAttribute in input_ThenDoNode.Attributes) { switch (findThenDoNodeAttribute.Name.ToLower()) { case "function": find_Function = findThenDoNodeAttribute.Value.ToLower(); break; case "functiontargetid": find_FunctionTarget = findThenDoNodeAttribute.Value.ToLower(); break; case "vote_opinion": find_VoteOpinion = findThenDoNodeAttribute.Value.ToLower(); break; case "vote_priority": switch (findThenDoNodeAttribute.Value.ToLower()) { case "low": find_VotePriority = Vote_Priority.LOW_PRIORITY; break; case "medium": find_VotePriority = Vote_Priority.MEDIUM_PRIORITY; break; case "high": find_VotePriority = Vote_Priority.HIGH_PRIORITY; break; default: //Error Or Unknown break; } break; case "vote": find_VoteType = findThenDoNodeAttribute.Value.ToLower(); break; default: //Error Or Unknown break; } } //DateTime _lastDate = DateTime.MinValue; //int _betweenTime = 0; /* if(logicI >= 0) { _lastDate = AllLogicXMlNodes[logicI]._logicLastVoteTime; _betweenTime = AllLogicXMlNodes[logicI]._logicMinimumTimeBetweenVotes; } */ if (DoSomethingWithLogicCommand(find_Function, find_FunctionTarget, find_VoteOpinion, find_VotePriority, find_VoteType, null, input_lastVoteTime, input_minimumVoteTime)) { //AllLogicXMlNodes[logicI]._logicLastVoteTime = DateTime.Now; //input_lastVoteTime = DateTime.Now; return true; } return false; } } }