Skip to content

Commit 4a4449e

Browse files
committed
ST6RI-178 Added basic branching capability
1 parent 5f37567 commit 4a4449e

14 files changed

Lines changed: 829 additions & 472 deletions

File tree

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

Lines changed: 221 additions & 221 deletions
Large diffs are not rendered by default.

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

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
import java.util.Arrays;
3030
import java.util.Date;
3131

32+
import org.apache.commons.cli.CommandLine;
33+
import org.apache.commons.cli.CommandLineParser;
34+
import org.apache.commons.cli.DefaultParser;
35+
import org.apache.commons.cli.Option;
36+
import org.apache.commons.cli.Options;
37+
import org.apache.commons.cli.ParseException;
3238
import org.omg.sysml.lang.sysml.util.SysMLLibraryUtil;
3339
import org.omg.sysml.util.traversal.facade.impl.ApiElementProcessingFacade;
3440

@@ -46,6 +52,7 @@ public class KerMLRepositorySaveUtil extends KerMLTraversalUtil {
4652
private boolean isAddDerivedElements = false;
4753
private boolean isAddImplicitElements = false;
4854
private String projectName;
55+
private String branchName;
4956
private boolean isCommitted = false;
5057

5158
public KerMLRepositorySaveUtil() {
@@ -116,30 +123,50 @@ public boolean isCommitted() {
116123
* prepended to arguments for library model files.
117124
*/
118125
protected String[] processArgs(String[] args) {
119-
int n = args.length;
120-
if (n > 0) {
121-
int i = 0;
122-
while(("-b".equals(args[i]) || "-l".equals(args[i]) || "-d".equals(args[i]) ||
123-
"-g".equals(args[i]) || "-v".equals(args[i])) &&
124-
i + 1 < n) {
125-
if ("-b".equals(args[i])) {
126-
this.basePath = args[++i];
127-
} else if ("-l".equals(args[i])) {
128-
this.libraryPath = args[++i];
129-
if (!libraryPath.endsWith("/")) {
130-
libraryPath += "/";
131-
}
132-
} else if ("-d".equals(args[i])) {
133-
this.isAddDerivedElements = true;
134-
} else if ("-g".equals(args[i])) {
135-
this.isAddImplicitElements = true;
136-
} else if ("-v".equals(args[i])) {
137-
this.setVerbose(true);
138-
}
139-
i++;
126+
var libraryPathOption = new Option("l", true, "gives the base path to used for reading model library resources");
127+
var basePathOption = new Option("b", true, "");
128+
var isVerboseOption = new Option("v", false, "");
129+
var addImplicitsOption = new Option("g", "");
130+
var addDerivedOption = new Option("d", "");
131+
var projectOption = new Option("p", true, "");
132+
var branchOption = new Option("br", true, "");
133+
134+
libraryPathOption.setRequired(true);
135+
basePathOption.setRequired(true);
136+
isVerboseOption.setRequired(false);
137+
addImplicitsOption.setRequired(false);
138+
addDerivedOption.setRequired(false);
139+
branchOption.setRequired(false);
140+
projectOption.setRequired(false);
141+
142+
Options options = new Options()
143+
.addOption(libraryPathOption)
144+
.addOption(basePathOption)
145+
.addOption(isVerboseOption)
146+
.addOption(addImplicitsOption)
147+
.addOption(addDerivedOption)
148+
.addOption(projectOption)
149+
.addOption(branchOption);
150+
151+
CommandLineParser parser = new DefaultParser();
152+
153+
try {
154+
CommandLine cli = parser.parse(options, args);
155+
basePath = cli.getOptionValue(basePathOption);
156+
libraryPath = cli.getOptionValue(libraryPathOption);
157+
if (!libraryPath.endsWith("/")) {
158+
libraryPath += "/";
140159
}
141-
if (i < n) {
142-
args = Arrays.copyOfRange(args, i, n);
160+
isAddImplicitElements = cli.hasOption(addImplicitsOption);
161+
isAddDerivedElements = cli.hasOption(addDerivedOption);
162+
setVerbose(cli.hasOption(isVerboseOption));
163+
projectName = cli.getOptionValue(projectOption);
164+
branchName = cli.getOptionValue(branchOption);
165+
166+
args = cli.getArgs();
167+
168+
if (args.length > 0) {
169+
args = Arrays.copyOf(args, args.length);
143170

144171
if (this.libraryPath != null) {
145172
for (int k = 1; k < args.length; k++) {
@@ -149,7 +176,10 @@ protected String[] processArgs(String[] args) {
149176

150177
return args;
151178
}
179+
} catch (ParseException e) {
180+
System.err.println(e.getMessage());
152181
}
182+
153183
return null;
154184
}
155185

@@ -166,14 +196,16 @@ protected void initialize(String[] args) {
166196
SysMLLibraryUtil.setModelLibraryDirectory(libraryPath);
167197
}
168198

169-
this.projectName = Paths.get(args[0]).getFileName().toString();
170-
int j = this.projectName.indexOf('.');
171-
if (j >= 0) {
172-
this.projectName = this.projectName.substring(0, j);
199+
if (this.projectName == null) {
200+
this.projectName = Paths.get(args[0]).getFileName().toString();
201+
int j = this.projectName.indexOf('.');
202+
if (j >= 0) {
203+
this.projectName = this.projectName.substring(0, j);
204+
}
205+
this.projectName += " " + new Date();
173206
}
174-
this.projectName += " " + new Date();
175207

176-
ApiElementProcessingFacade processingFacade = new ApiElementProcessingFacade(this.projectName, this.getBasePath());
208+
ApiElementProcessingFacade processingFacade = new ApiElementProcessingFacade(this.projectName, branchName, this.getBasePath());
177209
processingFacade.setTraversal(this.initialize(processingFacade));
178210
processingFacade.setIsVerbose(this.isVerbose());
179211
processingFacade.setIsIncludeDerived(this.isAddDerivedElements);
@@ -197,7 +229,6 @@ public void run(String[] args) {
197229
args = this.processArgs(args);
198230

199231
if (args != null) {
200-
201232
System.out.println("Saving " + args[0] + "...");
202233

203234
this.initialize(args);
@@ -211,7 +242,12 @@ public void run(String[] args) {
211242
System.out.println();
212243

213244
this.process();
214-
System.out.println("Saved to Project (" + this.getProjectName() + ") " + this.getProjectId());
245+
246+
if (isCommitted()) {
247+
System.out.println("Saved to Project (" + this.getProjectName() + ") " + this.getProjectId());
248+
} else {
249+
System.out.println("Failed to save Project (" + this.getProjectName() + ") ");
250+
}
215251
System.out.println();
216252
}
217253
}

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

Lines changed: 77 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import org.omg.kerml.xtext.KerMLStandaloneSetup;
6060
import org.omg.kerml.xtext.library.ILibraryIndexProvider;
6161
import org.omg.kerml.xtext.naming.KerMLQualifiedNameConverter;
62-
import org.omg.sysml.ApiException;
6362
import org.omg.sysml.execution.expressions.ExpressionEvaluator;
6463
import org.omg.sysml.lang.sysml.Element;
6564
import org.omg.sysml.lang.sysml.Expression;
@@ -365,7 +364,7 @@ public Object show(String name, List<String> styles, List<String> help) {
365364
else if (matchStyle(styles, "JSON")) {
366365
JsonElementProcessingFacade processingFacade = this.getJsonElementProcessingFacade();
367366
processingFacade.getTraversal().visit(element);
368-
return processingFacade.toJsonTree();
367+
return processingFacade.toJsonTree(true);
369368
}
370369
else if (styles.isEmpty() || matchStyle(styles, "TREE")){
371370
return SysMLInteractiveUtil.formatTree(element);
@@ -389,7 +388,7 @@ public Object export(String name, List<String> help) {
389388
}
390389
JsonElementProcessingFacade processingFacade = this.getJsonElementProcessingFacade();
391390
processingFacade.getTraversal().visit(element);
392-
return processingFacade.toJsonTree();
391+
return processingFacade.toJsonTree(true);
393392
} catch (Exception e) {
394393
return SysMLInteractiveUtil.formatException(e);
395394
}
@@ -409,7 +408,7 @@ public String show(String name) {
409408
show(name, Collections.emptyList(), Collections.emptyList()));
410409
}
411410

412-
public String publish(String elementName, String projectName, List<String> help) {
411+
public String publish(String elementName, String projectName, String branchName, List<String> help) {
413412
this.counter++;
414413
if (Strings.isNullOrEmpty(elementName)) {
415414
return help.isEmpty()? "": SysMLInteractiveHelp.getPublishHelp();
@@ -423,7 +422,7 @@ public String publish(String elementName, String projectName, List<String> help)
423422
} else {
424423
String remoteProjectName = projectName == null? element.getDeclaredName() + " " + new Date() : projectName;
425424

426-
ApiElementProcessingFacade processingFacade = this.getApiElementProcessingFacade(remoteProjectName);
425+
ApiElementProcessingFacade processingFacade = this.getApiElementProcessingFacade(remoteProjectName, branchName);
427426
processingFacade.getTraversal().visit(element);
428427
processingFacade.commit(element);
429428
System.out.println();
@@ -436,11 +435,11 @@ public String publish(String elementName, String projectName, List<String> help)
436435

437436
protected String publish(String name) {
438437
return "-h".equals(name)?
439-
publish(null, null, Collections.singletonList("true")):
440-
publish(name, null, Collections.emptyList());
438+
publish(null, null, null, Collections.singletonList("true")):
439+
publish(name, null, null, Collections.emptyList());
441440
}
442441

443-
public String loadByName(String projectName, List<String> help) {
442+
public String loadByName(String projectName, String branchName, List<String> help) {
444443
if (Strings.isNullOrEmpty(projectName)) {
445444
return help.isEmpty()? "": SysMLInteractiveHelp.getLoadHelp();
446445
}
@@ -454,75 +453,94 @@ public String loadByName(String projectName, List<String> help) {
454453
return "ERROR:Project doesn't exist.";
455454
}
456455

457-
return load(repositoryProject);
456+
return load(repositoryProject, branchName);
458457
}
459458

460-
public String loadById(String projectId, List<String> help) {
459+
public String loadById(String projectId, String branchId, List<String> help) {
461460
if (Strings.isNullOrEmpty(projectId)) {
462461
return help.isEmpty()? "": SysMLInteractiveHelp.getLoadHelp();
463462
}
464463

465464
ProjectRepository repository = new ProjectRepository(apiBasePath);
466465

467466
System.out.println("Locating model");
468-
RemoteProject repositoryProject = repository.getPRojectById(UUID.fromString(projectId));
467+
RemoteProject remoteProject = repository.getPRojectById(UUID.fromString(projectId));
469468

470-
if (repositoryProject == null) {
469+
if (remoteProject == null) {
471470
return "ERROR:Project doesn't exist.";
472471
}
473472

474-
return load(repositoryProject);
473+
if (branchId == null) {
474+
return load(remoteProject, (UUID) null);
475+
} else {
476+
UUID branchUUID = UUID.fromString(branchId);
477+
return load(remoteProject, branchUUID);
478+
}
479+
}
480+
481+
private String load(RemoteProject remoteProject, String branchName) {
482+
final RemoteBranch branch;
483+
if (branchName == null) {
484+
branch = remoteProject.getDefaultBranch();
485+
} else {
486+
branch = remoteProject.getBranch(branchName);
487+
}
488+
return load(branch);
489+
}
490+
491+
private String load(RemoteProject remoteProject, UUID branchId) {
492+
final RemoteBranch branch;
493+
if (branchId == null) {
494+
branch = remoteProject.getDefaultBranch();
495+
} else {
496+
branch = remoteProject.getBranch(branchId);
497+
}
498+
return load(branch);
475499
}
476500

477-
private String load(RemoteProject repositoryProject) {
478-
try {
479-
RemoteBranch defaultBranch = repositoryProject.getDefaultBranch();
480-
Revision headRevision = defaultBranch.getHeadRevision();
481-
APIModel model = headRevision.fetchRemote();
482-
483-
System.out.println("Collecting UUIDs...");
484-
if (!tracker.isLibraryTracked()) {
485-
tracker.trackLibraryUUIDs(getLibraryResources());
486-
}
487-
488-
tracker.clear();
489-
List<Resource> inputResources = getInputResources();
490-
//UUIDS coming from resources that were added later in time will shadow previous ones
491-
tracker.trackLocalUUIDs(inputResources);
492-
493-
EMFModelRefresher fetcher = new EMFModelRefresher(model, tracker);
494-
495-
System.out.println("Downloading model...");
496-
EMFModelDelta delta = fetcher.create();
497-
498-
System.out.println("Adding model to index");
499-
delta.getProjectRoots().forEach((eObject, dto) -> {
500-
next(SYSMLX_EXTENSION);
501-
Resource xmiResource = getResource();
502-
if (eObject instanceof Namespace) {
503-
xmiResource.getContents().add(eObject);
504-
} else {
505-
Namespace root = SysMLFactory.eINSTANCE.createNamespace();
506-
NamespaceUtil.addOwnedMemberTo(root, (Element) eObject);
507-
xmiResource.getContents().add(root);
508-
}
509-
addResourceToIndex(xmiResource);
510-
});
511-
512-
return "Project loaded: " + repositoryProject.getProjectName() + ", " + repositoryProject.getRemoteId().toString();
513-
} catch (NoSuchElementException e) {
514-
e.printStackTrace();
515-
return "Requested model doesn't exist.";
516-
} catch (ApiException e) {
517-
e.printStackTrace();
518-
return "Error occured while trying to load model";
501+
private String load(RemoteBranch branch) {
502+
RemoteProject remoteProject = branch.getRemoteProject();
503+
Revision headRevision = branch.getHeadRevision();
504+
APIModel model = headRevision.fetchRemote();
505+
506+
System.out.println("Selected branch " + branch.getName());
507+
508+
System.out.println("Collecting UUIDs...");
509+
if (!tracker.isLibraryTracked()) {
510+
tracker.trackLibraryUUIDs(getLibraryResources());
519511
}
512+
513+
tracker.clear();
514+
List<Resource> inputResources = getInputResources();
515+
//UUIDS coming from resources that were added later in time will shadow previous ones
516+
tracker.trackLocalUUIDs(inputResources);
517+
518+
EMFModelRefresher fetcher = new EMFModelRefresher(model, tracker);
519+
520+
System.out.println("Downloading model...");
521+
EMFModelDelta delta = fetcher.create();
522+
523+
System.out.println("Adding model to index");
524+
delta.getProjectRoots().forEach((eObject, dto) -> {
525+
next(SYSMLX_EXTENSION);
526+
Resource xmiResource = getResource();
527+
if (eObject instanceof Namespace) {
528+
xmiResource.getContents().add(eObject);
529+
} else {
530+
Namespace root = SysMLFactory.eINSTANCE.createNamespace();
531+
NamespaceUtil.addOwnedMemberTo(root, (Element) eObject);
532+
xmiResource.getContents().add(root);
533+
}
534+
addResourceToIndex(xmiResource);
535+
});
536+
537+
return "Project loaded: " + remoteProject.getProjectName() + ", " + remoteProject.getRemoteId().toString();
520538
}
521539

522540
protected String download(String name) {
523541
return "-h".equals(name)?
524-
loadByName(null, Collections.singletonList("true")):
525-
loadByName(name, Collections.emptyList());
542+
loadByName(null, null, Collections.singletonList("true")):
543+
loadByName(name, null, Collections.emptyList());
526544
}
527545

528546
public String listPublications(List<String> help) {
@@ -535,9 +553,9 @@ public String listPublications(List<String> help) {
535553
.collect(Collectors.joining("\n"));
536554
}
537555

538-
protected ApiElementProcessingFacade getApiElementProcessingFacade(String modelName) {
556+
protected ApiElementProcessingFacade getApiElementProcessingFacade(String modelName, String branchName) {
539557
System.out.println("API base path: " + this.apiBasePath);
540-
ApiElementProcessingFacade processingFacade = new ApiElementProcessingFacade(modelName, this.apiBasePath);
558+
ApiElementProcessingFacade processingFacade = new ApiElementProcessingFacade(modelName, branchName, this.apiBasePath);
541559
processingFacade.setIsIncludeDerived(true);
542560
processingFacade.setTraversal(new Traversal(processingFacade));
543561
return processingFacade;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class Load {
3535
.keyword("id")
3636
.keyword("name")
3737
.optional("name")
38+
.keyword("branch")
3839
.flag("help", 'h', "true")
3940
.build();
4041

@@ -44,13 +45,15 @@ public static String load(List<String> args) {
4445
List<String> help = vals.get("help");
4546
List<String> name = vals.get("name");
4647
List<String> id = vals.get("id");
48+
List<String> branches = vals.get("branch");
49+
String branch = branches.isEmpty()? null: branches.get(0);
4750

4851
if (!name.isEmpty() && !id.isEmpty()) {
4952
return "Name and id cannot be provided at the same time.";
5053
} else if (!name.isEmpty()) {
51-
return ISysML.getKernelInstance().getInteractive().loadByName(name.get(0), help);
54+
return ISysML.getKernelInstance().getInteractive().loadByName(name.get(0), branch, help);
5255
} else if (!id.isEmpty()) {
53-
return ISysML.getKernelInstance().getInteractive().loadById(id.get(0), help);
56+
return ISysML.getKernelInstance().getInteractive().loadById(id.get(0), branch, help);
5457
} else {
5558
return ISysML.getKernelInstance().getInteractive().help("%load");
5659
}

0 commit comments

Comments
 (0)