Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/SubOperatorsConstraintAnalyser.cs @ 257

Last change on this file since 257 was 155, checked in by gkronber, 17 years ago

merged FunctionsAndStructIdRefactoring-branch (r142, r143, r144, r145, r146, r147, r148, r149, r152, r153) back into the trunk (ticket #112)

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Constraints;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.Constraints {
30  public class SubOperatorsConstraintAnalyser {
31    private ICollection<IOperator> allPossibleOperators;
32
33    public ICollection<IOperator> AllPossibleOperators {
34      get { return allPossibleOperators; }
35      set { allPossibleOperators = value; }
36    }
37
38    public IList<IOperator> GetAllowedOperators(IOperator op, int childIndex) {
39
40      AndConstraint andConstraint = new AndConstraint();
41      foreach(IConstraint constraint in op.Constraints) {
42        andConstraint.Clauses.Add(constraint);
43      }
44
45      GetAllowedOperatorsVisitor visitor = new GetAllowedOperatorsVisitor(allPossibleOperators, childIndex);
46      andConstraint.Accept(visitor);
47
48      return visitor.AllowedOperators;
49    }
50
51    #region static set management methods
52    // better to use HashSets from .NET 3.5
53    // however we would need to switch the whole Constraints project to .NET 3.5 for that
54    private static IList<IOperator> Intersect(ICollection<IOperator> a, ICollection<IOperator> b) {
55      if(a.Count > b.Count) {
56        return Intersect(b, a);
57      }
58
59      List<IOperator> intersection = new List<IOperator>(a.Count);
60
61      foreach(IOperator element in a) {
62        if(InSet(element, b)) {
63          intersection.Add(element);
64        }
65      }
66      return intersection;
67    }
68
69    private static IList<IOperator> Union(ICollection<IOperator> a, ICollection<IOperator> b) {
70      List<IOperator> union = new List<IOperator>(a);
71      foreach(IOperator candidateElement in b) {
72        if(!InSet(candidateElement, union)) {
73          union.Add(candidateElement);
74        }
75      }
76
77      return union;
78    }
79
80    private static IList<IOperator> Substract(ICollection<IOperator> minuend, ICollection<IOperator> subtrahend) {
81      List<IOperator> difference = new List<IOperator>();
82      foreach(IOperator element in minuend) {
83        if(!InSet(element, subtrahend)) {
84          difference.Add(element);
85        }
86      }
87
88      return difference;
89    }
90
91    private static bool InSet(IOperator op, ICollection<IOperator> set) {
92      foreach(IOperator element in set) {
93        if(element==op ||
94          ((StringData)element.GetVariable("TypeId").Value).Data ==
95          ((StringData)op.GetVariable("TypeId").Value).Data) {
96          return true;
97        }
98      }
99
100      return false;
101    }
102    #endregion
103
104    #region visitor
105    /// <summary>
106    /// The visitor builds a set of allowed operators based on a tree of constraints.
107    /// </summary>
108    private class GetAllowedOperatorsVisitor : ConstraintVisitorBase {
109      private IList<IOperator> allowedOperators;
110
111      public IList<IOperator> AllowedOperators {
112        get { return allowedOperators; }
113      }
114      private ICollection<IOperator> possibleOperators;
115      private int childIndex;
116
117      public GetAllowedOperatorsVisitor(ICollection<IOperator> possibleOperators, int childIndex) {
118        // default is that all possible operators are allowed
119        allowedOperators = new List<IOperator>(possibleOperators);
120        this.possibleOperators = possibleOperators;
121        this.childIndex = childIndex;
122      }
123
124      public override void Visit(AndConstraint constraint) {
125        base.Visit(constraint);
126
127        // keep only the intersection of all subconstraints
128        foreach(ConstraintBase clause in constraint.Clauses) {
129          GetAllowedOperatorsVisitor visitor = new GetAllowedOperatorsVisitor(possibleOperators, childIndex);
130          clause.Accept(visitor);
131          allowedOperators = Intersect(allowedOperators, visitor.allowedOperators);
132        }
133      }
134     
135      public override void Visit(OrConstraint constraint) {
136        base.Visit(constraint);
137
138        // allowed operators is the union of all allowed operators as defined by the subconstraints
139        allowedOperators.Clear();
140
141        foreach(ConstraintBase clause in constraint.Clauses) {
142          GetAllowedOperatorsVisitor visitor = new GetAllowedOperatorsVisitor(possibleOperators, childIndex);
143          clause.Accept(visitor);
144          allowedOperators = Union(allowedOperators, visitor.allowedOperators);
145        }
146      }
147
148      public override void Visit(NotConstraint constraint) {
149        base.Visit(constraint);
150        GetAllowedOperatorsVisitor visitor = new GetAllowedOperatorsVisitor(possibleOperators, childIndex);
151        constraint.SubConstraint.Accept(visitor);
152
153        allowedOperators = Substract(possibleOperators, visitor.allowedOperators);
154      }
155
156      public override void Visit(AllSubOperatorsTypeConstraint constraint) {
157        base.Visit(constraint);
158
159        allowedOperators = Intersect(possibleOperators, constraint.AllowedSubOperators);
160      }
161
162      public override void Visit(SubOperatorTypeConstraint constraint) {
163        if(childIndex != constraint.SubOperatorIndex.Data) {
164          allowedOperators = new List<IOperator>(possibleOperators);
165        } else {
166          allowedOperators = Intersect(possibleOperators, constraint.AllowedSubOperators);
167        }
168      }
169    }
170    #endregion
171  }
172}
Note: See TracBrowser for help on using the repository browser.