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