/*
* Copyright (c) 2010-2011, IETR/INSA of Rennes
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the IETR/INSA of Rennes nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package net.sf.orcc.simulators;
import java.math.BigInteger;
import java.util.Map;
import org.eclipse.core.runtime.IProgressMonitor;
/**
* This class implements an abstract simulator.
*
* @author Matthieu Wipliez
* @author Pierre-Laurent Lagalaye
*
*/
public abstract class AbstractSimulator implements Simulator {
private IProgressMonitor monitor;
private Map<String, Object> options;
protected static boolean stopRequested = false;
public static int statusCode = 0;
/**
* Returns the boolean-valued attribute with the given name. Returns the
* given default value if the attribute is undefined.
*
* @param attributeName
* the name of the attribute
* @param defaultValue
* the value to use if no value is found
* @return the value or the default value if no value was found.
*/
final public boolean getAttribute(String attributeName, boolean defaultValue) {
Object obj = options.get(attributeName);
if (obj instanceof Boolean) {
return (Boolean) obj;
} else {
return defaultValue;
}
}
/**
* Returns the integer-valued attribute with the given name. Returns the
* given default value if the attribute is undefined.
*
* @param attributeName
* the name of the attribute
* @param defaultValue
* the value to use if no value is found
* @return the value or the default value if no value was found.
*/
final public int getAttribute(String attributeName, int defaultValue) {
Object obj = options.get(attributeName);
if (obj instanceof Integer) {
return (Integer) obj;
} else {
return defaultValue;
}
}
/**
* Returns the map-valued attribute with the given name. Returns the given
* default value if the attribute is undefined.
*
* @param attributeName
* the name of the attribute
* @param defaultValue
* the value to use if no value is found
* @return the value or the default value if no value was found.
*/
@SuppressWarnings("unchecked")
final public Map<String, String> getAttribute(String attributeName,
Map<String, String> defaultValue) {
Object obj = options.get(attributeName);
if (obj instanceof Map<?, ?>) {
return (Map<String, String>) obj;
} else {
return defaultValue;
}
}
/**
* Returns the string-valued attribute with the given name. Returns the
* given default value if the attribute is undefined.
*
* @param attributeName
* the name of the attribute
* @param defaultValue
* the value to use if no value is found
* @return the value or the default value if no value was found.
*/
final public String getAttribute(String attributeName, String defaultValue) {
Object obj = options.get(attributeName);
if (obj instanceof String) {
return (String) obj;
} else {
return defaultValue;
}
}
/**
* Returns a map containing the backend attributes in this launch
* configuration. Returns an empty map if the backend configuration has no
* attributes.
*
* @return a map of attribute keys and values.
*/
final public Map<String, Object> getAttributes() {
return options;
}
/**
* Called when options are initialized.
*/
abstract protected void initializeOptions();
/**
* Returns true if this process has been stopped or canceled.
*
* @return true if this process has been stopped or canceled
*/
protected boolean isStopped() {
return stopRequested || (monitor != null && monitor.isCanceled());
}
/*
* (non-Javadoc)
*
* @see net.sf.orcc.simulators.Simulator#stop()
*/
@Override
public void setOptions(Map<String, Object> options) {
this.options = options;
initializeOptions();
}
/*
* (non-Javadoc)
*
* @see net.sf.orcc.simulators.Simulator#stop()
*/
@Override
public void setProgressMonitor(IProgressMonitor monitor) {
this.monitor = monitor;
}
/**
* Stop the simulation. This method is called from application after a user
* action (ex: display closed)
*
* @param status
* the status code which should be returned
*/
public static void userStop() {
stopRequested = true;
}
/**
* Stop the simulation. This method is called from application itself.
*
* @param status
* the status code which should be returned
*/
public static void stop(BigInteger status) {
statusCode += status.intValue();
stopRequested = true;
}
/**
* Called by user when he manually stop the simulation.
*/
@Override
public void stop() {
stopRequested = true;
}
}