Index: branches/CEDMA-Exporter-715/tools/CedmaExporter/CedmaExporter.cs
===================================================================
--- branches/CEDMA-Exporter-715/tools/CedmaExporter/CedmaExporter.cs	(revision 2228)
+++ branches/CEDMA-Exporter-715/tools/CedmaExporter/CedmaExporter.cs	(revision 2232)
@@ -10,4 +10,5 @@
 using HeuristicLab.Data;
 using SVM;
+using HeuristicLab.DataAnalysis;
 
 namespace CedmaExporter {
@@ -19,58 +20,109 @@
       using (StreamWriter writer = File.CreateText(outputFileName)) {
         using (Store store = Store.Create(rdfConnectionString)) {
-          WriteColumnHeaders(store, writer);
-          WriteModels(store, writer, new ModelExporter(Path.GetDirectoryName(outputFileName), true));
+          var dsEntities = store.SelectSubjects(Ontology.InstanceOf, Ontology.TypeDataSet);
+          HeuristicLab.CEDMA.Core.Problem prob = (HeuristicLab.CEDMA.Core.Problem)PersistenceManager.RestoreFromGZip(Convert.FromBase64String(((Literal)store.SelectObjects(dsEntities[0], Ontology.SerializedData)[0]).Value));
+          Dataset ds = prob.Dataset;
+          WriteColumnHeaders(writer);
+          List<string> inputVariables = WriteVariableImpactHeaders(store, writer);
+          writer.WriteLine();
+          WriteModels(store, writer, inputVariables, new ModelExporter(ds, Path.GetDirectoryName(outputFileName), false));
         }
       }
     }
 
-    private static void WriteModels(Store store, StreamWriter writer, ModelExporter exporter) {
+    private static List<string> WriteVariableImpactHeaders(Store store, StreamWriter writer) {
+      List<string> inputVarNames = new List<string>();
+      Statement template = new Statement();
+      template.Predicate = Ontology.HasInputVariable;
+      var inputVars = store.Select(template).Select(x => x.Object);
+      foreach (Entity inputVar in inputVars) {
+        var names = store.SelectObjects(inputVar, Ontology.Name);
+        if (names.Count() > 0) {
+          var inputVarName = ((Literal)names[0]).Value;
+          if (!inputVarNames.Contains(inputVarName)) inputVarNames.Add(inputVarName);
+        }
+      }
+      inputVarNames.Sort();
+      foreach (string inputVarName in inputVarNames) {
+        writer.Write("EvaluationImpact ("); writer.Write(inputVarName); writer.Write("); ");
+      }
+      foreach (string inputVarName in inputVarNames) {
+        writer.Write("QualityImpact ("); writer.Write(inputVarName); writer.Write("); ");
+      }
+      writer.WriteLine();
+      return inputVarNames;
+    }
+
+    private static void WriteModels(Store store, StreamWriter writer, List<string> inputVariables, ModelExporter exporter) {
       var subjects = store.SelectSubjects(new Entity(Ontology.InstanceOf.Uri), new Entity(Ontology.TypeModel.Uri));
       int i = 0;
       foreach (var model in subjects) {
-        writer.Write(i++); writer.Write("; ");
-        string modelFileName = "model_" + i.ToString();
-        writer.Write(modelFileName); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TargetVariable)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanSquaredError)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanSquaredError)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanSquaredError)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
-        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
-        WriteVariableImpacts(writer, store, model);
-        writer.WriteLine();
-        var data = PersistenceManager.RestoreFromGZip(Convert.FromBase64String((string)LiteralValue(store.SelectObjects(model, Ontology.SerializedData)[0])));
-        exporter.Export(modelFileName, data);
+        try {
+          writer.Write(i++); writer.Write("; ");
+          string targetVariable = LiteralValue(store.SelectObjects(model, Ontology.TargetVariable)[0]).ToString();
+          string algoName = LiteralValue(store.SelectObjects(model, Ontology.Name)[0]).ToString();
+          string modelFileName = "model_" + targetVariable + "_" + i.ToString("000");
+          writer.Write(modelFileName); writer.Write("; ");
+          writer.Write(targetVariable); writer.Write("; ");
+          writer.Write(algoName); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanSquaredError)[0]).ToString()); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanSquaredError)[0]).ToString()); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanSquaredError)[0]).ToString()); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
+          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
+          WriteVariableImpacts(writer, store, model, inputVariables);
+          var data = PersistenceManager.RestoreFromGZip(Convert.FromBase64String((string)LiteralValue(store.SelectObjects(model, Ontology.SerializedData)[0])));
+          exporter.Export(modelFileName, data);
+        }
+        catch (FormatException ex) {
+          // ignore
+        }
+        finally {
+          writer.WriteLine();
+        }
       }
     }
 
-    private static void WriteVariableImpacts(StreamWriter writer, Store store, Entity model) {
-      var inputVariables = store.SelectObjects(model, Ontology.HasInputVariable);
-      Dictionary<string, List<double>> impacts = new Dictionary<string,List<double>>();
-      foreach (Entity inputVariable in inputVariables) {
+    private static void WriteVariableImpacts(StreamWriter writer, Store store, Entity model, List<string> inputVariables) {
+      var inputVariableEntities = store.SelectObjects(model, Ontology.HasInputVariable);
+      Dictionary<string, List<double>> impacts = new Dictionary<string, List<double>>();
+      foreach (Entity inputVariableEntity in inputVariableEntities) {
         var variableImpacts = new List<double>();
-        impacts[inputVariable.ToString()] = variableImpacts;
-        variableImpacts.Add((double)(LiteralValue(store.SelectObjects(inputVariable, Ontology.EvaluationImpact)[0])));
-        variableImpacts.Add((double)(LiteralValue(store.SelectObjects(inputVariable, Ontology.QualityImpact)[0])));
+        var names = store.SelectObjects(inputVariableEntity, Ontology.Name);
+        if (names.Count() == 0) throw new FormatException();
+        impacts[(string)(LiteralValue(names[0]))] = variableImpacts;
+        variableImpacts.Add((double)(LiteralValue(store.SelectObjects(inputVariableEntity, Ontology.EvaluationImpact)[0])));
+        variableImpacts.Add((double)(LiteralValue(store.SelectObjects(inputVariableEntity, Ontology.QualityImpact)[0])));
       }
 
-      foreach (string varName in impacts.Keys.OrderBy(x => x)) {
-        writer.Write(impacts[varName][0]); writer.Write("; ");
-        writer.Write(impacts[varName][1]); writer.Write("; ");
+      foreach (string varName in inputVariables) {
+        if (impacts.ContainsKey(varName)) {
+          writer.Write(impacts[varName][0]); writer.Write("; ");
+        } else {
+          writer.Write(" ; ");
+        }
+      }
+
+      foreach (string varName in inputVariables) {
+        if (impacts.ContainsKey(varName)) {
+          writer.Write(impacts[varName][1]); writer.Write("; ");
+        } else {
+          writer.Write(" ; ");
+        }
       }
     }
 
-    private static void WriteColumnHeaders(Store store, StreamWriter writer) {
-      writer.Write("Id; Filename; TargetVariable; " +
+    private static void WriteColumnHeaders(StreamWriter writer) {
+      writer.Write("Id; Filename; TargetVariable; Algorithm;" +
         "TrainingMSE; ValidationMSE; TestMSE; " +
         "TrainingR2; ValidationR2; TestR2; " +
@@ -78,19 +130,4 @@
         "TrainingMAPRE; ValidationMAPRE; TestMAPRE; " +
         "TrainingVAF; ValidationVAF; TestVAF; ");
-
-      List<string> inputVarNames = new List<string>();
-      Statement template = new Statement();
-      template.Predicate = Ontology.HasInputVariable;
-      var inputVars = store.Select(template).Select(x => x.Object);
-      foreach (Entity inputVar in inputVars) {
-        var inputVarName = ((Literal)store.SelectObjects(inputVar, Ontology.Name)[0]).Value;
-        if (!inputVarNames.Contains(inputVarName)) inputVarNames.Add(inputVarName);
-      }
-      inputVarNames.Sort();
-      foreach (string inputVarName in inputVarNames) {
-        writer.Write(inputVarName); writer.Write(": EvaluationImpact; "); 
-        writer.Write(inputVarName); writer.Write(": QualityImpact; ");
-      }
-      writer.WriteLine();
     }
 
@@ -103,13 +140,19 @@
     private string outputDir;
     private bool debugging;
-    IFunctionTreeExporter treeExporter = new SymbolicExpressionExporter();
+    private Dataset dataset;
+    IFunctionTreeExporter treeExporter;
 
-    public ModelExporter(string outputDir, bool debugging) {
+    public ModelExporter(Dataset ds, string outputDir, bool debugging) {
+      this.dataset = ds;
       this.outputDir = outputDir;
       this.debugging = debugging;
+      treeExporter = new SymbolicExpressionExporter(ds);
     }
 
     public void Export(string modelFileName, IStorable model) {
       if (debugging) return;
+      foreach (char c in Path.GetInvalidFileNameChars()) {
+        modelFileName = modelFileName.Replace(c, '_');
+      }
       if (model is IFunctionTree) {
         using (StreamWriter writer = File.CreateText(Path.Combine(outputDir, modelFileName + ".gp.txt"))) {
Index: branches/CEDMA-Exporter-715/tools/CedmaExporter/CedmaExporter.csproj
===================================================================
--- branches/CEDMA-Exporter-715/tools/CedmaExporter/CedmaExporter.csproj	(revision 2228)
+++ branches/CEDMA-Exporter-715/tools/CedmaExporter/CedmaExporter.csproj	(revision 2232)
@@ -32,7 +32,19 @@
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="HeuristicLab.CEDMA.Core-3.3, Version=3.3.0.2146, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>..\..\sources\HeuristicLab\bin\Debug\plugins\HeuristicLab.CEDMA.Core-3.3.dll</HintPath>
+    </Reference>
     <Reference Include="HeuristicLab.Core-3.2, Version=3.2.0.2143, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
       <SpecificVersion>False</SpecificVersion>
       <HintPath>..\..\..\..\..\Program Files\HeuristicLab 3.0\plugins\HeuristicLab.Core-3.2.dll</HintPath>
+    </Reference>
+    <Reference Include="HeuristicLab.Data-3.2, Version=3.2.0.1609, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>..\..\sources\HeuristicLab\bin\Debug\plugins\HeuristicLab.Data-3.2.dll</HintPath>
+    </Reference>
+    <Reference Include="HeuristicLab.DataAnalysis-3.2, Version=3.2.0.2162, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>..\..\sources\HeuristicLab.DataAnalysis\3.2\bin\Debug\HeuristicLab.DataAnalysis-3.2.dll</HintPath>
     </Reference>
     <Reference Include="HeuristicLab.GP-3.3, Version=3.3.0.1534, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
@@ -105,4 +117,5 @@
       <DesignTimeSharedInput>True</DesignTimeSharedInput>
     </Compile>
+    <Compile Include="SymbolicExpressionExporter.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Index: branches/CEDMA-Exporter-715/tools/CedmaExporter/SymbolicExpressionExporter.cs
===================================================================
--- branches/CEDMA-Exporter-715/tools/CedmaExporter/SymbolicExpressionExporter.cs	(revision 2232)
+++ branches/CEDMA-Exporter-715/tools/CedmaExporter/SymbolicExpressionExporter.cs	(revision 2232)
@@ -0,0 +1,227 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2008 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 <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using HeuristicLab.Data;
+using HeuristicLab.DataAnalysis;
+using HeuristicLab.GP.StructureIdentification;
+using HeuristicLab.GP;
+
+namespace CedmaExporter {
+  public class SymbolicExpressionExporter : IFunctionTreeExporter, IFunctionTreeNameGenerator {
+    private StringBuilder builder;
+    private string currentIndent;
+    private Dataset dataset;
+
+    public SymbolicExpressionExporter(Dataset ds) {
+      this.dataset = ds;
+    }
+
+    #region IFunctionTreeExporter Members
+
+    public string Name {
+      get { return "Symbolic Expression Exporter"; }
+    }
+
+
+    public string Export(IFunctionTree tree) {
+      builder = new StringBuilder();
+      currentIndent = "";
+      BuildExportString(tree);
+      return builder.ToString();
+    }
+
+    public bool TryExport(IFunctionTree tree, out string exported) {
+      try {
+        exported = Export(tree);
+        return true;
+      } catch(UnknownFunctionException) {
+        exported = "";
+        return false;
+      }
+    }
+
+    private void BuildExportString(IFunctionTree tree) {
+      builder.Append(currentIndent);
+      builder.Append("(" + ExportFunction(tree.Function, tree) + " ");
+      currentIndent += "  ";
+      foreach(IFunctionTree subTree in tree.SubTrees) {
+        builder.Append("\n");
+        BuildExportString(subTree);
+      }
+      if(tree.SubTrees.Count > 0) builder.Append(")");
+      currentIndent = currentIndent.Remove(0, 2);
+    }
+
+    private string ExportFunction(IFunction function, IFunctionTree tree) {
+      // this is smelly, if there is a cleaner way to have a 'dynamic' visitor 
+      // please let me know! (gkronber 14.10.2008)
+      if(function is Addition) return ((Addition)function).ExportToScheme();
+      if(function is And) return ((And)function).ExportToScheme();
+      if(function is Average) return ((Average)function).ExportToScheme();
+      if(function is Constant) return ((Constant)function).ExportToScheme(tree);
+      if(function is Cosinus) return ((Cosinus)function).ExportToScheme();
+      if(function is Differential) return ((Differential)function).ExportToScheme(tree, dataset);
+      if(function is Division) return ((Division)function).ExportToScheme();
+      if(function is Equal) return ((Equal)function).ExportToScheme();
+      if(function is Exponential) return ((Exponential)function).ExportToScheme();
+      if(function is GreaterThan) return ((GreaterThan)function).ExportToScheme();
+      if(function is IfThenElse) return ((IfThenElse)function).ExportToScheme();
+      if(function is LessThan) return ((LessThan)function).ExportToScheme();
+      if(function is Logarithm) return ((Logarithm)function).ExportToScheme();
+      if(function is Multiplication) return ((Multiplication)function).ExportToScheme();
+      if(function is Not) return ((Not)function).ExportToScheme();
+      if(function is Or) return ((Or)function).ExportToScheme();
+      if(function is Power) return ((Power)function).ExportToScheme();
+      if(function is Signum) return ((Signum)function).ExportToScheme();
+      if(function is Sinus) return ((Sinus)function).ExportToScheme();
+      if(function is Sqrt) return ((Sqrt)function).ExportToScheme();
+      if(function is Subtraction) return ((Subtraction)function).ExportToScheme();
+      if(function is Tangens) return ((Tangens)function).ExportToScheme();
+      if(function is Variable) return ((Variable)function).ExportToScheme(tree, dataset);
+      if(function is Xor) return ((Xor)function).ExportToScheme();
+      throw new UnknownFunctionException(function.Name);
+    }
+
+
+    #endregion
+
+    #region IFunctionTreeNameGenerator Members
+
+    string IFunctionTreeNameGenerator.Name {
+      get { return "Symbolic Expression"; }
+    }
+
+    public string GetName(IFunctionTree tree) {
+      string name = "";
+      try {
+        name = ExportFunction(tree.Function, tree);
+      } catch(UnknownFunctionException) {
+        name = "N/A";
+      }
+      return name;
+    }
+
+    #endregion
+  }
+
+  internal static class SchemeExporterExtensions {
+    public static string ExportToScheme(this Addition addition) {
+      return "+";
+    }
+
+    public static string ExportToScheme(this Constant constant, IFunctionTree tree) {
+      return tree.GetLocalVariable(Constant.VALUE).Value.ToString();
+    }
+
+    public static string ExportToScheme(this Cosinus cosinus) {
+      return "cos";
+    }
+
+    public static string ExportToScheme(this Division division) {
+      return "/";
+    }
+
+    public static string ExportToScheme(this Exponential exponential) {
+      return "exp";
+    }
+
+    public static string ExportToScheme(this Logarithm logarithm) {
+      return "log";
+    }
+
+    public static string ExportToScheme(this Multiplication multiplication) {
+      return "*";
+    }
+
+    public static string ExportToScheme(this Power power) {
+      return "expt";
+    }
+
+    public static string ExportToScheme(this Signum signum) {
+      return "sign";
+    }
+
+    public static string ExportToScheme(this Sinus sinus) {
+      return "sin";
+    }
+
+    public static string ExportToScheme(this Sqrt sqrt) {
+      return "sqrt";
+    }
+
+    public static string ExportToScheme(this Subtraction subtraction) {
+      return "-";
+    }
+
+    public static string ExportToScheme(this Tangens tangens) {
+      return "tan";
+    }
+
+    public static string ExportToScheme(this Variable variable, IFunctionTree tree, Dataset ds) {
+      return "(variable " + tree.GetLocalVariable(Variable.WEIGHT).Value + " " +
+        ds.GetVariableName(int.Parse(tree.GetLocalVariable(Variable.INDEX).Value.ToString())) + " " + tree.GetLocalVariable(Variable.OFFSET).Value + ")";
+    }
+    public static string ExportToScheme(this Differential differential, IFunctionTree tree, Dataset ds) {
+      return "(differential " + tree.GetLocalVariable(Differential.WEIGHT).Value + " " +
+        ds.GetVariableName(int.Parse(tree.GetLocalVariable(Differential.INDEX).Value.ToString())) + " " + tree.GetLocalVariable(Differential.OFFSET).Value + ")";
+    }
+
+    public static string ExportToScheme(this And and) {
+      return "and";
+    }
+
+    public static string ExportToScheme(this Average average) {
+      return "mean";
+    }
+
+    public static string ExportToScheme(this IfThenElse ifThenElse) {
+      return "if";
+    }
+
+    public static string ExportToScheme(this Not not) {
+      return "not";
+    }
+
+    public static string ExportToScheme(this Or or) {
+      return "or";
+    }
+
+    public static string ExportToScheme(this Xor xor) {
+      return "xor";
+    }
+
+    public static string ExportToScheme(this Equal equal) {
+      return "equ";
+    }
+
+    public static string ExportToScheme(this LessThan lessThan) {
+      return "<";
+    }
+
+    public static string ExportToScheme(this GreaterThan greaterThan) {
+      return ">";
+    }
+  }
+}
