Free cookie consent management tool by TermsFeed Policy Generator

Changeset 56


Ignore:
Timestamp:
03/07/08 23:52:38 (17 years ago)
Author:
swagner
Message:

Worked on ticket #48

  • added aliases to handle formal/actual name translations in scopes
Location:
branches/Modularization/HeuristicLab.Core
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/Modularization/HeuristicLab.Core/HeuristicLab.Core.csproj

    r30 r56  
    9191      <DependentUpon>ChooseTypeDialog.cs</DependentUpon>
    9292    </Compile>
     93    <Compile Include="AliasEventArgs.cs" />
    9394    <Compile Include="OperatorBaseDescriptionView.cs">
    9495      <SubType>UserControl</SubType>
  • branches/Modularization/HeuristicLab.Core/Interfaces/IScope.cs

    r2 r56  
    3030
    3131    ICollection<IVariable> Variables { get; }
     32    ICollection<string> Aliases { get; }
    3233    IList<IScope> SubScopes { get; }
    3334
     
    4243    IItem GetVariableValue(string name, bool recursiveLookup, bool throwOnError);
    4344
     45    string TranslateName(string name);
     46    void AddAlias(string alias, string name);
     47    void RemoveAlias(string alias);
     48
    4449    void AddSubScope(IScope scope);
    4550    void RemoveSubScope(IScope scope);
     
    5257    event EventHandler<VariableEventArgs> VariableAdded;
    5358    event EventHandler<VariableEventArgs> VariableRemoved;
     59    event EventHandler<AliasEventArgs> AliasAdded;
     60    event EventHandler<AliasEventArgs> AliasRemoved;
    5461    event EventHandler<ScopeIndexEventArgs> SubScopeAdded;
    5562    event EventHandler<ScopeIndexEventArgs> SubScopeRemoved;
  • branches/Modularization/HeuristicLab.Core/OperatorBase.cs

    r47 r56  
    360360        }
    361361      } else {
    362         return scope.GetVariableValue(info.ActualName, recursiveLookup, throwOnError);
     362        return scope.GetVariableValue(formalName, recursiveLookup, throwOnError);
    363363      }
    364364    }
     
    367367    public virtual IOperation Execute(IScope scope) {
    368368      myCanceled = false;
     369
     370      foreach (IVariableInfo variableInfo in VariableInfos)
     371        scope.AddAlias(variableInfo.FormalName, variableInfo.ActualName);
     372
    369373      IOperation next = Apply(scope);
    370374      OnExecuted();
  • branches/Modularization/HeuristicLab.Core/Scope.cs

    r2 r56  
    3838      get { return myVariables.Values; }
    3939    }
     40    private IDictionary<string, string> myAliases;
     41    public ICollection<string> Aliases {
     42      get { return myAliases.Values; }
     43    }
    4044    private List<IScope> mySubScopes;
    4145    public IList<IScope> SubScopes {
     
    4650      myName = "Anonymous";
    4751      myVariables = new Dictionary<string, IVariable>();
     52      myAliases = new Dictionary<string, string>();
    4853      mySubScopes = new List<IScope>();
    4954    }
     
    108113    public IItem GetVariableValue(string name, bool recursiveLookup, bool throwOnError) {
    109114      IVariable variable;
     115      name = TranslateName(name);
    110116      if (myVariables.TryGetValue(name, out variable)) {
    111117        return variable.Value;
     
    122128    }
    123129
     130    public string TranslateName(string name) {
     131      while (myAliases.ContainsKey(name))
     132        name = myAliases[name];
     133      if (parent != null)
     134        name = parent.TranslateName(name);
     135      return name;
     136    }
     137    public void AddAlias(string alias, string name) {
     138      if (alias != name) {
     139        if (myAliases.ContainsKey(alias))
     140          myAliases.Remove(alias);
     141        myAliases.Add(alias, name);
     142        OnAliasAdded(alias);
     143      }
     144    }
     145    public void RemoveAlias(string alias) {
     146      if (myAliases.ContainsKey(alias)) {
     147        myAliases.Remove(alias);
     148        OnAliasRemoved(alias);
     149      }
     150    }
     151
    124152    public void AddSubScope(IScope scope) {
    125153      scope.SetParent(this);
     
    172200        RemoveVariable(variableNames[j]);
    173201
     202      string[] aliases = new string[Aliases.Count];
     203      i = 0;
     204      foreach (string alias in myAliases.Keys) {
     205        aliases[i] = alias;
     206        i++;
     207      }
     208      for (int j = 0; j < aliases.Length; j++)
     209        RemoveAlias(aliases[j]);
     210
    174211      while (SubScopes.Count > 0)
    175212        RemoveSubScope(SubScopes[0]);
     
    182219      foreach (IVariable variable in myVariables.Values)
    183220        clone.AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
     221      foreach (KeyValuePair<string, string> alias in myAliases)
     222        clone.AddAlias(alias.Key, alias.Value);
    184223      for (int i = 0; i < SubScopes.Count; i++)
    185224        clone.AddSubScope((IScope)Auxiliary.Clone(SubScopes[i], clonedObjects));
     
    197236      if (VariableRemoved != null)
    198237        VariableRemoved(this, new VariableEventArgs(variable));
     238    }
     239    public event EventHandler<AliasEventArgs> AliasAdded;
     240    protected virtual void OnAliasAdded(string alias) {
     241      if (AliasAdded != null)
     242        AliasAdded(this, new AliasEventArgs(alias));
     243    }
     244    public event EventHandler<AliasEventArgs> AliasRemoved;
     245    protected virtual void OnAliasRemoved(string alias) {
     246      if (AliasRemoved != null)
     247        AliasRemoved(this, new AliasEventArgs(alias));
    199248    }
    200249    public event EventHandler<ScopeIndexEventArgs> SubScopeAdded;
     
    226275      node.AppendChild(variables);
    227276
     277      XmlNode aliases = document.CreateNode(XmlNodeType.Element, "Aliases", null);
     278      foreach (KeyValuePair<string, string> alias in myAliases) {
     279        XmlNode child = document.CreateNode(XmlNodeType.Element, alias.Key, null);
     280        child.InnerText = alias.Value;
     281        aliases.AppendChild(child);
     282      }
     283      node.AppendChild(aliases);
     284
    228285      XmlNode subScopes = document.CreateNode(XmlNodeType.Element, "SubScopes", null);
    229286      for (int i = 0; i < SubScopes.Count; i++)
     
    243300      }
    244301
     302      XmlNode aliases = node.SelectSingleNode("Aliases");
     303      if (aliases != null) {
     304        foreach (XmlNode aliasNode in aliases.ChildNodes)
     305          AddAlias(aliasNode.Name, aliasNode.InnerText);
     306      }
     307
    245308      XmlNode subScopes = node.SelectSingleNode("SubScopes");
    246309      for (int i = 0; i < subScopes.ChildNodes.Count; i++) {
Note: See TracChangeset for help on using the changeset viewer.