Free cookie consent management tool by TermsFeed Policy Generator

Changeset 764


Ignore:
Timestamp:
11/15/08 19:55:13 (16 years ago)
Author:
gkronber
Message:

removed visitor classes and methods in HeuristicLab.Constraints and replaced visitor with manual dispatch. #343 (Rethink about usefulness of visitors for ObjectData and Constraints)

Location:
trunk/sources/HeuristicLab.Constraints
Files:
2 deleted
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Constraints/AllSubOperatorsTypeConstraint.cs

    r467 r764  
    8585    }
    8686
    87     public override void Accept(IConstraintVisitor visitor) {
    88       visitor.Visit(this);
    89     }
    90 
    9187    #region persistence
    9288    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
  • trunk/sources/HeuristicLab.Constraints/AndConstraint.cs

    r40 r764  
    6868    }
    6969
    70     public override void Accept(IConstraintVisitor visitor) {
    71       visitor.Visit(this);
    72     }
    73 
    7470    #region persistence
    7571    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
  • trunk/sources/HeuristicLab.Constraints/ConstraintBase.cs

    r2 r764  
    3333
    3434    public abstract bool Check(IItem data);
    35 
    36     public virtual void Accept(IConstraintVisitor visitor) {
    37     }
    3835  }
    3936}
  • trunk/sources/HeuristicLab.Constraints/DoubleBoundedConstraint.cs

    r344 r764  
    128128    }
    129129
    130     public override void Accept(IConstraintVisitor visitor) {
    131       visitor.Visit(this);
    132     }
    133 
    134130    #region persistence
    135131    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
  • trunk/sources/HeuristicLab.Constraints/FalseConstraint.cs

    r2 r764  
    4545      return false;
    4646    }
    47 
    48 
    49     public override void Accept(IConstraintVisitor visitor) {
    50       visitor.Visit(this);
    51     }
    5247  }
    5348}
  • trunk/sources/HeuristicLab.Constraints/HeuristicLab.Constraints.csproj

    r582 r764  
    7676    </Compile>
    7777    <Compile Include="ConstraintBase.cs" />
    78     <Compile Include="ConstraintVisitorBase.cs">
    79       <SubType>Code</SubType>
    80     </Compile>
    8178    <Compile Include="DoubleBoundedConstraint.cs" />
    8279    <Compile Include="DoubleBoundedConstraintView.cs">
     
    8683      <DependentUpon>DoubleBoundedConstraintView.cs</DependentUpon>
    8784    </Compile>
    88     <Compile Include="IConstraintVisitor.cs" />
    8985    <Compile Include="IntBoundedConstraint.cs" />
    9086    <Compile Include="FalseConstraintView.cs">
  • trunk/sources/HeuristicLab.Constraints/IntBoundedConstraint.cs

    r2 r764  
    121121    }
    122122
    123     public override void Accept(IConstraintVisitor visitor) {
    124       visitor.Visit(this);
    125     }
    126 
    127 
    128123    #region persistence
    129124    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
  • trunk/sources/HeuristicLab.Constraints/IsIntegerConstraint.cs

    r2 r764  
    5050      return false;
    5151    }
    52 
    53     public override void Accept(IConstraintVisitor visitor) {
    54       visitor.Visit(this);
    55     }
    56 
    5752  }
    5853}
  • trunk/sources/HeuristicLab.Constraints/ItemTypeConstraint.cs

    r40 r764  
    6767    }
    6868
    69     public override void Accept(IConstraintVisitor visitor) {
    70       visitor.Visit(this);
    71     }
    72 
    7369    #region clone & persistence
    7470    public override object Clone(IDictionary<Guid, object> clonedObjects) {
  • trunk/sources/HeuristicLab.Constraints/NotConstraint.cs

    r2 r764  
    6262    }
    6363
    64     public override void Accept(IConstraintVisitor visitor) {
    65       visitor.Visit(this);
    66     }
    67 
    68 
    6964    #region persistence
    7065    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
  • trunk/sources/HeuristicLab.Constraints/NumberOfSubOperatorsConstraint.cs

    r2 r764  
    7373    }
    7474
    75     public override void Accept(IConstraintVisitor visitor) {
    76       visitor.Visit(this);
    77     }
    78 
    79 
    8075    #region persistence
    8176    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
  • trunk/sources/HeuristicLab.Constraints/OrConstraint.cs

    r40 r764  
    6868    }
    6969
    70     public override void Accept(IConstraintVisitor visitor) {
    71       visitor.Visit(this);
    72     }
    73 
    74 
    7570    #region persistence
    7671    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
  • trunk/sources/HeuristicLab.Constraints/SubOperatorsConstraintAnalyser.cs

    r431 r764  
    3838    public IList<IOperator> GetAllowedOperators(IOperator op, int childIndex) {
    3939      AndConstraint andConstraint = new AndConstraint();
    40       foreach(IConstraint constraint in op.Constraints) {
     40      foreach (IConstraint constraint in op.Constraints) {
    4141        andConstraint.Clauses.Add(constraint);
    4242      }
    43      
    44       GetAllowedOperatorsVisitor visitor = new GetAllowedOperatorsVisitor(allPossibleOperators, childIndex);
    45       andConstraint.Accept(visitor);
    46       return visitor.AllowedOperators;
     43
     44      return GetAllowedOperators(andConstraint, childIndex);
     45    }
     46
     47    private IList<IOperator> GetAllowedOperators(IConstraint constraint, int childIndex) {
     48      // manual dispatch on dynamic type
     49      if (constraint is AndConstraint)
     50        return GetAllowedOperators((AndConstraint)constraint, childIndex);
     51      else if (constraint is OrConstraint)
     52        return GetAllowedOperators((OrConstraint)constraint, childIndex);
     53      else if (constraint is NotConstraint)
     54        return GetAllowedOperators((NotConstraint)constraint, childIndex);
     55      else if (constraint is AllSubOperatorsTypeConstraint)
     56        return GetAllowedOperators((AllSubOperatorsTypeConstraint)constraint, childIndex);
     57      else if (constraint is SubOperatorTypeConstraint)
     58        return GetAllowedOperators((SubOperatorTypeConstraint)constraint, childIndex);
     59      else return new List<IOperator>(allPossibleOperators); // ignore all other constraints
    4760    }
    4861
     
    5164    // however we would need to switch the whole Constraints project to .NET 3.5 for that
    5265    private static IList<IOperator> Intersect(ICollection<IOperator> a, ICollection<IOperator> b) {
    53       if(a.Count > b.Count) {
     66      if (a.Count > b.Count) {
    5467        return Intersect(b, a);
    5568      }
     
    5770      List<IOperator> intersection = new List<IOperator>(a.Count);
    5871
    59       foreach(IOperator element in a) {
    60         if(InSet(element, b)) {
     72      foreach (IOperator element in a) {
     73        if (InSet(element, b)) {
    6174          intersection.Add(element);
    6275        }
     
    6780    private static IList<IOperator> Union(ICollection<IOperator> a, ICollection<IOperator> b) {
    6881      List<IOperator> union = new List<IOperator>(a);
    69       foreach(IOperator candidateElement in b) {
    70         if(!InSet(candidateElement, union)) {
     82      foreach (IOperator candidateElement in b) {
     83        if (!InSet(candidateElement, union)) {
    7184          union.Add(candidateElement);
    7285        }
     
    7891    private static IList<IOperator> Substract(ICollection<IOperator> minuend, ICollection<IOperator> subtrahend) {
    7992      List<IOperator> difference = new List<IOperator>();
    80       foreach(IOperator element in minuend) {
    81         if(!InSet(element, subtrahend)) {
     93      foreach (IOperator element in minuend) {
     94        if (!InSet(element, subtrahend)) {
    8295          difference.Add(element);
    8396        }
     
    88101
    89102    private static bool InSet(IOperator op, ICollection<IOperator> set) {
    90       foreach(IOperator element in set) {
    91         if(element == op)
     103      foreach (IOperator element in set) {
     104        if (element == op)
    92105          return true;
    93106      }
     
    96109    #endregion
    97110
    98     #region visitor
    99     /// <summary>
    100     /// The visitor builds a set of allowed operators based on a tree of constraints.
    101     /// </summary>
    102     private class GetAllowedOperatorsVisitor : ConstraintVisitorBase {
    103       private IList<IOperator> allowedOperators;
     111    public IList<IOperator> GetAllowedOperators(AndConstraint constraint, int childIndex) {
     112      IList<IOperator> allowedOperators = new List<IOperator>(allPossibleOperators);
     113      // keep only the intersection of all subconstraints
     114      foreach (ConstraintBase clause in constraint.Clauses) {
     115        allowedOperators = Intersect(allowedOperators, GetAllowedOperators(clause, childIndex));
     116      }
     117      return allowedOperators;
     118    }
    104119
    105       public IList<IOperator> AllowedOperators {
    106         get { return allowedOperators; }
     120    public IList<IOperator> GetAllowedOperators(OrConstraint constraint, int childIndex) {
     121      IList<IOperator> allowedOperators = new List<IOperator>();
     122      foreach (ConstraintBase clause in constraint.Clauses) {
     123        allowedOperators = Union(allowedOperators, GetAllowedOperators(clause, childIndex));
    107124      }
    108       private ICollection<IOperator> possibleOperators;
    109       private int childIndex;
     125      return allowedOperators;
     126    }
    110127
    111       public GetAllowedOperatorsVisitor(ICollection<IOperator> possibleOperators, int childIndex) {
    112         // default is that all possible operators are allowed
    113         allowedOperators = new List<IOperator>(possibleOperators);
    114         this.possibleOperators = possibleOperators;
    115         this.childIndex = childIndex;
    116       }
     128    public IList<IOperator> GetAllowedOperators(NotConstraint constraint, int childIndex) {
     129      return Substract(allPossibleOperators, GetAllowedOperators(constraint.SubConstraint, childIndex));
     130    }
    117131
    118       public override void Visit(AndConstraint constraint) {
    119         base.Visit(constraint);
     132    public IList<IOperator> GetAllowedOperators(AllSubOperatorsTypeConstraint constraint, int childIndex) {
     133      return Intersect(allPossibleOperators, constraint.AllowedSubOperators);
     134    }
    120135
    121         // keep only the intersection of all subconstraints
    122         foreach(ConstraintBase clause in constraint.Clauses) {
    123           GetAllowedOperatorsVisitor visitor = new GetAllowedOperatorsVisitor(possibleOperators, childIndex);
    124           clause.Accept(visitor);
    125           allowedOperators = Intersect(allowedOperators, visitor.allowedOperators);
    126         }
    127       }
    128 
    129       public override void Visit(OrConstraint constraint) {
    130         base.Visit(constraint);
    131 
    132         // allowed operators is the union of all allowed operators as defined by the subconstraints
    133         allowedOperators.Clear();
    134 
    135         foreach(ConstraintBase clause in constraint.Clauses) {
    136           GetAllowedOperatorsVisitor visitor = new GetAllowedOperatorsVisitor(possibleOperators, childIndex);
    137           clause.Accept(visitor);
    138           allowedOperators = Union(allowedOperators, visitor.allowedOperators);
    139         }
    140       }
    141 
    142       public override void Visit(NotConstraint constraint) {
    143         base.Visit(constraint);
    144         GetAllowedOperatorsVisitor visitor = new GetAllowedOperatorsVisitor(possibleOperators, childIndex);
    145         constraint.SubConstraint.Accept(visitor);
    146 
    147         allowedOperators = Substract(possibleOperators, visitor.allowedOperators);
    148       }
    149 
    150       public override void Visit(AllSubOperatorsTypeConstraint constraint) {
    151         base.Visit(constraint);
    152 
    153         allowedOperators = Intersect(possibleOperators, constraint.AllowedSubOperators);
    154       }
    155 
    156       public override void Visit(SubOperatorTypeConstraint constraint) {
    157         if(childIndex != constraint.SubOperatorIndex.Data) {
    158           allowedOperators = new List<IOperator>(possibleOperators);
    159         } else {
    160           allowedOperators = Intersect(possibleOperators, constraint.AllowedSubOperators);
    161         }
     136    public IList<IOperator> GetAllowedOperators(SubOperatorTypeConstraint constraint, int childIndex) {
     137      if (childIndex != constraint.SubOperatorIndex.Data) {
     138        return new List<IOperator>(allPossibleOperators);
     139      } else {
     140        return Intersect(allPossibleOperators, constraint.AllowedSubOperators);
    162141      }
    163142    }
    164     #endregion
    165143  }
    166144}
  • trunk/sources/HeuristicLab.Constraints/SubOperatorsTypeConstraint.cs

    r467 r764  
    9999    }
    100100
    101     public override void Accept(IConstraintVisitor visitor) {
    102       visitor.Visit(this);
    103     }
    104 
    105 
    106101    #region persistence
    107102    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
  • trunk/sources/HeuristicLab.Constraints/TrueConstraint.cs

    r2 r764  
    4242    }
    4343
    44     public override void Accept(IConstraintVisitor visitor) {
    45       visitor.Visit(this);
    46     }
    47 
    48 
    4944    public override bool Check(IItem data) {
    5045      return true;
Note: See TracChangeset for help on using the changeset viewer.