blob: 2f771e1bd503236153901a55b19bea45b8aa51b6 [file] [log] [blame]
package org.eclipse.viatra.query.application.queries.util;
import com.google.common.base.Objects;
import com.google.common.collect.Sets;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.viatra.query.application.queries.EClassNamesKeywordMatch;
import org.eclipse.viatra.query.application.queries.EClassNamesKeywordMatcher;
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine;
import org.eclipse.viatra.query.runtime.api.impl.BaseGeneratedEMFPQuery;
import org.eclipse.viatra.query.runtime.api.impl.BaseGeneratedEMFQuerySpecification;
import org.eclipse.viatra.query.runtime.emf.types.EClassTransitiveInstancesKey;
import org.eclipse.viatra.query.runtime.emf.types.EDataTypeInSlotsKey;
import org.eclipse.viatra.query.runtime.emf.types.EStructuralFeatureInstancesKey;
import org.eclipse.viatra.query.runtime.exception.ViatraQueryException;
import org.eclipse.viatra.query.runtime.matchers.psystem.IExpressionEvaluator;
import org.eclipse.viatra.query.runtime.matchers.psystem.IValueProvider;
import org.eclipse.viatra.query.runtime.matchers.psystem.PBody;
import org.eclipse.viatra.query.runtime.matchers.psystem.PVariable;
import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Equality;
import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExportedParameter;
import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExpressionEvaluation;
import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.TypeConstraint;
import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PParameter;
import org.eclipse.viatra.query.runtime.matchers.psystem.queries.QueryInitializationException;
import org.eclipse.viatra.query.runtime.matchers.tuple.FlatTuple;
/**
* A pattern-specific query specification that can instantiate EClassNamesKeywordMatcher in a type-safe way.
*
* @see EClassNamesKeywordMatcher
* @see EClassNamesKeywordMatch
*
*/
@SuppressWarnings("all")
public final class EClassNamesKeywordQuerySpecification extends BaseGeneratedEMFQuerySpecification<EClassNamesKeywordMatcher> {
private EClassNamesKeywordQuerySpecification() {
super(GeneratedPQuery.INSTANCE);
}
/**
* @return the singleton instance of the query specification
* @throws ViatraQueryException if the pattern definition could not be loaded
*
*/
public static EClassNamesKeywordQuerySpecification instance() throws ViatraQueryException {
try{
return LazyHolder.INSTANCE;
} catch (ExceptionInInitializerError err) {
throw processInitializerError(err);
}
}
@Override
protected EClassNamesKeywordMatcher instantiate(final ViatraQueryEngine engine) throws ViatraQueryException {
return EClassNamesKeywordMatcher.on(engine);
}
@Override
public EClassNamesKeywordMatch newEmptyMatch() {
return EClassNamesKeywordMatch.newEmptyMatch();
}
@Override
public EClassNamesKeywordMatch newMatch(final Object... parameters) {
return EClassNamesKeywordMatch.newMatch((org.eclipse.emf.ecore.EClass) parameters[0], (java.lang.String) parameters[1]);
}
/**
* Inner class allowing the singleton instance of {@link EClassNamesKeywordQuerySpecification} to be created
* <b>not</b> at the class load time of the outer class,
* but rather at the first call to {@link EClassNamesKeywordQuerySpecification#instance()}.
*
* <p> This workaround is required e.g. to support recursion.
*
*/
private static class LazyHolder {
private final static EClassNamesKeywordQuerySpecification INSTANCE = new EClassNamesKeywordQuerySpecification();
/**
* Statically initializes the query specification <b>after</b> the field {@link #INSTANCE} is assigned.
* This initialization order is required to support indirect recursion.
*
* <p> The static initializer is defined using a helper field to work around limitations of the code generator.
*
*/
private final static Object STATIC_INITIALIZER = ensureInitialized();
public static Object ensureInitialized() {
INSTANCE.ensureInitializedInternalSneaky();
return null;
}
}
private static class GeneratedPQuery extends BaseGeneratedEMFPQuery {
private final static EClassNamesKeywordQuerySpecification.GeneratedPQuery INSTANCE = new GeneratedPQuery();
@Override
public String getFullyQualifiedName() {
return "org.eclipse.viatra.query.application.queries.eClassNamesKeyword";
}
@Override
public List<String> getParameterNames() {
return Arrays.asList("c","n");
}
@Override
public List<PParameter> getParameters() {
return Arrays.asList(new PParameter("c", "org.eclipse.emf.ecore.EClass"),new PParameter("n", "java.lang.String"));
}
@Override
public Set<PBody> doGetContainedBodies() throws QueryInitializationException {
Set<PBody> bodies = Sets.newLinkedHashSet();
try {
{
PBody body = new PBody(this);
PVariable var_c = body.getOrCreateVariableByName("c");
PVariable var_n = body.getOrCreateVariableByName("n");
new TypeConstraint(body, new FlatTuple(var_c), new EClassTransitiveInstancesKey((EClass)getClassifierLiteral("http://www.eclipse.org/emf/2002/Ecore", "EClass")));
new TypeConstraint(body, new FlatTuple(var_n), new EDataTypeInSlotsKey((EDataType)getClassifierLiteral("http://www.eclipse.org/emf/2002/Ecore", "EString")));
body.setSymbolicParameters(Arrays.<ExportedParameter>asList(
new ExportedParameter(body, var_c, "c"),
new ExportedParameter(body, var_n, "n")
));
// EClass.name(c,n)
new TypeConstraint(body, new FlatTuple(var_c), new EClassTransitiveInstancesKey((EClass)getClassifierLiteral("http://www.eclipse.org/emf/2002/Ecore", "EClass")));
PVariable var__virtual_0_ = body.getOrCreateVariableByName(".virtual{0}");
new TypeConstraint(body, new FlatTuple(var_c, var__virtual_0_), new EStructuralFeatureInstancesKey(getFeatureLiteral("http://www.eclipse.org/emf/2002/Ecore", "ENamedElement", "name")));
new Equality(body, var__virtual_0_, var_n);
// check("A" == n)
new ExpressionEvaluation(body, new IExpressionEvaluator() {
@Override
public String getShortDescription() {
return "Expression evaluation from pattern eClassNamesKeyword";
}
@Override
public Iterable<String> getInputParameterNames() {
return Arrays.asList("n");
}
@Override
public Object evaluateExpression(IValueProvider provider) throws Exception {
java.lang.String n = (java.lang.String) provider.getValue("n");
return evaluateExpression_1_1(n);
}
}, null);
bodies.add(body);
}
// to silence compiler error
if (false) throw new ViatraQueryException("Never", "happens");
} catch (ViatraQueryException ex) {
throw processDependencyException(ex);
}
return bodies;
}
}
private static boolean evaluateExpression_1_1(final String n) {
boolean _equals = Objects.equal("A", n);
return _equals;
}
}