| /******************************************************************************* |
| * 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.interpreter.exception;
|
|
|
| import java.io.File; |
| import java.io.PrintStream; |
| import java.io.PrintWriter; |
| import java.util.ArrayList; |
| import java.util.Collection; |
| |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.compoundRules.ChooseRule; |
| import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.compoundRules.ForallRule; |
| import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.compoundRules.LetRule; |
| import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.core.AnnotatedElement; |
| import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.core.Annotation; |
| import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.core.GTASMElement; |
| import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.definitions.Machine; |
| import org.eclipse.viatra2.gtasmmodel.gtasm.metamodel.asm.terms.Constant; |
| import org.eclipse.viatra2.gtasmmodel.vpm.editmodel.ModelElement; |
| |
| /** |
| * This exception is used to signal the failure of the GTASM interpreter. |
| * @author Akos Horvath, Gergely Varro |
| * |
| */ |
| abstract public class GTASMException extends ViatraTransformationException {
|
| |
| |
| private static final long serialVersionUID = -3717049954955553256L;
|
|
|
| protected ArrayList<StackTraceElement> interpreterStackTrace = new ArrayList<StackTraceElement>(); |
| |
| public GTASMException(String msg, String[] context) { |
| super(msg, context); |
| } |
| public GTASMException(String msg, String[] context, Throwable cause) { |
| super(msg, context, cause); |
| } |
| |
| public Collection<StackTraceElement> getStackTraceCollection() { |
| return interpreterStackTrace; |
| } |
| |
|
|
| @Override |
| public StackTraceElement[] getStackTrace() { |
| StackTraceElement[] array = |
| new StackTraceElement[interpreterStackTrace.size()]; |
| return interpreterStackTrace.toArray(array); |
| } |
| |
| @Override |
| public void printStackTrace() { |
| StackTraceElement[] stackTrace = getStackTrace(); |
| for (int i = 0; i < stackTrace.length; i++) { |
| System.err.println("["+(stackTrace.length-i)+"] "+stackTrace[i].toString()); |
| } |
| } |
| |
| @Override |
| public void printStackTrace(PrintStream s) { |
| StackTraceElement[] stackTrace = getStackTrace(); |
| for (int i = 0; i < stackTrace.length; i++) { |
| s.println("["+(stackTrace.length-i)+"] "+stackTrace[i].toString()); |
| } |
| } |
| |
| @Override |
| public void printStackTrace(PrintWriter arg0) { |
| StackTraceElement[] stackTrace = getStackTrace(); |
| for (int i = 0; i < stackTrace.length; i++) { |
| arg0.println(stackTrace[i].toString()); |
| } |
| } |
| |
| /**Adds a new element to the stack trace |
| * @param element The element to be added |
| * @return |
| */ |
| // public GTASMException addNewStackElement(StackTraceElement element) { |
| // interpreterStackTrace.add(element); |
| // return this; |
| // } |
| |
| |
| @Override |
| public ViatraTransformationException addNewStackElement(EObject element) { |
| if(element != null && element instanceof AnnotatedElement) |
| { |
| StackTraceElement _ste = createStackTraceElement((AnnotatedElement)element); |
| interpreterStackTrace.add(_ste); |
| } |
| return this; |
| } |
| |
| /** Returns the FQN of the containing machine |
| * @param element The contained element |
| * @return |
| */ |
| private static String getMachineFQN(EObject element) { |
| while (!(element.eContainer() instanceof Machine) |
| && !(element.eContainer() == null)) { |
| element = element.eContainer(); |
| } |
| |
| if (element.eContainer() == null) { |
| return "Error, element not under any machine!!!"; |
| } else { |
| return ((Machine) element.eContainer()).getName(); |
| } |
| } |
| |
| /**Creates a stack trace element based on the information (source file, line info etc.) of the GTASMElement. |
| * @param element |
| * @return |
| */ |
| protected static StackTraceElement createStackTraceElement(AnnotatedElement element) { |
| int size = element.getAnnotations().size(); |
| String sourceFile = ""; |
| String elementName=""; |
| int sourceLine = -1; |
| // Have to check the annotations |
| for (int j = 0; j < size; j++) { |
| Annotation annotation = |
| element.getAnnotations().get(j); |
| // annotation handling of the Babo parser |
| if (annotation.getKey().equals("sourcefile")) { |
| sourceFile = new File(annotation.getValue()).getName(); |
| } else if (annotation.getKey().equals("sourceline")) { |
| String value = annotation.getValue(); |
| sourceLine = (value == null || value == "") |
| ? -1 |
| : Integer.parseInt(value); |
| } else if (annotation.getKey().equals("node_info")) { |
| String value = annotation.getValue(); |
| sourceLine = (value == null || value == "") |
| ? -1 : Integer.parseInt(value.substring(0,value.indexOf(','))); |
| } |
| } |
| |
| // Name cannot be null! |
| if (element instanceof GTASMElement && ((GTASMElement)element).getName() != null) { |
| elementName = ((GTASMElement)element).getName(); |
| } |
| else |
| if(element instanceof ModelElement && ((ModelElement)element).getName() != null){ |
| elementName = ((ModelElement)element).getName(); |
| }else { |
| if (element instanceof Constant) { |
| elementName = "Constant value ="+((Constant)element).getValue(); |
| } else if (element instanceof ForallRule) { |
| elementName = "forall"; |
| } else if(element instanceof ChooseRule) { |
| elementName = "choose"; |
| } else if(element instanceof LetRule) { |
| elementName = "let"; |
| } else { |
| elementName = element.eClass().getName(); |
| } |
| } |
| return new StackTraceElement(getMachineFQN(element), |
| elementName, |
| sourceFile, |
| sourceLine); |
| } |
| |
| /** Returns the complete Viatra specific stack trace |
| * @return |
| */ |
| public String getSerializedStackTrace(){
|
| String buffer=this.getMessage();
|
| StackTraceElement[] stackTrace = getStackTrace();
|
| for (int i = 0; i < stackTrace.length; i++) {
|
| buffer+=System.getProperty("line.separator")+stackTrace[i].toString();
|
| }
|
| return buffer;
|
| }
|
| }
|