Changeset 2629


Ignore:
Timestamp:
01/15/10 17:58:53 (10 years ago)
Author:
gkronber
Message:

Fixed display of unknown functions and removed UnknownFunctionException. #845 (In the FunctionTreeView only the string N/A is displayed for unknown functions)

Location:
trunk/sources
Files:
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.Boolean/3.3/BooleanTreeInterpreter.cs

    r2328 r2629  
    7272        case SymbolTable.UNKNOWN:
    7373        default:
    74           throw new UnknownFunctionException(t.Function.Name);
     74          throw new NotImplementedException(t.Function.Name);
    7575      }
    7676    }
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/ModelAnalyzerExporter.cs

    r2235 r2629  
    2222using HeuristicLab.GP.Interfaces;
    2323using System;
     24using System.Collections.Generic;
    2425
    2526namespace HeuristicLab.GP.StructureIdentification {
     
    3435
    3536    public bool TryExport(IFunctionTree tree, out string exported) {
    36       try {
    37         exported = Export(tree);
    38         return true;
    39       } catch(UnknownFunctionException) {
    40         exported = "";
    41         return false;
    42       }
     37      foreach (IFunctionTree t in FunctionTreeIterator.IteratePrefix(tree))
     38        if (!functionExporter.ContainsKey(t.Function.GetType())) {
     39          exported = null;
     40          return false;
     41        }
     42      exported = Export(tree);
     43      return true;
    4344    }
    4445
    4546    public string Export(IFunctionTree tree) {
    4647      string result = ExportFunction(tree.Function, tree);
    47       if(tree.SubTrees.Count>0)
     48      if (tree.SubTrees.Count > 0)
    4849        result += "(\n";
    49       foreach(IFunctionTree subTree in tree.SubTrees) {
     50      foreach (IFunctionTree subTree in tree.SubTrees) {
    5051        result += Export(subTree);
    5152        result += ";\n";
    5253      }
    5354      result = result.TrimEnd(';', '\n');
    54       if(tree.SubTrees.Count > 0) result += ")";
     55      if (tree.SubTrees.Count > 0) result += ")";
    5556      return result;
    5657    }
    57 
    58     public IFunctionTree Import(string tree) {
    59       throw new UnknownFunctionException(tree);
    60     }
    61 
    62     public bool TryImport(string tree, out IFunctionTree importedTree) {
    63       try {
    64         importedTree = Import(tree);
    65         return true;
    66       }
    67       catch (UnknownFunctionException) {
    68         importedTree = null;
    69         return false;
    70       }
    71     }
    72 
    7358    #endregion
    7459
     60    // try-export checks the keys of this dictionary
     61    private static Dictionary<Type, Func<IFunction, IFunctionTree, string>> functionExporter = new Dictionary<Type, Func<IFunction, IFunctionTree, string>>() {
     62      { typeof(Addition), (function, tree) => ((Addition)function).ExportToHL2(tree)},
     63      { typeof(And), (function, tree) => ((And)function).ExportToHL2(tree)},
     64      { typeof(Average), (function, tree) => ((Average)function).ExportToHL2(tree)},
     65      { typeof(Constant), (function, tree) => ((Constant)function).ExportToHL2(tree)},
     66      { typeof(Cosinus), (function, tree) => ((Cosinus)function).ExportToHL2(tree)},
     67      { typeof(Differential), (function, tree) => ((Differential)function).ExportToHL2(tree)},
     68      { typeof(Division), (function, tree) => ((Division)function).ExportToHL2(tree)},
     69      { typeof(Equal), (function, tree) => ((Equal)function).ExportToHL2(tree)},
     70      { typeof(Exponential), (function, tree) => ((Exponential)function).ExportToHL2(tree)},
     71      { typeof(GreaterThan), (function, tree) => ((GreaterThan)function).ExportToHL2(tree)},
     72      { typeof(IfThenElse), (function, tree) => ((IfThenElse)function).ExportToHL2(tree)},
     73      { typeof(LessThan), (function, tree) => ((LessThan)function).ExportToHL2(tree)},
     74      { typeof(Logarithm), (function, tree) => ((Logarithm)function).ExportToHL2(tree)},
     75      { typeof(Multiplication), (function, tree) => ((Multiplication)function).ExportToHL2(tree)},
     76      { typeof(Not), (function, tree) => ((Not)function).ExportToHL2(tree)},
     77      { typeof(Or), (function, tree) => ((Or)function).ExportToHL2(tree)},
     78      { typeof(Power), (function, tree) => ((Power)function).ExportToHL2(tree)},
     79      { typeof(Signum), (function, tree) => ((Signum)function).ExportToHL2(tree)},
     80      { typeof(Sinus), (function, tree) => ((Sinus)function).ExportToHL2(tree)},
     81      { typeof(Sqrt), (function, tree) => ((Sqrt)function).ExportToHL2(tree)},
     82      { typeof(Subtraction), (function, tree) => ((Subtraction)function).ExportToHL2(tree)},
     83      { typeof(Tangens), (function, tree) => ((Tangens)function).ExportToHL2(tree)},
     84      { typeof(Variable), (function, tree) => ((Variable)function).ExportToHL2(tree)},
     85      { typeof(Xor), (function, tree) => ((Xor)function).ExportToHL2(tree)},
     86    };
    7587    private static string ExportFunction(IFunction function, IFunctionTree tree) {
    7688      // this is smelly, if there is a cleaner way to have a 'dynamic' visitor
    7789      // please let me know! (gkronber 14.10.2008)
    78       if(function is Addition) return ((Addition)function).ExportToHL2(tree);
    79       if(function is And) return ((And)function).ExportToHL2(tree);
    80       if(function is Average) return ((Average)function).ExportToHL2(tree);
    81       if(function is Constant) return ((Constant)function).ExportToHL2(tree);
    82       if(function is Cosinus) return ((Cosinus)function).ExportToHL2(tree);
    83       if(function is Differential) return ((Differential)function).ExportToHL2(tree);
    84       if(function is Division) return ((Division)function).ExportToHL2(tree);
    85       if(function is Equal) return ((Equal)function).ExportToHL2(tree);
    86       if(function is Exponential) return ((Exponential)function).ExportToHL2(tree);
    87       if(function is GreaterThan) return ((GreaterThan)function).ExportToHL2(tree);
    88       if(function is IfThenElse) return ((IfThenElse)function).ExportToHL2(tree);
    89       if(function is LessThan) return ((LessThan)function).ExportToHL2(tree);
    90       if(function is Logarithm) return ((Logarithm)function).ExportToHL2(tree);
    91       if(function is Multiplication) return ((Multiplication)function).ExportToHL2(tree);
    92       if(function is Not) return ((Not)function).ExportToHL2(tree);
    93       if(function is Or) return ((Or)function).ExportToHL2(tree);
    94       if(function is Power) return ((Power)function).ExportToHL2(tree);
    95       if(function is Signum) return ((Signum)function).ExportToHL2(tree);
    96       if(function is Sinus) return ((Sinus)function).ExportToHL2(tree);
    97       if(function is Sqrt) return ((Sqrt)function).ExportToHL2(tree);
    98       if(function is Subtraction) return ((Subtraction)function).ExportToHL2(tree);
    99       if(function is Tangens) return ((Tangens)function).ExportToHL2(tree);
    100       if(function is Variable) return ((Variable)function).ExportToHL2(tree);
    101       if(function is Xor) return ((Xor)function).ExportToHL2(tree);
    102       throw new UnknownFunctionException(function.Name);
     90      if (functionExporter.ContainsKey(function.GetType())) return functionExporter[function.GetType()](function, tree);
     91      else return function.Name;
    10392    }
    10493
    10594    public static string GetName(IFunctionTree tree) {
    106       string name = "";
    107       try {
    108         name = ExportFunction(tree.Function, tree);
    109       } catch(UnknownFunctionException) {
    110         name = "N/A";
    111       }
    112       return name;
     95      return ExportFunction(tree.Function, tree);
    11396    }
    11497  }
     
    11699  internal static class HL2ExporterExtensions {
    117100    private static string GetHL2FunctionName(string name) {
    118       return "[F]" + name ;
     101      return "[F]" + name;
    119102    }
    120103
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/SymbolicExpressionExporter.cs

    r2423 r2629  
    4646
    4747    public bool TryExport(IFunctionTree tree, out string exported) {
    48       try {
    49         exported = Export(tree);
    50         return true;
    51       }
    52       catch (UnknownFunctionException) {
    53         exported = "";
    54         return false;
    55       }
     48      foreach (IFunctionTree t in FunctionTreeIterator.IteratePrefix(tree))
     49        if (!functionExporter.ContainsKey(t.Function.GetType())) {
     50          exported = null;
     51          return false;
     52        }
     53      exported = Export(tree);
     54      return true;
    5655    }
    5756
     
    6867    }
    6968
     69    // try-export checks the keys of this dictionary
     70    private static Dictionary<Type, Func<IFunction, IFunctionTree, string>> functionExporter = new Dictionary<Type, Func<IFunction, IFunctionTree, string>>() {
     71      { typeof(Addition), (function, tree) => ((Addition)function).ExportToScheme()},
     72      { typeof(And), (function, tree) => ((And)function).ExportToScheme()},
     73      { typeof(Average), (function, tree) => ((Average)function).ExportToScheme()},
     74      { typeof(Constant), (function, tree) => ((Constant)function).ExportToScheme(tree)},
     75      { typeof(Cosinus), (function, tree) => ((Cosinus)function).ExportToScheme()},
     76      { typeof(Differential), (function, tree) => ((Differential)function).ExportToScheme(tree)},
     77      { typeof(Division), (function, tree) => ((Division)function).ExportToScheme()},
     78      { typeof(Equal), (function, tree) => ((Equal)function).ExportToScheme()},
     79      { typeof(Exponential), (function, tree) => ((Exponential)function).ExportToScheme()},
     80      { typeof(GreaterThan), (function, tree) => ((GreaterThan)function).ExportToScheme()},
     81      { typeof(IfThenElse), (function, tree) => ((IfThenElse)function).ExportToScheme()},
     82      { typeof(LessThan), (function, tree) => ((LessThan)function).ExportToScheme()},
     83      { typeof(Logarithm), (function, tree) => ((Logarithm)function).ExportToScheme()},
     84      { typeof(Multiplication), (function, tree) => ((Multiplication)function).ExportToScheme()},
     85      { typeof(Not), (function, tree) => ((Not)function).ExportToScheme()},
     86      { typeof(Or), (function, tree) => ((Or)function).ExportToScheme()},
     87      { typeof(Power), (function, tree) => ((Power)function).ExportToScheme()},
     88      { typeof(Signum), (function, tree) => ((Signum)function).ExportToScheme()},
     89      { typeof(Sinus), (function, tree) => ((Sinus)function).ExportToScheme()},
     90      { typeof(Sqrt), (function, tree) => ((Sqrt)function).ExportToScheme()},
     91      { typeof(Subtraction), (function, tree) => ((Subtraction)function).ExportToScheme()},
     92      { typeof(Tangens), (function, tree) => ((Tangens)function).ExportToScheme()},
     93      { typeof(Variable), (function, tree) => ((Variable)function).ExportToScheme(tree)},
     94      { typeof(Xor), (function, tree) => ((Xor)function).ExportToScheme()},
     95    };
    7096    private static string ExportFunction(IFunction function, IFunctionTree tree) {
    7197      // this is smelly, if there is a cleaner way to have a 'dynamic' visitor
    7298      // please let me know! (gkronber 14.10.2008)
    73       if (function is Addition) return ((Addition)function).ExportToScheme();
    74       if (function is And) return ((And)function).ExportToScheme();
    75       if (function is Average) return ((Average)function).ExportToScheme();
    76       if (function is Constant) return ((Constant)function).ExportToScheme(tree);
    77       if (function is Cosinus) return ((Cosinus)function).ExportToScheme();
    78       if (function is Differential) return ((Differential)function).ExportToScheme(tree);
    79       if (function is Division) return ((Division)function).ExportToScheme();
    80       if (function is Equal) return ((Equal)function).ExportToScheme();
    81       if (function is Exponential) return ((Exponential)function).ExportToScheme();
    82       if (function is GreaterThan) return ((GreaterThan)function).ExportToScheme();
    83       if (function is IfThenElse) return ((IfThenElse)function).ExportToScheme();
    84       if (function is LessThan) return ((LessThan)function).ExportToScheme();
    85       if (function is Logarithm) return ((Logarithm)function).ExportToScheme();
    86       if (function is Multiplication) return ((Multiplication)function).ExportToScheme();
    87       if (function is Not) return ((Not)function).ExportToScheme();
    88       if (function is Or) return ((Or)function).ExportToScheme();
    89       if (function is Power) return ((Power)function).ExportToScheme();
    90       if (function is Signum) return ((Signum)function).ExportToScheme();
    91       if (function is Sinus) return ((Sinus)function).ExportToScheme();
    92       if (function is Sqrt) return ((Sqrt)function).ExportToScheme();
    93       if (function is Subtraction) return ((Subtraction)function).ExportToScheme();
    94       if (function is Tangens) return ((Tangens)function).ExportToScheme();
    95       if (function is Variable) return ((Variable)function).ExportToScheme(tree);
    96       if (function is Xor) return ((Xor)function).ExportToScheme();
    97       throw new UnknownFunctionException(function.Name);
    98     }
    99 
     99      if (functionExporter.ContainsKey(function.GetType())) return functionExporter[function.GetType()](function, tree);
     100      else return function.Name;
     101    }
    100102
    101103    #endregion
    102104
    103105    public static string GetName(IFunctionTree tree) {
    104       string name = "";
    105       try {
    106         name = ExportFunction(tree.Function, tree);
    107       }
    108       catch (UnknownFunctionException) {
    109         name = "N/A";
    110       }
    111       return name;
     106      return ExportFunction(tree.Function, tree);
    112107    }
    113108  }
  • trunk/sources/HeuristicLab.GP/3.3/HeuristicLab.GP-3.3.csproj

    r2328 r2629  
    109109    </Compile>
    110110    <Compile Include="GeneticProgrammingModel.cs" />
    111     <Compile Include="UnknownFunctionException.cs" />
    112111    <Compile Include="HeuristicLabGPPlugin.cs" />
    113112    <Compile Include="Properties\AssemblyInfo.cs" />
Note: See TracChangeset for help on using the changeset viewer.