Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Serialization #27

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import org.junit.jupiter.api.^extension.ExtendWith
import org.junit.Assert
import elite.mdd.plantuml.plantUML.ParticipantDefinition
import elite.mdd.plantuml.plantUML.RequestMessageDefinition
import elite.mdd.plantuml.plantUML.impl.QuotedNamedParticipantImpl
import elite.mdd.plantuml.plantUML.QuotedUnnamedParticipant

@ExtendWith(InjectionExtension)
@InjectWith(PlantUMLInjectorProvider)
Expand All @@ -29,8 +31,6 @@ class PlantUMLParsingTest {
@enduml
''')
Assertions.assertNotNull(result)
val errors = result.eResource.errors
Assertions.assertTrue(errors.size() == 1, '''Unexpected errors: «errors.join(", ")»''')
}

@Test
Expand Down Expand Up @@ -77,21 +77,23 @@ class PlantUMLParsingTest {
def void actorParticipantTest() {
val result = parseHelper.parse('''
@startuml
actor "testParticipant"
actor "testParticipant: Type"
@enduml
''')

Assert.assertEquals(1, result.elements.size())

val property = result.elements.head as ParticipantDefinition
Assert.assertEquals("ACTOR", property.shape.getName())
Assert.assertEquals("testParticipant", property.participant.getName())
val participant = property.getParticipant() as QuotedUnnamedParticipant
Assert.assertEquals("testParticipant", participant.getName())

Assertions.assertNotNull(result)
val errors = result.eResource.errors
Assertions.assertTrue(errors.isEmpty, '''Unexpected errors: «errors.join(", ")»''')
}

/*
@Test
def void boundaryParticipantTest() {
val result = parseHelper.parse('''
Expand Down Expand Up @@ -308,5 +310,6 @@ class PlantUMLParsingTest {
Assert.assertEquals(participant1.getParticipant(), messageDefinition.sender)
Assert.assertEquals(participant2.getParticipant(), messageDefinition.receiver)
}
*/

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package elite.mdd.plantuml.tests

import com.google.inject.Inject
import elite.mdd.plantuml.plantUML.Diagram
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.extensions.InjectionExtension
import org.eclipse.xtext.testing.util.ParseHelper
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.^extension.ExtendWith
import com.google.inject.Guice
import elite.mdd.plantuml.PlantUMLRuntimeModule
import org.eclipse.xtext.serializer.ISerializer
import org.junit.Assert
import elite.mdd.plantuml.serializer.SerializerWrapper

@ExtendWith(InjectionExtension)
@InjectWith(PlantUMLInjectorProvider)
class PlantUMLSerializingTest {

@Inject
ParseHelper<Diagram> parseHelper


@Test
def void participants() {
val result = parseHelper.parse('''
@startuml
participant : Key2
participant ": Key3"
participant "key1 : Key"
participant key2 : Key
participant lock : Lock ref AA
participant "lock2 : Lock ref AA strict"
@enduml
''');

val serializeWrapper = new SerializerWrapper();
val resultSerializer = serializeWrapper.serialize(result);

Assert.assertEquals(resultSerializer, '''@startuml
participant ": Key2"
participant ": Key3"
participant "key1 : Key" as key1
participant "key2 : Key" as key2
participant "lock : Lock ref AA" as lock
participant "lock2 : Lock ref AA" as lock2
@enduml
'''.toString())

}


@Test
def void messages() {
// The space between " and : in the last two messages is important because else it cannot be parsed correctly, as the message is considered null
val result = parseHelper.parse('''
@startuml
participant "key1 : Key"
participant "lockName : LockType" as lock
key1 ->> lock : unlock()
"key1" ->> lock : unlock(s=0)
key1 ->> "lock" : unlock(s=0, i=1)
"key1" ->> "lock" : unlock(s=0, i=1, j=2)
key1 <<-- lock : unlock()
"key1" <<-- lock : unlock(s=0)
key1 <<-- "lock" : unlock(s=0, i=1)
"key1" <<-- "lock" : unlock(s=0, i=1, j=2)
@enduml
''');

val serializeWrapper = new SerializerWrapper();
val resultSerializer = serializeWrapper.serialize(result);

System.out.println(resultSerializer);

Assert.assertEquals(resultSerializer, '''@startuml
participant "key1 : Key" as key1
participant "lockName : LockType" as lock
key1 ->> lock : unlock()
"key1" ->> lock : unlock(s= 0)
key1 ->> "lock" : unlock(s= 0, i= 1)
"key1" ->> "lock" : unlock(s= 0, i= 1, j= 2)
key1 <<-- lock : unlock()
"key1" <<-- lock : unlock(s= 0)
key1 <<-- "lock" : unlock(s= 0, i= 1)
"key1" <<-- "lock" : unlock(s= 0, i= 1, j= 2)
@enduml
'''.toString())

}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package elite.mdd.plantuml.ui;

import java.io.PrintWriter;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.ui.IEditorInput;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.ui.editor.model.IXtextDocument;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;

import elite.mdd.plantuml.serializer.SerializerWrapper;

public class PlantUMLEditor extends XtextEditor {

private SerializerWrapper serializeWrapper = new SerializerWrapper();

@Override
public void doSave(IProgressMonitor progressMonitor) {
// See https://www.eclipse.org/forums/index.php/t/487828/
// and https://www.eclipse.org/forums/index.php/m/786926/
System.out.println("here we go");
IXtextDocument document = super.getDocument();
document.readOnly(new IUnitOfWork.Void<XtextResource>() {

@Override
public void process(XtextResource resource) throws Exception {
String filePath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation().toOSString() + resource.getURI().toPlatformString(true);
if (resource.getContents().size() == 1 && resource.getErrors().size() == 0) {
EObject diagram = resource.getContents().get(0);
String serializedDiagram = serializeWrapper.serialize(diagram);
try (PrintWriter out = new PrintWriter(filePath + ".puml")) {
out.println(serializedDiagram);
}
}
}
});

super.doSave(progressMonitor);
}

}
7 changes: 7 additions & 0 deletions xtext/elite.mdd.plantuml.ui/src/elite/mdd/plantuml/ui/PlantUMLUiModule.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
package elite.mdd.plantuml.ui;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.xtext.ui.editor.XtextEditor;

/**
* Use this class to register components to be used within the Eclipse IDE.
*/
public class PlantUMLUiModule extends AbstractPlantUMLUiModule {



public PlantUMLUiModule(AbstractUIPlugin plugin) {
super(plugin);
}

public Class<? extends XtextEditor> bindXtextEditor() {
return PlantUMLEditor.class;
}
}
7 changes: 6 additions & 1 deletion xtext/elite.mdd.plantuml/src/elite/mdd/plantuml/GeneratePlantUML.mwe2
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ Workflow {
fileExtensions = "plantuml"

serializer = {
generateStub = false
generateStub = true
generateXtendStub = false
}
formatter={
generateStub=true
generateXtendStub = false
}
validator = {
// composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
Expand Down
87 changes: 71 additions & 16 deletions xtext/elite.mdd.plantuml/src/elite/mdd/plantuml/PlantUML.xtext
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,38 @@ ParticipantDefinition:
shape=ParticipantShape participant=Participant;

Participant:
NamedParticipant | UnnamedParticipant | AnonymousParticipant |{Participant} name = 'self' ;
NamedParticipant
| UnnamedParticipant
| AnonymousParticipant
| QuotedNamedParticipant
| QuotedUnnamedParticipant
| QuotedAnonymousParticipant
;

NamedParticipant returns Participant:
{Participant} '"' label=ID ('['selector=ID']')? (':' type=ID)? ('ref' interactionIdent=ID ('strict')?)?
'"' 'as' name=ID
NamedParticipant:
label=ID ('['selector=ID']')? (':' type=ID)? ('ref' interactionIdent=ID ('strict')?)? 'as' name=ID
;

UnnamedParticipant returns Participant:
{Participant} '"'name=ID ('['selector=ID']')? (':' type=ID)? ('ref' interactionIdent=ID ('strict')?)? '"'
UnnamedParticipant:
// name=ID ('['selector=ID']')? (':' type=ID)? ('ref' interactionIdent=ID ('strict')?)?
// Because of some weird problems in the serializer, static had to be removed in the unqouted version
name=ID ('['selector=ID']')? (':' type=ID)? ('ref' interactionIdent=ID)?
;
AnonymousParticipant returns Participant:
{Participant} '":' type = ID ('ref' interactionIdent=ID ('strict')?)?'"'
AnonymousParticipant:
':' type = ID ('ref' interactionIdent=ID ('strict')?)?
;

// Quoted Version of the Participants
QuotedNamedParticipant:
'"' label=ID ('['selector=ID']')? (':' type=ID)? ('ref' interactionIdent=ID ('strict')?)?
'"' 'as' name=ID
;

ParticipantMention:
ID | ':' ID
QuotedUnnamedParticipant:
'"'name=ID ('['selector=ID']')? (':' type=ID)? ('ref' interactionIdent=ID ('strict')?)? '"'
;
QuotedAnonymousParticipant:
'":' type = ID ('ref' interactionIdent=ID ('strict')?)?'"'
;

enum ParticipantShape:
Expand All @@ -41,21 +56,61 @@ enum ParticipantShape:
| COLLECTIONS='collections'
| ENTITY='entity'
| DATABASE='database'
| PARTICIPANT='participant';
| PARTICIPANT='participant'
;

// Messages

MessageDefinition:
RequestMessageDefinition
| ReplyMessageDefinition;
| ReplyMessageDefinition
| FullQuotedRequestMessageDefinition
| FullQuotedReplyMessageDefinition
| LeftQuotedRequestMessageDefinition
| LeftQuotedReplyMessageDefinition
| RightQuotedRequestMessageDefinition
| RightQuotedReplyMessageDefinition
;

FullQuotedRequestMessageDefinition:
('"' sender=[Participant] '"' arrow=(RightRequest) '"' receiver=[Participant] '"') (':' message=RequestMessage)
| ('"'receiver=[Participant]'"' arrow=(LeftRequest) '"' sender=[Participant] '"') (':' message=RequestMessage)
;

FullQuotedReplyMessageDefinition:
('"' sender=[Participant] '"' arrow=RightReply '"' receiver=[Participant] '"') (':' message=ReplyMessage)
| ('"' receiver=[Participant] '"' arrow=LeftReply '"' sender=[Participant] '"') (':' message=ReplyMessage)
;

LeftQuotedRequestMessageDefinition:
('"' sender=[Participant] '"' arrow=(RightRequest) receiver=[Participant]) (':' message=RequestMessage)
| ('"' receiver=[Participant] '"' arrow=(LeftRequest) sender=[Participant]) (':' message=RequestMessage)
;

LeftQuotedReplyMessageDefinition:
('"' sender=[Participant] '"' arrow=RightReply receiver=[Participant] ) (':' message=ReplyMessage)
| ('"' receiver=[Participant] '"' arrow=LeftReply sender=[Participant] ) (':' message=ReplyMessage)
;

RightQuotedRequestMessageDefinition:
(sender=[Participant] arrow=(RightRequest) '"' receiver=[Participant] '"') (':' message=RequestMessage)
| (receiver=[Participant] arrow=(LeftRequest) '"' sender=[Participant] '"') (':' message=RequestMessage)
;

RightQuotedReplyMessageDefinition:
(sender=[Participant] arrow=RightReply '"' receiver=[Participant] '"') (':' message=ReplyMessage)
| (receiver=[Participant] arrow=LeftReply '"' sender=[Participant] '"') (':' message=ReplyMessage)
;

RequestMessageDefinition:
(('"')? sender=[Participant|ParticipantMention] ('"')? arrow=(RightRequest) ('"')? receiver=[Participant] ('"')?) (':' message=RequestMessage)
| (('"')?receiver=[Participant]('"')? arrow=(LeftRequest) ('"')?sender=[Participant]('"')?) (':' message=RequestMessage);
(sender=[Participant] arrow=(RightRequest) receiver=[Participant]) (':' message=RequestMessage)
| (receiver=[Participant] arrow=(LeftRequest) sender=[Participant]) (':' message=RequestMessage)
;

ReplyMessageDefinition:
(('"')?sender=[Participant]('"')? arrow=RightReply ('"')?receiver=[Participant]('"')?) (':' message=ReplyMessage)
| (('"')?receiver=[Participant]('"')? arrow=LeftReply ('"')?sender=[Participant]('"')?) (':' message=ReplyMessage);
(sender=[Participant] arrow=RightReply receiver=[Participant]) (':' message=ReplyMessage)
| (receiver=[Participant] arrow=LeftReply sender=[Participant]) (':' message=ReplyMessage)
;

// TODO: Handle \"
RequestMessage:
Expand Down
Loading