| /******************************************************************************* |
| * Copyright (c) 2010, Gabor Bergmann, Zoltan Ujhelyi 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: |
| * Zoltan Ujhelyi - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.viatra2.gtasm.patternmatcher.incremental; |
| |
| import java.util.Collection; |
| import java.util.Map; |
| |
| import org.eclipse.emf.common.util.EList; |
| import org.eclipse.viatra2.framework.IFramework; |
| import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.construction.RetePatternBuildException; |
| import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.ReteEngine; |
| import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.RetePatternMatcher; |
| import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.tuple.Tuple; |
| import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.gt.GTPattern; |
| import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.gt.PatternVariable; |
| |
| /** |
| * A helper class for more simple handling of the incremental matcher |
| * @author Zoltan Ujhelyi |
| */ |
| public class ReteMatcherHelper { |
| |
| /** |
| * Returns all matches for a selected pattern in the selected framework with |
| * all parameters considered as unbounded. |
| * @param fw |
| * @param pattern |
| * @return the matches of the pattern |
| * @throws RetePatternBuildException |
| */ |
| public static Collection<Tuple> getMatchSet(IFramework fw, GTPattern pattern) |
| throws RetePatternBuildException { |
| int size = pattern.getSymParameters().size(); |
| Object[] inputMapping = new Object[size]; |
| boolean[] fixed = new boolean[size]; |
| for (int i = 0; i < size; i++) { |
| inputMapping[i] = null; |
| fixed[i] = false; |
| } |
| |
| return getMatchSet(fw, pattern, inputMapping, fixed); |
| } |
| |
| /** |
| * Returns all matches for a selected pattern in the selected framework with |
| * all non-null parameters considered as bounded. |
| * @param fw |
| * @param pattern |
| * @param parameters |
| * The parameter types are typically IModelElements |
| * @return the matches of the pattern |
| * @throws RetePatternBuildException |
| */ |
| public static Collection<Tuple> getMatchSet(IFramework fw, |
| GTPattern pattern, Object[] parameters) |
| throws RetePatternBuildException { |
| int size = parameters.length; |
| boolean[] fixed = new boolean[size]; |
| for (int i = 0; i < size; i++) { |
| fixed[i] = parameters[i] != null; |
| } |
| return getMatchSet(fw, pattern, parameters, fixed); |
| } |
| |
| /** |
| * Returns all matches for a selected pattern in the selected framework with |
| * the bound parameters described as a name-value map. |
| * @param fw |
| * @param pattern |
| * @param parameters |
| * the bound parameters are written as name-value pairs - the |
| * unbounded should not be present in the map. |
| * @return the matches of the pattern |
| * @throws RetePatternBuildException |
| */ |
| public static Collection<Tuple> getMatchSet(IFramework fw, |
| GTPattern pattern, Map<String, Object> parameters) |
| throws RetePatternBuildException { |
| EList<PatternVariable> symParameters = pattern.getSymParameters(); |
| int size = symParameters.size(); |
| Object[] inputMapping = new Object[size]; |
| boolean[] fixed = new boolean[size]; |
| for (int i = 0; i < size; i++) { |
| String key = symParameters.get(i).getName(); |
| fixed[i] = parameters.containsKey(key); |
| if (fixed[i]) { |
| inputMapping[i] = parameters.get(key); |
| } else { |
| inputMapping[i] = null; |
| } |
| } |
| return getMatchSet(fw, pattern, inputMapping, fixed); |
| } |
| |
| private static Collection<Tuple> getMatchSet(IFramework fw, |
| GTPattern pattern, Object[] inputMapping, boolean[] fixed) |
| throws RetePatternBuildException { |
| ReteEngine<GTPattern> reteEngine = EngineManager.getInstance() |
| .getReteEngine(fw); |
| RetePatternMatcher patternMatcher = reteEngine.accessMatcher(pattern); |
| return patternMatcher.matchAll(inputMapping, fixed); |
| } |
| } |