Changeset 654
- Timestamp:
- 10/14/08 14:42:29 (16 years ago)
- Location:
- branches/GpPluginsRefactoringBranch
- Files:
-
- 4 added
- 1 deleted
- 32 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Addition.cs
r645 r654 47 47 AddConstraint(new SubOperatorTypeConstraint(2)); 48 48 } 49 50 public override void Accept(IFunctionVisitor visitor) {51 visitor.Visit(this);52 }53 49 } 54 50 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/And.cs
r645 r654 43 43 AddConstraint(new SubOperatorTypeConstraint(2)); 44 44 } 45 46 public override void Accept(IFunctionVisitor visitor) {47 visitor.Visit(this);48 }49 45 } 50 46 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Average.cs
r645 r654 41 41 AddConstraint(new SubOperatorTypeConstraint(2)); 42 42 } 43 44 public override void Accept(IFunctionVisitor visitor) {45 visitor.Visit(this);46 }47 43 } 48 44 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Constant.cs
r645 r654 90 90 AddVariable(new HeuristicLab.Core.Variable(MANIPULATION, combinedOp)); 91 91 } 92 93 public override void Accept(IFunctionVisitor visitor) {94 visitor.Visit(this);95 }96 92 } 97 93 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Cosinus.cs
r645 r654 40 40 AddConstraint(new SubOperatorTypeConstraint(0)); 41 41 } 42 43 public override void Accept(IFunctionVisitor visitor) {44 visitor.Visit(this);45 }46 42 } 47 43 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Differential.cs
r645 r654 43 43 : base() { 44 44 } 45 46 public override void Accept(IFunctionVisitor visitor) {47 visitor.Visit(this);48 }49 45 } 50 46 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Division.cs
r645 r654 54 54 AddConstraint(new SubOperatorTypeConstraint(2)); 55 55 } 56 57 public override void Accept(IFunctionVisitor visitor) {58 visitor.Visit(this);59 }60 56 } 61 57 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Equal.cs
r645 r654 41 41 AddConstraint(new SubOperatorTypeConstraint(1)); 42 42 } 43 44 public override void Accept(IFunctionVisitor visitor) {45 visitor.Visit(this);46 }47 43 } 48 44 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Exponential.cs
r645 r654 40 40 AddConstraint(new SubOperatorTypeConstraint(0)); 41 41 } 42 43 public override void Accept(IFunctionVisitor visitor) {44 visitor.Visit(this);45 }46 42 } 47 43 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/GreaterThan.cs
r645 r654 40 40 AddConstraint(new SubOperatorTypeConstraint(1)); 41 41 } 42 public override void Accept(IFunctionVisitor visitor) {43 visitor.Visit(this);44 }45 42 } 46 43 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/IfThenElse.cs
r645 r654 43 43 AddConstraint(new SubOperatorTypeConstraint(2)); 44 44 } 45 46 public override void Accept(IFunctionVisitor visitor) {47 visitor.Visit(this);48 }49 45 } 50 46 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/LessThan.cs
r645 r654 41 41 AddConstraint(new SubOperatorTypeConstraint(1)); 42 42 } 43 44 public override void Accept(IFunctionVisitor visitor) {45 visitor.Visit(this);46 }47 43 } 48 44 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Logarithm.cs
r645 r654 40 40 AddConstraint(new SubOperatorTypeConstraint(0)); 41 41 } 42 43 public override void Accept(IFunctionVisitor visitor) {44 visitor.Visit(this);45 }46 42 } 47 43 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/ModelAnalyzerExporter.cs
r645 r654 27 27 28 28 namespace HeuristicLab.GP.StructureIdentification { 29 public class ModelAnalyzerExporter : IFunctionVisitor { 30 private string prefix; 31 private string currentIndent = ""; 32 private IFunctionTree currentBranch; 33 public string ModelAnalyzerPrefix { 34 get { return prefix; } 35 } 36 public void Reset() { 37 prefix = ""; 38 } 39 40 private void VisitFunction(string name, IFunction f) { 41 prefix += currentIndent + "[F]" + name + "(\n"; 42 } 43 44 #region IFunctionVisitor Members 45 46 public void Visit(IFunction function) { 47 prefix += function.Name; 48 } 49 50 public void Visit(Addition addition) { 51 VisitFunction("Addition[0]", addition); 52 } 53 54 public void Visit(Constant constant) { 55 double value = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(Constant.VALUE).Value).Data; 56 prefix += currentIndent + "[T]Constant(" + value.ToString("r") + ";0;0)"; 57 } 58 59 public void Visit(Cosinus cosinus) { 60 VisitFunction("Trigonometrics[1]", cosinus); 61 } 62 63 public void Visit(Differential differential) { 64 double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(Differential.WEIGHT).Value).Data; 65 double index = ((ConstrainedIntData)currentBranch.GetLocalVariable(Differential.INDEX).Value).Data; 66 double offset = ((ConstrainedIntData)currentBranch.GetLocalVariable(Differential.OFFSET).Value).Data; 67 68 prefix += currentIndent + "[T]Differential(" + weight.ToString("r") + ";" + index + ";" + -offset + ")"; 69 } 70 71 public void Visit(Division division) { 72 VisitFunction("Division[0]", division); 73 } 74 75 public void Visit(Exponential exponential) { 76 VisitFunction("Exponential[0]", exponential); 77 } 78 79 public void Visit(Logarithm logarithm) { 80 VisitFunction("Logarithm[0]", logarithm); 81 } 82 83 public void Visit(Multiplication multiplication) { 84 VisitFunction("Multiplication[0]", multiplication); 85 } 86 87 public void Visit(Power power) { 88 VisitFunction("Power[0]", power); 89 } 90 91 public void Visit(Signum signum) { 92 VisitFunction("Signum[0]", signum); 93 } 94 95 public void Visit(Sinus sinus) { 96 VisitFunction("Trigonometrics[0]", sinus); 97 } 98 99 public void Visit(Sqrt sqrt) { 100 VisitFunction("Sqrt[0]", sqrt); 101 } 102 103 public void Visit(Subtraction substraction) { 104 VisitFunction("Subtraction[0]", substraction); 105 } 106 107 public void Visit(Tangens tangens) { 108 VisitFunction("Trigonometrics[2]", tangens); 109 } 110 111 public void Visit(Variable variable) { 112 double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(Variable.WEIGHT).Value).Data; 113 double index = ((ConstrainedIntData)currentBranch.GetLocalVariable(Variable.INDEX).Value).Data; 114 double offset = ((ConstrainedIntData)currentBranch.GetLocalVariable(Variable.OFFSET).Value).Data; 115 116 prefix += currentIndent + "[T]Variable(" + weight.ToString("r") + ";" + index + ";" + -offset + ")"; 117 } 118 119 public void Visit(And and) { 120 VisitFunction("Logical[0]", and); 121 } 122 123 public void Visit(Average average) { 124 VisitFunction("N/A (average)", average); 125 } 126 127 public void Visit(IfThenElse ifThenElse) { 128 VisitFunction("Conditional[0]", ifThenElse); 129 } 130 131 public void Visit(Not not) { 132 VisitFunction("Logical[2]", not); 133 } 134 135 public void Visit(Or or) { 136 VisitFunction("Logical[1]", or); 137 } 138 139 public void Visit(Xor xor) { 140 VisitFunction("N/A (xor)", xor); 141 } 142 143 public void Visit(Equal equal) { 144 VisitFunction("Boolean[2]", equal); 145 } 146 147 public void Visit(LessThan lessThan) { 148 VisitFunction("Boolean[0]", lessThan); 149 } 150 151 public void Visit(GreaterThan greaterThan) { 152 VisitFunction("Boolean[4]", greaterThan); 153 } 29 public class ModelAnalyzerExporter : IFunctionTreeExporter, IFunctionTreeNameGenerator { 30 #region IFunctionTreeExporter Members 31 32 public string Name { 33 get { 34 return "HL2 ModelAnalyzer Exporter"; 35 } 36 } 37 38 public bool TryExport(IFunctionTree tree, out string exported) { 39 try { 40 exported = Export(tree); 41 return true; 42 } catch(UnknownFunctionException) { 43 exported = ""; 44 return false; 45 } 46 } 47 48 public string Export(IFunctionTree tree) { 49 string result = ExportFunction(tree.Function, tree); 50 result += "(\n"; 51 foreach(IFunctionTree subTree in tree.SubTrees) { 52 result += Export(subTree); 53 result += ";\n"; 54 } 55 result = result.TrimEnd(';', '\n'); 56 if(tree.SubTrees.Count > 0) result += ")"; 57 return result; 58 } 59 154 60 #endregion 155 61 156 public void Visit(IFunctionTree functionTree) { 157 currentBranch = functionTree; 158 functionTree.Function.Accept(this); 159 currentIndent += " "; 160 foreach(IFunctionTree subTree in functionTree.SubTrees) { 161 Visit(subTree); 162 prefix += ";\n"; 163 } 164 prefix = prefix.TrimEnd(';', '\n'); 165 if(functionTree.SubTrees.Count>0) prefix += ")"; 166 currentIndent = currentIndent.Remove(0, 2); 62 private string ExportFunction(IFunction function, IFunctionTree tree) { 63 // this is smelly, if there is a cleaner way to have a 'dynamic' visitor 64 // please let me know! (gkronber 14.10.2008) 65 if(function is Addition) return ((Addition)function).ExportToHL2(tree); 66 if(function is And) return ((And)function).ExportToHL2(tree); 67 if(function is Average) return ((Average)function).ExportToHL2(tree); 68 if(function is Constant) return ((Constant)function).ExportToHL2(tree); 69 if(function is Cosinus) return ((Cosinus)function).ExportToHL2(tree); 70 if(function is Differential) return ((Differential)function).ExportToHL2(tree); 71 if(function is Division) return ((Division)function).ExportToHL2(tree); 72 if(function is Equal) return ((Equal)function).ExportToHL2(tree); 73 if(function is Exponential) return ((Exponential)function).ExportToHL2(tree); 74 if(function is GreaterThan) return ((GreaterThan)function).ExportToHL2(tree); 75 if(function is IfThenElse) return ((IfThenElse)function).ExportToHL2(tree); 76 if(function is LessThan) return ((LessThan)function).ExportToHL2(tree); 77 if(function is Logarithm) return ((Logarithm)function).ExportToHL2(tree); 78 if(function is Multiplication) return ((Multiplication)function).ExportToHL2(tree); 79 if(function is Not) return ((Not)function).ExportToHL2(tree); 80 if(function is Or) return ((Or)function).ExportToHL2(tree); 81 if(function is Power) return ((Power)function).ExportToHL2(tree); 82 if(function is Signum) return ((Signum)function).ExportToHL2(tree); 83 if(function is Sinus) return ((Sinus)function).ExportToHL2(tree); 84 if(function is Sqrt) return ((Sqrt)function).ExportToHL2(tree); 85 if(function is Subtraction) return ((Subtraction)function).ExportToHL2(tree); 86 if(function is Tangens) return ((Tangens)function).ExportToHL2(tree); 87 if(function is Variable) return ((Variable)function).ExportToHL2(tree); 88 if(function is Xor) return ((Xor)function).ExportToHL2(tree); 89 throw new UnknownFunctionException(function.Name); 90 } 91 92 #region IFunctionTreeNameGenerator Members 93 94 string IFunctionTreeNameGenerator.Name { 95 get { return "HL2 Representation"; } 96 } 97 98 public string GetName(IFunctionTree tree) { 99 string name = ""; 100 try { 101 name = ExportFunction(tree.Function, tree); 102 } catch(UnknownFunctionException) { 103 name = "N/A"; 104 } 105 return name; 106 } 107 108 #endregion 109 } 110 111 internal static class HL2ExporterExtensions { 112 private static string GetHL2FunctionName(string name) { 113 return "[F]" + name ; 114 } 115 116 public static string ExportToHL2(this Addition addition, IFunctionTree tree) { 117 return GetHL2FunctionName("Addition[0]"); 118 } 119 120 public static string ExportToHL2(this Constant constant, IFunctionTree tree) { 121 double value = ((ConstrainedDoubleData)tree.GetLocalVariable(Constant.VALUE).Value).Data; 122 return "[T]Constant(" + value.ToString("r") + ";0;0)"; 123 } 124 125 public static string ExportToHL2(this Cosinus cosinus, IFunctionTree tree) { 126 return GetHL2FunctionName("Trigonometrics[1]"); 127 } 128 129 public static string ExportToHL2(this Differential differential, IFunctionTree tree) { 130 double weight = ((ConstrainedDoubleData)tree.GetLocalVariable(Differential.WEIGHT).Value).Data; 131 double index = ((ConstrainedIntData)tree.GetLocalVariable(Differential.INDEX).Value).Data; 132 double offset = ((ConstrainedIntData)tree.GetLocalVariable(Differential.OFFSET).Value).Data; 133 134 return "[T]Differential(" + weight.ToString("r") + ";" + index + ";" + -offset + ")"; 135 } 136 137 public static string ExportToHL2(this Division division, IFunctionTree tree) { 138 return GetHL2FunctionName("Division[0]"); 139 } 140 141 public static string ExportToHL2(this Exponential exponential, IFunctionTree tree) { 142 return GetHL2FunctionName("Exponential[0]"); 143 } 144 145 public static string ExportToHL2(this Logarithm logarithm, IFunctionTree tree) { 146 return GetHL2FunctionName("Logarithm[0]"); 147 } 148 149 public static string ExportToHL2(this Multiplication multiplication, IFunctionTree tree) { 150 return GetHL2FunctionName("Multiplication[0]"); 151 } 152 153 public static string ExportToHL2(this Power power, IFunctionTree tree) { 154 return GetHL2FunctionName("Power[0]"); 155 } 156 157 public static string ExportToHL2(this Signum signum, IFunctionTree tree) { 158 return GetHL2FunctionName("Signum[0]"); 159 } 160 161 public static string ExportToHL2(this Sinus sinus, IFunctionTree tree) { 162 return GetHL2FunctionName("Trigonometrics[0]"); 163 } 164 165 public static string ExportToHL2(this Sqrt sqrt, IFunctionTree tree) { 166 return GetHL2FunctionName("Sqrt[0]"); 167 } 168 169 public static string ExportToHL2(this Subtraction substraction, IFunctionTree tree) { 170 return GetHL2FunctionName("Subtraction[0]"); 171 } 172 173 public static string ExportToHL2(this Tangens tangens, IFunctionTree tree) { 174 return GetHL2FunctionName("Trigonometrics[2]"); 175 } 176 177 public static string ExportToHL2(this Variable variable, IFunctionTree tree) { 178 double weight = ((ConstrainedDoubleData)tree.GetLocalVariable(Variable.WEIGHT).Value).Data; 179 double index = ((ConstrainedIntData)tree.GetLocalVariable(Variable.INDEX).Value).Data; 180 double offset = ((ConstrainedIntData)tree.GetLocalVariable(Variable.OFFSET).Value).Data; 181 182 return "[T]Variable(" + weight.ToString("r") + ";" + index + ";" + -offset + ")"; 183 } 184 185 public static string ExportToHL2(this And and, IFunctionTree tree) { 186 return GetHL2FunctionName("Logical[0]"); 187 } 188 189 public static string ExportToHL2(this Average average, IFunctionTree tree) { 190 return GetHL2FunctionName("Average[0]"); 191 } 192 193 public static string ExportToHL2(this IfThenElse ifThenElse, IFunctionTree tree) { 194 return GetHL2FunctionName("Conditional[0]"); 195 } 196 197 public static string ExportToHL2(this Not not, IFunctionTree tree) { 198 return GetHL2FunctionName("Logical[2]"); 199 } 200 201 public static string ExportToHL2(this Or or, IFunctionTree tree) { 202 return GetHL2FunctionName("Logical[1]"); 203 } 204 205 public static string ExportToHL2(this Xor xor, IFunctionTree tree) { 206 return GetHL2FunctionName("Logical[3]"); 207 } 208 209 public static string ExportToHL2(this Equal equal, IFunctionTree tree) { 210 return GetHL2FunctionName("Boolean[2]"); 211 } 212 213 public static string ExportToHL2(this LessThan lessThan, IFunctionTree tree) { 214 return GetHL2FunctionName("Boolean[0]"); 215 } 216 217 public static string ExportToHL2(this GreaterThan greaterThan, IFunctionTree tree) { 218 return GetHL2FunctionName("Boolean[4]"); 167 219 } 168 220 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Multiplication.cs
r645 r654 48 48 AddConstraint(new SubOperatorTypeConstraint(2)); 49 49 } 50 51 public override void Accept(IFunctionVisitor visitor) {52 visitor.Visit(this);53 }54 50 } 55 51 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Not.cs
r645 r654 40 40 AddConstraint(new SubOperatorTypeConstraint(0)); 41 41 } 42 43 public override void Accept(IFunctionVisitor visitor) {44 visitor.Visit(this);45 }46 42 } 47 43 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Or.cs
r645 r654 43 43 AddConstraint(new SubOperatorTypeConstraint(2)); 44 44 } 45 46 public override void Accept(IFunctionVisitor visitor) {47 visitor.Visit(this);48 }49 45 } 50 46 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Power.cs
r645 r654 41 41 AddConstraint(new SubOperatorTypeConstraint(1)); 42 42 } 43 44 public override void Accept(IFunctionVisitor visitor) {45 visitor.Visit(this);46 }47 43 } 48 44 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Signum.cs
r645 r654 40 40 AddConstraint(new SubOperatorTypeConstraint(0)); 41 41 } 42 43 public override void Accept(IFunctionVisitor visitor) {44 visitor.Visit(this);45 }46 42 } 47 43 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Sinus.cs
r645 r654 40 40 AddConstraint(new SubOperatorTypeConstraint(0)); 41 41 } 42 43 public override void Accept(IFunctionVisitor visitor) {44 visitor.Visit(this);45 }46 42 } 47 43 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Sqrt.cs
r645 r654 40 40 AddConstraint(new SubOperatorTypeConstraint(0)); 41 41 } 42 43 public override void Accept(IFunctionVisitor visitor) {44 visitor.Visit(this);45 }46 42 } 47 43 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Subtraction.cs
r645 r654 48 48 AddConstraint(new SubOperatorTypeConstraint(2)); 49 49 } 50 51 public override void Accept(IFunctionVisitor visitor) {52 visitor.Visit(this);53 }54 50 } 55 51 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/SymbolicExpressionExporter.cs
r645 r654 27 27 28 28 namespace HeuristicLab.GP.StructureIdentification { 29 public class SymbolicExpressionExporter : IFunctionVisitor { 30 private IFunctionTree currentBranch; 29 public class SymbolicExpressionExporter : IFunctionTreeExporter, IFunctionTreeNameGenerator { 31 30 private StringBuilder builder; 32 31 private string currentIndent; 33 public SymbolicExpressionExporter() { 34 Reset(); 35 } 36 37 public void Reset() { 32 33 #region IFunctionTreeExporter Members 34 35 public string Name { 36 get { return "Symbolic Expression Exporter"; } 37 } 38 39 40 public string Export(IFunctionTree tree) { 41 builder = new StringBuilder(); 38 42 currentIndent = ""; 39 builder = new StringBuilder(); 40 } 41 42 public string GetStringRepresentation() { 43 BuildExportString(tree); 43 44 return builder.ToString(); 44 45 } 45 46 46 private void VisitFunction(string name, IFunction f) { 47 public bool TryExport(IFunctionTree tree, out string exported) { 48 try { 49 exported = Export(tree); 50 return true; 51 } catch(UnknownFunctionException) { 52 exported = ""; 53 return false; 54 } 55 } 56 57 private void BuildExportString(IFunctionTree tree) { 47 58 builder.Append(currentIndent); 48 builder.Append("(" + name + " "); 49 } 50 51 #region IFunctionVisitor Members 52 53 public void Visit(IFunction function) { 54 builder.Append(function.Name); 55 } 56 57 public void Visit(Addition addition) { 58 VisitFunction("+", addition); 59 } 60 61 public void Visit(Constant constant) { 62 builder.Append(currentIndent+currentBranch.GetLocalVariable(Constant.VALUE).Value); 63 } 64 65 public void Visit(Cosinus cosinus) { 66 VisitFunction("cos", cosinus); 67 } 68 69 public void Visit(Division division) { 70 VisitFunction("/", division); 71 } 72 73 public void Visit(Exponential exponential) { 74 VisitFunction("exp", exponential); 75 } 76 77 public void Visit(Logarithm logarithm) { 78 VisitFunction("log", logarithm); 79 } 80 81 public void Visit(Multiplication multiplication) { 82 VisitFunction("*", multiplication); 83 } 84 85 public void Visit(Power power) { 86 VisitFunction("expt", power); 87 } 88 89 public void Visit(Signum signum) { 90 VisitFunction("sign", signum); 91 } 92 93 public void Visit(Sinus sinus) { 94 VisitFunction("sin", sinus); 95 } 96 97 public void Visit(Sqrt sqrt) { 98 VisitFunction("sqrt", sqrt); 99 } 100 101 public void Visit(Subtraction subtraction) { 102 VisitFunction("-", subtraction); 103 } 104 105 public void Visit(Tangens tangens) { 106 VisitFunction("tan", tangens); 107 } 108 109 public void Visit(Variable variable) { 110 builder.Append(currentIndent + "(variable " + currentBranch.GetLocalVariable(Variable.WEIGHT).Value + " " + 111 currentBranch.GetLocalVariable(Variable.INDEX).Value + " " + currentBranch.GetLocalVariable(Variable.OFFSET).Value + ")"); 112 } 113 public void Visit(Differential differential) { 114 builder.Append(currentIndent+"(differential " + currentBranch.GetLocalVariable(Differential.WEIGHT).Value + " "+ 115 currentBranch.GetLocalVariable(Differential.INDEX).Value+ " " + currentBranch.GetLocalVariable(Differential.OFFSET).Value+")"); 116 } 117 118 public void Visit(And and) { 119 VisitFunction("and", and); 120 } 121 122 public void Visit(Average average) { 123 VisitFunction("mean", average); 124 } 125 126 public void Visit(IfThenElse ifThenElse) { 127 VisitFunction("if", ifThenElse); 128 } 129 130 public void Visit(Not not) { 131 VisitFunction("not", not); 132 } 133 134 public void Visit(Or or) { 135 VisitFunction("or", or); 136 } 137 138 public void Visit(Xor xor) { 139 VisitFunction("xor", xor); 140 } 141 142 public void Visit(Equal equal) { 143 VisitFunction("equ", equal); 144 } 145 146 public void Visit(LessThan lessThan) { 147 VisitFunction("<", lessThan); 148 } 149 150 public void Visit(GreaterThan greaterThan) { 151 VisitFunction(">", greaterThan); 152 } 59 builder.Append("(" + ExportFunction(tree.Function, tree) + " "); 60 currentIndent += " "; 61 foreach(IFunctionTree subTree in tree.SubTrees) { 62 builder.Append("\n"); 63 BuildExportString(subTree); 64 } 65 if(tree.SubTrees.Count > 0) builder.Append(")"); 66 currentIndent = currentIndent.Remove(0, 2); 67 } 68 69 private string ExportFunction(IFunction function, IFunctionTree tree) { 70 // this is smelly, if there is a cleaner way to have a 'dynamic' visitor 71 // please let me know! (gkronber 14.10.2008) 72 if(function is Addition) return ((Addition)function).ExportToScheme(); 73 if(function is And) return ((And)function).ExportToScheme(); 74 if(function is Average) return ((Average)function).ExportToScheme(); 75 if(function is Constant) return ((Constant)function).ExportToScheme(tree); 76 if(function is Cosinus) return ((Cosinus)function).ExportToScheme(); 77 if(function is Differential) return ((Differential)function).ExportToScheme(tree); 78 if(function is Division) return ((Division)function).ExportToScheme(); 79 if(function is Equal) return ((Equal)function).ExportToScheme(); 80 if(function is Exponential) return ((Exponential)function).ExportToScheme(); 81 if(function is GreaterThan) return ((GreaterThan)function).ExportToScheme(); 82 if(function is IfThenElse) return ((IfThenElse)function).ExportToScheme(); 83 if(function is LessThan) return ((LessThan)function).ExportToScheme(); 84 if(function is Logarithm) return ((Logarithm)function).ExportToScheme(); 85 if(function is Multiplication) return ((Multiplication)function).ExportToScheme(); 86 if(function is Not) return ((Not)function).ExportToScheme(); 87 if(function is Or) return ((Or)function).ExportToScheme(); 88 if(function is Power) return ((Power)function).ExportToScheme(); 89 if(function is Signum) return ((Signum)function).ExportToScheme(); 90 if(function is Sinus) return ((Sinus)function).ExportToScheme(); 91 if(function is Sqrt) return ((Sqrt)function).ExportToScheme(); 92 if(function is Subtraction) return ((Subtraction)function).ExportToScheme(); 93 if(function is Tangens) return ((Tangens)function).ExportToScheme(); 94 if(function is Variable) return ((Variable)function).ExportToScheme(tree); 95 if(function is Xor) return ((Xor)function).ExportToScheme(); 96 throw new UnknownFunctionException(function.Name); 97 } 98 99 153 100 #endregion 154 101 155 public void Visit(IFunctionTree functionTree) { 156 currentBranch = functionTree; 157 functionTree.Function.Accept(this); 158 currentIndent += " "; 159 160 foreach(IFunctionTree subTree in functionTree.SubTrees) { 161 builder.Append("\n"); 162 Visit(subTree); 102 #region IFunctionTreeNameGenerator Members 103 104 string IFunctionTreeNameGenerator.Name { 105 get { return "Symbolic Expression"; } 106 } 107 108 public string GetName(IFunctionTree tree) { 109 string name = ""; 110 try { 111 name = ExportFunction(tree.Function, tree); 112 } catch(UnknownFunctionException) { 113 name = "N/A"; 163 114 } 164 if(functionTree.SubTrees.Count > 0) builder.Append(")"); 165 currentIndent = currentIndent.Remove(0, 2); 115 return name; 116 } 117 118 #endregion 119 } 120 121 internal static class SchemeExporterExtensions { 122 public static string ExportToScheme(this Addition addition) { 123 return "+"; 124 } 125 126 public static string ExportToScheme(this Constant constant, IFunctionTree tree) { 127 return tree.GetLocalVariable(Constant.VALUE).Value.ToString(); 128 } 129 130 public static string ExportToScheme(this Cosinus cosinus) { 131 return "cos"; 132 } 133 134 public static string ExportToScheme(this Division division) { 135 return "/"; 136 } 137 138 public static string ExportToScheme(this Exponential exponential) { 139 return "exp"; 140 } 141 142 public static string ExportToScheme(this Logarithm logarithm) { 143 return "log"; 144 } 145 146 public static string ExportToScheme(this Multiplication multiplication) { 147 return "*"; 148 } 149 150 public static string ExportToScheme(this Power power) { 151 return "expt"; 152 } 153 154 public static string ExportToScheme(this Signum signum) { 155 return "sign"; 156 } 157 158 public static string ExportToScheme(this Sinus sinus) { 159 return "sin"; 160 } 161 162 public static string ExportToScheme(this Sqrt sqrt) { 163 return "sqrt"; 164 } 165 166 public static string ExportToScheme(this Subtraction subtraction) { 167 return "-"; 168 } 169 170 public static string ExportToScheme(this Tangens tangens) { 171 return "tan"; 172 } 173 174 public static string ExportToScheme(this Variable variable, IFunctionTree tree) { 175 return "(variable " + tree.GetLocalVariable(Variable.WEIGHT).Value + " " + 176 tree.GetLocalVariable(Variable.INDEX).Value + " " + tree.GetLocalVariable(Variable.OFFSET).Value + ")"; 177 } 178 public static string ExportToScheme(this Differential differential, IFunctionTree tree) { 179 return "(differential " + tree.GetLocalVariable(Differential.WEIGHT).Value + " " + 180 tree.GetLocalVariable(Differential.INDEX).Value + " " + tree.GetLocalVariable(Differential.OFFSET).Value + ")"; 181 } 182 183 public static string ExportToScheme(this And and) { 184 return "and"; 185 } 186 187 public static string ExportToScheme(this Average average) { 188 return "mean"; 189 } 190 191 public static string ExportToScheme(this IfThenElse ifThenElse) { 192 return "if"; 193 } 194 195 public static string ExportToScheme(this Not not) { 196 return "not"; 197 } 198 199 public static string ExportToScheme(this Or or) { 200 return "or"; 201 } 202 203 public static string ExportToScheme(this Xor xor) { 204 return "xor"; 205 } 206 207 public static string ExportToScheme(this Equal equal) { 208 return "equ"; 209 } 210 211 public static string ExportToScheme(this LessThan lessThan) { 212 return "<"; 213 } 214 215 public static string ExportToScheme(this GreaterThan greaterThan) { 216 return ">"; 166 217 } 167 218 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Tangens.cs
r645 r654 40 40 AddConstraint(new SubOperatorTypeConstraint(0)); 41 41 } 42 43 public override void Accept(IFunctionVisitor visitor) {44 visitor.Visit(this);45 }46 42 } 47 43 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Variable.cs
r645 r654 207 207 SetupManipulation(); 208 208 } 209 210 public override void Accept(IFunctionVisitor visitor) {211 visitor.Visit(this);212 }213 209 } 214 210 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification/Xor.cs
r645 r654 41 41 AddConstraint(new SubOperatorTypeConstraint(1)); 42 42 } 43 44 public override void Accept(IFunctionVisitor visitor) {45 visitor.Visit(this);46 }47 43 } 48 44 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP/FunctionBase.cs
r645 r654 43 43 public virtual double Apply() { 44 44 throw new NotImplementedException(); 45 }46 47 public virtual void Accept(IFunctionVisitor visitor) {48 visitor.Visit(this);49 45 } 50 46 -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP/FunctionView.Designer.cs
r645 r654 51 51 this.components = new System.ComponentModel.Container(); 52 52 this.funTreeView = new System.Windows.Forms.TreeView(); 53 this.representationContextMenu = new System.Windows.Forms.ContextMenuStrip(this.components); 53 54 this.splitContainer = new System.Windows.Forms.SplitContainer(); 54 55 this.variablesGroupBox = new System.Windows.Forms.GroupBox(); … … 59 60 this.editButton = new System.Windows.Forms.Button(); 60 61 this.treeNodeContextMenu = new System.Windows.Forms.ContextMenuStrip(this.components); 61 this.copyToClipboardMenuItem = new System.Windows.Forms.ToolStripMenuItem();62 this.copyToClipboardSExpressionToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();63 62 this.splitContainer.Panel1.SuspendLayout(); 64 63 this.splitContainer.Panel2.SuspendLayout(); … … 67 66 this.variablesSplitContainer.Panel1.SuspendLayout(); 68 67 this.variablesSplitContainer.SuspendLayout(); 69 this.treeNodeContextMenu.SuspendLayout();70 68 this.SuspendLayout(); 71 69 // 72 70 // funTreeView 73 71 // 72 this.funTreeView.ContextMenuStrip = this.representationContextMenu; 74 73 this.funTreeView.Dock = System.Windows.Forms.DockStyle.Fill; 75 74 this.funTreeView.HideSelection = false; … … 80 79 this.funTreeView.MouseUp += new System.Windows.Forms.MouseEventHandler(this.funTreeView_MouseUp); 81 80 this.funTreeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.functionTreeView_AfterSelect); 81 // 82 // representationContextMenu 83 // 84 this.representationContextMenu.Name = "representationContextMenu"; 85 this.representationContextMenu.Size = new System.Drawing.Size(61, 4); 82 86 // 83 87 // splitContainer … … 167 171 // treeNodeContextMenu 168 172 // 169 this.treeNodeContextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {170 this.copyToClipboardMenuItem,171 this.copyToClipboardSExpressionToolStripMenuItem});172 173 this.treeNodeContextMenu.Name = "treeNodeContextMenu"; 173 this.treeNodeContextMenu.Size = new System.Drawing.Size(259, 70); 174 // 175 // copyToClipboardMenuItem 176 // 177 this.copyToClipboardMenuItem.Name = "copyToClipboardMenuItem"; 178 this.copyToClipboardMenuItem.Size = new System.Drawing.Size(258, 22); 179 this.copyToClipboardMenuItem.Text = "Copy to clip-board (Model-Analyzer)"; 180 this.copyToClipboardMenuItem.Click += new System.EventHandler(this.copyToClipboardMenuItem_Click); 181 // 182 // copyToClipboardSExpressionToolStripMenuItem 183 // 184 this.copyToClipboardSExpressionToolStripMenuItem.Name = "copyToClipboardSExpressionToolStripMenuItem"; 185 this.copyToClipboardSExpressionToolStripMenuItem.Size = new System.Drawing.Size(258, 22); 186 this.copyToClipboardSExpressionToolStripMenuItem.Text = "Copy to clip-board (S-Expression)"; 187 this.copyToClipboardSExpressionToolStripMenuItem.Click += new System.EventHandler(this.copyToClipboardSExpressionToolStripMenuItem_Click); 174 this.treeNodeContextMenu.Size = new System.Drawing.Size(61, 4); 188 175 // 189 176 // FunctionTreeView … … 201 188 this.variablesSplitContainer.Panel1.ResumeLayout(false); 202 189 this.variablesSplitContainer.ResumeLayout(false); 203 this.treeNodeContextMenu.ResumeLayout(false);204 190 this.ResumeLayout(false); 205 191 … … 217 203 private System.Windows.Forms.Label label1; 218 204 private System.Windows.Forms.ContextMenuStrip treeNodeContextMenu; 219 private System.Windows.Forms.ToolStripMenuItem copyToClipboardMenuItem; 220 private System.Windows.Forms.ToolStripMenuItem copyToClipboardSExpressionToolStripMenuItem; 205 private System.Windows.Forms.ContextMenuStrip representationContextMenu; 221 206 222 207 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP/FunctionView.cs
r645 r654 38 38 private IFunctionTree selectedBranch; 39 39 private IVariable selectedVariable; 40 private IFunctionTreeNameGenerator nameGenerator; 41 private IFunctionTreeNameGenerator[] allNameGenerators; 40 42 41 // private FunctionNameVisitor functionNameVisitor;42 43 public FunctionTreeView() { 44 nameGenerator = new DefaultFunctionTreeNameGenerator(); 43 45 InitializeComponent(); 44 // functionNameVisitor = new FunctionNameVisitor(); 46 47 DiscoveryService discoveryService = new DiscoveryService(); 48 allNameGenerators = discoveryService.GetInstances<IFunctionTreeNameGenerator>(); 49 50 foreach(IFunctionTreeNameGenerator generator in allNameGenerators) { 51 ToolStripButton button = new ToolStripButton(generator.Name, null, 52 delegate(object source, EventArgs args) { 53 this.nameGenerator = generator; 54 foreach(ToolStripButton otherButton in representationContextMenu.Items) otherButton.Checked = false; 55 ((ToolStripButton)source).Checked = true; 56 UpdateControls(); 57 }); 58 if(generator is DefaultFunctionTreeNameGenerator) button.Checked = true; 59 else button.Checked = false; 60 representationContextMenu.Items.Add(button); 61 } 45 62 } 46 63 … … 53 70 protected override void UpdateControls() { 54 71 funTreeView.Nodes.Clear(); 55 //functionNameVisitor.Visit(functionTree);56 72 TreeNode rootNode = new TreeNode(); 57 73 rootNode.Name = functionTree.Function.Name; 58 //rootNode.Text = functionNameVisitor.Name;74 rootNode.Text = nameGenerator.GetName(functionTree); 59 75 rootNode.Tag = functionTree; 76 treeNodeContextMenu.Items.Clear(); 77 DiscoveryService discoveryService = new DiscoveryService(); 78 IFunctionTreeExporter[] exporters = discoveryService.GetInstances<IFunctionTreeExporter>(); 79 if(exporters.Length>0) 80 treeNodeContextMenu.Items.Add(new ToolStripSeparator()); 81 foreach(IFunctionTreeExporter exporter in exporters) { 82 string result; 83 // register a menu item for the exporter 84 ToolStripItem item = new ToolStripButton("Copy to clip-board (" + exporter.Name + ")", null, 85 delegate(object source, EventArgs args) { 86 TreeNode node = funTreeView.SelectedNode; 87 if(node == null || node.Tag == null) return; 88 Clipboard.SetText(exporter.Export((IFunctionTree)node.Tag)); 89 }); 90 // try to export the whole tree 91 if(exporter.TryExport(functionTree, out result)) { 92 // if it worked enable the context-menu item 93 item.Enabled = true; 94 } else { 95 item.Enabled = false; 96 } 97 treeNodeContextMenu.Items.Add(item); 98 } 60 99 rootNode.ContextMenuStrip = treeNodeContextMenu; 61 100 funTreeView.Nodes.Add(rootNode); … … 69 108 private void CreateTree(TreeNode rootNode, IFunctionTree functionTree) { 70 109 TreeNode node = new TreeNode(); 71 // functionNameVisitor.Visit(functionTree);72 110 node.Name = functionTree.Function.Name; 73 // node.Text = functionNameVisitor.Name;111 node.Text = nameGenerator.GetName(functionTree); 74 112 node.Tag = functionTree; 75 113 node.ContextMenuStrip = treeNodeContextMenu; … … 88 126 IFunctionTree selectedBranch = (IFunctionTree)funTreeView.SelectedNode.Tag; 89 127 UpdateVariablesList(selectedBranch); 90 templateTextBox.Text = selectedBranch.Function.Name;128 templateTextBox.Text = nameGenerator.GetName(selectedBranch); 91 129 this.selectedBranch = selectedBranch; 92 130 editButton.Enabled = true; … … 122 160 if(funTreeView.SelectedNode != null && funTreeView.SelectedNode.Tag != null) { 123 161 TreeNode node = funTreeView.SelectedNode; 124 // functionNameVisitor.Visit(selectedBranch); 125 // node.Text = functionNameVisitor.Name; 162 node.Text = nameGenerator.GetName(functionTree); 126 163 } 127 164 } … … 129 166 protected virtual void editButton_Click(object sender, EventArgs e) { 130 167 PluginManager.ControlManager.ShowControl(selectedBranch.Function.CreateView()); 131 }132 133 private void copyToClipboardMenuItem_Click(object sender, EventArgs e) {134 TreeNode node = funTreeView.SelectedNode;135 if(node == null || node.Tag == null) return;136 137 //ModelAnalyzerExporter visitor = new ModelAnalyzerExporter();138 //visitor.Visit((IFunctionTree)node.Tag);139 //Clipboard.SetText(visitor.ModelAnalyzerPrefix);140 }141 142 private void copyToClipboardSExpressionToolStripMenuItem_Click(object sender, EventArgs e) {143 TreeNode node = funTreeView.SelectedNode;144 if(node == null || node.Tag == null) return;145 146 //string sexp = ((IFunctionTree)node.Tag).ToString();147 //Clipboard.SetText(sexp);148 168 } 149 169 … … 154 174 } 155 175 } 156 157 //private class FunctionNameVisitor : IFunctionVisitor {158 // string name;159 // IFunctionTree currentBranch;160 161 // public string Name {162 // get { return name; }163 // }164 165 // public void Visit(IFunctionTree tree) {166 // currentBranch = tree;167 // tree.Function.Accept(this);168 // }169 170 // #region IFunctionVisitor Members171 // public void Visit(IFunction function) {172 // name = function.Name;173 // }174 175 // public void Visit(Addition addition) {176 // name = "+";177 // }178 179 // public void Visit(Constant constant) {180 // name = ((ConstrainedDoubleData)(currentBranch.GetLocalVariable(HeuristicLab.Functions.Constant.VALUE).Value)).Data + "";181 // }182 183 // public void Visit(Cosinus cosinus) {184 // name = "Sin";185 // }186 187 // public void Visit(Differential diff) {188 // string timeOffset = "";189 // int sampleOffset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.OFFSET).Value).Data;190 // int variableIndex = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.INDEX).Value).Data;191 // double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.WEIGHT).Value).Data;192 // if(sampleOffset < 0) {193 // timeOffset = "(t" + sampleOffset + ")";194 // } else if(sampleOffset > 0) {195 // timeOffset = "(t+" + sampleOffset + ")";196 // } else {197 // timeOffset = "";198 // }199 // name = "Diff" + variableIndex + timeOffset + " * " + weight;200 // }201 202 // public void Visit(Division division) {203 // name = "/";204 // }205 206 // public void Visit(Exponential exponential) {207 // name = "Exp";208 // }209 210 // public void Visit(Logarithm logarithm) {211 // name = "Log";212 // }213 214 // public void Visit(Multiplication multiplication) {215 // name = "*";216 // }217 218 // public void Visit(Power power) {219 // name = "Pow";220 // }221 222 // public void Visit(Signum signum) {223 // name = "Sign";224 // }225 226 // public void Visit(Sinus sinus) {227 // name = "Sin";228 // }229 230 // public void Visit(Sqrt sqrt) {231 // name = "Sqrt";232 // }233 234 // public void Visit(Subtraction substraction) {235 // name = "-";236 // }237 238 // public void Visit(Tangens tangens) {239 // name = "Tan";240 // }241 242 // public void Visit(Variable variable) {243 // string timeOffset = "";244 // int sampleOffset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.OFFSET).Value).Data;245 // int variableIndex = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.INDEX).Value).Data;246 // double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.WEIGHT).Value).Data;247 // if(sampleOffset < 0) {248 // timeOffset = "(t" + sampleOffset + ")";249 // } else if(sampleOffset > 0) {250 // timeOffset = "(t+" + sampleOffset + ")";251 // } else {252 // timeOffset = "";253 // }254 // name = "Var" + variableIndex + timeOffset + " * " + weight;255 // }256 257 // public void Visit(And and) {258 // name = "And";259 // }260 261 // public void Visit(Average average) {262 // name = "Avg";263 // }264 265 // public void Visit(IfThenElse ifThenElse) {266 // name = "IFTE";267 // }268 269 // public void Visit(Not not) {270 // name = "Not";271 // }272 273 // public void Visit(Or or) {274 // name = "Or";275 // }276 277 // public void Visit(Xor xor) {278 // name = "Xor";279 // }280 281 // public void Visit(Equal equal) {282 // name = "eq?";283 // }284 285 // public void Visit(LessThan lessThan) {286 // name = "<";287 // }288 289 // public void Visit(GreaterThan greaterThan) {290 // name = ">";291 // }292 // #endregion293 //}294 176 } 295 177 } -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP/FunctionView.resx
r645 r654 118 118 <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> 119 119 </resheader> 120 <metadata name="representationContextMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> 121 <value>180, 17</value> 122 </metadata> 120 123 <metadata name="treeNodeContextMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> 121 124 <value>17, 17</value> -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP/HeuristicLab.GP.csproj
r651 r654 67 67 <ItemGroup> 68 68 <Compile Include="BakedFunctionTree.cs" /> 69 <Compile Include="DefaultFunctionTreeNameGenerator.cs" /> 70 <Compile Include="UnknownFunctionException.cs" /> 71 <Compile Include="IFunctionTreeNameGenerator.cs" /> 72 <Compile Include="IFunctionTreeExporter.cs" /> 69 73 <Compile Include="FunctionBase.cs" /> 70 74 <Compile Include="FunctionView.cs"> … … 85 89 <Compile Include="IFunction.cs" /> 86 90 <Compile Include="IFunctionTree.cs" /> 87 <Compile Include="IFunctionVisitor.cs">88 <SubType>Code</SubType>89 </Compile>90 91 <Compile Include="Manipulation\ChangeNodeTypeManipulation.cs" /> 91 92 <Compile Include="Manipulation\CutOutNodeManipulation.cs" /> -
branches/GpPluginsRefactoringBranch/HeuristicLab.GP/IFunction.cs
r645 r654 29 29 IFunctionTree GetTreeNode(); 30 30 double Apply(); 31 void Accept(IFunctionVisitor visitor);32 31 IList<IFunction> AllowedSubFunctions(int index); 33 32 int MinArity { get; }
Note: See TracChangeset
for help on using the changeset viewer.