All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.reprezen.genflow.common.xtend.ZenModelHelper.xtend Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright © 2013, 2016 Modelsolv, Inc.
 * All Rights Reserved.
 * 
 * NOTICE: All information contained herein is, and remains the property
 * of ModelSolv, Inc. See the file license.html in the root directory of
 * this project for further information.
 *******************************************************************************/
package com.reprezen.genflow.common.xtend

import com.reprezen.rapidml.Documentable
import com.reprezen.rapidml.Enumeration
import com.reprezen.rapidml.Feature
import com.reprezen.rapidml.PrimitiveProperty
import com.reprezen.rapidml.RealizationContainer
import com.reprezen.rapidml.ReferenceEmbed
import com.reprezen.rapidml.ReferenceLink
import com.reprezen.rapidml.ReferenceProperty
import com.reprezen.rapidml.ReferenceTreatment
import com.reprezen.rapidml.ResourceAPI
import com.reprezen.rapidml.ServiceDataResource
import com.reprezen.rapidml.TypedMessage
import com.reprezen.rapidml.UserDefinedType
import com.reprezen.rapidml.ZenModel
import java.util.ArrayList
import java.util.Arrays
import java.util.Collection
import java.util.Collections
import java.util.List
import org.eclipse.emf.ecore.EObject

class ZenModelHelper {
	protected def dispatch List getReferenceTreatmentIncludedProperties(ReferenceEmbed refEmbed) {
		// contains Features and ReferenceTreatments
		val includedProps = new ArrayList();
		includedProps.addAll(getIncludedPrimitiveProperties(refEmbed))
		includedProps.addAll(refEmbed.nestedReferenceTreatments)
		return includedProps
	}

	protected def dispatch getReferenceTreatmentIncludedProperties(ReferenceLink refEmbed) {
		return getIncludedPrimitiveProperties(refEmbed)
	}

	def List getIncludedPrimitiveProperties(ReferenceTreatment refTreatment) {
		return getIncludedProperties(refTreatment).filter[e|e instanceof PrimitiveProperty].toList
	}

	def List getIncludedProperties(ReferenceTreatment refLink) {
		refLink.linkDescriptor?.allIncludedProperties?.map[baseProperty] ?: Collections::emptyList
	}

	def boolean hasReferenceTreatment(RealizationContainer resource, Feature feature) {
		resource.referenceTreatments.map[referenceElement].exists[f|f === feature]
	}

	def boolean hasReferenceTreatment(TypedMessage resource, Feature feature) {
		// TODO remove this method when TypedMessage and ServiceDataResource have a shared ObjectRealization supertype
		resource.referenceTreatments.map[referenceElement].exists[f|f === feature]
	}
	
	def dispatch Collection getUsedEnums(Object o) {
		// For Xtend compilation. Otherwise it tries to cast to Extensible
		throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.asList(o).toString());
	}

	def dispatch Collection getUsedEnums(ZenModel model) {
		model.resourceAPIs.map[it.usedEnums].flatten.toSet
	}

	def dispatch Collection getUsedEnums(ResourceAPI resourceAPI) {
		resourceAPI.ownedResourceDefinitions.filter(ServiceDataResource).map[dataInterface|getUsedEnums(dataInterface)].
			flatten.toSet
	}

	def dispatch Collection getUsedEnums(ServiceDataResource resource) {
		(resource.includedProperties.filter[e|!hasReferenceTreatment(resource, e.baseProperty)].map [
			getUsedEnums(it.baseProperty)
		].flatten + resource.referenceTreatments.filter(ReferenceEmbed).map[getUsedEnums(it)].flatten).toSet
	}

	def dispatch Collection getUsedEnums(ReferenceTreatment ref) {
		getReferenceTreatmentIncludedProperties(ref).map[getUsedEnums(it)].flatten.toSet
	}

	def dispatch Collection getUsedEnums(PrimitiveProperty pp) {
		if (pp.type instanceof Enumeration)
			Collections.singletonList(pp.type as Enumeration)
		else
			Collections.emptyList()
	}

	def dispatch Collection getUsedEnums(ReferenceProperty ref) {
		Collections.emptyList()
	}

	def dispatch Collection getUsedUserDefinedTypes(Object o) {
		// For Xtend compilation. Otherwise it tries to cast to Extensible
		throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.asList(o).toString());
	}

	def dispatch Collection getUsedUserDefinedTypes(ZenModel model) {
		model.resourceAPIs.map[it.getUsedUserDefinedTypes].flatten.toSet
	}

	def dispatch Collection getUsedUserDefinedTypes(ResourceAPI resourceAPI) {
		resourceAPI.ownedResourceDefinitions.filter(ServiceDataResource).map [ dataInterface |
			getUsedUserDefinedTypes(dataInterface)
		].flatten.toSet
	}

	def dispatch Collection getUsedUserDefinedTypes(ServiceDataResource resource) {
		(resource.includedProperties.filter[e|!hasReferenceTreatment(resource, e.baseProperty)].map [
			getUsedUserDefinedTypes(it.baseProperty)
		].flatten + resource.referenceTreatments.filter(ReferenceEmbed).map[getUsedUserDefinedTypes(it)].flatten).toSet
	}

	def dispatch Collection getUsedUserDefinedTypes(ReferenceTreatment ref) {
		getReferenceTreatmentIncludedProperties(ref).map[getUsedUserDefinedTypes(it)].flatten.toSet
	}

	def dispatch Collection getUsedUserDefinedTypes(PrimitiveProperty pp) {
		if (pp.type instanceof UserDefinedType) {
			var List list = new ArrayList
			list += pp.type as UserDefinedType
			var parent = (pp.type as UserDefinedType).baseType;
			while (parent instanceof UserDefinedType) {
				list += parent as UserDefinedType
				parent = (parent as UserDefinedType).baseType
			}
			list
		} else {
			Collections.emptyList()
		}
	}

	def dispatch Collection getUsedUserDefinedTypes(ReferenceProperty ref) {
		Collections.emptyList()
	}

	def String getDocumentation(EObject obj) {
		if (obj instanceof Documentable) {
			val doc = (obj as Documentable).documentation?.text ?: ""
			doc.trim()
		} else {
			""
		}
	}

}