blob: 1a37ced936f2e539344994edccd68f1464de16bd [file] [log] [blame]
/*******************************************************************************
* 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 direct instantiation.
* Enumerates type-instance pairs for direct instanceOf relationships, i.e. subtyping is ignored.
*
* Usage:
* @Incremental('reinterpret'='directInstantiation')
* pattern immediateInstanceOf(Type, Instance) = {}
*
* @author Bergmann Gábor
*
*/
public class HardwiredDirectInstantiation<StubHandle, Collector> {
protected GTASMBuildable<StubHandle, Collector> buildable;
protected IPatternMatcherContext<GTPattern> context;
protected GTPattern gtPattern;
protected Collector prodNode;
/**
* @param buildable
* @param context
* @param gtPattern
* @param prodNode
*/
public HardwiredDirectInstantiation(
GTASMBuildable<StubHandle, Collector> buildable,
IPatternMatcherContext<GTPattern> context,
GTPattern gtPattern, Collector prodNode) {
super();
this.buildable = buildable.getNextContainer().putOnTab(gtPattern);
this.context = context;
this.gtPattern = gtPattern;
this.prodNode = prodNode;
}
/**
* @return
*/
public Collector run() throws RetePatternBuildException {
EList<PatternVariable> symParameters = gtPattern.getSymParameters();
if (symParameters.size() != 2) {
throw new RetePatternBuildException(
"Pattern {1} is annotated as directInstatiation, should have two arguments",
new String[]{gtPattern.getFqn()}, gtPattern);
}
// TODO nicer Variable name
Stub<StubHandle> instantiationDirect =
buildable.instantiationDirectStub(new FlatTuple(symParameters.toArray()));
buildable.buildConnection(instantiationDirect, prodNode);
return prodNode;
}
}