blob: 4d09f849632e6ad699a78314d36927060f6e66af [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*
* This copyright notice shows up in the generated Java code
*
*/
package org.eclipse.osbp.xtext.functionlibrarydsl.jvmmodel
import com.google.inject.Inject
import java.util.Locale
import org.eclipse.emf.common.util.BasicEList
import org.eclipse.emf.common.util.EList
import org.eclipse.osbp.bpm.BlipBaseFunctionGroup
import org.eclipse.osbp.ui.api.functionlibrary.IFunctionLibraryGroup
import org.eclipse.osbp.ui.api.functionlibrary.IFunctionLibraryPackage
import org.eclipse.osbp.utils.annotation.CommonUtils
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryBaseFunction
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryBlipGroup
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryConverterGroup
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryGroup
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryPackage
import org.eclipse.xtext.common.types.JvmField
import org.eclipse.xtext.common.types.JvmGenericType
import org.eclipse.xtext.common.types.JvmOperation
import org.eclipse.xtext.common.types.JvmTypeReference
import org.eclipse.xtext.common.types.JvmVisibility
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.osgi.service.component.annotations.Component
import org.slf4j.Logger
/**
* <p>Infers a JVM model from the source model.</p>
*
* <p>The JVM model should contain all elements that would appear in the Java code
* which is generated from the source model. Other models link against the JVM model rather than the source model.</p>
*/
class FunctionLibraryDslJvmModelInferrer extends AbstractModelInferrer {
protected final static class MethodParamHelper {
var String name
var JvmTypeReference reference
new (String aname, JvmTypeReference areference) {
name = aname
reference = areference
}
}
val public static FUNCTION_LIBRARY_PACKAGE = "FunctionLibraryPackage"
/**
* generate the class name for a mock entity
*/
def static String getFunctionLibraryPackageName(FunctionLibraryPackage pckg) {
var pckgName = '''«pckg.name»'''
return pckgName
}
/**
* convenience API to build and initialize JVM types and their members.
*/
@Inject extension JvmTypesBuilder
@Inject extension CommonUtils
def dispatch void infer(FunctionLibraryPackage pkg, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
try {
val generatorName = '''«FunctionLibraryDslJvmModelInferrer.getFunctionLibraryPackageName(pkg)».«FunctionLibraryDslJvmModelInferrer.FUNCTION_LIBRARY_PACKAGE»'''
acceptor.accept(pkg.toClass(generatorName)) [
var annotationRef = _annotationTypesBuilder.annotationRef(typeof(Component))
annotationRef.addAnnAttr(pkg, "service", _typeReferenceBuilder.typeRef(IFunctionLibraryPackage) as JvmTypeReference)
annotations += annotationRef
superTypes += _typeReferenceBuilder.typeRef(typeof(IFunctionLibraryPackage))
final = true
]
}
catch (Exception e) {
e.printStackTrace();
}
pkg.ratings.forEach[ rating |
try {
acceptor.accept(rating.toEnumerationType(pkg.name.concat(".").concat(rating.name))[],
[
documentation = rating.documentation
rating.items.forEach [ item |
it.members += pkg.toEnumerationLiteral(item.name)[
documentation = item.documentation
]
]
]
)
}
catch (Exception e) {
e.printStackTrace();
}
]
pkg.groups.forEach[ group |
try {
acceptor.accept(pkg.toClass(pkg.name.concat(".").concat(group.name)),
[
superTypes += _typeReferenceBuilder.typeRef(IFunctionLibraryGroup)
if (group instanceof FunctionLibraryBlipGroup) {
superTypes += _typeReferenceBuilder.typeRef(BlipBaseFunctionGroup)
}
documentation = group.documentation
it.fileHeader = pkg.documentation
it.toFields(group)
final = true
group.functions.forEach[ function |
it.members += function.toMethod(function.oftype ?: inferredType)
]
group.operations.forEach[ operation |
it.members += operation.toMethod(_typeReferenceBuilder.typeRef(boolean))
]
group.rates.forEach[ rate |
it.members += rate.toMethod(_typeReferenceBuilder.typeRef(pkg.name.concat(".").concat(rate.oftype.name)))
]
group.tests.forEach[ test |
it.members += test.toMethod(_typeReferenceBuilder.typeRef(boolean))
]
group.guards.forEach[ guard |
it.members += guard.toMethod(_typeReferenceBuilder.typeRef(boolean))
]
group.canExecutes.forEach[ can |
it.members += can.toMethod(_typeReferenceBuilder.typeRef(Boolean))
]
group.executes.forEach[ execute |
it.members += execute.toMethod(_typeReferenceBuilder.typeRef(Boolean))
]
if (group instanceof FunctionLibraryConverterGroup){
var converterGroup = (group as FunctionLibraryConverterGroup)
if (converterGroup.convertToModel != null) {
var mustParams = new BasicEList()
mustParams.add(new MethodParamHelper("presentationValue", converterGroup.convertToModelDatatype.jvmTypeReference))
mustParams.add(new MethodParamHelper("presentationLocale", _typeReferenceBuilder.typeRef(Locale)))
mustParams.add(new MethodParamHelper("presentationParams", _jvmTypesBuilder.addArrayTypeDimension(_typeReferenceBuilder.typeRef(Object))))
it.members += converterGroup.convertToModel.toMethod("valueToModelConverter", converterGroup.convertToModelDatatype.jvmTypeReference, mustParams)
}
if (converterGroup.convertToPresentation != null) {
var mustParams = new BasicEList()
mustParams.add(new MethodParamHelper("modelValue", converterGroup.convertToModelDatatype.jvmTypeReference))
mustParams.add(new MethodParamHelper("modelLocale", _typeReferenceBuilder.typeRef(Locale)))
mustParams.add(new MethodParamHelper("modelParams", _jvmTypesBuilder.addArrayTypeDimension(_typeReferenceBuilder.typeRef(Object))))
it.members += converterGroup.convertToPresentation.toMethod("valueToPresentationConverter", converterGroup.convertToPresentationDatatype.jvmTypeReference, mustParams)
}
}
]
)
}
catch (Exception e) {
e.printStackTrace();
}
]
}
// def JvmWildcardTypeReference wildCardSuper(JvmTypeReference clone) {
// JvmWildcardTypeReference result = factory.createJvmWildcardTypeReference();
// JvmLowerBound lowerBound = factory.createJvmLowerBound();
// lowerBound.setTypeReference(clone);
// result.getConstraints().add(lowerBound);
// return result;
//}
def JvmOperation toMethod(FunctionLibraryBaseFunction baseFunction, JvmTypeReference returnType) {
return baseFunction.toMethod(baseFunction.name, returnType, null)
}
def JvmOperation toMethod(FunctionLibraryBaseFunction baseFunction, String methodName, JvmTypeReference returnType, EList<MethodParamHelper> mustParams) {
return baseFunction.toMethod(methodName, returnType) [
documentation = baseFunction.documentation
if (mustParams != null) {
for (param : mustParams) {
parameters += baseFunction.toParameter(param.name, param.reference);
}
}
for (param : baseFunction.params) {
parameters += param.toParameter(param.name, param.parameterType);
}
static = true
final = true
visibility = JvmVisibility.PUBLIC
body = baseFunction.body
]
}
def void toFields(JvmGenericType type, FunctionLibraryGroup group) {
var JvmField field = null
// create logger
field = group.toField("log", _typeReferenceBuilder.typeRef(Logger))[setInitializer([append('''org.slf4j.LoggerFactory.getLogger("functiongroup")''')])]
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
}
}