001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: package de.uka.ilkd.key.casetool.patternimplementor.patterns;
012:
013: import de.uka.ilkd.key.casetool.patternimplementor.*;
014:
015: public class Observer implements AbstractPatternImplementor {
016: private PIParameterGroup observer;
017: private ConstraintMechanism constraintMechanism;
018:
019: public PIParameterGroup getParameters() {
020: if (observer == null) {
021: createParameters();
022: }
023:
024: return observer;
025: }
026:
027: protected void createParameters() {
028: observer = new PIParameterGroup("Observer", "Observer");
029:
030: PIParameterString observer_class = new PIParameterString(
031: "Observer_class", "Observer", "AbstractObserver");
032: //PIParameterString concreteobserver_class = new PIParameterString("ConcreteObserver_class",
033: // "ConcreteObserver", "ConcreteObserver");
034: PIParameterString subject_class = new PIParameterString(
035: "Subject_class", "Subject", "Subject");
036: PIParameterString concretesubject_class = new PIParameterString(
037: "ConcreteSubject_class", "ConcreteSubject",
038: "ConcreteSubject");
039: PIParameterString observers_ass = new PIParameterString(
040: "observers_ass", "Observers Association", "observers");
041: PIParameterString subject_ass = new PIParameterString(
042: "subject_ass", "Subject Association", "subject");
043:
044: PIParameterMultiString concreteobservers = new PIParameterMultiString(
045: "ConcreteObserver_class", "Concrete observers",
046: "ConcreteObserver1 ConcreteObserver2");
047: //PIParameterDependent cp = new PIParameterDependent("ConcreteProduct",
048: // "Concrete products", cf);
049:
050: observer.add(observer_class);
051: //observer.add(concreteobserver_class);
052: observer.add(concreteobservers);
053: observer.add(subject_class);
054: observer.add(concretesubject_class);
055: observer.add(observers_ass);
056: observer.add(subject_ass);
057: constraintMechanism = new ConstraintMechanism(
058: "Observer.constraints", observer, this );
059: }
060:
061: private SourceCode createObserver() {
062: String observerName = observer.get("Observer_class").getValue();
063: String subjectAssName = observer.get("subject_ass").getValue();
064: String concSubjName = observer.get("ConcreteSubject_class")
065: .getValue();
066:
067: SourceCode source = new SourceCode();
068:
069: source.beginClass(observerName);
070: source.add("/**");
071: source.add(constraintMechanism.getConstraints(" * ",
072: "Observer", observerName));
073: source.add(" */");
074: source.add("public class " + observerName + " {");
075: source.add("\tpublic void update() {}");
076: source.add("\t/**");
077: source.add("\t * @supplierCardinality 0..1");
078: source.add("\t * @supplierRole " + subjectAssName);
079: source.add("\t */");
080: source.add("\tprivate " + concSubjName + " lnk" + concSubjName
081: + ";");
082: source.add("}");
083:
084: return source;
085: }
086:
087: private SourceCode createConcreteObservers() {
088: String[] concObsName = ((PIParameterMultiString) observer
089: .get("ConcreteObserver_class")).getValues();
090: String observerName = observer.get("Observer_class").getValue();
091: String subjectAssName = observer.get("subject_ass").getValue();
092: String concSubjName = observer.get("ConcreteSubject_class")
093: .getValue();
094:
095: SourceCode source = new SourceCode();
096:
097: for (int i = 0; i < concObsName.length; i++) {
098: source.beginClass(concObsName[i]);
099: source.add("/**");
100: source.add(constraintMechanism.getConstraints(" * ",
101: "ConcreteObserver", concObsName[i]));
102: source.add(" */");
103: source.add("public class " + concObsName[i] + " extends "
104: + observerName + " {");
105: source.add("\tpublic void update() {}");
106: source.add("\t/**");
107: source.add("\t * @supplierCardinality 0..1");
108: source.add("\t * @supplierRole " + subjectAssName);
109: source.add("\t */");
110: source.add("\tprivate " + concSubjName + " lnk"
111: + concSubjName + ";");
112: source.add("}");
113: }
114:
115: return source; //classes;
116: }
117:
118: private SourceCode createConcreteObserver() {
119: String concObsName = observer.get("ConcreteObserver_class")
120: .getValue();
121: String observerName = observer.get("Observer_class").getValue();
122: String subjectAssName = observer.get("subject_ass").getValue();
123: String concSubjName = observer.get("ConcreteSubject_class")
124: .getValue();
125:
126: SourceCode source = new SourceCode();
127:
128: source.beginClass(concObsName);
129: source.add("/**");
130: source.add(constraintMechanism.getConstraints(" * ",
131: "ConcreteObserver", concObsName));
132: source.add(" */");
133: source.add("public class " + concObsName + " extends "
134: + observerName + " {");
135: source.add("\tpublic void update() {}");
136: source.add("\t/**");
137: source.add("\t * @supplierCardinality 0..1");
138: source.add("\t * @supplierRole " + subjectAssName);
139: source.add("\t */");
140: source.add("\tprivate " + concSubjName + " lnk" + concSubjName
141: + ";");
142: source.add("}");
143:
144: return source; //classes;
145: }
146:
147: private SourceCode createSubject() {
148: String subjectName = observer.get("Subject_class").getValue();
149: String observerName = observer.get("Observer_class").getValue();
150: String observersAssName = observer.get("observers_ass")
151: .getValue();
152:
153: SourceCode source = new SourceCode();
154:
155: source.beginClass(subjectName);
156: source.add("/**");
157: source.add(constraintMechanism.getConstraints(" * ", "Subject",
158: subjectName));
159: source.add(" */");
160: source.add("public class " + subjectName + " {");
161: source.add("\tpublic void attach(" + observerName + " o) {}");
162: source.add("\tpublic void detach(" + observerName + " o) {}");
163: source.add("\tpublic void notify() {}");
164: source.add("\t/**");
165: source.add("\t * @supplierCardinality 0..*");
166: source.add("\t * @supplierRole " + observersAssName);
167: source.add("\t */");
168: source.add("\tprivate " + observerName + " lnk" + observerName
169: + ";");
170: source.add("}");
171:
172: return source; //classes;
173: }
174:
175: private SourceCode createConcreteSubject() {
176: String concSubjName = observer.get("ConcreteSubject_class")
177: .getValue();
178: String subjectName = observer.get("Subject_class").getValue();
179:
180: SourceCode source = new SourceCode();
181:
182: source.beginClass(concSubjName);
183: source.add("/**");
184: source.add(constraintMechanism.getConstraints(" * ",
185: "ConcreteSubject", concSubjName));
186: source.add(" */");
187: source.add("public class " + concSubjName + " extends "
188: + subjectName + " {");
189: source.add("\tpublic Object getState() {}");
190: source.add("\tpublic void setState(Object state) {}");
191: source.add("}");
192:
193: return source;
194: }
195:
196: public SourceCode getImplementation() {
197: if (observer == null) {
198: System.err
199: .println("ERROR - Observer.getImplementation : observer == null");
200:
201: return null;
202: }
203:
204: SourceCode code = new SourceCode();
205: code.add(createObserver());
206: code.add(createSubject());
207: code.add(createConcreteObservers());
208: code.add(createConcreteSubject());
209:
210: return code;
211: }
212:
213: public String getName() {
214: return "Creational Patterns:Observer";
215: }
216: }
|