Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 765 was 764, checked in by gkronber, 16 years ago

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

File size: 5.5 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      AndConstraint andConstraint = new AndConstraint();
40      foreach (IConstraint constraint in op.Constraints) {
41        andConstraint.Clauses.Add(constraint);
42      }
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
60    }
61
62    #region static set management methods
63    // better to use HashSets from .NET 3.5
64    // however we would need to switch the whole Constraints project to .NET 3.5 for that
65    private static IList<IOperator> Intersect(ICollection<IOperator> a, ICollection<IOperator> b) {
66      if (a.Count > b.Count) {
67        return Intersect(b, a);
68      }
69
70      List<IOperator> intersection = new List<IOperator>(a.Count);
71
72      foreach (IOperator element in a) {
73        if (InSet(element, b)) {
74          intersection.Add(element);
75        }
76      }
77      return intersection;
78    }
79
80    private static IList<IOperator> Union(ICollection<IOperator> a, ICollection<IOperator> b) {
81      List<IOperator> union = new List<IOperator>(a);
82      foreach (IOperator candidateElement in b) {
83        if (!InSet(candidateElement, union)) {
84          union.Add(candidateElement);
85        }
86      }
87
88      return union;
89    }
90
91    private static IList<IOperator> Substract(ICollection<IOperator> minuend, ICollection<IOperator> subtrahend) {
92      List<IOperator> difference = new List<IOperator>();
93      foreach (IOperator element in minuend) {
94        if (!InSet(element, subtrahend)) {
95          difference.Add(element);
96        }
97      }
98
99      return difference;
100    }
101
102    private static bool InSet(IOperator op, ICollection<IOperator> set) {
103      foreach (IOperator element in set) {
104        if (element == op)
105          return true;
106      }
107      return false;
108    }
109    #endregion
110
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    }
119
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));
124      }
125      return allowedOperators;
126    }
127
128    public IList<IOperator> GetAllowedOperators(NotConstraint constraint, int childIndex) {
129      return Substract(allPossibleOperators, GetAllowedOperators(constraint.SubConstraint, childIndex));
130    }
131
132    public IList<IOperator> GetAllowedOperators(AllSubOperatorsTypeConstraint constraint, int childIndex) {
133      return Intersect(allPossibleOperators, constraint.AllowedSubOperators);
134    }
135
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);
141      }
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.