001 /*
002 * SimuCS - Simulator to use with Classifier Systems
003 * MSc project - Oxford University
004 * by Benoit Isaac - Summer 2005
005 */
006
007 package simuLCS;
008 import java.util.Comparator;
009 import java.util.HashSet;
010 import java.util.Iterator;
011 import java.util.Set;
012 import java.util.TreeSet;
013
014 /**
015 * Set of classifiers (might be ordered) used to describe a behaviour or the population inside the LCS.
016 * @see Classifier
017 * @author Benoit
018 *
019 */
020 public class ClassifierSet {
021
022
023 protected Template template = null;
024 protected boolean isSorted;
025
026 /**
027 * The classifiers are in a Java Set.
028 */
029 protected Set classifiers;
030
031 /**
032 * Constructs a non sorted Set of Classifiers.
033 * @param t the template of the classifiers
034 */
035 public ClassifierSet(Template t) {
036 this(t, false);
037 }
038
039 /**
040 * Constructs a Set of Classifiers, sorted if isSort = true, non sorted otherwise.
041 * @param t the template of the classifiers
042 * @param isSort determines if the Set has to be sorted.
043 */
044 public ClassifierSet(Template t, boolean isSort) {
045 template = t;
046 isSorted = isSort;
047 if (isSorted) {
048 classifiers = new TreeSet();
049 } else {
050 classifiers = new HashSet();
051 }
052 }
053
054 /**
055 * @param template
056 * @param order
057 */
058 public ClassifierSet(Template template, Comparator order) {
059 isSorted = true;
060 classifiers = new TreeSet(order);
061 }
062
063
064
065 public void addClassifier(Classifier c) {
066 classifiers.add(c);
067 }
068
069 public void addClassifierSet(ClassifierSet cs){
070 Iterator iter = cs.getIterator();
071 while(iter.hasNext())
072 {
073 addClassifier((Classifier) iter.next());
074 }
075 }
076
077 public void removeClassifier(Classifier c) {
078 classifiers.remove(c);
079 }
080
081 /**
082 * Returns true if this Set contains the classifier <code>c</code>
083 * @param c
084 * @return
085 */
086 public boolean contains(Classifier c) {
087 return classifiers.contains(c);
088 }
089
090 public Iterator getIterator() {
091 Iterator iter = classifiers.iterator();
092 return iter;
093 }
094
095 public int getSize() {
096 return classifiers.size();
097 }
098
099 /**
100 * Get an array with all the classifiers in this set
101 * @return
102 */
103 public Classifier[] getClassifiers() {
104 Classifier[] c = new Classifier[0];
105 return (Classifier[]) classifiers.toArray(c);
106 }
107
108 /**
109 * Extract the classifiers of the set which have a Condition part
110 * that satisfies the condToSatisfy
111 * i.e. each bit of the condition part of classifier is either '#'
112 * or the same symbol (0 or 1) as the condToSatisfy one.
113 * @param condToSatisfy
114 * @return an array of Classifiers satisfying the condition
115 */
116 public ClassifierSet getMatchSet(String condToSatisfy) {
117 ClassifierSet satisfiedClassifiers =
118 new ClassifierSet(this.getTemplate());
119 Iterator iter = classifiers.iterator();
120 int i;
121 boolean isSatisfied;
122
123 while (iter.hasNext()) {
124 Classifier c = (Classifier) iter.next();
125 if (c.match(condToSatisfy))
126 satisfiedClassifiers.addClassifier(c);
127
128 }
129
130 return satisfiedClassifiers;
131 }
132
133 /**
134 * Operate a selection among those Classifiers (the satisfied)
135 * By default, no selection, all the classifiers are kept
136 * Should be overriden by subclasses
137 */
138 public ClassifierSet getActionSet()
139 {
140 return this ;
141 }
142
143
144
145 public Template getTemplate() {
146 return template;
147 }
148
149
150 public void setTemplate(Template template) {
151 this.template = template;
152 }
153
154 /**
155 * Returns the list of the Classifiers to print them
156 */
157 public String toString() {
158 StringBuffer b = new StringBuffer();
159 Iterator iter = classifiers.iterator();
160 while (iter.hasNext()) {
161 Classifier c = (Classifier) iter.next();
162 b.append(c.toString());
163 b.append("\n");
164 }
165 return b.toString();
166
167 }
168 }