Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » EMF - adding new model objects programmatically
EMF - adding new model objects programmatically [message #1837938] Thu, 11 February 2021 06:24 Go to next message
Andrej Liebert is currently offline Andrej LiebertFriend
Messages: 18
Registered: January 2021
Junior Member
Hello together,

I have a simple test code called example.tdlan2 written in Test Description Language (TDL) generated by Xtext.
Package Example {
	Type STRING ;
	Gate Type HTTP accepts STRING ;
	Component Type EndPoint having {
		gate http of type HTTP ;
	}
	Test Configuration BasicClientServer {
		create SUT server of type EndPoint ;
		create Tester client of type EndPoint ;
		connect server.http to client.http ;
	}
	Test Description TD1 uses configuration BasicClientServer {
		client.http sends "GET /store" to server.http ;
		server.http sends "200 OK" to client.http ;
	}
}


which can be transferred to an XMI representation:
<?xml version="1.0" encoding="UTF-8"?>
<tdl:Package xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tdl="http://www.etsi.org/spec/TDL/1.4.1" name="Example">
  <packagedElement xsi:type="tdl:SimpleDataType" name="STRING"/>
  <packagedElement xsi:type="tdl:GateType" name="HTTP" dataType="//@packagedElement.0"/>
  <packagedElement xsi:type="tdl:ComponentType" name="EndPoint">
    <gateInstance name="http" type="//@packagedElement.1"/>
  </packagedElement>
  
<packagedElement xsi:type="tdl:TestConfiguration" name="BasicClientServer">
    <componentInstance name="server" type="//@packagedElement.2"/>
    <componentInstance name="client" type="//@packagedElement.2" role="Tester"/>
    <connection>
      <endPoint component="//@packagedElement.3/@componentInstance.0" gate="//@packagedElement.2/@gateInstance.0"/>
      <endPoint component="//@packagedElement.3/@componentInstance.1" gate="//@packagedElement.2/@gateInstance.0"/>
    </connection>
  </packagedElement>
  
<packagedElement xsi:type="tdl:TestDescription" name="TD1" testConfiguration="//@packagedElement.3">
    <behaviourDescription>
      <behaviour xsi:type="tdl:CompoundBehaviour">
        <block>
          <behaviour xsi:type="tdl:Message" sourceGate="//@packagedElement.3/@connection.0/@endPoint.1">
            <target targetGate="//@packagedElement.3/@connection.0/@endPoint.0"/>
            <argument xsi:type="tdl:LiteralValueUse" value="&quot;GET /store&quot;"/>
          </behaviour>
          <behaviour xsi:type="tdl:Message" sourceGate="//@packagedElement.3/@connection.0/@endPoint.0">
            <target targetGate="//@packagedElement.3/@connection.0/@endPoint.1"/>
            <argument xsi:type="tdl:LiteralValueUse" value="&quot;200 OK&quot;"/>
          </behaviour>
        </block>
      </behaviour>
    </behaviourDescription>
  </packagedElement>
  </packagedElement>
</tdl:Package>


The model was loaded into an EMF resource (org.eclipse.emf.ecore.resource) as List <EObjects>:
public static void main(String [] args) {
			
		//+1 added package initialisation as indicated in the wiki for predefined models
		tdlPackage.eINSTANCE.eClass();

		//+2 added OCL initialisation as it is used in the TDL implementation
		EssentialOCLStandaloneSetup.doSetup();
	
		//adapted for the specific Xtext-based language implementation
		Injector injector = new TDLan2StandaloneSetup().createInjectorAndDoEMFRegistration();
		XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
		
		//adapted for the filename declared above (relative to working directory)
		Resource resource = resourceSet.getResource(URI.createURI("model/example.tdlan2"), true);
		
		//adapted to our root / top-level element type - Package (TDL Package, not Java Package!) 
		Package p = (Package) resource.getContents().get(0);


Now I want to add another TestDescription containting a BehaviourDesription which also serves as a container for more Elements. There is also a missing nesting of a CompoundBehaviour (type Behaviour) and a Block.

//get the TestConfiguration from the EObject List  
		TestConfiguration TC = tdlFactory.eINSTANCE.createTestConfiguration();
		for(PackageableElement pe : p.getPackagedElement()) {
			if(pe instanceof TestConfiguration)
				TC = (TestConfiguration) pe;

		//get EndPoints defined in the TestConfiguration
		GateReference gateSource = tdlFactory.eINSTANCE.createGateReference();	
		GateReference gateTarget = tdlFactory.eINSTANCE.createGateReference();
		gateSource = TC.getConnection().get(0).getEndPoint().get(1);
		gateTarget = TC.getConnection().get(0).getEndPoint().get(0);

		Target t = tdlFactory.eINSTANCE.createTarget();
		t.setTargetGate(gateTarget);

		//Create an Interaction (Behaviour) and set EndPoints
		Interaction iAction = tdlFactory.eINSTANCE.createInteraction();
		iAction.setSourceGate(gateSource);
		iAction.getTarget().add(t);

		//Somewhere here should be CompoundBevahiour containing a block that contains the Action ??
		CompoundBehaviour cb = tdlFactory.eINSTANCE.createCompoundBehaviour();
		Block block = tdlFactory.eINSTANCE.createBlock();
		cb.setBlock(block);

		//Create BehaviourDescription and set Behaviour	
		BehaviourDescription bd = tdlFactory.eINSTANCE.createBehaviourDescription();
		bd1.setBehaviour(iAction);

  		//Create a new TestDescription based on the TestConfiguration of the "TD1"
		TestDescription td2 = tdlFactory.eINSTANCE.createTestDescription();
		td3.setName("TD2");
		td3.setTestConfiguration(TC);
		td3.setBehaviourDescription(bd);
		}


Finally, I try to save the changes into the input file (example.tdlan2) by calling the "resource" reference saved in "Package p", adding it to the List and saving into "example.tdlan2" again
//adding the new TestDescription to the List
		p.getPackagedElement().add(td2);

		 //finally time to save the resource - no further modification possible
		try {
			resource.save(Collections.emptyMap());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


the result is getting a red wall of errors leading to the line:
resource.save(Collections.emptyMap());

and clearing the input file completely

Errors:
Exception in thread "main" java.lang.IllegalStateException
	at org.eclipse.xtext.serializer.sequencer.AbstractSyntacticSequencer.enterAssignedParserRuleCall(AbstractSyntacticSequencer.java:354)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.acceptEObjectRuleCall(SequenceFeeder.java:325)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.acceptRuleCall(SequenceFeeder.java:354)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.accept(SequenceFeeder.java:248)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.accept(BacktrackingSemanticSequencer.java:452)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.createSequence(BacktrackingSemanticSequencer.java:512)
	at org.etsi.mts.tdl.serializer.TDLan2SemanticSequencer.sequence_BehaviourDescription(TDLan2SemanticSequencer.java:592)
	at org.etsi.mts.tdl.serializer.TDLan2SemanticSequencer.sequence(TDLan2SemanticSequencer.java:140)
	at org.eclipse.xtext.serializer.sequencer.AbstractSemanticSequencer.createSequence(AbstractSemanticSequencer.java:68)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.acceptEObjectRuleCall(SequenceFeeder.java:327)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.acceptRuleCall(SequenceFeeder.java:354)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.accept(SequenceFeeder.java:248)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.accept(BacktrackingSemanticSequencer.java:452)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.createSequence(BacktrackingSemanticSequencer.java:512)
	at org.etsi.mts.tdl.serializer.TDLan2SemanticSequencer.sequence_TestDescription(TDLan2SemanticSequencer.java:1797)
	at org.etsi.mts.tdl.serializer.TDLan2SemanticSequencer.sequence(TDLan2SemanticSequencer.java:351)
	at org.eclipse.xtext.serializer.sequencer.AbstractSemanticSequencer.createSequence(AbstractSemanticSequencer.java:68)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.acceptEObjectRuleCall(SequenceFeeder.java:327)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.acceptRuleCall(SequenceFeeder.java:354)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.accept(SequenceFeeder.java:265)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.accept(BacktrackingSemanticSequencer.java:445)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.createSequence(BacktrackingSemanticSequencer.java:512)
	at org.etsi.mts.tdl.serializer.TDLan2SemanticSequencer.sequence_Package(TDLan2SemanticSequencer.java:1303)
	at org.etsi.mts.tdl.serializer.TDLan2SemanticSequencer.sequence(TDLan2SemanticSequencer.java:262)
	at org.eclipse.xtext.serializer.sequencer.AbstractSemanticSequencer.createSequence(AbstractSemanticSequencer.java:68)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:122)
	at org.eclipse.xtext.serializer.impl.Serializer.serializeToRegions(Serializer.java:144)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:150)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:202)
	at org.eclipse.xtext.resource.XtextResource.doSave(XtextResource.java:387)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:1475)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:1044)
	at org.etsi.example.load.run.exe.main(exe.java:210)


Any advise is appreciated.
Re: EMF - adding new model objects programmatically [message #1837939 is a reply to message #1837938] Thu, 11 February 2021 06:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13887
Registered: July 2009
Senior Member
no idea from what you have provided.
the serializer may have some bugs.
could you provide a minimal hello world grammar and unit test (no ocl etc required)


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: EMF - adding new model objects programmatically [message #1837940 is a reply to message #1837939] Thu, 11 February 2021 08:25 Go to previous messageGo to next message
Andrej Liebert is currently offline Andrej LiebertFriend
Messages: 18
Registered: January 2021
Junior Member
Sorry, I have never written anything in Xtext so I am not able to provide any code specially in regards of the Test Description Language.
But I have found the .xtext file for tdlan2 https://labs.etsi.org/rep/top/ide/blob/5bb5c34ad7f7673618028d16c7c33feef7d310c5/plugins/org.etsi.mts.tdl.TDLan2/src/org/etsi/mts/tdl/TDLan2.xtext
For navigation I only use the manual https://www.etsi.org/deliver/etsi_es/203100_203199/20311901/01.04.01_60/es_20311901v010401p.pdf. For example, Interaction is modelled on page 81 clause 9.4.6.
So far I understand is the unit testing an assurance to provide maintainability and the quality of the software product, which I assume is given by its developers.

But if you still insist on your request, I am going to need a more detailed approach.

Re: EMF - adding new model objects programmatically [message #1837946 is a reply to message #1837940] Thu, 11 February 2021 09:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13887
Registered: July 2009
Senior Member
the grammar says

Interaction returns tdl::Interaction:
Message | ProcedureCall
;

Message returns tdl::Message: .....
ProcedureCall returns tdl::ProcedureCall: .....


but you create an Interaction directly and not one of the subtypes. but that is not permitted according to the grammar


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: EMF - adding new model objects programmatically [message #1837947 is a reply to message #1837940] Thu, 11 February 2021 09:16 Go to previous messageGo to next message
Andrej Liebert is currently offline Andrej LiebertFriend
Messages: 18
Registered: January 2021
Junior Member
You are totally right, the model also shows that that an Interaction is a Generalization, which has to be specified.

[Updated on: Thu, 11 February 2021 09:35]

Report message to a moderator

Re: EMF - adding new model objects programmatically [message #1837949 is a reply to message #1837947] Thu, 11 February 2021 09:33 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13887
Registered: July 2009
Senior Member
yes you have to create a Message or ProcedureCall, not the (abstract but not abstract) Interaction

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Cannot set map object to context of XBaseInterpreter
Next Topic:Unclear (or problematic?) hidden tokens related behaviour
Goto Forum:
  


Current Time: Tue Apr 20 11:36:00 GMT 2021

Powered by FUDForum. Page generated in 0.02206 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top