#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Text; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using System.Collections.Generic; namespace HeuristicLab.Problems.DataAnalysis.Views.Symbolic { [StorableClass] public class SymbolicExpressionTreeGraphvizFormatter { [Storable] public bool Indent { get; set; } private readonly static Dictionary symbolNameMap = new Dictionary() { {"Multiplication", "Mul"}, {"Addition", "Add"}, {"Division", "Div"}, {"Subtraction", "Sub"}, {"Average", "Avg"}, {"Logarithm", "Log"}, {"Exponential", "Exp"}, {"Cosine", "Cos"}, {"Sine", "Sin"}, {"Tangent", "Tan"}, {"GreaterThan", ">"}, {"LessThan", "<"}, {"IfThenElse", "If"}, // match Koza style {"ProgramRootSymbol", "Prog"}, {"StartSymbol", "RPB"}, }; public SymbolicExpressionTreeGraphvizFormatter() : base() { Indent = true; } public string Format(SymbolicExpressionTree symbolicExpressionTree) { int nodeCounter = 1; StringBuilder strBuilder = new StringBuilder(); strBuilder.AppendLine("graph {"); strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root, 0, ref nodeCounter)); strBuilder.AppendLine("}"); return strBuilder.ToString(); } private string FormatRecursively(SymbolicExpressionTreeNode node, int indentLength, ref int nodeId) { // save id of current node int currentNodeId = nodeId; // increment id for next node nodeId++; StringBuilder strBuilder = new StringBuilder(); if (Indent) strBuilder.Append(' ', indentLength); // get label for node and map if necessary string nodeLabel = node.ToString(); if (symbolNameMap.ContainsKey(nodeLabel)) { nodeLabel = symbolNameMap[nodeLabel]; } strBuilder.Append("node" + currentNodeId + "[label=\"" + nodeLabel + "\""); // leaf nodes should have box shape if (node.SubTrees.Count == 0) { strBuilder.AppendLine(", shape=\"box\"];"); } else { strBuilder.AppendLine("];"); } // internal nodes or leaf nodes? foreach (SymbolicExpressionTreeNode subTree in node.SubTrees) { // add an edge if (Indent) strBuilder.Append(' ', indentLength); strBuilder.AppendLine("node" + currentNodeId + " -- node" + nodeId + ";"); // format the whole subtree strBuilder.Append(FormatRecursively(subTree, indentLength + 2, ref nodeId)); } return strBuilder.ToString(); } } }