public Operand(AIHandler overlord, double value, bool constant, int dataSource) { this.overlord = overlord; this.value = value; this.constant = constant; this.dataSource = dataSource; }
public Subtask(String name, AIHandler overlord, String priorityExpression) { this.name = name; children = new List <TaskNode>(); if (!BuildPriorityTree(overlord, priorityExpression, ref priorityRoot)) { throw new InvalidOperationException(); } }
public FunctionNode(AIHandler aiHandler, String name, int functionId, String param, AIHandler overlord, String priorityExpression) { this.aiHandler = aiHandler; this.name = name; if (!BuildPriorityTree(overlord, priorityExpression, ref priorityRoot)) { throw new InvalidOperationException(); } if (!BuildPriorityTree(overlord, param, ref paramRoot)) { throw new InvalidOperationException(); } this.functionId = functionId; }
//builds the TaskNode's priority evalutation tree using a postfix mathematical expression protected bool BuildPriorityTree(AIHandler overlord, String priorityExpression, ref ExpressionNode givenRoot) { char[] splitLegend = { ' ' }; String[] expression = priorityExpression.Split(splitLegend, System.StringSplitOptions.RemoveEmptyEntries); //create the tree using a stack method, then assign the end result to the priorityRoot field Stack <ExpressionNode> stack = new Stack <ExpressionNode>(); foreach (String s in expression) { if (s.Equals("+") || s.Equals("-") || s.Equals("/") || s.Equals("*") || s.Equals("=") || s.Equals("<") || s.Equals(">") || s.Equals("^") || s.Equals("|") || s.Equals("&") || s.Equals("%") || s.Equals("`") || s.Equals("~")) { //create an operator, pop two from stack and set them as children, push operator ExpressionNode left = stack.Pop(); ExpressionNode right = stack.Pop(); Operator op = new Operator(s.ToCharArray()[0], left, right); stack.Push(op); } else { //create an operand, push to stack //check if the operand is a variable or a constant if (s.StartsWith("$")) { int source; if (!Int32.TryParse(s.Substring(1), out source)) { overlord.form1.WriteAI("ERROR: Invalid variable \"" + s + "\" in \"" + ToString() + "\""); return(false); } Operand c = new Operand(overlord, 0, false, source); stack.Push(c); } else { //parse the string into a double d double d; if (!Double.TryParse(s, out d)) { overlord.form1.WriteAI("ERROR: Invalid constant \"" + s + "\" in \"" + ToString() + "\""); return(false); } Operand c = new Operand(overlord, d, true, 0); stack.Push(c); } } } //foreach term in the expression if (stack.Count != 1) { overlord.form1.WriteAI("ERROR: Invalid postfix expression \"" + priorityExpression + "\""); return(false); } givenRoot = stack.Pop(); return(true); }