/******************************************************************************* | |
* Copyright (c) 2004-2010 Gabor Bergmann 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: | |
* Gabor Bergmann - initial API and implementation | |
*******************************************************************************/ | |
package org.eclipse.viatra2.gtasm.patternmatcher.incremental.hardwired; | |
import org.eclipse.emf.common.util.EList; | |
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.adapters.GTASMBuildable; | |
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.construction.RetePatternBuildException; | |
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.construction.Stub; | |
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.matcher.IPatternMatcherContext; | |
import org.eclipse.viatra2.gtasm.patternmatcher.incremental.rete.tuple.FlatTuple; | |
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.gt.GTPattern; | |
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.gt.PatternVariable; | |
/** | |
* Hardwired pattern matcher for transitive closure. | |
* Enumerates transitive closure of a binary pattern referred to as 'ofPattern'. | |
* | |
* The 'ofPattern' must belong to the same machine (more precisely, they have to be siblings). | |
* Therefore it is only referred by its name, not the FQN. | |
* | |
* Usage: | |
* @Incremental('reinterpret'='transitiveClosure', 'ofPattern'='someSiblingPattern') | |
* pattern transitiveClosureOfSomeSiblingPattern(Source, Target) = {} | |
* | |
* @author Bergmann Gábor | |
* | |
*/ | |
public class HardwiredTransitiveClosure<StubHandle, Collector> { | |
protected GTASMBuildable<StubHandle, Collector> buildable; | |
protected IPatternMatcherContext<GTPattern> context; | |
protected GTPattern gtPattern; | |
protected GTPattern ofPattern; | |
protected Collector prodNode; | |
/** | |
* @param buildable | |
* @param context | |
* @param gtPattern | |
* @param prodNode | |
*/ | |
public HardwiredTransitiveClosure( | |
GTASMBuildable<StubHandle, Collector> buildable, | |
IPatternMatcherContext<GTPattern> context, | |
GTPattern gtPattern, GTPattern ofPattern, Collector prodNode) { | |
super(); | |
this.buildable = buildable.getNextContainer().putOnTab(gtPattern); | |
this.context = context; | |
this.gtPattern = gtPattern; | |
this.prodNode = prodNode; | |
this.ofPattern = ofPattern; | |
} | |
/** | |
* @return | |
*/ | |
public Collector run() throws RetePatternBuildException { | |
EList<PatternVariable> symParameters = gtPattern.getSymParameters(); | |
if (symParameters.size() != 2) { | |
throw new RetePatternBuildException( | |
"Pattern {1} is annotated as transitiveClosure, should have two arguments", | |
new String[]{gtPattern.getFqn()}, gtPattern); | |
} | |
if (ofPattern.getSymParameters().size() != 2) { | |
throw new RetePatternBuildException( | |
"Pattern {1} is annotated as transitiveClosure, 'ofPattern' [2] have two arguments", | |
new String[]{gtPattern.getFqn(), ofPattern.getFqn()}, gtPattern); | |
} | |
Stub<StubHandle> ofPatternStub = buildable.patternCallStub(new FlatTuple(symParameters.toArray()), ofPattern); | |
Stub<StubHandle> transitiveClosure = buildable.buildTransitiveClosure(ofPatternStub); | |
buildable.buildConnection(transitiveClosure, prodNode); | |
return prodNode; | |
} | |
} | |