//method to convert comparison names into the proper symbols private static string translateComparisons(Block b) { if (b.ToString().Contains("AND") || b.ToString().Contains("OR")) { return b.ToString().ToLower(); } switch (b.ToString()) { case "COMPARISON-greater": return ">"; case "COMPARISON-less": return "<"; case "COMPARISON-greaterequal": return ">="; case "COMPARISON-lessequal": return "<="; case "COMPARISON-equal": return "=="; case "COMPARISON-notequal": return "!="; case "ASSIGNMENT": return "="; default: return "error"; } }
/// <summary> /// Convert an flow document paragraph into its html representation. /// </summary> /// <param name="block">Flow document block.</param> /// <param name="htmlWriter">XmlTextWriter producing resulting html.</param> /// <param name="conversionResult">Conversion result to store error and warning messages. Can be null.</param> /// <remarks> /// List, Paragraph, Table are supported Block elements. Section is not supported. /// </remarks> private static void AddBlock(Block block, XmlTextWriter htmlWriter, ValidationResult conversionResult) { if (block is List) { AddList(block as List, htmlWriter, conversionResult); } else if (block is Paragraph) { AddParagraph(block as Paragraph, htmlWriter, conversionResult); } else if (block is Table) { AddTable(block as Table, htmlWriter, conversionResult); } else { // not supported: Section if (conversionResult != null) { conversionResult.AddMessage(new ConversionMessage(ModelValidationViolationType.Warning, "AddBlock: Unknown block element: " + block.ToString())); } } }
//checks if block is a loop which will require no parens, i.e. for/wait for private static bool checkSpecialLoop(Block b) { if (b.ToString().Contains("FOR")) { return true; } else { return false; } }
//these are methods that convert block formats into their objective G counterparts, mostly used for robot functions #region Translation Methods //method to translate robot functions into code public static string translateRobotFunctions(Block b) { string functName = b.ToString(); switch (functName) { case "DRIVE": return "method drive " + readSocket(b); //break; case "DRIVEDISTANCE": return "method driveDistance " + readSocket(b); // break; case "TURN": return "method turn " + readSocket(b); //break; case "TURNDEGREES": return "method turnAngle " + readSocket(b); //break; case "TURNTOBEARING": return "method turnToBearing " + readSocket(b); //break; case "CHECKRANGE": return "method getSonars " + readSocket(b); //break; case "GETBEARING": return "method getBearing ()"; //break; case "STOP": return "method stop ()"; //break; default: Debug.WriteLine("ERROR: METHOD NOT RECOGNIZED"); return ""; //break; } }
//method to read socket values and place them into parentheses public static String readSocket(Block source) { if (source.flag_hasSocks) { String plaintext =""; //string that will become the output ListBox socket; //listbox to be checked for values bool parenCheck = checkLocation(source); //bool to see if socket needs parentheses bool specialLoop = checkSpecialLoop(source); //bool to see if socket is in for/wait for loops, which have special outputs //adding opening bracket or paren if necessary if (source.ToString().Equals("ASSIGNMENT") || source.ToString().Equals("RETURN")) { plaintext = " "; } else if (parenCheck || source.flag_isCustom) { plaintext = " ( "; } else { plaintext = "[ "; } //getting all sockets in the block List<int> locations = socketFinder(source); foreach (int i in locations) { socket = socketMole(source, i); //ensuring that the socket contains a block if (socket.Items.Count > 0) { Block infoCube = (Block)socket.Items.ElementAt(0); //handling nested conditions, logic statements, and methods since they have unique styles if (infoCube.flag_transformer) { if (infoCube.flag_isCustom) { plaintext += "method "+ infoCube.metadataList[1] + readSocket(infoCube); } else { plaintext += readSocket(infoCube) + " "; } } //handling blocks with text else if (infoCube.flag_isConstant && infoCube.flag_hasSocks && !infoCube.flag_robotOnly) { plaintext += (infoCube.ToString()).ToLower(); int slot = textFinder(infoCube); //general case if (!specialLoop) { //handling string blocks if (infoCube.ToString().Contains("STRING")) { plaintext += (" \"" + textReader(infoCube, slot) + "\" ").ToLower(); } //handling other, numerical blocks else { plaintext += (" " + textReader(infoCube, slot) + " ").ToLower(); } } //case of for/wait for else { if(source.ToString().Contains("WAIT")) { return " " + textReader(infoCube, slot) + ";"; } else{ return " " + textReader(infoCube, slot); } } } //handling robot functions and constants else if (infoCube.flag_robotOnly && (infoCube.flag_hasSocks || infoCube.ToString().Contains("GETBEARING"))) { plaintext += translateRobotFunctions(infoCube); } //handling blocks with comboboxes or non-text constants else if ((infoCube.flag_isRobotConstant || infoCube.flag_isConstant) && !infoCube.flag_hasSocks) { //infinity block case, to be placed into for/wait for if (specialLoop) { return " -1"; } //handling any combobox blocks else { ComboBox cb = (ComboBox)infoCube.innerPane.Children.ElementAt(1); //differentiating between the combobox types since bearing/range, direction, and others have different processes if (infoCube.ToString().Contains("BEARING") || source.ToString().Contains("RANGE")) { plaintext += SocketReader.translateDistanceInputs(cb); } else if (infoCube.ToString().Contains("DIRECTION")) { plaintext += SocketReader.translateDriveInputs(infoCube); } else { plaintext += (((TextBlock)cb.SelectionBoxItem).Text + " ").ToLower(); } } } //handling variables, since they require 2 strings for identification else if (infoCube.flag_isCustom) { //accounting for using var in front of variable names...except in assign for some reason, which doesn't use it if (source.ToString().Contains("ASSIGN")) { plaintext += infoCube.metadataList[1] + " "; } else { plaintext += "var " + infoCube.metadataList[1] + " "; } plaintext += readSocket(infoCube); } //all other blocks else { plaintext += (infoCube.metadataList[0] + " ").ToLower(); plaintext += readSocket(infoCube); } //adding the operator after the first comparator, if necessary if (i == 0 && !source.flag_isCustom) { plaintext += " " + translateComparisons(source) + " "; } //adding the comma needed in parameters else if (source.flag_isCustom && i != locations.ElementAt(locations.Count() - 1)) { plaintext += ", "; } //accounting for the equal sign needed in assign else if (i == 2 && locations.Contains(4)) { plaintext += " = "; } } } //adding closing brackets and parens if (source.ToString().Equals("ASSIGNMENT") || source.ToString().Equals("RETURN")) { //removing the extra space at the end if (plaintext[plaintext.Length-1] != ')') { plaintext = plaintext.Substring(0, plaintext.Length - 1); } } else if (parenCheck || source.flag_isCustom) { plaintext += ")"; } else { plaintext += "]"; } return plaintext; } else { return ""; } }
//method to transfer textbox data public static Block textTransfer(Block source, Block destination) { int location = textFinder(source); if (location != -1) { //accounting for transferring comboboxes if (source.ToString().Contains("DIRECTION")) { ComboBox x = (ComboBox)source.innerPane.Children.ElementAt(location); int selection = x.SelectedIndex; //copying the combobox and selection options since stupid silverlight apparently has no simple method for it that doesn't crash after opening the new box... List<TextBlock> itemCopies = new List<TextBlock>(); for (int i = 0; i < x.Items.Count; i++) { x.SelectedIndex = i; TextBlock temp = new TextBlock(); temp.Text = ((TextBlock)x.SelectionBoxItem).Text; itemCopies.Add(temp); } //creating and inserting new block's combobox ComboBox y = new ComboBox(); y.ItemsSource = itemCopies; y.SelectedIndex = selection;//x.SelectedIndex; destination.innerPane.Children.Add(y); } else { String text = textReader(source, location); int dstLocation = textFinder(destination); TextBox x = (TextBox)destination.innerPane.Children.ElementAt(dstLocation); x.Text = text; } } return destination; }
//method to find and return the indices of the sockets in a block public static List<int> socketFinder(Block source) { List<int> socketList = new List<int>(); List<System.Windows.UIElement> components; //checking for logic-style blocks if (!(source.innerPane.Children.ElementAt(0) is TextBlock)) { StackPanel innards = (StackPanel)source.innerPane.Children.ElementAt(0); components = innards.Children.ToList(); } //checking for assignment blocks and diverting accordingly else if (source.ToString().Contains("ASSIGN") && source.innerPane.Children.Count > 2) { StackPanel innards = (StackPanel)source.innerPane.Children.ElementAt(2); components = innards.Children.ToList(); } //checking for method blocks else if (source.flag_isCustom && source.flag_transformer) { //checking if in main panel or socket, based on the presence of the line number if (source.innerPane.Children.Count == 4) { StackPanel innards = (StackPanel)source.innerPane.Children.ElementAt(3); components = innards.Children.ToList(); } else { //accounting for a method with no parameters if (source.innerPane.Children.ElementAt(2) is TextBlock) { components = new List<UIElement>(); } else { StackPanel innards = (StackPanel)source.innerPane.Children.ElementAt(2); components = innards.Children.ToList(); } } } //all other cases else { components = source.innerPane.Children.ToList(); } //cycling through and grabbing any actual sockets in the block for (int i = 0; i < components.Count; i++) { if (components.ElementAt(i) is SocketDragDropTarget) { socketList.Add(i); } } return socketList; }
//these are methods used to locate blocks and sockets, as well as performing potential transfers #region Socket Location and Transfer Methods //method to tunnel into the block and return the socket that is at the given position public static ListBox socketMole(Block source, int i) { List<System.Windows.UIElement> components; //checking for methods if (source.flag_isCustom && source.flag_transformer) { //checking if in main panel or socket based on whether the line number is present if (source.innerPane.Children.Count == 4) { StackPanel innards = (StackPanel)source.innerPane.Children.ElementAt(3); components = innards.Children.ToList(); } else { StackPanel innards = (StackPanel)source.innerPane.Children.ElementAt(2); components = innards.Children.ToList(); } } //checking for assignment blocks and diverting accordingly else if (source.ToString().Contains("ASSIGN")) { StackPanel innards = (StackPanel)source.innerPane.Children.ElementAt(2); components = innards.Children.ToList(); } //checking for logic-style blocks else if (source.flag_transformer) { //accounting for situations where the block is already transformed if (source.innerPane.Children.ElementAt(0) is StackPanel) { StackPanel innards = (StackPanel)source.innerPane.Children.ElementAt(0); components = innards.Children.ToList(); } else { i++; components = source.innerPane.Children.ToList(); } } else { components = source.innerPane.Children.ToList(); } //getting the socket to be returned SocketDragDropTarget SDDT = (SocketDragDropTarget)components.ElementAt(i); ListBox listBox = (ListBox)SDDT.Content; return listBox; }
public static void AddToSocket(Block source, int i, Block child) { List<System.Windows.UIElement> components; //checking for logic-style blocks if (!(source.innerPane.Children.ElementAt(0) is TextBlock)) { StackPanel innards = (StackPanel)source.innerPane.Children.ElementAt(0); components = innards.Children.ToList(); } //checking for assignment blocks and diverting accordingly else if (source.ToString().Contains("ASSIGN") && source.innerPane.Children.Count > 2) { StackPanel innards = (StackPanel)source.innerPane.Children.ElementAt(2); components = innards.Children.ToList(); } //checking for method blocks else if (source.flag_isCustom && source.flag_transformer) { //checking if in main panel or socket if (source.innerPane.Children.Count == 4) { StackPanel innards = (StackPanel)source.innerPane.Children.ElementAt(3); components = innards.Children.ToList(); } else { if (source.innerPane.Children.ElementAt(2) is TextBlock) { components = new List<UIElement>(); } else { StackPanel innards = (StackPanel)source.innerPane.Children.ElementAt(2); components = innards.Children.ToList(); } } } else { components = source.innerPane.Children.ToList(); } SocketDragDropTarget SDDT = (SocketDragDropTarget)components.ElementAt(i); ListBox listBox = (ListBox)SDDT.Content; SDDT.ResizeAndAdd(listBox, child); }
//method to change combo boxes in directional blocks private static void directionalCombos(Block Parent, Block copyBlock) { ComboBox cb = (ComboBox)copyBlock.innerPane.Children.ElementAt(2); //if turn, remove front, rear, backwards and forwards if (Parent.ToString().Contains("TURN")) { Debug.WriteLine(copyBlock.innerPane.Children.ElementAt(2)); cb.Items.RemoveAt(3); cb.Items.RemoveAt(2); cb.Items.RemoveAt(1); cb.Items.RemoveAt(0); cb.SelectedItem = cb.Items.ElementAt(0); } //if drive, remove front,rear, left and right else if (Parent.ToString().Contains("DRIVE")) { Debug.WriteLine(copyBlock.innerPane.Children.ElementAt(2)); cb.Items.RemoveAt(5); cb.Items.RemoveAt(4); cb.Items.RemoveAt(3); cb.Items.RemoveAt(2); } //if distance, remove backwards and forwards else if (Parent.ToString().Contains("RANGE")) { Debug.WriteLine(copyBlock.innerPane.Children.ElementAt(2)); cb.Items.RemoveAt(1); cb.Items.RemoveAt(0); cb.SelectedIndex = 0; } }
//method to write the socket xml data private static void WriteSockets(XmlWriter writer, Block b) { List<int> socketList = SocketReader.socketFinder(b); //writing data from text boxes and combo boxes if((b.flag_isConstant || b.flag_isRobotConstant )&& !b.ToString().Contains("RANGE")) { //handling basic text if (b.flag_hasSocks ) { int slot = SocketReader.textFinder(b); writer.WriteStartElement("VALUE"); writer.WriteString(SocketReader.textReader(b, slot)); writer.WriteEndElement(); } else if (b.Text.Equals("GETBEARING") || b.Text.Equals("INFINITY")) //getBearing and infinity both dont have any metadata { } //handling combo boxes else { ComboBox cb = (ComboBox)b.innerPane.Children.ElementAt(1); writer.WriteStartElement("INDEX"); writer.WriteString(cb.SelectedIndex.ToString()); writer.WriteEndElement(); } } //handling standard sockets else { foreach (int location in socketList) { writer.WriteStartElement("SOCKET"); ListBox socket = SocketReader.socketMole(b, location); //ensuring the socket isn't empty if (socket.Items.Count > 0) { Block infosphere = (Block)socket.Items.ElementAt(0); //checking if block is a variable or method if (infosphere.flag_isCustom) { //if (infosphere.Name.Equals("VARIABLE")) // { writer.WriteStartElement("BLOCK"); writer.WriteAttributeString("type", infosphere.Text); //type of block // writer.WriteStartElement("VARIABLE"); //} // else if (infosphere.Name.Equals("METHOD")) // { // writer.WriteStartElement("METHOD"); // } writer.WriteStartElement("name"); writer.WriteString(infosphere.metadataList[1]); //name of variable writer.WriteEndElement(); } else { writer.WriteStartElement("BLOCK"); writer.WriteAttributeString("type", infosphere.Text); //type of block //writer.WriteStartElement(infosphere.ToString()); } WriteSockets(writer, infosphere); writer.WriteEndElement(); } writer.WriteEndElement(); } } }