Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Create and configure instances(Declare a entity and create and configure an instance of the declared entity)
Create and configure instances [message #1855312] Fri, 07 October 2022 11:18 Go to next message
Helmut Kopf is currently offline Helmut KopfFriend
Messages: 2
Registered: October 2022
Junior Member
Hi,

I am new in Xtext and the grammar syntax. Therefore, the question might be stupid, but I would like to express this use-case:
I would like to define an entity with features:

entity MyEntityDeclaration {
name : String
description : String
}

The Xtext grammer to define this is clear to me, it's part of the 15 minute tutorial on Xtext documentation site. The attached file contains the grammer of the entity declaration.
But now I would like to create an instance of this entity. Something like this:

new MyEntityDeclaration A {
name = "MyName"
description = "MyDescription"
}

new MyEntityDeclaration B {
name = "AnotherName"
description = "AnotherDescription"
}

How would I specify the grammar to achieve this?

Thanks for help.
Re: Create and configure instances [message #1855313 is a reply to message #1855312] Fri, 07 October 2022 12:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
am not sure if i get your point at all. i propose to do the tutorial.

Instance:
"new" type=[Entity] name=ID "{"
propertyInstances+=PropertyInstance*
"}";

PropertyInstance:
attr=[Attribute] "=" value=Literal // Expresssion, whatever you have;

+ implement scoping for PropertyInstance


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 07 October 2022 12:46]

Report message to a moderator

Re: Create and configure instances [message #1855380 is a reply to message #1855313] Tue, 11 October 2022 14:35 Go to previous messageGo to next message
Helmut Kopf is currently offline Helmut KopfFriend
Messages: 2
Registered: October 2022
Junior Member
Thank you for your help.

I tried to extend my grammar according to your proposal (see attached MyDsl.xtext). Unfortunately, I was not able to specify the scope for the FeatureInstance.

Therefore, I can specify something like this in my dsl file:

package my.sample {
datatype String

entity User {
firstName : String
lastName : String
}

entity Address {
street : String
country : String
phone : String
}

new User MyUser {
set User.firstName = "Ny first name"
set User.lastName = "Ny last name"

set Address.country = "this is the wrong feature scope..."
}
}

Can you please explaint how to implement the scoping for the PropertyInstance (in my file it's ther FeatureInstance)?

Thank you for your help.
  • Attachment: MyDsl.xtext
    (Size: 1.07KB, Downloaded 56 times)
Re: Create and configure instances [message #1855403 is a reply to message #1855380] Wed, 12 October 2022 09:30 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
would expect

new User MyUser {
set firstName = "Ny first name"
set lastName = "Ny last name"
}

FeatureInstance: // remove the bad return
'set' type=[Feature] "=" value=Literal;
in grammar and a scope provider like (untessted)

public class MyDslScopeProvider extends AbstractMyDslScopeProvider {
	
	@Override
	public IScope getScope(EObject context, EReference reference) {
		if (reference == MyDslPackage.Literals.FEATURE_INSTANCE__TYPE) {
			Instance inst = null;
			if (context instanceof Instance) {
				inst = (Instance) context;
			} else if (context instanceof FeatureInstance) {
				inst = (Instance) context.eContainer();
			}
			if (inst != null) {
				return Scopes.scopeFor(inst.getType().getFeatures());
			} else {
				return IScope.NULLSCOPE;
			}
		}
		return super.getScope(context, reference);
	}

}



is it intentional you have this wired syntax set User.xxxxx
then you sould have also two references there,
but the user would be redundant / implemented as in
https://dietrich-it.de/xtext/2013/05/18/xtext-and-dot/path-expressions/


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Wed, 12 October 2022 09:33]

Report message to a moderator

Previous Topic:Modern Gradle Setup
Next Topic:Limitation on recursive objects?
Goto Forum:
  


Current Time: Tue Apr 23 11:57:57 GMT 2024

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

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

Back to the top