extensions for model elements of the enhanced DomainModel example (Xtext 1.0)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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