Skip to content

Commit fe8ecfd

Browse files
committed
ST6RI-178 Added workarounds to fix upload/download in Jupyter
- added ability to add any element as root element to the APIModel
1 parent af9dfb1 commit fe8ecfd

6 files changed

Lines changed: 80 additions & 21 deletions

File tree

org.omg.kerml.xtext/src/org/omg/kerml/xtext/util/KerMLRepositorySaveUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ protected void initialize(String[] args) {
185185
@Override
186186
public void process() {
187187
super.process();
188-
this.isCommitted = ((ApiElementProcessingFacade)this.traversal.getFacade()).commit();
188+
this.isCommitted = ((ApiElementProcessingFacade)this.traversal.getFacade()).commit(null);
189189
}
190190

191191
/**

org.omg.sysml/src/org/omg/sysml/util/repository/APIModel.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,18 @@ public Map<UUID, Element> getModelRoots() {
7474

7575

7676
/**
77-
* Adds root level model element to the model.
77+
* Adds root level model element to the model. Adding an element this way will unset the owner
78+
* and owning attributes.
7879
*
7980
* @param id UUID of the element
8081
* @param rootElement element to add
8182
*/
8283
public void addModelRoot(UUID id, Element rootElement) {
84+
for (var key: rootElement.keySet()) {
85+
if (key.equals("owner") || key.startsWith("owning")) {
86+
rootElement.replace(key, null);
87+
}
88+
}
8389
modelRoots.put(id, rootElement);
8490
modelElements.put(id, rootElement);
8591
}
@@ -137,6 +143,10 @@ public Element getElement(UUID uuid) {
137143
return modelElements.get(uuid);
138144
}
139145

146+
public Element getElement(String uuid) {
147+
return modelElements.get(UUID.fromString(uuid));
148+
}
149+
140150
public void addOutOfScopeReferencesAsProxies() {
141151
Set<LocalReference> localReferences = modelElements.values().stream()
142152
.flatMap(el -> el.values().stream())
@@ -150,6 +160,7 @@ public void addOutOfScopeReferencesAsProxies() {
150160
Element proxy = new Element();
151161
proxy.put(Identified.SERIALIZED_NAME_AT_ID, uuid.toString());
152162
proxy.put("@type", ref.getType());
163+
proxy.put("isLibraryElement", ref.isLibraryElement());
153164
addModelRoot(uuid, proxy);
154165
}
155166
}
@@ -188,15 +199,21 @@ public JsonElement toJsonTree(Gson gson) {
188199
public static class LocalReference extends HashMap<String, UUID> {
189200

190201
private transient String type;
202+
private boolean isLibraryElement;
191203

192-
public LocalReference(UUID identifier, String type) {
204+
public LocalReference(UUID identifier, String type, boolean isLibraryElement) {
193205
put(Identified.SERIALIZED_NAME_AT_ID, identifier);
194206
this.type = type;
207+
this.isLibraryElement = isLibraryElement;
195208
}
196209

197210
public String getType() {
198211
return type;
199212
}
213+
214+
public boolean isLibraryElement() {
215+
return isLibraryElement;
216+
}
200217

201218
public UUID getUUID() {
202219
return get(Identified.SERIALIZED_NAME_AT_ID);

org.omg.sysml/src/org/omg/sysml/util/repository/APIModelDelta.java

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646
public class APIModelDelta {
4747

4848
private final Collection<Element> additions;
49-
private final Map<UUID, Data> changes;
49+
private final Map<UUID, ChangedElement> changes;
5050
private final Collection<UUID> deletions;
5151

52-
private APIModelDelta(Set<Element> additions, Map<UUID, Data> changes, Set<UUID> deletions) {
52+
private APIModelDelta(Set<Element> additions, Map<UUID, ChangedElement> changes, Set<UUID> deletions) {
5353
this.additions = Collections.unmodifiableCollection(additions);
5454
this.changes = Collections.unmodifiableMap(changes);
5555
this.deletions = Collections.unmodifiableCollection(deletions);
@@ -69,7 +69,7 @@ public Collection<Element> getAdditions() {
6969
*
7070
* @return UUID and field changes of elements
7171
*/
72-
public Map<UUID, Data> getChanges() {
72+
public Map<UUID, ChangedElement> getChanges() {
7373
return changes;
7474
}
7575

@@ -82,6 +82,10 @@ public Collection<UUID> getDeletions() {
8282
return deletions;
8383
}
8484

85+
public boolean isEmpty() {
86+
return deletions.isEmpty() && additions.isEmpty() && changes.isEmpty();
87+
}
88+
8589
/**
8690
* Converts the {@link APIModelDelta} to a data structure
8791
* that the standard API server accepts as a model delta.
@@ -102,7 +106,7 @@ public List<DataVersion> toTrasferableDelta() {
102106
}
103107

104108
for (var change: changes.entrySet()) {
105-
delta.add(new DataVersion().payload(change.getValue()).identity(new DataIdentity().atId(change.getKey())));
109+
delta.add(new DataVersion().payload(change.getValue().toData()).identity(new DataIdentity().atId(change.getKey())));
106110
}
107111

108112
for (UUID deletion: getDeletions()) {
@@ -127,7 +131,7 @@ public static APIModelDelta create(APIModel thisModel, APIModel baseline) {
127131

128132
Set<Element> additions = new HashSet<>();
129133
Set<UUID> deletions = new HashSet<>();
130-
Map<UUID, Data> changes = new HashMap<>();
134+
Map<UUID, ChangedElement> changes = new HashMap<>();
131135

132136
Map<UUID, Element> elementsFromThis = thisModel.getModelElements();
133137

@@ -137,9 +141,8 @@ public static APIModelDelta create(APIModel thisModel, APIModel baseline) {
137141
additions.add(thisModel.getElement(id));
138142
} else {
139143
Element thisElement = thisModel.getElement(id);
140-
Data change = createElementDelta(thisElement, baselineElement);
141-
if (!change.isEmpty()) {
142-
change.put("@type", thisElement.get("@type"));
144+
ChangedElement change = createElementDelta(thisElement, baselineElement);
145+
if (change != null) {
143146
changes.put(id, change);
144147
}
145148
}
@@ -164,14 +167,15 @@ public static APIModelDelta create(APIModel thisModel, APIModel baseline) {
164167
* @param baselineElement element to use as a baseline
165168
* @return changed fields and their new values wrapped in a {@link Data} object.
166169
*/
167-
public static Data createElementDelta(Element thisElement, Element baselineElement) {
168-
Data change = new Data();
170+
public static ChangedElement createElementDelta(Element thisElement, Element baselineElement) {
171+
ChangedElement change = null;
169172
Set<String> fieldsInThis = thisElement.keySet();
170173
for (String field: fieldsInThis) {
171174
Object thisValue = thisElement.get(field);
172175
Object baselineValue = baselineElement.get(field);
173176
if (!Objects.equal(thisValue, baselineValue)) {
174-
change.put(field, thisValue);
177+
if (change == null) change = new ChangedElement(baselineElement);
178+
change.add(field, thisValue);
175179
}
176180
}
177181
//set removed fields to null
@@ -184,4 +188,24 @@ public static Data createElementDelta(Element thisElement, Element baselineEleme
184188

185189
return change;
186190
}
191+
192+
private static class ChangedElement {
193+
private Element originalElement;
194+
private Map<String, Object> changes = new HashMap<>();
195+
196+
public ChangedElement(Element originalElement) {
197+
this.originalElement = originalElement;
198+
}
199+
200+
public void add(String field, Object thisValue) {
201+
changes.put(field, thisValue);
202+
}
203+
204+
public Data toData() {
205+
Data data = new Data();
206+
data.putAll(originalElement);
207+
data.putAll(changes);
208+
return data;
209+
}
210+
}
187211
}

org.omg.sysml/src/org/omg/sysml/util/repository/Revision.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,17 @@ public Revision pushLocalChanges() throws ApiException {
106106
}
107107

108108
public Revision pushChanges(APIModelDelta changes) throws ApiException {
109+
if (changes.isEmpty()) {
110+
return this;
111+
}
109112
APIModelDelta localChanges = getLocalChanges();
110113
return pushChanges(localChanges.toTrasferableDelta());
111114
}
112115

113116
public Revision pushChanges(List<DataVersion> transferableChanges) throws ApiException {
117+
if (transferableChanges.isEmpty()) {
118+
return this;
119+
}
114120
CommitApi commitApi = getRemoteProject().getProjectRepository().getCommitApi();
115121
Commit commit = new Commit();
116122
commit.setChange(transferableChanges);

org.omg.sysml/src/org/omg/sysml/util/traversal/facade/impl/ApiElementProcessingFacade.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package org.omg.sysml.util.traversal.facade.impl;
2727

2828
import java.util.List;
29+
import java.util.UUID;
2930

3031
import org.omg.sysml.ApiException;
3132
import org.omg.sysml.lang.sysml.Element;
@@ -104,12 +105,14 @@ public Object process(Element element) {
104105
}
105106

106107
/**
107-
* Create a new project in the repository, then create and post a commit to the project to save the
108+
* Create or update a project in the repository, then create and post a commit to the project to save the
108109
* ElementVersions constructed from the processed model Elements.
109110
*
111+
* @param element to use as project root.
112+
*
110113
* @return whether the commit succeeded without an ApiException
111114
*/
112-
public boolean commit() {
115+
public boolean commit(Element useAsRoot) {
113116
try {
114117
RemoteProject project = getProject();
115118
RemoteBranch defaultBranch = project.getDefaultBranch();
@@ -121,10 +124,18 @@ public boolean commit() {
121124
}
122125
// System.out.println(toJson());
123126
APIModel localState = getLocalModel();
127+
128+
if (useAsRoot != null) {
129+
UUID rootUUID = UUID.fromString(useAsRoot.getElementId());
130+
var root = localState.getElement(rootUUID);
131+
//reinsert element as root, this will unset its owners turning it into a root element
132+
localState.addModelRoot(rootUUID, root);
133+
}
134+
124135
localState.addOutOfScopeReferencesAsProxies();
125136
headRevision.setLocalState(localState);
126137
List<DataVersion> localChanges = headRevision.getLocalChanges().toTrasferableDelta();
127-
System.out.println(new org.omg.sysml.JSON().serialize(localChanges));
138+
// System.out.println(new org.omg.sysml.JSON().serialize(localChanges));
128139

129140
int n = localChanges.size();
130141
System.out.print("\nPosting Commit (" + n + " element" + (n == 1? ")...": "s)..."));

org.omg.sysml/src/org/omg/sysml/util/traversal/facade/impl/JsonElementProcessingFacade.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ public boolean isIncludeDerived() {
128128
* Create an Identified object with the given identifier.
129129
*
130130
* @param identifier the UUID of the object being identified
131+
* @param isLibraryElement
131132
* @return an Identified object with the given identifier
132133
*/
133-
protected static Map<String, UUID> identified(UUID identifier, String type) {
134-
return new APIModel.LocalReference(identifier, type);
134+
protected static Map<String, UUID> identified(UUID identifier, String type, boolean isLibraryElement) {
135+
return new APIModel.LocalReference(identifier, type, isLibraryElement);
135136
}
136137

137138
/**
@@ -141,7 +142,7 @@ protected static Map<String, UUID> identified(UUID identifier, String type) {
141142
* @return an Identified object for the given Element (or null if the input is null).
142143
*/
143144
protected Map<String, UUID> getIdentified(Element element) {
144-
return element == null? null: identified(UUID.fromString(element.getElementId()), element.eClass().getName());
145+
return element == null? null: identified(UUID.fromString(element.getElementId()), element.eClass().getName(), element.isLibraryElement());
145146
}
146147

147148
/**
@@ -178,7 +179,7 @@ protected org.omg.sysml.model.Element createApiModelElement(Element element) {
178179
String featureName = feature.getName();
179180
if ((this.isIncludeDerived() || !feature.isDerived()) &&
180181
// Skip implementation-specific features.
181-
!("Feature".equals(className) && "isNonunique".equals(featureName) ||
182+
!(/*"Feature".equals(className) && */"isNonunique".equals(featureName) ||
182183
"OperatorExpression".equals(className) && "operand".equals(featureName) ||
183184
apiElement.containsKey(featureName))) {
184185
Object value = element.eGet(feature);

0 commit comments

Comments
 (0)