Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extensions for model elements of the enhanced DomainModel example (Xtext 1.0)
package nl.dslmeinte.xtext.examples.domainmodel.services
import nl.dslmeinte.xtext.examples.domainmodel.domainModel.Entity
import nl.dslmeinte.xtext.examples.domainmodel.domainModel.Reference
import nl.dslmeinte.xtext.examples.domainmodel.domainModel.Attribute
import nl.dslmeinte.xtext.examples.domainmodel.domainModel.StructuralFeature
/**
* Extensions for model elements of a domain model.
* Usage in Xtend files:
* <pre>
* @Inject extension DomainModelExtensions
*
* // ...
*
* entity.structuralFeatures
* reference.structuralFeatures
* reference.entity
* </pre>
*/
class DomainModelExtensions {
def dispatch structuralFeatures(Entity it) {
features.filter(typeof(StructuralFeature))
}
def dispatch Iterable<StructuralFeature> structuralFeatures(Reference it) {
type.referenced.structuralFeatures
}
// (need to specify return type because of recursion)
def dispatch Iterable<StructuralFeature> structuralFeatures(Attribute it) {
emptyList
}
// (need to specify return type because common super type of List<T> and Iterable<T> is Object)
/**
* Returns the referenced Entity or null if type references something else (which is an error).
*/
def entity(Reference it) {
if( type.referenced instanceof Entity) type.referenced as Entity else null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.