|
| 1 | +/***************************************************************************** |
| 2 | + * SysML 2 Pilot Implementation, PlantUML Visualization |
| 3 | + * Copyright (c) 2022 Mgnite Inc. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of theGNU Lesser General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + * |
| 18 | + * @license LGPL-3.0-or-later <http://spdx.org/licenses/LGPL-3.0-or-later> |
| 19 | + * |
| 20 | + * Contributors: |
| 21 | + * Hisashi Miyashita, Mgnite Inc. |
| 22 | + * |
| 23 | + *****************************************************************************/ |
| 24 | + |
| 25 | +package org.omg.sysml.plantuml; |
| 26 | + |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import org.omg.sysml.lang.sysml.Feature; |
| 30 | +import org.omg.sysml.lang.sysml.Membership; |
| 31 | +import org.omg.sysml.lang.sysml.Namespace; |
| 32 | +import org.omg.sysml.lang.sysml.Type; |
| 33 | + |
| 34 | +class InheritKey { |
| 35 | + public final Type[] keys; |
| 36 | + private final boolean isDirect; |
| 37 | + |
| 38 | + private static boolean isBelonging(Type typ, Feature f) { |
| 39 | + if (typ.getOwnedFeature().contains(f)) return true; |
| 40 | + if (typ.getInheritedFeature().contains(f)) return true; |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + private static boolean isBelonging(Type typ, Membership ms) { |
| 45 | + if (typ.getOwnedMembership().contains(ms)) return true; |
| 46 | + if (typ.getInheritedMembership().contains(ms)) return true; |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public int hashCode() { |
| 52 | + int code = 0; |
| 53 | + for (Type t: keys) { |
| 54 | + code ^= t.hashCode(); |
| 55 | + } |
| 56 | + return code; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean equals(Object o) { |
| 61 | + if (!(o instanceof InheritKey)) return false; |
| 62 | + InheritKey ki = (InheritKey) o; |
| 63 | + int len = keys.length; |
| 64 | + if (len != ki.keys.length) return false; |
| 65 | + for (int i = 0; i < len; i++) { |
| 66 | + if (!keys[i].equals(ki.keys[i])) return false; |
| 67 | + } |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + // where ctx is (n1, n2, ..., nk), keyType is the nearest enclosing type owning `ref' feature; |
| 72 | + private static int findKeyType(List<Namespace> ctx, Feature ref) { |
| 73 | + for (int i = ctx.size() - 1; i >=0; i--) { |
| 74 | + Namespace ns = ctx.get(i); |
| 75 | + if (!(ns instanceof Type)) continue; |
| 76 | + Type typ = (Type) ns; |
| 77 | + if (isBelonging(typ, ref)) return i; |
| 78 | + } |
| 79 | + return -1; |
| 80 | + } |
| 81 | + |
| 82 | + private static int findKeyType(List<Namespace> ctx, Membership ref) { |
| 83 | + for (int i = ctx.size() - 1; i >=0; i--) { |
| 84 | + Namespace ns = ctx.get(i); |
| 85 | + if (!(ns instanceof Type)) continue; |
| 86 | + Type typ = (Type) ns; |
| 87 | + if (isBelonging(typ, ref)) return i; |
| 88 | + } |
| 89 | + return -1; |
| 90 | + } |
| 91 | + |
| 92 | + private static void fill(Type[] keys, List<Namespace> ctx, List<Integer> inheritIdices, int i) { |
| 93 | + for (int k = 0; k < i; k++) { |
| 94 | + int idx = inheritIdices.get(k); |
| 95 | + // It must not cause ClassCastException |
| 96 | + Type typ = (Type) ctx.get(idx); |
| 97 | + keys[k] = typ; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private InheritKey(List<Namespace> ctx, List<Integer> inheritIdices, int i) { |
| 102 | + Type[] keys = new Type[i]; |
| 103 | + fill(keys, ctx, inheritIdices, i); |
| 104 | + this.keys = keys; |
| 105 | + this.isDirect = true; |
| 106 | + } |
| 107 | + |
| 108 | + private InheritKey(List<Namespace> ctx, List<Integer> inheritIdices, int i, Type tail) { |
| 109 | + Type[] keys = new Type[i + 1]; |
| 110 | + fill(keys, ctx, inheritIdices, i); |
| 111 | + keys[i] = tail; |
| 112 | + this.keys = keys; |
| 113 | + this.isDirect = false; |
| 114 | + } |
| 115 | + |
| 116 | + private InheritKey(Type tail) { |
| 117 | + Type[] keys = new Type[1]; |
| 118 | + keys[0] = tail; |
| 119 | + this.keys = keys; |
| 120 | + this.isDirect = false; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public String toString() { |
| 125 | + StringBuilder sb = new StringBuilder(); |
| 126 | + if (keys.length == 0) return "Empty InherityKey"; |
| 127 | + sb.append("InheritKey: ["); |
| 128 | + sb.append(keys[0].getName()); |
| 129 | + for (int i = 1; i < keys.length; i++) { |
| 130 | + sb.append(", "); |
| 131 | + sb.append(keys[i].getName()); |
| 132 | + } |
| 133 | + sb.append(']'); |
| 134 | + return sb.toString(); |
| 135 | + } |
| 136 | + |
| 137 | + private static InheritKey constructInternal(List<Namespace> ctx, List<Integer> inheritIdices, int idx) { |
| 138 | + if (idx < 0) return null; |
| 139 | + for (int i = inheritIdices.size(); i > 0; i--) { |
| 140 | + int idx2 = inheritIdices.get(i - 1); |
| 141 | + if (idx2 == idx) { |
| 142 | + // case of [t0, t1, ..., t_idx2 == ^ow] |
| 143 | + return new InheritKey(ctx, inheritIdices, i); |
| 144 | + } else if (idx2 < idx) { |
| 145 | + // case of [t0, t1, ..., t_idx2, ^ow] |
| 146 | + Type typ = (Type) ctx.get(idx); |
| 147 | + return new InheritKey(ctx, inheritIdices, i, typ); |
| 148 | + } |
| 149 | + } |
| 150 | + { |
| 151 | + // case of (^ow) |
| 152 | + Type typ = (Type) ctx.get(idx); |
| 153 | + return new InheritKey(typ); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public static InheritKey construct(List<Namespace> ctx, List<Integer> inheritIdices, Feature ref) { |
| 158 | + int idx = findKeyType(ctx, ref); |
| 159 | + return constructInternal(ctx, inheritIdices, idx); |
| 160 | + } |
| 161 | + |
| 162 | + public static InheritKey construct(List<Namespace> ctx, List<Integer> inheritIdices, Membership ref) { |
| 163 | + int idx = findKeyType(ctx, ref); |
| 164 | + return constructInternal(ctx, inheritIdices, idx); |
| 165 | + } |
| 166 | + |
| 167 | + public static boolean match(InheritKey ik, List<Namespace> ctx, List<Integer> inheritIdices) { |
| 168 | + if (ik == null) { |
| 169 | + return inheritIdices.isEmpty(); |
| 170 | + } else { |
| 171 | + int ctxSize = ctx.size(); |
| 172 | + if (ctxSize == 0) { |
| 173 | + return false; |
| 174 | + } |
| 175 | + int iSize = inheritIdices.size(); |
| 176 | + int kLen = ik.keys.length; |
| 177 | + int diff = kLen - iSize; |
| 178 | + if (diff < 1) return false; |
| 179 | + for (int i = 0; i < iSize; i++) { |
| 180 | + int idx = inheritIdices.get(i); |
| 181 | + Namespace ns = ctx.get(idx); |
| 182 | + if (!ik.keys[i].equals(ns)) return false; |
| 183 | + } |
| 184 | + if (diff == 0) return true; |
| 185 | + // In the case that ik is in the form of [..., ^ow] |
| 186 | + return ik.keys[kLen - 1].equals(ctx.get(ctxSize - 1)); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + public static boolean isDirectInherit(InheritKey ik) { |
| 191 | + if (ik == null) return false; |
| 192 | + return ik.isDirect; |
| 193 | + } |
| 194 | +} |
0 commit comments