- Timestamp:
- 11/15/08 19:55:13 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.