Changeset 764
- Timestamp:
- 11/15/08 19:55:13 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Constraints
- Files:
-
- 2 deleted
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Constraints/AllSubOperatorsTypeConstraint.cs
r467 r764 85 85 } 86 86 87 public override void Accept(IConstraintVisitor visitor) {88 visitor.Visit(this);89 }90 91 87 #region persistence 92 88 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) { -
trunk/sources/HeuristicLab.Constraints/AndConstraint.cs
r40 r764 68 68 } 69 69 70 public override void Accept(IConstraintVisitor visitor) {71 visitor.Visit(this);72 }73 74 70 #region persistence 75 71 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) { -
trunk/sources/HeuristicLab.Constraints/ConstraintBase.cs
r2 r764 33 33 34 34 public abstract bool Check(IItem data); 35 36 public virtual void Accept(IConstraintVisitor visitor) {37 }38 35 } 39 36 } -
trunk/sources/HeuristicLab.Constraints/DoubleBoundedConstraint.cs
r344 r764 128 128 } 129 129 130 public override void Accept(IConstraintVisitor visitor) {131 visitor.Visit(this);132 }133 134 130 #region persistence 135 131 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) { -
trunk/sources/HeuristicLab.Constraints/FalseConstraint.cs
r2 r764 45 45 return false; 46 46 } 47 48 49 public override void Accept(IConstraintVisitor visitor) {50 visitor.Visit(this);51 }52 47 } 53 48 } -
trunk/sources/HeuristicLab.Constraints/HeuristicLab.Constraints.csproj
r582 r764 76 76 </Compile> 77 77 <Compile Include="ConstraintBase.cs" /> 78 <Compile Include="ConstraintVisitorBase.cs">79 <SubType>Code</SubType>80 </Compile>81 78 <Compile Include="DoubleBoundedConstraint.cs" /> 82 79 <Compile Include="DoubleBoundedConstraintView.cs"> … … 86 83 <DependentUpon>DoubleBoundedConstraintView.cs</DependentUpon> 87 84 </Compile> 88 <Compile Include="IConstraintVisitor.cs" />89 85 <Compile Include="IntBoundedConstraint.cs" /> 90 86 <Compile Include="FalseConstraintView.cs"> -
trunk/sources/HeuristicLab.Constraints/IntBoundedConstraint.cs
r2 r764 121 121 } 122 122 123 public override void Accept(IConstraintVisitor visitor) {124 visitor.Visit(this);125 }126 127 128 123 #region persistence 129 124 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) { -
trunk/sources/HeuristicLab.Constraints/IsIntegerConstraint.cs
r2 r764 50 50 return false; 51 51 } 52 53 public override void Accept(IConstraintVisitor visitor) {54 visitor.Visit(this);55 }56 57 52 } 58 53 } -
trunk/sources/HeuristicLab.Constraints/ItemTypeConstraint.cs
r40 r764 67 67 } 68 68 69 public override void Accept(IConstraintVisitor visitor) {70 visitor.Visit(this);71 }72 73 69 #region clone & persistence 74 70 public override object Clone(IDictionary<Guid, object> clonedObjects) { -
trunk/sources/HeuristicLab.Constraints/NotConstraint.cs
r2 r764 62 62 } 63 63 64 public override void Accept(IConstraintVisitor visitor) {65 visitor.Visit(this);66 }67 68 69 64 #region persistence 70 65 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) { -
trunk/sources/HeuristicLab.Constraints/NumberOfSubOperatorsConstraint.cs
r2 r764 73 73 } 74 74 75 public override void Accept(IConstraintVisitor visitor) {76 visitor.Visit(this);77 }78 79 80 75 #region persistence 81 76 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) { -
trunk/sources/HeuristicLab.Constraints/OrConstraint.cs
r40 r764 68 68 } 69 69 70 public override void Accept(IConstraintVisitor visitor) {71 visitor.Visit(this);72 }73 74 75 70 #region persistence 76 71 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) { -
trunk/sources/HeuristicLab.Constraints/SubOperatorsConstraintAnalyser.cs
r431 r764 38 38 public IList<IOperator> GetAllowedOperators(IOperator op, int childIndex) { 39 39 AndConstraint andConstraint = new AndConstraint(); 40 foreach (IConstraint constraint in op.Constraints) {40 foreach (IConstraint constraint in op.Constraints) { 41 41 andConstraint.Clauses.Add(constraint); 42 42 } 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 47 60 } 48 61 … … 51 64 // however we would need to switch the whole Constraints project to .NET 3.5 for that 52 65 private static IList<IOperator> Intersect(ICollection<IOperator> a, ICollection<IOperator> b) { 53 if (a.Count > b.Count) {66 if (a.Count > b.Count) { 54 67 return Intersect(b, a); 55 68 } … … 57 70 List<IOperator> intersection = new List<IOperator>(a.Count); 58 71 59 foreach (IOperator element in a) {60 if (InSet(element, b)) {72 foreach (IOperator element in a) { 73 if (InSet(element, b)) { 61 74 intersection.Add(element); 62 75 } … … 67 80 private static IList<IOperator> Union(ICollection<IOperator> a, ICollection<IOperator> b) { 68 81 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)) { 71 84 union.Add(candidateElement); 72 85 } … … 78 91 private static IList<IOperator> Substract(ICollection<IOperator> minuend, ICollection<IOperator> subtrahend) { 79 92 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)) { 82 95 difference.Add(element); 83 96 } … … 88 101 89 102 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) 92 105 return true; 93 106 } … … 96 109 #endregion 97 110 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 } 104 119 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)); 107 124 } 108 private ICollection<IOperator> possibleOperators;109 private int childIndex;125 return allowedOperators; 126 } 110 127 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 } 117 131 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 } 120 135 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); 162 141 } 163 142 } 164 #endregion165 143 } 166 144 } -
trunk/sources/HeuristicLab.Constraints/SubOperatorsTypeConstraint.cs
r467 r764 99 99 } 100 100 101 public override void Accept(IConstraintVisitor visitor) {102 visitor.Visit(this);103 }104 105 106 101 #region persistence 107 102 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) { -
trunk/sources/HeuristicLab.Constraints/TrueConstraint.cs
r2 r764 42 42 } 43 43 44 public override void Accept(IConstraintVisitor visitor) {45 visitor.Visit(this);46 }47 48 49 44 public override bool Check(IItem data) { 50 45 return true;
Note: See TracChangeset
for help on using the changeset viewer.