Created
March 11, 2013 06:03
-
-
Save kthoms/5132153 to your computer and use it in GitHub Desktop.
LWC13: QlDslJvmModelInferrer.xtend
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 org.eclipse.xtext.example.ql.jvmmodel | |
import com.google.inject.Inject | |
import java.io.Serializable | |
import org.eclipse.xtext.common.types.JvmOperation | |
import org.eclipse.xtext.common.types.util.TypeReferences | |
import org.eclipse.xtext.example.ql.qlDsl.ConditionalQuestionGroup | |
import org.eclipse.xtext.example.ql.qlDsl.Question | |
import org.eclipse.xtext.example.ql.qlDsl.Questionnaire | |
import org.eclipse.xtext.xbase.XExpression | |
import org.eclipse.xtext.xbase.XbaseFactory | |
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer | |
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor | |
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder | |
class QlDslJvmModelInferrer extends AbstractModelInferrer { | |
@Inject extension JvmTypesBuilder | |
@Inject TypeReferences typeReferences | |
def dispatch void infer(Questionnaire element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) { | |
for (form: element.forms) { | |
acceptor.accept(form.toClass("forms."+form.name)) | |
.initializeLater[ | |
//implements Serializable | |
it.superTypes +=typeReferences.getTypeForName(typeof(Serializable),element,null) | |
it.members += toField("serialVersionUID",typeReferences.getTypeForName("long",element),[final = false ^static = false ]) | |
val allQuestions = form.eAllContents.filter(typeof(Question)).toList | |
for (question: allQuestions) { | |
members += question.toField(question.name, question.type) | |
} | |
for (question: allQuestions) { | |
if (question.expression == null) { | |
members += question.toGetter(question.name, question.type) | |
members += question.toSetter(question.name, question.type) | |
} else { | |
val getter = question.toGetter(question.name, question.type) | |
getter.body = question.expression | |
members += getter | |
} | |
members += question.createIsEnabledMethod | |
} | |
val allQuestionGroups = form.eAllContents.filter(typeof(ConditionalQuestionGroup)).toList | |
var groupIndex=0; | |
for (questionGroup: allQuestionGroups) { | |
members += questionGroup.createIsGroupVisibleMethod(groupIndex) | |
groupIndex = groupIndex+1 | |
} | |
] | |
} | |
} | |
def JvmOperation createIsEnabledMethod (Question question) { | |
question.toMethod("is"+question.name.toFirstUpper+"Enabled", typeReferences.getTypeForName("boolean", question, null)) [ | |
body = [it.append('''return «question.expression == null»;''')] | |
] | |
} | |
/** Create a method <code>public boolean isGroup[groupIndex]Visible ()</code>.*/ | |
def JvmOperation createIsGroupVisibleMethod (ConditionalQuestionGroup group, int groupIndex) { | |
group.toMethod("isGroup"+groupIndex+"Visible", typeReferences.getTypeForName("boolean", group, null)) [ | |
if(group.condition != null) { | |
body = group.condition | |
} else { | |
body = [it.append('''return true;''')] | |
} | |
] | |
} | |
/** | |
* Computes the expression that must be evaluated to determines whether a question is visible. | |
* Visibility can be influenced by conditionally grouping | |
*/ | |
def XExpression getVisibleExpression (Question question) { | |
if (question.eContainer instanceof ConditionalQuestionGroup) { | |
return (question.eContainer as ConditionalQuestionGroup).condition | |
} else { | |
val booleanLiteral = XbaseFactory::eINSTANCE.createXBooleanLiteral | |
booleanLiteral.isTrue = true | |
return booleanLiteral | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment