001: /**
002: * <copyright>
003: * </copyright>
004: *
005: * $Id$
006: */package newprocess.impl;
007:
008: import java.util.Collection;
009: import java.util.Iterator;
010:
011: import newprocess.AndOperator;
012: import newprocess.ConditionTerm;
013: import newprocess.Expression;
014: import newprocess.NewprocessPackage;
015: import newprocess.OperatorTerm;
016: import newprocess.Root;
017: import newprocess.Term;
018:
019: import org.eclipse.emf.common.util.EList;
020: import org.eclipse.emf.ecore.EClass;
021: import org.eclipse.emf.ecore.util.EObjectResolvingEList;
022:
023: /**
024: * <!-- begin-user-doc -->
025: * An implementation of the model object '<em><b>Operator Term</b></em>'.
026: * <!-- end-user-doc -->
027: * <p>
028: * The following features are implemented:
029: * <ul>
030: * <li>{@link newprocess.impl.OperatorTermImpl#getSubTerm <em>Sub Term</em>}</li>
031: * </ul>
032: * </p>
033: *
034: * @generated
035: */
036: public class OperatorTermImpl extends TermImpl implements OperatorTerm {
037: /**
038: * The cached value of the '{@link #getSubTerm() <em>Sub Term</em>}' reference list.
039: * <!-- begin-user-doc -->
040: * <!-- end-user-doc -->
041: * @see #getSubTerm()
042: * @generated
043: * @ordered
044: */
045: protected EList<Term> subTerm = null;
046:
047: /**
048: * <!-- begin-user-doc -->
049: * <!-- end-user-doc -->
050: * @generated
051: */
052: protected OperatorTermImpl() {
053: super ();
054: }
055:
056: /**
057: * <!-- begin-user-doc -->
058: * <!-- end-user-doc -->
059: * @generated
060: */
061: @Override
062: protected EClass eStaticClass() {
063: return NewprocessPackage.Literals.OPERATOR_TERM;
064: }
065:
066: /**
067: * <!-- begin-user-doc -->
068: * <!-- end-user-doc -->
069: * @generated
070: */
071: public EList<Term> getSubTerm() {
072: if (subTerm == null) {
073: subTerm = new EObjectResolvingEList<Term>(Term.class, this ,
074: NewprocessPackage.OPERATOR_TERM__SUB_TERM);
075: }
076: return subTerm;
077: }
078:
079: /**
080: * <!-- begin-user-doc -->
081: * get the level of this operator or 0 if not correctly linked.
082: * <!-- end-user-doc -->
083: * @generated NOT
084: * @author sh
085: */
086: public int getLevel() {
087: int level = 0;
088: OperatorTerm operator = this ;
089: Expression condition = (Expression) this .eContainer();
090: Root root = condition.getHasRoot();
091: if (root.getTerm() == null)
092: return 0; // no operator is connected to root
093: ++level; // first is guaranteed
094: while (operator != root.getTerm()) {
095: operator = getParentTerm(operator); // get the parent
096: if (operator == null)
097: return 0; // no parent found means: disconnected somewhere
098: ++level;
099: }
100: return level;
101: }
102:
103: /**
104: * getTermLevel needs to find parents
105: * @param op
106: * @return a parent or null
107: * @author ts
108: */
109: private OperatorTerm getParentTerm(OperatorTerm op) {
110: Iterator<Term> it = ((Expression) op.eContainer())
111: .getHasTerms().iterator();
112: while (it.hasNext()) {
113: Term term = it.next();
114: if (term instanceof OperatorTerm
115: && ((OperatorTerm) term).getSubTerm().contains(op))
116: return (OperatorTerm) term;
117: }
118: return null; // no parent found
119: }
120:
121: /**
122: * <!-- begin-user-doc -->
123: * each operator must only have one incoming link.
124: * <!-- end-user-doc -->
125: * @generated NOT
126: * @author sh
127: */
128: public boolean acceptIncomingLink() {
129: if (((Expression) eContainer()).getHasRoot().getTerm() == this )
130: return false;
131:
132: Iterator<Term> it = ((Expression) eContainer()).getHasTerms()
133: .iterator();
134: while (it.hasNext()) {
135: Term term = it.next();
136: if (term instanceof OperatorTerm
137: && term != this
138: && ((OperatorTerm) term).getSubTerm()
139: .contains(this ))
140: return false;
141: }
142: return true;
143: }
144:
145: /**
146: * <!-- begin-user-doc -->
147: * <!-- end-user-doc -->
148: * @generated
149: */
150: @Override
151: public Object eGet(int featureID, boolean resolve, boolean coreType) {
152: switch (featureID) {
153: case NewprocessPackage.OPERATOR_TERM__SUB_TERM:
154: return getSubTerm();
155: }
156: return super .eGet(featureID, resolve, coreType);
157: }
158:
159: /**
160: * <!-- begin-user-doc -->
161: * <!-- end-user-doc -->
162: * @generated
163: */
164: @SuppressWarnings("unchecked")
165: @Override
166: public void eSet(int featureID, Object newValue) {
167: switch (featureID) {
168: case NewprocessPackage.OPERATOR_TERM__SUB_TERM:
169: getSubTerm().clear();
170: getSubTerm().addAll((Collection<? extends Term>) newValue);
171: return;
172: }
173: super .eSet(featureID, newValue);
174: }
175:
176: /**
177: * <!-- begin-user-doc -->
178: * <!-- end-user-doc -->
179: * @generated
180: */
181: @Override
182: public void eUnset(int featureID) {
183: switch (featureID) {
184: case NewprocessPackage.OPERATOR_TERM__SUB_TERM:
185: getSubTerm().clear();
186: return;
187: }
188: super .eUnset(featureID);
189: }
190:
191: /**
192: * <!-- begin-user-doc -->
193: * <!-- end-user-doc -->
194: * @generated
195: */
196: @Override
197: public boolean eIsSet(int featureID) {
198: switch (featureID) {
199: case NewprocessPackage.OPERATOR_TERM__SUB_TERM:
200: return subTerm != null && !subTerm.isEmpty();
201: }
202: return super .eIsSet(featureID);
203: }
204:
205: /**
206: * @see de.wilken.mod.process.impl.AbstractTermImpl#getExpression()
207: *
208: * build a expression by navigation over the subterms.
209: *
210: * @generated NOT
211: * @author sh
212: */
213: @Override
214: public String getExpression() {
215: String operator;
216: if (this instanceof AndOperator)
217: operator = " && ";
218: else
219: operator = " || ";
220:
221: String expr = null;
222: Iterator<Term> it = getSubTerm().iterator();
223: while (it.hasNext()) {
224: Term term = it.next();
225: String subExpr = term.getExpression();
226:
227: // add brackets if complex expression
228: if (term instanceof OperatorTerm
229: && ((OperatorTerm) term).getSubTerm().size() > 1)
230: if (subExpr != null)
231: subExpr = "(".concat(subExpr).concat(")");
232:
233: if (term instanceof ConditionTerm) {
234: ConditionTerm port = (ConditionTerm) term;
235: if (port.getConditionProxy() != null) {
236: if (port.getConditionProxy().getProxyName() != null)
237: subExpr = port.getConditionProxy()
238: .getProxyName();
239: }
240: }
241:
242: if (expr == null)
243: expr = subExpr;
244: // add operator
245: else {
246: if (subExpr != null)
247: expr = expr.concat(operator).concat(subExpr);
248: }
249:
250: }
251: return expr;
252: }
253:
254: } //OperatorTermImpl
|