Skip to content

Commit 09a120d

Browse files
committed
ST6RI-178 WIP - pushing incremental changes
1 parent a4d3e12 commit 09a120d

10 files changed

Lines changed: 263 additions & 93 deletions

File tree

org.omg.sysml.interactive/src/org/omg/sysml/interactive/SysMLInteractive.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@
7474
import org.omg.sysml.lang.sysml.util.SysMLLibraryUtil;
7575
import org.omg.sysml.plantuml.SysML2PlantUMLLinkProvider;
7676
import org.omg.sysml.plantuml.SysML2PlantUMLSvc;
77+
import org.omg.sysml.util.ElementUtil;
7778
import org.omg.sysml.util.NamespaceUtil;
7879
import org.omg.sysml.util.SysMLUtil;
7980
import org.omg.sysml.util.TypeUtil;
8081
import org.omg.sysml.util.repository.EObjectUUIDTracker;
82+
import org.omg.sysml.util.repository.APIModel;
8183
import org.omg.sysml.util.repository.EMFModelRefresh;
8284
import org.omg.sysml.util.repository.ProjectRepository;
8385
import org.omg.sysml.util.repository.ProjectRevision;
84-
import org.omg.sysml.util.repository.ProjectRevision.APIModel;
8586
import org.omg.sysml.util.repository.RemoteProject;
8687
import org.omg.sysml.util.repository.RemoteProject.RemoteBranch;
8788
import org.omg.sysml.util.repository.EMFModelRefreshCreator;
@@ -409,24 +410,25 @@ public String show(String name) {
409410
show(name, Collections.emptyList(), Collections.emptyList()));
410411
}
411412

412-
public String publish(String name, List<String> help) {
413+
public String publish(String elementName, String projectName, List<String> help) {
413414
this.counter++;
414-
if (Strings.isNullOrEmpty(name)) {
415+
if (Strings.isNullOrEmpty(elementName)) {
415416
return help.isEmpty()? "": SysMLInteractiveHelp.getPublishHelp();
416417
}
417418
try {
418-
Element element = this.resolve(name);
419+
Element element = this.resolve(elementName);
419420
if (element == null) {
420-
return "ERROR:Couldn't resolve reference to Element '" + name + "'\n";
421+
return "ERROR:Couldn't resolve reference to Element '" + elementName + "'\n";
421422
} else if (!this.isInputResource(element.eResource())) {
422-
return "ERROR:'" + name + "' is a library element\n";
423+
return "ERROR:'" + elementName + "' is a library element\n";
423424
} else {
424-
String modelName = element.getDeclaredName() + " " + new Date();
425-
ApiElementProcessingFacade processingFacade = this.getApiElementProcessingFacade(modelName);
425+
String remoteProjectName = projectName == null? element.getDeclaredName() + " " + new Date() : projectName;
426+
427+
ApiElementProcessingFacade processingFacade = this.getApiElementProcessingFacade(remoteProjectName);
426428
processingFacade.getTraversal().visit(element);
427429
processingFacade.commit();
428430
System.out.println();
429-
return "Saved to Project " + modelName + " (" + processingFacade.getProjectId() + ")\n";
431+
return "Saved to Project " + remoteProjectName + " (" + processingFacade.getProjectId() + ")\n";
430432
}
431433
} catch (Exception e) {
432434
return SysMLInteractiveUtil.formatException(e);
@@ -435,8 +437,8 @@ public String publish(String name, List<String> help) {
435437

436438
protected String publish(String name) {
437439
return "-h".equals(name)?
438-
publish(null, Collections.singletonList("true")):
439-
publish(name, Collections.emptyList());
440+
publish(null, null, Collections.singletonList("true")):
441+
publish(name, null, Collections.emptyList());
440442
}
441443

442444
public String loadByName(String projectName, List<String> help) {
@@ -450,7 +452,7 @@ public String loadByName(String projectName, List<String> help) {
450452
RemoteProject repositoryProject = repository.getProjectByName(projectName);
451453

452454
if (repositoryProject == null) {
453-
return "ERROR:Publication doesn't exist.";
455+
return "ERROR:Project doesn't exist.";
454456
}
455457

456458
return load(repositoryProject);
@@ -467,7 +469,7 @@ public String loadById(String projectId, List<String> help) {
467469
RemoteProject repositoryProject = repository.getPRojectById(UUID.fromString(projectId));
468470

469471
if (repositoryProject == null) {
470-
return "ERROR:Publication doesn't exist.";
472+
return "ERROR:Project doesn't exist.";
471473
}
472474

473475
return load(repositoryProject);
@@ -492,7 +494,7 @@ private String load(RemoteProject repositoryProject) {
492494
EMFModelRefreshCreator fetcher = new EMFModelRefreshCreator(model, tracker);
493495

494496
System.out.println("Downloading model...");
495-
EMFModelRefresh delta = fetcher.fetch();
497+
EMFModelRefresh delta = fetcher.create();
496498

497499
System.out.println("Adding model to index");
498500
delta.getProjectRoots().forEach((eObject, dto) -> {

org.omg.sysml.jupyter.kernel/src/main/java/org/omg/sysml/jupyter/kernel/magic/Publish.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@
2929
public class Publish {
3030
private static final MagicsArgs SHOW_ARGS = MagicsArgs.builder().onlyKnownKeywords().onlyKnownFlags()
3131
.optional("element")
32+
.keyword("project")
33+
.keyword("branch")
3234
.flag("help", 'h', "true")
3335
.build();
3436

3537
@LineMagic
3638
public static String publish(List<String> args) {
3739
Map<String, List<String>> vals = SHOW_ARGS.parse(args);
3840
List<String> elements = vals.get("element");
41+
List<String> projects = vals.get("project");
3942
String element = elements.isEmpty()? null:elements.get(0);
43+
String project = projects.isEmpty()? null:projects.get(0);
4044
List<String> help = vals.get("help");
41-
return ISysML.getKernelInstance().getInteractive().publish(element, help);
45+
return ISysML.getKernelInstance().getInteractive().publish(element, project, help);
4246
}
4347
}

org.omg.sysml.xtext.ui/src/org/omg/sysml/xtext/ui/handlers/PullRepositoryProject.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
import org.eclipse.xtext.ui.resource.IResourceSetProvider;
5151
import org.omg.sysml.util.repository.EObjectUUIDTracker;
5252
import org.omg.sysml.ApiException;
53+
import org.omg.sysml.util.repository.APIModel;
5354
import org.omg.sysml.util.repository.EMFModelRefresh;
5455
import org.omg.sysml.util.repository.ProjectRepository;
5556
import org.omg.sysml.util.repository.ProjectRevision;
56-
import org.omg.sysml.util.repository.ProjectRevision.APIModel;
5757
import org.omg.sysml.util.repository.RemoteProject;
5858
import org.omg.sysml.util.repository.RemoteProject.RemoteBranch;
5959
import org.omg.sysml.util.repository.EMFModelRefreshCreator;
@@ -134,7 +134,7 @@ private void tryPullRemoteFor(IProject project, IProgressMonitor monitor) {
134134
ProjectRevision headRevision = defaultBranch.getHeadRevision();
135135
APIModel model = headRevision.fetchRemote();
136136
EMFModelRefreshCreator repositoryFetcher = new EMFModelRefreshCreator(model, tracker);
137-
EMFModelRefresh delta = repositoryFetcher.fetch();
137+
EMFModelRefresh delta = repositoryFetcher.create();
138138
delta.save(resourceSet, URI.createPlatformResourceURI(targetPath, false));
139139
}
140140
} catch (IOException | CoreException e) {

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,21 @@
2222
*/
2323
package org.omg.sysml.util.repository;
2424

25+
import java.util.Collection;
2526
import java.util.Collections;
2627
import java.util.HashMap;
28+
import java.util.HashSet;
2729
import java.util.Map;
30+
import java.util.Optional;
31+
import java.util.Set;
2832
import java.util.UUID;
33+
import java.util.stream.Collectors;
34+
import java.util.stream.Stream;
2935

3036
import org.apache.commons.lang3.NotImplementedException;
3137
import org.omg.sysml.lang.sysml.OwningMembership;
3238
import org.omg.sysml.model.Element;
39+
import org.omg.sysml.model.Identified;
3340

3441
import com.google.gson.Gson;
3542
import com.google.gson.JsonElement;
@@ -131,6 +138,35 @@ public Element getElement(UUID uuid) {
131138
return modelElements.get(uuid);
132139
}
133140

141+
public void addOutOfScopeReferencesAsProxies() {
142+
Set<LocalReference> localReferences = modelElements.values().stream()
143+
.flatMap(el -> el.values().stream())
144+
.flatMap(this::flatten)
145+
.collect(Collectors.toSet());
146+
147+
Set<UUID> added = new HashSet<>();
148+
for (LocalReference ref: localReferences) {
149+
UUID uuid = ref.getUUID();
150+
if (getElement(uuid) == null && !added.contains(uuid)) {
151+
Element proxy = new Element();
152+
proxy.put(Identified.SERIALIZED_NAME_AT_ID, uuid.toString());
153+
proxy.put("@type", ref.getType());
154+
addModelRoot(uuid, proxy);
155+
}
156+
}
157+
}
158+
159+
private Stream<LocalReference> flatten(Object object) {
160+
if (object instanceof LocalReference ref) {
161+
return Stream.of(ref);
162+
} else if (object instanceof Collection<?> col) {
163+
return col.stream().filter(LocalReference.class::isInstance)
164+
.map(LocalReference.class::cast);
165+
} else {
166+
return Stream.empty();
167+
}
168+
}
169+
134170
/**
135171
* Creates a JSON representation of the model.
136172
*
@@ -148,4 +184,39 @@ public String toJson(Gson gson) {
148184
public JsonElement toJsonTree(Gson gson) {
149185
return gson.toJsonTree(getModelElements().values());
150186
}
187+
188+
@SuppressWarnings("serial")
189+
public static class LocalReference extends HashMap<String, UUID> {
190+
191+
private transient String type;
192+
193+
public LocalReference(UUID identifier, String type) {
194+
put(Identified.SERIALIZED_NAME_AT_ID, identifier);
195+
this.type = type;
196+
}
197+
198+
public String getType() {
199+
return type;
200+
}
201+
202+
public UUID getUUID() {
203+
return get(Identified.SERIALIZED_NAME_AT_ID);
204+
}
205+
206+
@Override
207+
public boolean equals(Object o) {
208+
return o == this || (o instanceof Map m && m.containsKey(Identified.SERIALIZED_NAME_AT_ID) && idEquals(m));
209+
}
210+
211+
private boolean idEquals(Map<?,?> other) {
212+
UUID id = this.get(Identified.SERIALIZED_NAME_AT_ID);
213+
Object otherid = other.get(Identified.SERIALIZED_NAME_AT_ID);
214+
if (otherid instanceof String idAsString) {
215+
return UUID.fromString(idAsString).equals(id);
216+
} else if (otherid instanceof UUID idAsUUID) {
217+
return idAsUUID.equals(id);
218+
}
219+
return false;
220+
}
221+
}
151222
}

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

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.omg.sysml.model.DataIdentity;
3737
import org.omg.sysml.model.DataVersion;
3838
import org.omg.sysml.model.Element;
39+
import org.omg.sysml.model.Identified;
3940

4041
import com.google.common.base.Objects;
4142

@@ -44,16 +45,17 @@
4445
*/
4546
public class APIModelDelta {
4647

47-
private static final String ID_FIELD_NAME = "@id";
48-
4948
private final Collection<Element> additions;
5049
private final Map<UUID, Data> changes;
5150
private final Collection<UUID> deletions;
5251

53-
private APIModelDelta(Set<Element> additions, Map<UUID, Data> changes, Set<UUID> deletions) {
52+
private APIModel thisModel;
53+
54+
private APIModelDelta(APIModel thisModel, Set<Element> additions, Map<UUID, Data> changes, Set<UUID> deletions) {
5455
this.additions = Collections.unmodifiableCollection(additions);
5556
this.changes = Collections.unmodifiableMap(changes);
5657
this.deletions = Collections.unmodifiableCollection(deletions);
58+
this.thisModel = thisModel;
5759
}
5860

5961
/**
@@ -94,8 +96,11 @@ public List<DataVersion> toTrasferableDelta() {
9496

9597
for (Element addition: getAdditions()) {
9698
Data data = new Data();
97-
data.putAll(addition);
98-
UUID id = UUID.fromString(addition.get(ID_FIELD_NAME).toString());
99+
for (String field: addition.keySet()) {
100+
Object value = addition.get(field);
101+
data.put(field, value);
102+
}
103+
UUID id = UUID.fromString(addition.get(Identified.SERIALIZED_NAME_AT_ID).toString());
99104
delta.add(new DataVersion().payload(data).identity(new DataIdentity().atId(id)));
100105
}
101106

@@ -104,9 +109,8 @@ public List<DataVersion> toTrasferableDelta() {
104109
}
105110

106111
for (UUID deletion: getDeletions()) {
107-
//leave empty = remove
108-
Data data = new Data();
109-
delta.add(new DataVersion().payload(data).identity(new DataIdentity().atId(deletion)));
112+
//set to null for deletion
113+
delta.add(new DataVersion().payload(null).identity(new DataIdentity().atId(deletion)));
110114
}
111115

112116
return delta;
@@ -121,6 +125,9 @@ public List<DataVersion> toTrasferableDelta() {
121125
* @return {@link APIModelDelta} of the two models
122126
*/
123127
public static APIModelDelta create(APIModel thisModel, APIModel baseline) {
128+
if (thisModel == null) thisModel = APIModel.createEmpty();
129+
if (baseline == null) baseline = APIModel.createEmpty();
130+
124131
Set<Element> additions = new HashSet<>();
125132
Set<UUID> deletions = new HashSet<>();
126133
Map<UUID, Data> changes = new HashMap<>();
@@ -135,12 +142,13 @@ public static APIModelDelta create(APIModel thisModel, APIModel baseline) {
135142
Element thisElement = thisModel.getElement(id);
136143
Data change = createElementDelta(thisElement, baselineElement);
137144
if (!change.isEmpty()) {
145+
change.put("@type", thisElement.get("@type"));
138146
changes.put(id, change);
139147
}
140148
}
141149
}
142150

143-
Map<UUID, Element> elementsFromBaseline = thisModel.getModelElements();
151+
Map<UUID, Element> elementsFromBaseline = baseline.getModelElements();
144152

145153
for (UUID id: elementsFromBaseline.keySet()) {
146154
Element element = thisModel.getElement(id);
@@ -149,8 +157,7 @@ public static APIModelDelta create(APIModel thisModel, APIModel baseline) {
149157
}
150158
}
151159

152-
153-
return new APIModelDelta(additions, changes, deletions);
160+
return new APIModelDelta(thisModel, additions, changes, deletions);
154161
}
155162

156163
/**
@@ -164,19 +171,19 @@ public static Data createElementDelta(Element thisElement, Element baselineEleme
164171
Data change = new Data();
165172
Set<String> fieldsInThis = thisElement.keySet();
166173
for (String field: fieldsInThis) {
167-
Object thisValue = baselineElement.get(field);
174+
Object thisValue = thisElement.get(field);
168175
Object baselineValue = baselineElement.get(field);
169176
if (!Objects.equal(thisValue, baselineValue)) {
170177
change.put(field, thisValue);
171178
}
172179
}
173180
//set removed fields to null
174-
Set<String> fieldsInTo = baselineElement.keySet();
175-
for (String field: fieldsInTo) {
176-
if (!thisElement.containsKey(field)) {
177-
change.put(field, null);
178-
}
179-
}
181+
// Set<String> fieldsInTo = baselineElement.keySet();
182+
// for (String field: fieldsInTo) {
183+
// if (!thisElement.containsKey(field)) {
184+
// change.put(field, null);
185+
// }
186+
// }
180187

181188
return change;
182189
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.io.IOException;
2626
import java.util.Collections;
2727
import java.util.HashMap;
28-
import java.util.LinkedList;
2928
import java.util.List;
3029
import java.util.Map;
3130
import java.util.Objects;
@@ -185,6 +184,13 @@ public RemoteProject getProjectByName(String projectName) {
185184
}
186185
}
187186

187+
public RemoteProject createProject(String projectName) throws ApiException {
188+
Project project = new Project();
189+
project.setName(projectName);
190+
Project createdProject = getProjectApi().postProject(project);
191+
return new RemoteProject(this, createdProject.getAtId(), createdProject.getName());
192+
}
193+
188194
Map<UUID, Element> getModelRoots(ProjectRevision revision) throws ApiException {
189195
pager.reset();
190196
Map<UUID, Element> projectRoots = new HashMap<>();

0 commit comments

Comments
 (0)