Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/AllSubOperatorsTypeConstraint.cs @ 626

Last change on this file since 626 was 467, checked in by gkronber, 16 years ago

worked on #237 (FunctionLibraryInjector that injects default function libraries with a few parameters)

  • added operator
  • added helper functions in Variable, Differential and Constraints to support the function library injector
File size: 3.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.Data;
27using System.Xml;
28using System.Diagnostics;
29
30namespace HeuristicLab.Constraints {
31  public class AllSubOperatorsTypeConstraint : ConstraintBase {
32
33    private SubOperatorTypeConstraint groupConstraint;
34    public IList<IOperator> AllowedSubOperators {
35      get {
36        return groupConstraint.AllowedSubOperators;
37      }
38    }
39
40    public override string Description {
41      get { return "All sub-operators have to be elements of a pre-defined group."; }
42    }
43
44    public AllSubOperatorsTypeConstraint()
45      : base() {
46      groupConstraint = new SubOperatorTypeConstraint();
47    }
48
49    public void AddOperator(IOperator op) {
50      groupConstraint.AddOperator(op);
51      FireChanged();
52    }
53
54    public void RemoveOperator(IOperator op) {
55      groupConstraint.RemoveOperator(op);
56      FireChanged();
57    }
58
59    public override bool Check(IItem data) {
60      IOperator op = data as IOperator;
61      if(data == null) return false;
62
63      for(int i = 0; i < op.SubOperators.Count; i++) {
64        groupConstraint.SubOperatorIndex.Data = i;
65        if(groupConstraint.Check(data) == false) {
66          return false;
67        }
68      }
69      return true;
70    }
71
72    public void Clear() {
73      groupConstraint.Clear();
74    }
75
76    public override object Clone(IDictionary<Guid, object> clonedObjects) {
77      AllSubOperatorsTypeConstraint clone = new AllSubOperatorsTypeConstraint();
78      clonedObjects.Add(Guid, clone);
79      clone.groupConstraint = (SubOperatorTypeConstraint)Auxiliary.Clone(groupConstraint, clonedObjects);
80      return clone;
81    }
82
83    public override IView CreateView() {
84      return new AllSubOperatorsTypeConstraintView(groupConstraint);
85    }
86
87    public override void Accept(IConstraintVisitor visitor) {
88      visitor.Visit(this);
89    }
90
91    #region persistence
92    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
93      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
94      XmlNode subOperatorsNode = PersistenceManager.Persist("SubOperatorsGroupConstraint", groupConstraint, document, persistedObjects);
95      node.AppendChild(subOperatorsNode);
96
97      return node;
98    }
99
100    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
101      base.Populate(node, restoredObjects);
102      groupConstraint = (SubOperatorTypeConstraint)PersistenceManager.Restore(node.SelectSingleNode("SubOperatorsGroupConstraint"), restoredObjects);
103    }
104    #endregion persistence
105  }
106}
Note: See TracBrowser for help on using the repository browser.