Skip to content

Instantly share code, notes, and snippets.

@dslmeinte
Created November 23, 2011 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dslmeinte/1388765 to your computer and use it in GitHub Desktop.
Save dslmeinte/1388765 to your computer and use it in GitHub Desktop.
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