Search in sources :

Example 6 with IConstraint

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;
}
Also used : SerializationContextMap(org.eclipse.xtext.serializer.analysis.SerializationContextMap) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with IConstraint

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";
}
Also used : Arrays(java.util.Arrays) Inject(com.google.inject.Inject) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) IGrammarConstraintProvider(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider) ParseHelper(org.eclipse.xtext.testing.util.ParseHelper) XtextRunner(org.eclipse.xtext.testing.XtextRunner) List(java.util.List) Grammar(org.eclipse.xtext.Grammar) Lists(com.google.common.collect.Lists) InjectWith(org.eclipse.xtext.testing.InjectWith) ValidationTestHelper(org.eclipse.xtext.testing.validation.ValidationTestHelper) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) XtextInjectorProvider(org.eclipse.xtext.tests.XtextInjectorProvider) Assert(org.junit.Assert) Joiner(com.google.common.base.Joiner) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) Grammar(org.eclipse.xtext.Grammar)

Example 8 with IConstraint

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);
}
Also used : IGrammarConstraintProvider(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) Grammar(org.eclipse.xtext.Grammar)

Example 9 with IConstraint

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;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint)

Example 10 with IConstraint

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;
}
Also used : ValueTransient(org.eclipse.xtext.serializer.sequencer.ITransientValueService.ValueTransient) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) IFeatureInfo(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IFeatureInfo)

Aggregations

IConstraint (org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint)16 ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)9 List (java.util.List)4 AbstractElement (org.eclipse.xtext.AbstractElement)4 IFeatureInfo (org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IFeatureInfo)4 EClass (org.eclipse.emf.ecore.EClass)3 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)3 Grammar (org.eclipse.xtext.Grammar)3 IGrammarConstraintProvider (org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider)3 SerializationContextMap (org.eclipse.xtext.serializer.analysis.SerializationContextMap)3 Collection (java.util.Collection)2 EObject (org.eclipse.emf.ecore.EObject)2 EObjectChange (org.eclipse.xtext.ide.serializer.impl.ChangeTreeProvider.EObjectChange)2 IConstraintElement (org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraintElement)2 Joiner (com.google.common.base.Joiner)1 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 Inject (com.google.inject.Inject)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1