Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/08/09 12:48:18 (16 years ago)
Author:
gkronber
Message:

Merged change sets from CEDMA branch to trunk:

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.CEDMA.Core/Results.cs

    r1075 r1287  
    3636
    3737namespace HeuristicLab.CEDMA.Core {
    38   public class Results : IViewable, IEnumerable<Record> {
    39     private Dictionary<Record, Dataset> datasets;
     38  public class Results : ItemBase {
     39    private string[] categoricalVariables = null;
     40    public string[] CategoricalVariables {
     41      get {
     42        if (categoricalVariables == null) {
     43          LoadModelAttributes();
     44        }
     45        return categoricalVariables;
     46      }
     47    }
     48
     49    private string[] ordinalVariables = null;
     50    public string[] OrdinalVariables {
     51      get {
     52        if (ordinalVariables == null) {
     53          LoadModelAttributes();
     54        }
     55        return ordinalVariables;
     56      }
     57    }
    4058
    4159    private IStore store;
     
    4765    }
    4866
    49     private void ReloadList() {
    50       var results = store.Select(new Statement(Ontology.AnyEntity, Ontology.PredicateInstanceOf, Ontology.TypeGeneticProgrammingFunctionTree))
    51       .Select(x => store.Select(new SelectFilter(
    52         new Entity[] { new Entity(x.Subject.Uri) },
    53         new Entity[] { Ontology.PredicateModelAttribute },
    54           new Resource[] { Ontology.AnyEntity })));
     67    private Entity dataSetEntity;
    5568
    56       Random random = new Random();
    57       foreach(Statement[] ss in results) {
    58         if(ss.Length > 0) {
    59           Record r = new Record(this, ss[0].Subject.Uri);
    60           r.Set(Record.X_JITTER, random.NextDouble() * 2.0 - 1.0);
    61           r.Set(Record.Y_JITTER, random.NextDouble() * 2.0 - 1.0);
    62           foreach(Statement s in ss) {
    63             string varName;
    64             predicateToVariableName.TryGetValue(s.Predicate, out varName);
    65             if(varName != null) {
    66               if(varName == Record.TREE_HEIGHT || varName == Record.TREE_SIZE || varName == Record.TARGET_VARIABLE) {
    67                 r.Set(varName, (double)(int)((Literal)s.Property).Value);
    68               } else {
    69                 r.Set(varName, (double)((Literal)s.Property).Value);
    70               }
     69    public Results(IStore store) {
     70      this.store = store;
     71    }
     72
     73    internal void FilterDataSet(Entity entity) {
     74      this.dataSetEntity = entity;
     75    }
     76
     77    private List<ResultsEntry> entries = null;
     78    private bool cached = false;
     79    public IEnumerable<ResultsEntry> GetEntries() {
     80      if (!cached)
     81        return SelectRows();
     82      return entries.AsEnumerable();
     83    }
     84
     85    private IEnumerable<ResultsEntry> SelectRows() {
     86      if (store == null) yield break;
     87      entries = new List<ResultsEntry>();
     88      var results = store.Query("<" + dataSetEntity + "> <" + Ontology.PredicateHasModel + "> ?Model ." + Environment.NewLine +
     89        "?Model ?Attribute ?Value .")
     90        .GroupBy(x => (Entity)x.Get("Model"));
     91      foreach (var modelBindings in results) {
     92        ResultsEntry entry = new ResultsEntry(modelBindings.Key.Uri);
     93        foreach (var binding in modelBindings) {
     94          if (binding.Get("Value") is Literal) {
     95            string name = ((Entity)binding.Get("Attribute")).Uri.Replace(Ontology.CedmaNameSpace, "");
     96            if (entry.Get(name) == null) {
     97              object value = ((Literal)binding.Get("Value")).Value;
     98              entry.Set(name, value);
    7199            }
    72100          }
    73           lock(records) {
    74             records.Add(r);
    75           }
    76           FireRecordAdded(r);
    77101        }
     102        entries.Add(entry);
     103        yield return entry;
    78104      }
     105
    79106      FireChanged();
     107      cached = true;
    80108    }
    81109
    82     public Results(IStore store)
    83       : base() {
    84       this.store = store;
    85       records = new List<Record>();
    86       datasets = new Dictionary<Record, Dataset>();
    87       //predicateToVariableName = new Dictionary<Entity, string>();
    88       //predicateToVariableName[targetVariablePredicate] = Record.TARGET_VARIABLE;
    89       //predicateToVariableName[treeSizePredicate] = Record.TREE_SIZE;
    90       //predicateToVariableName[treeHeightPredicate] = Record.TREE_HEIGHT;
    91       //predicateToVariableName[selectionPressurePredicate] = Record.SELECTIONPRESSURE;
    92       //predicateToVariableName[trainingMAPEPredicate] = Record.MAPE_TRAINING;
    93       //predicateToVariableName[validationMAPEPredicate] = Record.MAPE_VALIDATION;
    94       //predicateToVariableName[testMAPEPredicate] = Record.MAPE_TEST;
    95       //predicateToVariableName[trainingR2Predicate] = Record.R2_TRAINING;
    96       //predicateToVariableName[validationR2Predicate] = Record.R2_VALIDATION;
    97       //predicateToVariableName[testR2Predicate] = Record.R2_TEST;
     110    internal IEnumerable<string> SelectModelAttributes() {
     111      return CategoricalVariables.Concat(OrdinalVariables);
    98112    }
    99113
    100     public override IView CreateView() {
    101       return new ResultsView(this);
     114    private void LoadModelAttributes() {
     115      this.ordinalVariables =
     116        store.Query("?ModelAttribute <" + Ontology.PredicateInstanceOf + "> <" + Ontology.TypeOrdinalAttribute + "> .")
     117        .Select(s => ((Entity)s.Get("ModelAttribute")).Uri.Replace(Ontology.CedmaNameSpace, ""))
     118        .ToArray();
     119      this.categoricalVariables =
     120        store.Query("?ModelAttribute <" + Ontology.PredicateInstanceOf + "> <" + Ontology.TypeCategoricalAttribute + "> .")
     121        .Select(s => ((Entity)s.Get("ModelAttribute")).Uri.Replace(Ontology.CedmaNameSpace, ""))
     122        .ToArray();
    102123    }
    103 
    104     //internal void OpenModel(Record record) {
    105     //  IList<Statement> modelResults = store.Select(new Statement(new Entity(record.Uri), Ontology.PredicateSerializedData, Ontology.AnyEntity));
    106     //  if(modelResults.Count == 1) {
    107     //    string rawData = ((SerializedLiteral)modelResults[0].Property).RawData;
    108     //    XmlDocument doc = new XmlDocument();
    109     //    doc.LoadXml(rawData);
    110     //    IFunctionTree tree = (IFunctionTree)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
    111     //    int targetVariable = (int)record.Get(Record.TARGET_VARIABLE);
    112     //    Dataset dataset = GetDataset(record);
    113 
    114     //    ModelView modelView = new ModelView(record, dataset, tree, targetVariable);
    115     //    PluginManager.ControlManager.ShowControl(modelView);
    116     //  }
    117     //}
    118 
    119     //private Dataset GetDataset(Record record) {
    120     //  if(!datasets.ContainsKey(record)) {
    121     //    IList<Statement> result = store.Select(new Statement(Ontology.AnyEntity, Ontology.PredicateHasModel, new Entity(record.Uri)));
    122     //    if(result.Count == 1) {
    123     //      IList<Statement> datasetResult = store.Select(new Statement(result[0].Subject, Ontology.PredicateSerializedData, Ontology.AnyEntity));
    124     //      if(datasetResult.Count == 1) {
    125     //        string rawData = ((SerializedLiteral)datasetResult[0].Property).RawData;
    126     //        XmlDocument doc = new XmlDocument();
    127     //        doc.LoadXml(rawData);
    128     //        Dataset dataset = (Dataset)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
    129     //        datasets.Add(record, dataset);
    130     //      }
    131     //    }
    132     //  }
    133     //  return datasets[record];
    134     //}
    135 
    136     //internal void OpenAlgorithm(Record record) {
    137     //  IList<Statement> generatedBy = store.Select(new Statement(new Entity(record.Uri), Ontology.PredicateGeneratedBy, Ontology.AnyEntity));
    138     //  if(generatedBy.Count == 1) {
    139     //    IList<Statement> algoResult = store.Select(new Statement((Entity)generatedBy[0].Property, Ontology.PredicateSerializedData, Ontology.AnyEntity));
    140     //    if(algoResult.Count == 1) {
    141     //      string rawData = ((SerializedLiteral)algoResult[0].Property).RawData;
    142     //      XmlDocument doc = new XmlDocument();
    143     //      doc.LoadXml(rawData);
    144     //      IItem algo = (IItem)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
    145     //      PluginManager.ControlManager.ShowControl(algo.CreateView());
    146     //    }
    147     //  }
    148     //}
    149 
    150     internal void FilterDataSet(Entity entity) {
    151 
    152     }
    153 
    154     #region IEnumerable<Record> Members
    155 
    156     public IEnumerator<Record> GetEnumerator() {
    157       throw new NotImplementedException();
    158     }
    159 
    160     #endregion
    161 
    162     #region IEnumerable Members
    163 
    164     IEnumerator IEnumerable.GetEnumerator() {
    165       return this.GetEnumerator();
    166     }
    167 
    168     #endregion
    169124  }
    170125}
Note: See TracChangeset for help on using the changeset viewer.