From fe2d0aa1040d3e37d828b536c092558293d793fe Mon Sep 17 00:00:00 2001 From: n_jansen Date: Thu, 5 Dec 2024 14:27:07 +0100 Subject: [PATCH 1/3] Add workflow doc. --- .github/workflows/gradle.yml | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 .github/workflows/gradle.yml diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml new file mode 100644 index 00000000..15ad7c5f --- /dev/null +++ b/.github/workflows/gradle.yml @@ -0,0 +1,52 @@ +# (c) https://github.com/MontiCore/monticore +name: Gradle + + +concurrency: # run this test workflow only once per "branch" + group: ${{ github.repository }}-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +on: + push: # run this test pipeline on every push + pull_request: # and pull request + repository_dispatch: # and on request of upstream projects + types: [ trigger_after_upstream_deploy ] + + +env: + GRADLE_VERSION: 7.4 # Gradle version used + GRADLE_CLI_OPTS: "-Pci -PgenTR=false --max-workers=4 -PgenTagging=true --build-cache " # CLI options passed to Gradle + +permissions: + contents: read # This action may run somewhat unsafe code + + +jobs: + build: + runs-on: ubuntu-20.04 + steps: + - name: Checkout project sources + uses: actions/checkout@v3 + - name: Gradle build + uses: gradle/actions/setup-gradle@v3 + with: + gradle-version: ${{env.GRADLE_VERSION}} + arguments: build ${{env.GRADLE_CLI_OPTS}} + + deploy: + permissions: + packages: write + runs-on: ubuntu-20.04 + needs: build + if: github.ref == 'refs/heads/dev' + steps: + - name: Checkout project sources + uses: actions/checkout@v3 + - name: Gradle publish + uses: gradle/actions/setup-gradle@v3 + with: + gradle-version: ${{env.GRADLE_VERSION}} + arguments: publish ${{env.GRADLE_CLI_OPTS}} -PmavenPassword=${{ secrets.SE_NEXUS_PASSWORD }} -PmavenUser=${{ secrets.SE_NEXUS_USER }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + From d8604233eaf1ed9ba112a28b625e592dd68e7c27 Mon Sep 17 00:00:00 2001 From: n_jansen Date: Thu, 5 Dec 2024 16:40:58 +0100 Subject: [PATCH 2/3] Correct CLI tool parameter assignment and fix symbol table storage. --- gentest/build.gradle | 2 +- .../monticore/od2cd/CompositionPrinter.java | 8 ++--- .../monticore/od2cd/OD2CDObjectVisitor.java | 4 +-- .../od4development/OD4DevelopmentTool.java | 35 ++++++++++++++++--- .../OD4DevelopmentScopesGenitor.java | 12 +++++-- .../OD4DevelopmentToolTest.java | 13 +++++-- .../resources/symboltable/tooltest/A.cdsym | 14 -------- .../symboltable/tooltest/examples/A.cdsym | 15 ++++++++ 8 files changed, 73 insertions(+), 30 deletions(-) delete mode 100644 src/test/resources/symboltable/tooltest/A.cdsym create mode 100644 src/test/resources/symboltable/tooltest/examples/A.cdsym diff --git a/gentest/build.gradle b/gentest/build.gradle index e61f480b..0791f7c5 100644 --- a/gentest/build.gradle +++ b/gentest/build.gradle @@ -31,7 +31,7 @@ sourceSets { task genExample(type: JavaExec) { classpath += parent.tasks.shadowJar.outputs.files mainClass = 'de.monticore.od4development.OD4DevelopmentTool' - args '-i','src/main/resources/Example.od', '-s' , 'src/main/resources/symboltable/', '-o', 'target/generated-sources/' + args '-i','src/main/resources/Example.od', '-path' , 'src/main/resources/symboltable/', '-o', 'target/generated-sources/' dependsOn parent.tasks.shadowJar } diff --git a/src/main/java/de/monticore/od2cd/CompositionPrinter.java b/src/main/java/de/monticore/od2cd/CompositionPrinter.java index 3855998b..760f7747 100644 --- a/src/main/java/de/monticore/od2cd/CompositionPrinter.java +++ b/src/main/java/de/monticore/od2cd/CompositionPrinter.java @@ -16,7 +16,7 @@ public String create(ASTMCType type) { IOD4DevelopmentGlobalScope gs = OD4DevelopmentMill.globalScope(); - if (gs.getSubScopes().size() == 1) { + if (gs.getSubScopes().size() >= 1) { IOD4DevelopmentArtifactScope as = (IOD4DevelopmentArtifactScope) gs.getSubScopes().get(0); if (as.getTypeSymbols().containsKey(type.printType()) || @@ -39,7 +39,7 @@ public String read() { public String write(String type) { IOD4DevelopmentGlobalScope gs = OD4DevelopmentMill.globalScope(); - if (gs.getSubScopes().size() == 1) { + if (gs.getSubScopes().size() >= 1) { return ".build().get()"; } return ".build()"; @@ -48,7 +48,7 @@ public String write(String type) { public String update(String attribute, String value) { IOD4DevelopmentGlobalScope gs = OD4DevelopmentMill.globalScope(); - if (gs.getSubScopes().size() == 1) { + if (gs.getSubScopes().size() >= 1) { String asName = gs.getSubScopes().get(0).getName(); String res = ""; @@ -69,7 +69,7 @@ public String update(String attribute, String value) { public String genType(ASTMCType type) { IOD4DevelopmentGlobalScope gs = OD4DevelopmentMill.globalScope(); String genType = OD4DevelopmentMill.prettyPrint(type, false); - if (gs.getSubScopes().size() == 1) { + if (gs.getSubScopes().size() >= 1) { return genType + "Builder"; } else { return genType; diff --git a/src/main/java/de/monticore/od2cd/OD2CDObjectVisitor.java b/src/main/java/de/monticore/od2cd/OD2CDObjectVisitor.java index fc3bcd62..e8667877 100644 --- a/src/main/java/de/monticore/od2cd/OD2CDObjectVisitor.java +++ b/src/main/java/de/monticore/od2cd/OD2CDObjectVisitor.java @@ -286,7 +286,7 @@ protected String constructLink(String src, String tgt, String roleName, Optional cardModifier = cardModifier(cdRole.get()); } - if (OD4DevelopmentMill.globalScope().getSubScopes().size() == 1) { + if (OD4DevelopmentMill.globalScope().getSubScopes().size() >= 1) { return src + "." + ((roleName.isEmpty()) ? findObjectAccessorName4Role(tgt) + cardModifier : roleName + cardModifier) + "(" + tgt + ")"; @@ -300,7 +300,7 @@ protected String constructLink(String src, String tgt, String roleName, Optional protected String cardModifier(CDRoleAdapter cdRole) { String cardModifier = ""; - if (OD4DevelopmentMill.globalScope().getSubScopes().size() == 1) { + if (OD4DevelopmentMill.globalScope().getSubScopes().size() >= 1) { switch (cdRole.getCardinality()) { case ONE: cardModifier = ""; diff --git a/src/main/java/de/monticore/od4development/OD4DevelopmentTool.java b/src/main/java/de/monticore/od4development/OD4DevelopmentTool.java index ebe27184..e50e67ad 100644 --- a/src/main/java/de/monticore/od4development/OD4DevelopmentTool.java +++ b/src/main/java/de/monticore/od4development/OD4DevelopmentTool.java @@ -14,16 +14,20 @@ import de.monticore.od4development._cocos.OD4DevelopmentCoCoChecker; import de.monticore.od4development._cocos.OD4DevelopmentCoCos; import de.monticore.od4development._symboltable.CDRoleSymbolDeSer; +import de.monticore.od4development._symboltable.IOD4DevelopmentArtifactScope; import de.monticore.odbasis._ast.ASTODArtifact; import de.monticore.odbasis._prettyprint.ODBasisFullPrettyPrinter; import de.monticore.prettyprint.IndentPrinter; import de.monticore.symbols.basicsymbols.BasicSymbolsMill; import de.monticore.types.mcbasictypes._ast.ASTMCImportStatement; +import de.se_rwth.commons.Names; import de.se_rwth.commons.logging.Log; import org.apache.commons.cli.*; import org.apache.commons.lang3.StringUtils; import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; @@ -65,9 +69,9 @@ public void run(String[] args) { // parse input file, which is now available // (only returns if successful) ASTODArtifact ast = parse(cmd.getOptionValue("i")); - - if(cmd.hasOption("s")) { - MCPath mcPath = new MCPath(cmd.getOptionValue("s")); + + if(cmd.hasOption("path")) { + MCPath mcPath = new MCPath(cmd.getOptionValue("path")); OD4DevelopmentMill.globalScope().setSymbolPath(mcPath); OD4DevelopmentMill.globalScope().putTypeSymbolDeSer("de.monticore.cdbasis._symboltable.CDTypeSymbol"); OD4DevelopmentMill.globalScope().putSymbolDeSer("de.monticore.cdassociation._symboltable.CDRoleSymbol", new CDRoleSymbolDeSer()); @@ -76,8 +80,12 @@ public void run(String[] args) { for (ASTMCImportStatement i : ast.getMCImportStatementList()) { OD4DevelopmentMill.globalScope().loadDiagram(i.getQName()); } - } else { - createSymbolTable(ast); + } + + IOD4DevelopmentArtifactScope as = createSymbolTable(ast); + + if (cmd.hasOption("s")) { + storeSymTab(as, cmd.getOptionValue("s")); } if (cmd.hasOption("c")) { @@ -145,4 +153,21 @@ public Options addAdditionalOptions(Options options) { options.addOption(new Option("o","output",true,"Sets the output path")); return options; } + + /** + * prints the symboltable of the given scope out to a file + * + * @param as symboltable to store + * @param path location of the file or directory containing the printed table + */ + public void storeSymTab(IOD4DevelopmentArtifactScope as, String path) { + if (Path.of(path).toFile().isFile()) { + this.storeSymbols(as, path); + } + else { + this.storeSymbols( + as, + Paths.get(path, Names.getPathFromPackage(as.getFullName()) + ".odsym").toString()); + } + } } \ No newline at end of file diff --git a/src/main/java/de/monticore/od4development/_symboltable/OD4DevelopmentScopesGenitor.java b/src/main/java/de/monticore/od4development/_symboltable/OD4DevelopmentScopesGenitor.java index 9bf49491..15cddcb5 100644 --- a/src/main/java/de/monticore/od4development/_symboltable/OD4DevelopmentScopesGenitor.java +++ b/src/main/java/de/monticore/od4development/_symboltable/OD4DevelopmentScopesGenitor.java @@ -2,6 +2,7 @@ package de.monticore.od4development._symboltable; import de.monticore.odbasis._ast.ASTODArtifact; +import de.monticore.symboltable.ImportStatement; import de.se_rwth.commons.logging.Log; import java.util.ArrayList; @@ -11,14 +12,21 @@ public class OD4DevelopmentScopesGenitor extends OD4DevelopmentScopesGenitorTOP public IOD4DevelopmentArtifactScope createFromAST (ASTODArtifact rootNode) { Log.errorIfNull(rootNode, "0xA7004x78995 Error by creating of the OD4DevelopmentScopesGenitor symbol table: top ast node is null"); IOD4DevelopmentArtifactScope artifactScope = de.monticore.od4development.OD4DevelopmentMill.artifactScope(); + + // set package if (rootNode.isPresentMCPackageDeclaration()) { artifactScope.setPackageName( rootNode.getMCPackageDeclaration().getMCQualifiedName().getQName()); - } - else { + } else { artifactScope.setPackageName(""); } + + // add imports artifactScope.setImportsList(new ArrayList<>()); + rootNode + .getMCImportStatementList() + .forEach(i -> artifactScope.addImports(new ImportStatement(i.getQName(), i.isStar()))); + artifactScope.setName(rootNode.getObjectDiagram().getName()); artifactScope.setAstNode(rootNode); putOnStack(artifactScope); diff --git a/src/test/java/de/monticore/od4development/OD4DevelopmentToolTest.java b/src/test/java/de/monticore/od4development/OD4DevelopmentToolTest.java index 3d7df1e9..778efdc8 100644 --- a/src/test/java/de/monticore/od4development/OD4DevelopmentToolTest.java +++ b/src/test/java/de/monticore/od4development/OD4DevelopmentToolTest.java @@ -6,6 +6,8 @@ import org.junit.Test; import org.junit.jupiter.api.BeforeEach; +import java.io.File; + import static org.junit.Assert.assertTrue; public class OD4DevelopmentToolTest { @@ -21,8 +23,15 @@ public void before() { @Test public void testAddSymtabFile() { assertTrue(OD4DevelopmentMill.globalScope().getSymbolPath().isEmpty()); - OD4DevelopmentToolTOP.main(new String[] {"-i","src/test/resources/examples/od2cd/Example.od", "-s", "resources/symboltable/tooltest/"}); - assertTrue(OD4DevelopmentMill.globalScope().getSymbolPath().toString().endsWith("resources/symboltable/tooltest]")); + OD4DevelopmentToolTOP.main(new String[] {"-i","src/test/resources/examples/od2cd/Example.od", "-path", "src/test/resources/symboltable/tooltest/"}); + assertTrue(OD4DevelopmentMill.globalScope().getSymbolPath().toString().endsWith("resources/symboltable/tooltest/]")); + } + + @Test + public void testStoreSymtabFile() { + OD4DevelopmentToolTOP.main(new String[] {"-i","src/test/resources/examples/od2cd/Example.od", "-path", "src/test/resources/symboltable/tooltest/", "-s", "target/generated-test-sources/tooltest/symboltable/"}); + File symTab = new File("target/generated-test-sources/tooltest/symboltable/examples/od2cd/Examples.odsym"); + assertTrue(symTab.exists() && symTab.isFile()); } } diff --git a/src/test/resources/symboltable/tooltest/A.cdsym b/src/test/resources/symboltable/tooltest/A.cdsym deleted file mode 100644 index 68b1f6a8..00000000 --- a/src/test/resources/symboltable/tooltest/A.cdsym +++ /dev/null @@ -1,14 +0,0 @@ -{ - "generated-using": "www.MontiCore.de technology", - "name": "examples.A", - "symbols": [ - { - "kind": "de.monticore.symbols._symboltable.Type", - "name": "examples.A" - }, - { - "kind": "de.monticore.symbols._symboltable.Type", - "name": "examples.B" - } - ] -} \ No newline at end of file diff --git a/src/test/resources/symboltable/tooltest/examples/A.cdsym b/src/test/resources/symboltable/tooltest/examples/A.cdsym new file mode 100644 index 00000000..cfc554ed --- /dev/null +++ b/src/test/resources/symboltable/tooltest/examples/A.cdsym @@ -0,0 +1,15 @@ +{ + "generated-using": "www.MontiCore.de technology", + "package": "examples", + "name": "A", + "symbols": [ + { + "kind": "de.monticore.symbols.basicsymbols._symboltable.TypeSymbol", + "name": "A" + }, + { + "kind": "de.monticore.symbols.basicsymbols._symboltable.TypeSymbol", + "name": "B" + } + ] +} \ No newline at end of file From 8d2eb5c26ba4bea9bfcc497c5d6320174843e1c4 Mon Sep 17 00:00:00 2001 From: n_jansen Date: Wed, 11 Dec 2024 18:25:43 +0100 Subject: [PATCH 3/3] Add instances container as result of object initialization. --- gentest/src/test/java/GeneratedClassesTest.java | 7 +++++-- .../de/monticore/od2cd/OD2CDObjectVisitor.java | 17 ++++++++++++++++- src/main/resources/od2cd/InstList.ftl | 10 ++++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/gentest/src/test/java/GeneratedClassesTest.java b/gentest/src/test/java/GeneratedClassesTest.java index 5df2bab2..eb216c8e 100644 --- a/gentest/src/test/java/GeneratedClassesTest.java +++ b/gentest/src/test/java/GeneratedClassesTest.java @@ -6,6 +6,8 @@ import example.B; import examples.ExamplesInstantiator; import java.util.List; + +import examples.ExamplesODInstances; import org.junit.Before; import org.junit.Test; @@ -21,7 +23,8 @@ public void setup() { @Test public void test() { - List list = new ExamplesInstantiator().instantiate(); - assertEquals(5, list.size()); + ExamplesODInstances objs = new ExamplesInstantiator().instantiate(); + assertEquals(5, objs.getFoo().getX()); + assertEquals("hello", objs.getFoo().getS()); } } \ No newline at end of file diff --git a/src/main/java/de/monticore/od2cd/OD2CDObjectVisitor.java b/src/main/java/de/monticore/od2cd/OD2CDObjectVisitor.java index e8667877..5f72cc6a 100644 --- a/src/main/java/de/monticore/od2cd/OD2CDObjectVisitor.java +++ b/src/main/java/de/monticore/od2cd/OD2CDObjectVisitor.java @@ -31,6 +31,7 @@ public class OD2CDObjectVisitor implements ODBasisVisitor2 { protected ASTCDPackage cdPackage; protected ASTCDClass instantiatorClass; + protected ASTCDClass instancesClass; protected ASTCDClass checkerClass; @@ -83,6 +84,11 @@ public void visit(ASTODArtifact odArtifact) { .setModifier(CDBasisMill.modifierBuilder().PUBLIC().build()) .build(); + this.instancesClass = CDBasisMill.cDClassBuilder() + .setName(odArtifact.getObjectDiagram().getName() + "ODInstances") + .setModifier(CDBasisMill.modifierBuilder().PUBLIC().build()) + .build(); + this.checkerClass = CDBasisMill.cDClassBuilder() .setName(odArtifact.getObjectDiagram().getName() + "Checker") .setModifier(CDBasisMill.modifierBuilder().PUBLIC().build()) @@ -90,10 +96,13 @@ public void visit(ASTODArtifact odArtifact) { cd4C.addImport(instantiatorClass, imp.getQName() + ".*"); cd4C.addImport(instantiatorClass, "java.time.*"); + cd4C.addImport(instancesClass, imp.getQName() + ".*"); + cd4C.addImport(instancesClass, "java.time.*"); cd4C.addImport(checkerClass, imp.getQName() + ".*"); this.cdPackage.addCDElement(instantiatorClass); + this.cdPackage.addCDElement(instancesClass); // this.cdPackage.addCDElement(checkerClass); } @@ -113,7 +122,8 @@ public void endVisit(ASTODArtifact odArtifact) { .map(e -> ((ASTODNamedObject) e).getName()) .collect(Collectors.toList()), this.linkAttributeList, - this.objectNameList); + this.objectNameList, + odArtifact.getObjectDiagram().getName()); } public void createInstantiator(ASTODNamedObject odElement) { @@ -132,6 +142,10 @@ public void createInstantiator(ASTODNamedObject odElement) { this.objectNameList.add(odElement.getName()); } + + public void createInstanceAccess(ASTODNamedObject odElement) { + this.cd4C.addAttribute(this.instancesClass, true, true, cp.genType(odElement.getMCObjectType()) + " " + odElement.getName()); + } public void createChecker(ASTODNamedObject odElement) { this.cd4C.addMethod(checkerClass, "od2cd.Check", @@ -224,6 +238,7 @@ else if (cdRoleOfLeft.isPresent() && !cdRoleOfRight.isPresent()) { public void visit(ASTODElement odElement) { if(odElement instanceof ASTODNamedObject) { createInstantiator((ASTODNamedObject) odElement); + createInstanceAccess((ASTODNamedObject) odElement); createChecker((ASTODNamedObject) odElement); } if(odElement instanceof ASTODLink) { diff --git a/src/main/resources/od2cd/InstList.ftl b/src/main/resources/od2cd/InstList.ftl index 85617bb1..6d5b829f 100644 --- a/src/main/resources/od2cd/InstList.ftl +++ b/src/main/resources/od2cd/InstList.ftl @@ -1,7 +1,8 @@ <#-- (c) https://github.com/MontiCore/monticore --> -${tc.signature("types", "names", "links", "objects")} -${cd4c.method("public List instantiate()")} +${tc.signature("types", "names", "links", "objects", "odname")} +${cd4c.method("public " + odname + "ODInstances instantiate()")} +${odname}ODInstances _inst = new ${odname}ODInstances(); <#list objects as object> ${types[object?index]} ${names[object?index]} = instantiate${object?capFirst}(); @@ -10,6 +11,7 @@ ${cd4c.method("public List instantiate()")} List objects = new ArrayList<>(); <#list objects as object> - objects.add(${object}${cp.write(types[object?index])}); + //# objects.add(...) + _inst.set${object?capFirst}((${types[object?index]})${object}${cp.write(types[object?index])}); -return objects; \ No newline at end of file +return _inst; \ No newline at end of file