use of org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint in project xtext-core by eclipse.
the class SemanticSequencerExtensions method getGrammarConstraints.
public Map<IConstraint, List<ISerializationContext>> getGrammarConstraints(Grammar grammar, EClass clazz) {
Map<IConstraint, List<ISerializationContext>> result = new LinkedHashMap<>();
SerializationContextMap<IConstraint> constraints = gcp.getConstraints(grammar);
for (SerializationContextMap.Entry<IConstraint> e : constraints.values()) {
IConstraint constraint = e.getValue();
if (constraint.getType() == clazz) {
List<ISerializationContext> contexts = result.get(constraint);
if (contexts == null) {
contexts = new ArrayList<>();
result.put(constraint, contexts);
}
contexts.addAll(e.getContexts(clazz));
}
}
return result;
}
use of org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint in project xtext-core by eclipse.
the class GrammarConstraintProviderFeatureTest method toFeatureInfo.
public String toFeatureInfo(CharSequence grammarString) throws Exception {
Grammar grammar = parser.parse("grammar org.eclipse.xtext.serializer.GrammarConstraintProviderFeatureTestLanguage with org.eclipse.xtext.common.Terminals\n" + "\n" + "generate GrammarConstraintProviderFeatureTest 'http://www.eclipse.org/2010/tmf/xtext/GrammarConstraintProviderFeatureTestLanguage'\n" + "\n" + grammarString);
validator.assertNoErrors(grammar);
List<IConstraint> constraints = Lists.transform(constraintProvider.getConstraints(grammar).values(), it -> it.getValue());
return Joiner.on("\n").join(Lists.transform(constraints, (IConstraint c) -> {
return c.getName() + "{\n " + Joiner.on("\n ").join(Lists.transform(Arrays.asList(c.getFeatures()), f -> asString(f))) + "\n}";
})) + "\n";
}
use of org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint in project xtext-core by eclipse.
the class GrammarConstraintProviderTest method getParserRule.
private String getParserRule(String body) throws Exception {
Grammar grammar = (Grammar) getModel(HEADER + body);
IGrammarConstraintProvider gcp = get(IGrammarConstraintProvider.class);
SerializationContextMap<IConstraint> constraints = gcp.getConstraints(grammar);
List<String> result = Lists.newArrayList();
for (Entry<IConstraint> r : constraints.sortedCopy().values()) {
result.add(Joiner.on(", ").join(r.getContexts()) + ":");
result.add(" " + r.getValue());
}
return Joiner.on("\n").join(result);
}
use of org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint in project xtext-core by eclipse.
the class ContextFinder method findByContents.
@Override
public Set<ISerializationContext> findByContents(EObject semanticObject, Iterable<ISerializationContext> contextCandidates) {
if (semanticObject == null)
throw new NullPointerException();
initConstraints();
Multimap<IConstraint, ISerializationContext> constraints;
if (contextCandidates != null)
constraints = getConstraints(semanticObject, contextCandidates);
else
constraints = getConstraints(semanticObject);
if (constraints.size() < 2)
return Sets.newLinkedHashSet(constraints.values());
for (IConstraint cand : Lists.newArrayList(constraints.keySet())) if (!isValidValueQuantity(cand, semanticObject))
constraints.removeAll(cand);
if (constraints.size() < 2)
return Sets.newLinkedHashSet(constraints.values());
LinkedHashSet<ISerializationContext> result = Sets.newLinkedHashSet(constraints.values());
for (EStructuralFeature feat : semanticObject.eClass().getEAllStructuralFeatures()) {
if (transientValueUtil.isTransient(semanticObject, feat) != ValueTransient.NO)
continue;
if (feat.isMany() && ((List<?>) semanticObject.eGet(feat)).isEmpty())
continue;
Multimap<AbstractElement, ISerializationContext> assignments = collectAssignments(constraints, feat);
Set<AbstractElement> assignedElements = findAssignedElements(semanticObject, feat, assignments);
Set<ISerializationContext> keep = Sets.newHashSet();
for (AbstractElement ele : assignedElements) keep.addAll(assignments.get(ele));
result.retainAll(keep);
}
return result;
}
use of org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint in project xtext-core by eclipse.
the class ContextFinder method isValidValueQuantity.
protected boolean isValidValueQuantity(IConstraint constraint, EObject semanicObj) {
if (constraint == null)
return false;
for (int featureID = 0; featureID < semanicObj.eClass().getFeatureCount(); featureID++) {
IFeatureInfo featureInfo = constraint.getFeatures()[featureID];
EStructuralFeature feature = semanicObj.eClass().getEStructuralFeature(featureID);
if (feature.isMany()) {
int count = transientValueUtil.countNonTransientListValues(semanicObj, feature);
if (count > featureInfo.getUpperBound())
return false;
if (count < featureInfo.getLowerBound())
return false;
} else {
ValueTransient valueTransient = transientValues.isValueTransient(semanicObj, feature);
switch(valueTransient) {
case NO:
if (featureInfo == null)
return false;
if (featureInfo.getUpperBound() <= 0)
return false;
break;
case YES:
if (featureInfo == null)
break;
if (featureInfo.getLowerBound() > 0)
return false;
break;
case PREFERABLY:
break;
}
}
}
return true;
}
Aggregations