001: package com.xoetrope.survey;
002:
003: import java.awt.event.ActionListener;
004: import java.util.Vector;
005: import java.util.Enumeration;
006:
007: /**
008: * DefaultRule is a set of conditions being used by the DefaultRuleEngine.
009: *
010: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
011: * the GNU Public License (GPL), please see license.txt for more details. If
012: * you make commercial use of this software you must purchase a commercial
013: * license from Xoetrope.</p>
014: * <p> $Revision: 1.5 $</p>
015: */
016: public class DefaultRule {
017: protected int id;
018: protected String name;
019: protected QuestionGroup group;
020: protected QuestionGroup target;
021: protected Vector conditions;
022:
023: /**
024: * Creates a new instance of the DefaultRule
025: * @param i the if of the rule
026: * @param g question group that this rule belongs to
027: * @param n the name of the rule
028: */
029: public DefaultRule(int i, QuestionGroup g, String n) {
030: this (i, g, null, n);
031: }
032:
033: /**
034: * Creates a new instance of the default rule.
035: * @param i the id of this rule
036: * @param g question group that this rule belongs to
037: * @param t question group that this rule leads to
038: */
039: public DefaultRule(int i, QuestionGroup g, QuestionGroup t, String n) {
040: id = i;
041: group = g;
042: target = t;
043: name = (n != null ? n : "rule " + id);
044: conditions = new Vector();
045: }
046:
047: /**
048: * Gets the name of this rule
049: * @rule the name
050: */
051: public String getName() {
052: return name;
053: }
054:
055: /**
056: * Sets the name of this rule
057: * @param n the new name of this rule
058: */
059: public void setName(String n) {
060: name = n;
061: }
062:
063: /**
064: * Gets the id of this rule
065: * @return the id
066: */
067: public int getId() {
068: return id;
069: }
070:
071: /**
072: * Gets the question group that this rule belongs to
073: * @return the question group.
074: */
075: public QuestionGroup getGroup() {
076: return group;
077: }
078:
079: /**
080: * Gets the question group that this rule leads to
081: * @reutrn the question group
082: */
083: public QuestionGroup getTarget() {
084: return target;
085: }
086:
087: /**
088: * Sets the new question group that this
089: * rule will lead to.
090: * @param the new question group.
091: */
092: public void setTarget(QuestionGroup at) {
093: target = at;
094: }
095:
096: /**
097: * Sets the new id of this rule
098: * @param i the new id
099: */
100: public void setId(int i) {
101: id = i;
102: }
103:
104: /**
105: * Gets the conditions being contained by this rule
106: * @return Vector containing conditions
107: */
108: public Vector getConditions() {
109: return conditions;
110: }
111:
112: /**
113: * Gets the condition for the specified question.
114: * @param question question whose condition is to
115: * be obtained
116: * @return condition for the specified question.
117: */
118: public Condition getCondition(Question question) {
119: Condition condition = null;
120: Enumeration enumConditions = conditions.elements();
121: while (enumConditions.hasMoreElements() && (condition == null)) {
122: Condition c = (Condition) enumConditions.nextElement();
123: if (c.getQuestion().equals(question))
124: condition = c;
125: }
126: return condition;
127: }
128:
129: /**
130: * Creates and sdds the new condition to this rule
131: * @param question the question of the condition
132: * which is to be added
133: * @param option the option, the answer for which
134: * is specified
135: * @param answer answer for the option
136: * @return the new condition
137: */
138: public Condition addCondition(Question question, Option option,
139: String answer) {
140: Condition condition = getCondition(question);
141: if (condition == null) {
142: condition = new Condition(question);
143: conditions.add(condition);
144: }
145: condition.addAnswer(option, answer);
146: return condition;
147: }
148:
149: /**
150: * Removes the specified conditon from this rule
151: * @param condition the condition to be removed
152: */
153: public void removeCondtion(Condition condition) {
154: conditions.remove(condition);
155: }
156:
157: /**
158: * Removes all conditions of the speicifed question
159: * @param questionId the id of the question whose
160: * conditions will be removed
161: */
162: public void removeCondition(int questionId) {
163: Enumeration enumeration = conditions.elements();
164: while (enumeration.hasMoreElements()) {
165: Condition condition = (Condition) enumeration.nextElement();
166: if (condition.getQuestion().getId() == questionId)
167: conditions.remove(condition);
168: }
169: }
170:
171: /**
172: * Check whether specified users responses match the conditions
173: * contained in this rule.
174: * @param userResponses responses given by the user
175: * @return id of the target questions group if the responses match,
176: * -1 otherwise
177: */
178: public int checkResponses(Vector userResponses) {
179: if (target == null)
180: return -1;
181:
182: boolean rf = true;
183: Enumeration enumResponses = conditions.elements();
184: while (enumResponses.hasMoreElements() && rf) {
185: Condition response = (Condition) enumResponses
186: .nextElement();
187: rf = false;
188: Enumeration enumUserResponses = userResponses.elements();
189: while (enumUserResponses.hasMoreElements() && !rf) {
190: Condition userResponse = (Condition) enumUserResponses
191: .nextElement();
192: rf |= response.equals(userResponse);
193: }
194: }
195:
196: return (rf ? target.getId() : -1);
197: }
198:
199: public boolean equals(Object o) {
200: if (!(o instanceof DefaultRule))
201: return false;
202: DefaultRule rule = (DefaultRule) o;
203: return (id == rule.getId());
204: }
205:
206: }
|