|
| 1 | +/****************************** |
| 2 | +* Copyright (c) 2003,2020 Kevin Lano |
| 3 | +* This program and the accompanying materials are made available under the |
| 4 | +* terms of the Eclipse Public License 2.0 which is available at |
| 5 | +* http://www.eclipse.org/legal/epl-2.0 |
| 6 | +* |
| 7 | +* SPDX-License-Identifier: EPL-2.0 |
| 8 | +* *****************************/ |
| 9 | +/* Package: CSTL */ |
| 10 | + |
| 11 | +import java.util.Vector; |
| 12 | + |
| 13 | +public class CGCondition |
| 14 | +{ String stereotype = ""; |
| 15 | + String variable = ""; |
| 16 | + boolean positive = true; |
| 17 | + |
| 18 | + public CGCondition() |
| 19 | + { } |
| 20 | + |
| 21 | + public CGCondition(String prop, String var) |
| 22 | + { stereotype = prop; |
| 23 | + variable = var; |
| 24 | + } |
| 25 | + |
| 26 | + public void setVariable(String v) |
| 27 | + { variable = v; } |
| 28 | + |
| 29 | + public void setStereotype(String st) |
| 30 | + { stereotype = st; } |
| 31 | + |
| 32 | + public void setPositive() |
| 33 | + { positive = true; } |
| 34 | + |
| 35 | + public void setNegative() |
| 36 | + { positive = false; } |
| 37 | + |
| 38 | + public String toString() |
| 39 | + { String res = variable; |
| 40 | + if (positive) { } |
| 41 | + else |
| 42 | + { res = res + " not"; } |
| 43 | + return res + " " + stereotype; |
| 44 | + } |
| 45 | + |
| 46 | + public static boolean conditionsSatisfied(Vector conditions, Vector args, Vector entities) |
| 47 | + { boolean res = true; |
| 48 | + for (int i = 0; i < args.size(); i++) |
| 49 | + { Object m = args.get(i); |
| 50 | + String var = "_" + (i+1); |
| 51 | + |
| 52 | + for (int j = 0; j < conditions.size(); j++) |
| 53 | + { CGCondition cond = (CGCondition) conditions.get(j); |
| 54 | + if (cond.variable != null && var.equals(cond.variable)) |
| 55 | + { if (m instanceof Type && |
| 56 | + cond.conditionSatisfied((Type) m, entities)) |
| 57 | + { } |
| 58 | + else if (m instanceof Expression && |
| 59 | + cond.conditionSatisfied((Expression) m, entities)) |
| 60 | + { } |
| 61 | + else if (m instanceof Statement && |
| 62 | + cond.conditionSatisfied((Statement) m, entities) ) |
| 63 | + { } |
| 64 | + else if (m instanceof Attribute && |
| 65 | + cond.conditionSatisfied((Attribute) m, entities) ) |
| 66 | + { } |
| 67 | + else if (m instanceof ModelElement && |
| 68 | + cond.stereotypeConditionSatisfied((ModelElement) m, entities)) |
| 69 | + { } |
| 70 | + else |
| 71 | + { return false; } |
| 72 | + System.out.println(">>> Condition " + cond + " is satisfied by " + m); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + return res; |
| 77 | + } |
| 78 | + |
| 79 | + public boolean stereotypeConditionSatisfied(ModelElement m, Vector entities) |
| 80 | + { if (m.hasStereotype(stereotype)) |
| 81 | + { return positive; } |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + public boolean conditionSatisfied(Type t, Vector entities) |
| 86 | + { if ("string".equals(stereotype.toLowerCase()) && t.isStringType()) |
| 87 | + { return positive; } |
| 88 | + if ("class".equals(stereotype.toLowerCase()) && t.isEntityType()) |
| 89 | + { return positive; } |
| 90 | + if ("void".equals(stereotype.toLowerCase()) && (t == null || t.isVoidType())) |
| 91 | + { return positive; } |
| 92 | + if ("enumerated".equals(stereotype.toLowerCase()) && t.isEnumeratedType()) |
| 93 | + { return positive; } |
| 94 | + if ("collection".equals(stereotype.toLowerCase()) && t.isCollectionType()) |
| 95 | + { return positive; } |
| 96 | + if ("sequence".equals(stereotype.toLowerCase()) && t.isSequenceType()) |
| 97 | + { return positive; } |
| 98 | + if ("set".equals(stereotype.toLowerCase()) && t.isSetType()) |
| 99 | + { return positive; } |
| 100 | + if ("class".equals(stereotype.toLowerCase()) && !(t.isEntityType())) |
| 101 | + { return !positive; } |
| 102 | + if ("void".equals(stereotype.toLowerCase()) && t != null && !t.isVoidType()) |
| 103 | + { return !positive; } |
| 104 | + if ("enumerated".equals(stereotype.toLowerCase()) && !(t.isEnumeratedType())) |
| 105 | + { return !positive; } |
| 106 | + if ("collection".equals(stereotype.toLowerCase()) && !(t.isCollectionType())) |
| 107 | + { return !positive; } |
| 108 | + if ("sequence".equals(stereotype.toLowerCase()) && !(t.isSequenceType())) |
| 109 | + { return !positive; } |
| 110 | + if ("set".equals(stereotype.toLowerCase()) && !(t.isSetType())) |
| 111 | + { return !positive; } |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + public boolean conditionSatisfied(Attribute a, Vector entities) |
| 116 | + { if ("primary".equals(stereotype.toLowerCase()) && a.isPrimaryAttribute()) |
| 117 | + { return positive; } |
| 118 | + if (a.hasStereotype(stereotype)) |
| 119 | + { return positive; } |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + public boolean conditionSatisfied(Expression e, Vector entities) |
| 124 | + { Type t = e.getType(); |
| 125 | + int kind = e.getUMLKind(); |
| 126 | + String edata = e + ""; |
| 127 | + Entity ent = (Entity) ModelElement.lookupByName(edata,entities); |
| 128 | + |
| 129 | + // if (ent != null) |
| 130 | + // { System.out.println(">> Expression " + e + " is a class"); } |
| 131 | + |
| 132 | + // System.out.println(">> Testing condition " + stereotype + " on expression " + e + " type = " + t); |
| 133 | + |
| 134 | + if (t == null) |
| 135 | + { System.err.println("!! ERROR: null type in: " + e); |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + String tname = t.getName(); |
| 140 | + |
| 141 | + if ("Set".equals(stereotype)) |
| 142 | + { if (positive) |
| 143 | + { return "Set".equals(tname); } |
| 144 | + else |
| 145 | + { return !("Set".equals(tname)); } |
| 146 | + } |
| 147 | + else if ("Sequence".equals(stereotype)) |
| 148 | + { if (positive) |
| 149 | + { return "Sequence".equals(tname); } |
| 150 | + else |
| 151 | + { return !("Sequence".equals(tname)); } |
| 152 | + } |
| 153 | + else if ("Map".equals(stereotype)) |
| 154 | + { if (positive) |
| 155 | + { return "Map".equals(tname); } |
| 156 | + else |
| 157 | + { return !("Map".equals(tname)); } |
| 158 | + } |
| 159 | + else if ("collection".equals(stereotype.toLowerCase())) |
| 160 | + { if (positive) |
| 161 | + { return ("Set".equals(tname) || "Sequence".equals(tname)); } |
| 162 | + else |
| 163 | + { return !("Set".equals(tname)) && !("Sequence".equals(tname)); } |
| 164 | + } |
| 165 | + else if ("String".equals(stereotype)) |
| 166 | + { if (positive) |
| 167 | + { return "String".equals(tname); } |
| 168 | + else |
| 169 | + { return !("String".equals(tname)); } |
| 170 | + } |
| 171 | + else if ("numeric".equals(stereotype)) |
| 172 | + { if (positive) |
| 173 | + { return t.isNumericType(); } |
| 174 | + else |
| 175 | + { return !(t.isNumericType()); } |
| 176 | + } |
| 177 | + else if ("object".equals(stereotype)) |
| 178 | + { if (positive) |
| 179 | + { return t.isEntityType() && ent == null; } |
| 180 | + else |
| 181 | + { return !(t.isEntityType() && ent == null); } |
| 182 | + } |
| 183 | + else if ("enumerated".equals(stereotype)) |
| 184 | + { if (positive) |
| 185 | + { return t.isEnumeratedType(); } |
| 186 | + else |
| 187 | + { return !(t.isEnumeratedType()); } |
| 188 | + } |
| 189 | + else if ("enumerationLiteral".equals(stereotype)) |
| 190 | + { if (positive) |
| 191 | + { return t.isEnumeratedType() && t.hasValue(e); } |
| 192 | + else |
| 193 | + { return !(t.isEnumeratedType() && t.hasValue(e)); } |
| 194 | + } |
| 195 | + else if ("classId".equals(stereotype)) |
| 196 | + { if (positive) |
| 197 | + { return kind == Expression.CLASSID; } |
| 198 | + else |
| 199 | + { return kind != Expression.CLASSID; } |
| 200 | + } |
| 201 | + else if ("value".equals(stereotype)) |
| 202 | + { if (positive) |
| 203 | + { return e.umlkind == Expression.VALUE; } |
| 204 | + else |
| 205 | + { return e.umlkind != Expression.VALUE; } |
| 206 | + } |
| 207 | + else if ("variable".equals(stereotype)) |
| 208 | + { if (positive) |
| 209 | + { return e.umlkind == Expression.VARIABLE; } |
| 210 | + else |
| 211 | + { return e.umlkind != Expression.VARIABLE; } |
| 212 | + } |
| 213 | + return false; |
| 214 | + } |
| 215 | + |
| 216 | + public boolean conditionSatisfied(Statement e, Vector entities) |
| 217 | + { if (e instanceof AssignStatement) |
| 218 | + { AssignStatement st = (AssignStatement) e; |
| 219 | + Expression left = st.getLeft(); |
| 220 | + return conditionSatisfied(left,entities); |
| 221 | + } |
| 222 | + return false; |
| 223 | + } // and for other kinds of statement also |
| 224 | +} |
| 225 | + |
0 commit comments