blob: 2d2e1a84d922c4e2c07a230557fea164c87891c0 [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.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;
import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.core.AnnotatedElement;
/**
* @author Gergely Varro, Akos Horvath
*
*/
public class BoundToBoundCheckOperation extends EdgeConstraintCheckOperation {
private Integer source;
private Integer target;
private SearchGraphEdge edge;
public BoundToBoundCheckOperation(Integer source,
Integer target,
SearchGraphEdge edge) {
this.source = source;
this.target = target;
this.edge = edge;
}
protected boolean check(MatchingFrame frame)
throws PatternMatcherRuntimeException {
IModelElement sourceElement = null;
IModelElement targetElement = null;
try {
sourceElement = (IModelElement) frame.getValue(source);
} catch (ClassCastException e) {
String[] context = {edge.getVPMEdgeType().toString(), frame.getPattern().getSearchGraph().getSearchNode(source).getName(), frame.getValue(source).getClass().getName()};
throw new PatternMatcherRuntimeException(PatternMatcherErrorStrings.NOT_A_MODELELEMENT_CHECK
,context
,frame.getPattern().getSearchGraph().getSearchNode(source).getTraceabilityElement().getRepresentativeEMFElement());
}
try {
targetElement = (IModelElement) frame.getValue(target);
} catch (ClassCastException e) {
String[] context = {edge.getVPMEdgeType().toString(), frame.getPattern().getSearchGraph().getSearchNode(target).getName(), frame.getValue(target).getClass().getName()};
throw new PatternMatcherRuntimeException(PatternMatcherErrorStrings.NOT_A_MODELELEMENT_CHECK
,context
,frame.getPattern().getSearchGraph().getSearchNode(target).getTraceabilityElement().getRepresentativeEMFElement());
}
if (sourceElement == null) {
String[] context = {frame.getPattern().getSearchGraph().getSearchNode(source).getName(),getClass().getSimpleName()};
throw new PatternMatcherRuntimeException(PatternMatcherErrorStrings.INTERNAL_EMPTY_VALUE
,context
,frame.getPattern().getSearchGraph().getSearchNode(source).getTraceabilityElement().getRepresentativeEMFElement());
}
if (targetElement == null) {
String[] context = {frame.getPattern().getSearchGraph().getSearchNode(target).getName(),getClass().getSimpleName()};
throw new PatternMatcherRuntimeException(PatternMatcherErrorStrings.INTERNAL_EMPTY_VALUE
,context
,frame.getPattern().getSearchGraph().getSearchNode(target).getTraceabilityElement().getRepresentativeEMFElement());
}
if (edge.getVPMEdgeType() == EdgeType.INSTANCEOF && !edge.isSource()) {
//return sourceElement.getAllTypes().contains(targetElement);
return sourceElement.isInstanceOf(targetElement);
} else if (edge.getVPMEdgeType() == EdgeType.INSTANCEOF && edge.isSource()) {
//return sourceElement.getAllInstances().contains(targetElement);
return targetElement.isInstanceOf(sourceElement);
} else if (edge.getVPMEdgeType() == EdgeType.SUPERTYPEOF && !edge.isSource()) {
//return sourceElement.getAllSubtypes().contains(targetElement);
return targetElement.isSubtypeOf(sourceElement);
} else if (edge.getVPMEdgeType() == EdgeType.SUPERTYPEOF && edge.isSource()) {
return sourceElement.isSubtypeOf(targetElement);
//return sourceElement.getAllSupertypes().contains(targetElement);
} else if (edge.getVPMEdgeType() == EdgeType.SOURCE && !edge.isSource()) {
return (targetElement instanceof IRelation ?
((IRelation) targetElement).getFrom().equals(sourceElement) :
false);
} else if (edge.getVPMEdgeType() == EdgeType.SOURCE && edge.isSource()) {
return (sourceElement instanceof IRelation ?
((IRelation) sourceElement).getFrom().equals(targetElement) :
false);
} else if (edge.getVPMEdgeType() == EdgeType.TARGET && !edge.isSource()) {
return (targetElement instanceof IRelation ?
((IRelation) targetElement).getTo().equals(sourceElement) :
false);
} else if (edge.getVPMEdgeType() == EdgeType.TARGET && edge.isSource()) {
return (sourceElement instanceof IRelation ?
((IRelation) sourceElement).getTo().equals(targetElement) :
false);
} else if (edge.getVPMEdgeType() == EdgeType.IN && !edge.isSource()) {
return sourceElement.getNamespace().compareTo(targetElement) == 0;
} else if (edge.getVPMEdgeType() == EdgeType.IN && edge.isSource()) {
return targetElement.getNamespace().compareTo(sourceElement) == 0;
} else if (edge.getVPMEdgeType() == EdgeType.BELOW && !edge.isSource()) {
return sourceElement.isBelowNamespace(targetElement);
} else if (edge.getVPMEdgeType() == EdgeType.BELOW && edge.isSource()) {
return targetElement.isBelowNamespace(sourceElement);
}
String [] context = {edge.getVPMEdgeType().toString(),getClass().getSimpleName()};
throw new PatternMatcherRuntimeException(PatternMatcherErrorStrings.INTERNAL_OPERATION_NOT_SUPPORTED
,context
,edge.getTraceabilityElement().getRepresentativeEMFElement());
}
public String toString() {
return getClass().getSimpleName() + " : " +
source + " -- " + edge.getVPMEdgeType().name() + " -> " + target;
}
public String toString(MatchingFrame frame) {
IModelElement sourceElement = null;
IModelElement targetElement = null;
try {
sourceElement = (IModelElement) frame.getValue(source);
} catch (ClassCastException e) {
return toString();
}
try {
targetElement = (IModelElement) frame.getValue(target);
} catch (ClassCastException e) {
return toString();
}
if (sourceElement == null) return toString();
if (targetElement == null) return toString();
//in which order the relation is pointing
if(edge.getVPMEdgeType() == EdgeType.INSTANCEOF)
return getClass().getSimpleName() + " : " +
edge.getTargetNode().getName() + " -- " + edge.getVPMEdgeType().name() + " -> " + edge.getSourceNode().getName();
else //may require additional filtering
return getClass().getSimpleName() + " : " +
edge.getSourceNode().getName() + " -- " + edge.getVPMEdgeType().name() + " -> " + edge.getTargetNode().getName();
}
public void calculateSidewaysPassedVariables(Set<Integer> variables) {
variables.add(source);
variables.add(target);
}
public void calculateLocalVariables(Set<Integer> variables) {
variables.add(source);
variables.add(target);
}
public AnnotatedElement getErrorfulElement(MatchingFrame frame) {
return edge.getTraceabilityElement().getRepresentativeEMFElement();
}
}