Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » EMF - adding new model objects programmatically
EMF - adding new model objects programmatically [message #1837924] Wed, 10 February 2021 16:44 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.

[Updated on: Thu, 11 February 2021 06:23]

Report message to a moderator

Re: EMF - adding new model objects programmatically [message #1837936 is a reply to message #1837924] Thu, 11 February 2021 05:47 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 31828
Registered: July 2009
Senior Member
Given that the problem is in the Xtext serializer, it's best to ask on the TMF (Xtext) forum:

https://www.eclipse.org/forums/index.php?t=thread&frm_id=27


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EMF - adding new model objects programmatically [message #1837937 is a reply to message #1837936] Thu, 11 February 2021 06:24 Go to previous message
Andrej Liebert is currently offline Andrej LiebertFriend
Messages: 18
Registered: January 2021
Junior Member
Thank you, with EMF and Xtext in use I am never sure where to post any problem.
Previous Topic:[Edapt] replace attribute with existing abstract class
Next Topic:[EMF Forms]
Goto Forum:
  


Current Time: Tue Apr 13 02:45:35 GMT 2021

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

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

Back to the top