blob: 26471940cb3af802fc203f74fdcce7e16e52f4a3 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Peter Pasztor, Akos Horvath, Gergely Varro, Istvan Rath and Daniel Varro
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Peter Pasztor, Akos Horvath, Gergely Varro, Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.gtasm.patternmatcher.patterns;
import java.util.Map;
import org.eclipse.viatra2.gtasm.interpreter.exception.ViatraTransformationException;
import org.eclipse.viatra2.gtasm.interpreter.executionEnvironment.IExecutionEnvironment;
import org.eclipse.viatra2.gtasm.patternmatcher.PatternMatcherParameters;
import org.eclipse.viatra2.gtasm.patternmatcher.extension.ViatraGTASMPatternMatcherPlugin;
import org.eclipse.viatra2.gtasm.support.helper.GTASMHelper;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.gt.GTPattern;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.gt.GTRule;
public class PatternMatcherProvider {
private static PatternMatcherProvider _instance = new PatternMatcherProvider();
private static String patternMathcerIDToUse = ViatraGTASMPatternMatcherPlugin.DEFAULT_PM_FACTORY_ID;
private static String gtRuleMathcerIDToUse = ViatraGTASMPatternMatcherPlugin.DEFAULT_GT_FACTORY_ID;
public static PatternMatcherProvider getInstance() {
return _instance;
}
public static void setPatternMatcherIDToUse(String id) {
patternMathcerIDToUse = id;
}
public static String getPatternMatcherIDToUse() {
return patternMathcerIDToUse;
}
public static String getGTRuleMathcerIDToUse() {
return gtRuleMathcerIDToUse;
}
public static void setGTRuleMathcerIDToUse(String ruleMathcerIDToUse) {
gtRuleMathcerIDToUse = ruleMathcerIDToUse;
}
/**
* Returns a pattern matcher to work with
*
* @param executionEnvironment
* the execution environment of the invocation context
* @param gtPattern
* the invoked pattern
* @return a patternMatcher for the pattern
* @throws ViatraTransformationException
*/
public IPatternMatcher getPatternMatcher(
IExecutionEnvironment executionEnvironment, GTPattern gtPattern)
throws ViatraTransformationException {
Map<String, String> patternMatcherIDbyAnnotationName =
ViatraGTASMPatternMatcherPlugin.getDefault().getPatternMatcherIDbyAnnotationName();
for (Map.Entry<String, String> entry : patternMatcherIDbyAnnotationName.entrySet()) {
Map<String, String> runtimeAnnotation =
GTASMHelper.extractLowerCaseRuntimeAnnotation(gtPattern, "@" + entry.getKey());
if (runtimeAnnotation != null) {
return ViatraGTASMPatternMatcherPlugin.getDefault()
.getPatternMatcherFactory(entry.getValue())
.getPatternMatcher(executionEnvironment, gtPattern);
}
}
return ViatraGTASMPatternMatcherPlugin.getDefault()
.getPatternMatcherFactory(patternMathcerIDToUse)
.getPatternMatcher(executionEnvironment, gtPattern);
}
/**
* Returns a GT rule matcher to work with
*
* @param gtEnviroment
* The execution environment on which the TERMs are evaluated
* @param gtRule
* The invoked GT rule
* @param mp
* The invocation context of the GT rule
* @return a GT rule matcher for the rule
* @throws ViatraTransformationException
*/
public IGTRuleMatcher getGtRuleMatcher(IExecutionEnvironment gtEnviroment,
GTRule gtRule, PatternMatcherParameters mp)
throws ViatraTransformationException {
return ViatraGTASMPatternMatcherPlugin
.getDefault()
.getGTMatcherFactory(
ViatraGTASMPatternMatcherPlugin.DEFAULT_GT_FACTORY_ID)
.getGTMatcher(gtEnviroment, gtRule, mp);
}
/**
* Sets the pattern matcher provider to the default pattern and GT matcher
*/
public static void resetPatternMatcher() {
setPatternMatcherIDToUse(ViatraGTASMPatternMatcherPlugin.DEFAULT_PM_FACTORY_ID);
setGTRuleMathcerIDToUse(ViatraGTASMPatternMatcherPlugin.DEFAULT_GT_FACTORY_ID);
}
}