Skip to content

Commit 23fd314

Browse files
committed
ST6RI-682 Added model download functionality to jupyter kernel
- added command to list repository contents - added command to download and index repository project - refactored code
1 parent f36638f commit 23fd314

13 files changed

Lines changed: 1057 additions & 748 deletions

File tree

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

Lines changed: 100 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.stream.Collectors;
3939

4040
import org.eclipse.emf.common.util.BasicEList;
41+
import org.eclipse.emf.common.util.EList;
4142
import org.eclipse.emf.ecore.EObject;
4243
import org.eclipse.emf.ecore.EPackage;
4344
import org.eclipse.emf.ecore.resource.Resource;
@@ -52,6 +53,7 @@
5253
import org.eclipse.xtext.validation.CheckMode;
5354
import org.eclipse.xtext.validation.IResourceValidator;
5455
import org.eclipse.xtext.validation.Issue;
56+
import org.omg.kerml.xmi.KerMLxStandaloneSetup;
5557
import org.omg.kerml.xtext.KerMLStandaloneSetup;
5658
import org.omg.kerml.xtext.library.ILibraryIndexProvider;
5759
import org.omg.kerml.xtext.naming.KerMLQualifiedNameConverter;
@@ -69,11 +71,18 @@
6971
import org.omg.sysml.lang.sysml.util.SysMLLibraryUtil;
7072
import org.omg.sysml.plantuml.SysML2PlantUMLLinkProvider;
7173
import org.omg.sysml.plantuml.SysML2PlantUMLSvc;
74+
import org.omg.sysml.util.NamespaceUtil;
7275
import org.omg.sysml.util.SysMLUtil;
7376
import org.omg.sysml.util.TypeUtil;
77+
import org.omg.sysml.util.repository.EObjectUUIDTracker;
78+
import org.omg.sysml.util.repository.ProjectDelta;
79+
import org.omg.sysml.util.repository.ProjectRepository;
80+
import org.omg.sysml.util.repository.ProjectRepository.RepositoryProject;
81+
import org.omg.sysml.util.repository.RepositoryContentFetcher;
7482
import org.omg.sysml.util.traversal.Traversal;
7583
import org.omg.sysml.util.traversal.facade.impl.ApiElementProcessingFacade;
7684
import org.omg.sysml.util.traversal.facade.impl.JsonElementProcessingFacade;
85+
import org.omg.sysml.xmi.SysMLxStandaloneSetup;
7786
import org.omg.sysml.xtext.SysMLStandaloneSetup;
7887

7988
import com.google.common.base.Predicates;
@@ -89,14 +98,16 @@ public class SysMLInteractive extends SysMLUtil {
8998

9099
public static final String KERML_EXTENSION = ".kerml";
91100
public static final String SYSML_EXTENSION = ".sysml";
101+
public static final String KERMLX_EXTENSION = ".kermlx";
102+
public static final String SYSMLX_EXTENSION = ".sysmlx";
92103

93104
protected static Injector injector;
94105
protected static SysMLInteractive instance = null;
95106

96107
protected String apiBasePath = ApiElementProcessingFacade.DEFAULT_BASE_PATH;
97108

98109
protected int counter = 1;
99-
protected XtextResource resource;
110+
protected Resource resource;
100111

101112
protected Traversal traversal;
102113

@@ -116,6 +127,8 @@ public class SysMLInteractive extends SysMLUtil {
116127
@Inject
117128
private ILibraryIndexProvider libraryIndexCache;
118129

130+
private EObjectUUIDTracker tracker = new EObjectUUIDTracker();
131+
119132
@Inject
120133
private SysMLInteractive() {
121134
super(new StrictShadowingResourceDescriptionData());
@@ -137,13 +150,13 @@ public void setApiBasePath(String apiBasePath) {
137150
this.apiBasePath = apiBasePath;
138151
}
139152

140-
public int next() {
141-
this.resource = (XtextResource)this.createResource(counter + SYSML_EXTENSION);
153+
public int next(String extension) {
154+
this.resource = this.createResource(counter + extension);
142155
this.addInputResource(this.resource);
143156
return this.counter++;
144157
}
145158

146-
public XtextResource getResource() {
159+
public Resource getResource() {
147160
return this.resource;
148161
}
149162

@@ -163,24 +176,27 @@ public void removeResource() {
163176
}
164177

165178
public Element getRootElement() {
166-
XtextResource resource = this.getResource();
167-
if (resource == null) {
168-
return null;
169-
} else {
170-
final IParseResult result = resource.getParseResult();
179+
Resource resource = this.getResource();
180+
if (resource instanceof XtextResource xtextResource) {
181+
final IParseResult result = xtextResource.getParseResult();
171182
return result == null? null: (Element)result.getRootASTElement();
183+
} else {
184+
EList<EObject> contents = resource.getContents();
185+
return contents.isEmpty()? null: (Element)contents.get(0);
172186
}
173187
}
174188

175189
public void parse(String input) throws IOException {
176-
XtextResource resource = this.getResource();
177-
if (resource != null) {
178-
resource.reparse(input);
190+
Resource resource = this.getResource();
191+
if (resource instanceof XtextResource xtextResource) {
192+
xtextResource.reparse(input);
193+
} else {
194+
//TODO: add warning when resource is not meant to be parsed
179195
}
180196
}
181197

182198
public List<Issue> validate() {
183-
XtextResource resource = this.getResource();
199+
Resource resource = this.getResource();
184200
return resource == null? Collections.emptyList():
185201
validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
186202
}
@@ -213,7 +229,7 @@ public SysMLInteractiveResult process(String input) {
213229
}
214230

215231
public SysMLInteractiveResult process(String input, boolean isAddResource) {
216-
this.next();
232+
this.next(SYSML_EXTENSION);
217233
try {
218234
this.parse(input);
219235
List<Issue> issues = this.validate();
@@ -417,6 +433,74 @@ protected String publish(String name) {
417433
publish(name, Collections.emptyList());
418434
}
419435

436+
public String download(String publication, List<String> help) {
437+
if (Strings.isNullOrEmpty(publication)) {
438+
return help.isEmpty()? "": SysMLInteractiveHelp.getDownloadHelp();
439+
}
440+
441+
ProjectRepository repository = new ProjectRepository(apiBasePath);
442+
443+
System.out.println("Locating model");
444+
RepositoryProject project = repository.getProjectByName(publication);
445+
446+
if (project == null) {
447+
return "ERROR:Publication doesn't exist.";
448+
}
449+
450+
boolean success = project.loadRemote();
451+
452+
if (!success) {
453+
return "ERROR:Could not download the publication.";
454+
}
455+
456+
System.out.println("Collecting UUIDs...");
457+
if (!tracker.isLibraryTracked()) {
458+
tracker.trackLibraryUUIDs(getLibraryResources());
459+
}
460+
461+
tracker.clearTrackedUserElements();
462+
List<Resource> inputResources = getInputResources();
463+
//UUIDS coming from resources that were added later in time will shadow previous ones
464+
tracker.trackUserUUIDs(inputResources);
465+
466+
RepositoryContentFetcher fetcher = new RepositoryContentFetcher(project, tracker);
467+
468+
System.out.println("Downloading model...");
469+
ProjectDelta delta = fetcher.fetch();
470+
471+
System.out.println("Adding model to index");
472+
delta.getProjectRoots().forEach((eObject, dto) -> {
473+
next(SYSMLX_EXTENSION);
474+
Resource xmiResource = getResource();
475+
if (eObject instanceof Namespace) {
476+
xmiResource.getContents().add(eObject);
477+
} else {
478+
Namespace root = SysMLFactory.eINSTANCE.createNamespace();
479+
NamespaceUtil.addOwnedMemberTo(root, (Element) eObject);
480+
xmiResource.getContents().add(root);
481+
}
482+
addResourceToIndex(xmiResource);
483+
});
484+
485+
return "Loaded project: " + project.getProjectName();
486+
}
487+
488+
protected String download(String name) {
489+
return "-h".equals(name)?
490+
download(null, Collections.singletonList("true")):
491+
download(name, Collections.emptyList());
492+
}
493+
494+
public String listPublications(List<String> help) {
495+
if (help != null && !help.isEmpty()) {
496+
return SysMLInteractiveHelp.getPublicationsHelp();
497+
}
498+
ProjectRepository projectRepository = new ProjectRepository(apiBasePath);
499+
List<RepositoryProject> repositoryProjects = projectRepository.getProjects();
500+
return repositoryProjects.stream().map(p -> String.format("name=%s, id=%s", p.getProjectName(), p.getProjectId()))
501+
.collect(Collectors.joining("\n"));
502+
}
503+
420504
protected ApiElementProcessingFacade getApiElementProcessingFacade(String modelName) {
421505
System.out.println("API base path: " + this.apiBasePath);
422506
ApiElementProcessingFacade processingFacade = new ApiElementProcessingFacade(modelName, this.apiBasePath);
@@ -640,6 +724,8 @@ public static SysMLInteractive createInstance() {
640724
// CompositeEValidator is used.
641725
EPackage.Registry.INSTANCE.put(SysMLPackage.eNS_URI, SysMLPackage.eINSTANCE);
642726
KerMLStandaloneSetup.doSetup();
727+
KerMLxStandaloneSetup.doSetup();
728+
SysMLxStandaloneSetup.doSetup();
643729
injector = new SysMLStandaloneSetup().createInjectorAndDoEMFRegistration();
644730
}
645731
return injector.getInstance(SysMLInteractive.class);

0 commit comments

Comments
 (0)