blob: 75c81876c93d3f32385297da001ea0cff488527e [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;
public class ConstantToBoundCheckOperation extends EdgeConstraintCheckOperation {
private IModelElement source;
private Integer target;
private SearchGraphEdge edge;
public ConstantToBoundCheckOperation(IModelElement source,
Integer target,
SearchGraphEdge edge) {
this.source = source;
this.target = target;
this.edge = edge;
}
protected boolean check(MatchingFrame frame)
throws PatternMatcherRuntimeException {
IModelElement targetElement = null;
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 (source == null) {
String[] context = {edge.getSourceNode().getName(),getClass().getSimpleName()};
throw new PatternMatcherRuntimeException(PatternMatcherErrorStrings.INTERNAL_EMPTY_VALUE
,context
,edge.getSourceNode().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 source.getAllTypes().contains(targetElement);
return source.isInstanceOf(targetElement);
} else if (edge.getVPMEdgeType() == EdgeType.INSTANCEOF && edge.isSource()) {
////return source.getAllInstances().contains(targetElement);
return targetElement.isInstanceOf(source);
} else if (edge.getVPMEdgeType() == EdgeType.SUPERTYPEOF && !edge.isSource()) {
//return source.getAllSubtypes().contains(targetElement);
return targetElement.isSubtypeOf(source);
} else if (edge.getVPMEdgeType() == EdgeType.SUPERTYPEOF && edge.isSource()) {
//return source.getAllSupertypes().contains(targetElement);
return source.isSubtypeOf(targetElement);
} else if (edge.getVPMEdgeType() == EdgeType.SOURCE && !edge.isSource()) {
return (targetElement instanceof IRelation ?
((IRelation) targetElement).getFrom().equals(source) :
false);
} else if (edge.getVPMEdgeType() == EdgeType.SOURCE && edge.isSource()) {
return (source instanceof IRelation ?
((IRelation) source).getFrom().equals(targetElement) :
false);
} else if (edge.getVPMEdgeType() == EdgeType.TARGET && !edge.isSource()) {
return (targetElement instanceof IRelation ?
((IRelation) targetElement).getTo().equals(source) :
false);
} else if (edge.getVPMEdgeType() == EdgeType.TARGET && edge.isSource()) {
return (source instanceof IRelation ?
((IRelation) source).getTo().equals(targetElement) :
false);
} else if (edge.getVPMEdgeType() == EdgeType.IN && !edge.isSource()) {
return source.getNamespace().compareTo(targetElement) == 0;
} else if (edge.getVPMEdgeType() == EdgeType.IN && edge.isSource()) {
return targetElement.getNamespace().compareTo(source) == 0;
} else if (edge.getVPMEdgeType() == EdgeType.BELOW && !edge.isSource()) {
return source.isBelowNamespace(targetElement);
} else if (edge.getVPMEdgeType() == EdgeType.BELOW && edge.isSource()) {
return targetElement.isBelowNamespace(source);
}
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.getName() + " -- " + edge.getVPMEdgeType().name() + " -> " + target;
}
public String toString(MatchingFrame frame) {
IModelElement targetElement = null;
try {
targetElement = (IModelElement) frame.getValue(target);
} catch (ClassCastException e) {
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> variableSet) {
variableSet.add(target);
}
public void calculateLocalVariables(Set<Integer> variableSet) {
variableSet.add(target);
}
public AnnotatedElement getErrorfulElement(MatchingFrame frame) {
return edge.getTraceabilityElement().getRepresentativeEMFElement();
}
}