[serializer] only check parameters when needed

only check the value of parameters when the same context eobject maps to
different constraints. 

Signed-off-by: Moritz Eysholdt <moritz.eysholdt@itemis.de>
This commit is contained in:
Moritz Eysholdt 2015-10-20 23:49:22 +02:00
parent e4f2c37d9d
commit 6b88799f19

View file

@ -66,6 +66,8 @@ import static extension org.eclipse.xtext.GrammarUtil.*
import static extension org.eclipse.xtext.serializer.analysis.SerializationContext.*
import static extension org.eclipse.xtext.xtext.generator.model.TypeReference.*
import static extension org.eclipse.xtext.xtext.generator.util.GenModelUtil2.*
import com.google.common.collect.Multimaps
import com.google.common.collect.Multimap
class SerializerFragment2 extends AbstractXtextGeneratorFragment {
@ -283,10 +285,14 @@ class SerializerFragment2 extends AbstractXtextGeneratorFragment {
private def StringConcatenationClient genMethodCreateSequenceCaseBody(Map<IConstraint, IConstraint> superConstraints, EClass type) {
val contexts = grammar.getGrammarConstraints(type).entrySet.sortBy[key.name]
val context2constraint = LinkedHashMultimap.create
for (e : contexts)
for (ctx : e.value)
context2constraint.put((ctx as SerializationContext).actionOrRule, e.key)
'''
«IF contexts.size > 1»
«FOR ctx : contexts.indexed»
«IF ctx.key > 0»else «ENDIF»if («genCondition(ctx.value.value, ctx.value.key)») {
«IF ctx.key > 0»else «ENDIF»if («genCondition(ctx.value.value, ctx.value.key, context2constraint)») {
«genMethodCreateSequenceCall(superConstraints, type, ctx.value.key)»
}
«ENDFOR»
@ -299,13 +305,13 @@ class SerializerFragment2 extends AbstractXtextGeneratorFragment {
'''
}
private def StringConcatenationClient genCondition(List<ISerializationContext> contexts, IConstraint constraint) {
private def StringConcatenationClient genCondition(List<ISerializationContext> contexts, IConstraint constraint, Multimap<EObject, IConstraint> ctx2ctr) {
val sorted = contexts.sort
val index = LinkedHashMultimap.create
sorted.forEach [
index.put(contextObject, it)
]
'''«FOR obj : index.keySet SEPARATOR "\n\t\t|| "»«obj.genObjectSelector»«obj.genParameterSelector(index.get(obj), constraint)»«ENDFOR»'''
'''«FOR obj : index.keySet SEPARATOR "\n\t\t|| "»«obj.genObjectSelector»«IF ctx2ctr.get(obj).size > 1»«obj.genParameterSelector(index.get(obj), constraint)»«ENDIF»«ENDFOR»'''
}
private def StringConcatenationClient genObjectSelector(EObject obj) {
@ -316,34 +322,34 @@ class SerializerFragment2 extends AbstractXtextGeneratorFragment {
}
private def StringConcatenationClient genParameterSelector(EObject obj, Set<ISerializationContext> contexts, IConstraint constraint) {
val rule = GrammarUtil.containingParserRule(obj)
if (rule.parameters.isEmpty || !constraint.contexts.exists[!(it as SerializationContext).declaredParameters.isEmpty]) {
return ''''''
}
// figure out which scenarios are independent from the parameter values
val withParamsByRule = LinkedHashMultimap.create
contexts.forEach [
val param = enabledBooleanParameters.head
if (param !== null) {
withParamsByRule.put(GrammarUtil.containingParserRule(param), it)
}
]
val copy = newLinkedHashSet
copy.addAll(contexts)
// and remove these
withParamsByRule.keySet.forEach [
val entries = withParamsByRule.get(it)
if (entries.size === (1 << it.parameters.size) - 1) {
copy.removeAll(entries)
}
]
if (copy.exists [ !enabledBooleanParameters.isEmpty ]) {
// param configuration doesn't matter
return ''''''
}
return ''' && («FOR context : copy SEPARATOR "\n\t\t\t|| "»«context.genParameterCondition(constraint)»«ENDFOR»)'''
// val rule = GrammarUtil.containingParserRule(obj)
// if (rule.parameters.isEmpty || !constraint.contexts.exists[!(it as SerializationContext).declaredParameters.isEmpty]) {
// return ''''''
// }
// // figure out which scenarios are independent from the parameter values
// val withParamsByRule = LinkedHashMultimap.create
// contexts.forEach [
// val param = enabledBooleanParameters.head
// if (param !== null) {
// withParamsByRule.put(GrammarUtil.containingParserRule(param), it)
// }
// ]
// val copy = newLinkedHashSet
// copy.addAll(contexts)
//
// // and remove these
// withParamsByRule.keySet.forEach [
// val entries = withParamsByRule.get(it)
// if (entries.size === (1 << it.parameters.size) - 1) {
// copy.removeAll(entries)
// }
// ]
//
// if (copy.isEmpty || copy.exists [ !enabledBooleanParameters.isEmpty ]) {
// // param configuration doesn't matter
// return ''''''
// }
return ''' && («FOR context : contexts SEPARATOR "\n\t\t\t|| "»«context.genParameterCondition(constraint)»«ENDFOR»)'''
}