blob: 11d0bbef6943d1944e75a74d643cc47e2b32740d [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Akos Horvath, Gergely Varro 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:
* Akos Horvath, Gergely Varro - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.operation;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.viatra2.core.IModelElement;
import org.eclipse.viatra2.core.IRelation;
import org.eclipse.viatra2.gtasm.patternmatcher.exceptions.PatternMatcherRuntimeException;
import org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.EdgeType;
import org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.MatchingFrame;
import org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.PatternMatcherErrorStrings;
import org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.searchgraph.SearchGraphEdge;
/**
* The ExtendBoundNodeOperation is an ExtendOperation
* where the fixed node has been bound in an earlier phase
* of the algorithm.
*/
public class ExtendBoundNodeOperation extends ExtendOperation {
private Integer bound;
/**
* @param bound the bound pattern variable.
* @param unbound the unbound pattern variable.
* @param edgeType type of the edge.
* @param isSrc true, if the bound node is the source of the edge.
*/
public ExtendBoundNodeOperation(Integer bound, Integer unbound,
SearchGraphEdge edge) {
super(unbound, edge);
this.bound = bound;
}
public void preprocess(MatchingFrame frame)
throws PatternMatcherRuntimeException {
IModelElement boundElement = null;
try {
boundElement = (IModelElement) frame.getValue(bound);
} catch (ClassCastException e) {
String[] context = {edge.getVPMEdgeType().toString(), frame.getPattern().getSearchGraph().getSearchNode(bound).getName(), frame.getValue(bound).getClass().getName()};
throw new PatternMatcherRuntimeException(PatternMatcherErrorStrings.NOT_A_MODELELEMENT_EXTEND
,context
,frame.getPattern().getSearchGraph().getSearchNode(bound).getTraceabilityElement().getRepresentativeEMFElement());
}
if (boundElement == null) {
String[] context = {frame.getPattern().getSearchGraph().getSearchNode(bound).getName(),getClass().getSimpleName()};
throw new PatternMatcherRuntimeException(PatternMatcherErrorStrings.INTERNAL_EMPTY_VALUE
,context
,frame.getPattern().getSearchGraph().getSearchNode(bound).getTraceabilityElement().getRepresentativeEMFElement());
}
if (edge.getVPMEdgeType() == EdgeType.INSTANCEOF && !edge.isSource()) {
iterator = boundElement.getAllTypes().iterator();
} else if (edge.getVPMEdgeType() == EdgeType.INSTANCEOF && edge.isSource()) {
iterator = boundElement.getAllInstances().iterator();
} else if (edge.getVPMEdgeType() == EdgeType.SUPERTYPEOF && !edge.isSource()) {
iterator = boundElement.getAllSubtypes().iterator();
} else if (edge.getVPMEdgeType() == EdgeType.SUPERTYPEOF && edge.isSource()) {
iterator = boundElement.getAllSupertypes().iterator();
} else if (edge.getVPMEdgeType() == EdgeType.SOURCE && !edge.isSource()) {
iterator = boundElement.getRelationsFrom().iterator();
} else if (edge.getVPMEdgeType() == EdgeType.SOURCE && edge.isSource()) {
Collection<IModelElement> coll = new HashSet<IModelElement>();
if (boundElement instanceof IRelation) {
coll.add(((IRelation) boundElement).getFrom());
}
iterator = coll.iterator();
} else if (edge.getVPMEdgeType() == EdgeType.TARGET && !edge.isSource()) {
iterator = boundElement.getRelationsTo().iterator();
} else if (edge.getVPMEdgeType() == EdgeType.TARGET && edge.isSource()) {
Collection<IModelElement> coll = new HashSet<IModelElement>();
if (boundElement instanceof IRelation) {
coll.add(((IRelation) boundElement).getTo());
}
iterator = coll.iterator();
} else if (edge.getVPMEdgeType() == EdgeType.IN && edge.isSource()) {
iterator = boundElement.getElementsInNamespace().iterator();
} else if (edge.getVPMEdgeType() == EdgeType.BELOW && edge.isSource()) {
iterator = boundElement.getAllElementsInNamespace().iterator();
} else if (edge.getVPMEdgeType() == EdgeType.IN && !edge.isSource()) {
Collection<IModelElement> coll = new HashSet<IModelElement>();
if(boundElement.getNamespace() != null)
coll.add(boundElement.getNamespace());
iterator = coll.iterator();
} else if (edge.getVPMEdgeType() == EdgeType.BELOW && !edge.isSource()) {
String[] context = {frame.getPattern().getSearchGraph().getSearchNode(unbound).getName(),edge.getVPMEdgeType().toString()};
throw new PatternMatcherRuntimeException(PatternMatcherErrorStrings.INTERNAL_OPERATION_NOT_POSSIBLE
,context
,edge.getTraceabilityElement().getRepresentativeEMFElement());
} else if (edge.getVPMEdgeType() == EdgeType.NACCHECK) {
String[] context = {frame.getPattern().getSearchGraph().getSearchNode(unbound).getName(),edge.getVPMEdgeType().toString()};
throw new PatternMatcherRuntimeException(PatternMatcherErrorStrings.INTERNAL_OPERATION_NOT_POSSIBLE
,context
,edge.getTraceabilityElement().getRepresentativeEMFElement());
}
}
public String toString() {
return getClass().getSimpleName() + " : " +
bound + " -- " + edge.getVPMEdgeType().name() + " -> " + unbound;
}
public void calculateSidewaysPassedVariables(Set<Integer> variableSet) {
variableSet.add(bound);
super.calculateSidewaysPassedVariables(variableSet);
}
public void calculateLocalVariables(Set<Integer> variableSet) {
variableSet.add(bound);
}
}