Merge pull request #1507 from thzero/scripting
Resolving issue #1270, unexpected exception with simulation scripting
This commit is contained in:
commit
67d7eae42b
@ -270,5 +270,32 @@
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MODULE_DIR$/lib/js-scriptengine-22.1.0.1.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MODULE_DIR$/lib/js-22.1.0.1.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MODULE_DIR$/lib/graal-sdk-22.1.0.1.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
||||
BIN
core/lib/graal-sdk-22.1.0.1.jar
Normal file
BIN
core/lib/graal-sdk-22.1.0.1.jar
Normal file
Binary file not shown.
BIN
core/lib/icu4j-71.1.jar
Normal file
BIN
core/lib/icu4j-71.1.jar
Normal file
Binary file not shown.
BIN
core/lib/js-22.1.0.1.jar
Normal file
BIN
core/lib/js-22.1.0.1.jar
Normal file
Binary file not shown.
BIN
core/lib/js-scriptengine-22.1.0.1.jar
Normal file
BIN
core/lib/js-scriptengine-22.1.0.1.jar
Normal file
Binary file not shown.
BIN
core/lib/truffle-api-22.1.0.1.jar
Normal file
BIN
core/lib/truffle-api-22.1.0.1.jar
Normal file
Binary file not shown.
BIN
core/lib/yasson-1.0.2.jar
Normal file
BIN
core/lib/yasson-1.0.2.jar
Normal file
Binary file not shown.
@ -227,8 +227,8 @@ public abstract class FinSet extends ExternalComponent implements AxialPositiona
|
||||
if (MathUtil.equals(clampedCant, this.cantRadians))
|
||||
return;
|
||||
this.cantRadians = clampedCant;
|
||||
|
||||
fireComponentChangeEvent(ComponentChangeEvent.BOTH_CHANGE);
|
||||
|
||||
fireComponentChangeEvent(ComponentChangeEvent.AERODYNAMIC_CHANGE);
|
||||
}
|
||||
|
||||
public Transformation getCantRotation() {
|
||||
|
||||
@ -6,6 +6,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -62,7 +63,7 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
|
||||
/* Cached data */
|
||||
final protected Map<Integer, StageFlags> stages = new HashMap<Integer, StageFlags>(); // Map of stage number to StageFlags of the corresponding stage
|
||||
final protected Map<MotorConfigurationId, MotorConfiguration> motors = new HashMap<MotorConfigurationId, MotorConfiguration>();
|
||||
final private Collection<MotorConfiguration> activeMotors = new ArrayList<MotorConfiguration>();
|
||||
final private Collection<MotorConfiguration> activeMotors = new ConcurrentLinkedQueue<MotorConfiguration>();
|
||||
final private InstanceMap activeInstances = new InstanceMap();
|
||||
|
||||
private int boundsModID = -1;
|
||||
@ -225,8 +226,7 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
|
||||
for (AxialStage stage : rocket.getStage(stageNumber).getSubStages()) {
|
||||
stages.get(stage.getStageNumber()).active = flags.active;
|
||||
}
|
||||
updateMotors();
|
||||
updateActiveInstances();
|
||||
fireChangeEvent();
|
||||
|
||||
return;
|
||||
}
|
||||
@ -377,7 +377,11 @@ public class FlightConfiguration implements FlightConfigurableParameter<FlightCo
|
||||
|
||||
for (StageFlags flags : this.stages.values()) {
|
||||
if (flags.active) {
|
||||
activeStages.add( rocket.getStage( flags.stageNumber) );
|
||||
AxialStage stage = rocket.getStage(flags.stageNumber);
|
||||
if (stage == null) {
|
||||
continue;
|
||||
}
|
||||
activeStages.add(stage);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
package net.sf.openrocket.rocketcomponent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import net.sf.openrocket.util.Transformation;
|
||||
|
||||
@ -13,7 +13,7 @@ import net.sf.openrocket.util.Transformation;
|
||||
* @author teyrana (aka Daniel Williams) <equipoise@gmail.com>
|
||||
*
|
||||
*/
|
||||
public class InstanceMap extends HashMap<RocketComponent, ArrayList<InstanceContext>> {
|
||||
public class InstanceMap extends ConcurrentHashMap<RocketComponent, ArrayList<InstanceContext>> {
|
||||
|
||||
// =========== Public Functions ========================
|
||||
|
||||
|
||||
@ -514,9 +514,9 @@ public class Rocket extends ComponentAssembly {
|
||||
// Notify all components first
|
||||
Iterator<RocketComponent> iterator = this.iterator(true);
|
||||
while (iterator.hasNext()) {
|
||||
iterator.next().componentChanged(cce);
|
||||
RocketComponent next = iterator.next();
|
||||
next.componentChanged(cce);
|
||||
}
|
||||
|
||||
updateConfigurations();
|
||||
|
||||
notifyAllListeners(cce);
|
||||
@ -556,7 +556,6 @@ public class Rocket extends ComponentAssembly {
|
||||
}
|
||||
|
||||
private void updateConfigurations(){
|
||||
this.selectedConfiguration.update();
|
||||
for( FlightConfiguration config : configSet ){
|
||||
config.update();
|
||||
}
|
||||
|
||||
@ -0,0 +1,101 @@
|
||||
package net.sf.openrocket.scripting;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineFactory;
|
||||
import java.util.*;
|
||||
|
||||
import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine;
|
||||
|
||||
import org.graalvm.polyglot.Context;
|
||||
import org.graalvm.polyglot.HostAccess;
|
||||
|
||||
public class GraalJSScriptEngineFactory implements ScriptEngineFactory {
|
||||
private static final String ENGINE_NAME = "Graal.js";
|
||||
private static final String LANGUAGE = "ECMAScript";
|
||||
private static final String LANGUAGE_VERSION = "ECMAScript 262 Edition 11";
|
||||
private static final String[] NAMES = new String[]{"js", "JS", "JavaScript", "javascript", LANGUAGE, LANGUAGE.toLowerCase(), ENGINE_NAME, ENGINE_NAME.toLowerCase(), "Graal-js", "graal-js", "Graal.JS", "Graal-JS", "GraalJS", "GraalJSPolyglot"};
|
||||
private static final String[] MIME_TYPES = new String[]{"application/javascript", "application/ecmascript", "text/javascript", "text/ecmascript"};
|
||||
private static final String[] EXTENSIONS = new String[]{"js", "mjs"};
|
||||
private static final List names;
|
||||
private static final List mimeTypes;
|
||||
private static final List extensions;
|
||||
|
||||
public GraalJSScriptEngineFactory() {
|
||||
}
|
||||
|
||||
public ScriptEngine getScriptEngine() {
|
||||
// https://github.com/oracle/graaljs/blob/master/docs/user/RunOnJDK.md
|
||||
// https://github.com/oracle/graaljs/blob/master/docs/user/ScriptEngine.md#setting-options-via-bindings
|
||||
return GraalJSScriptEngine.create(null,
|
||||
Context.newBuilder("js")
|
||||
.allowHostAccess(HostAccess.ALL)
|
||||
.allowHostClassLookup(s -> true)
|
||||
.option("js.ecmascript-version", "2021"));
|
||||
}
|
||||
|
||||
public String getEngineName() {
|
||||
return ENGINE_NAME;
|
||||
}
|
||||
|
||||
public String getEngineVersion() {
|
||||
return ((GraalJSScriptEngine)getScriptEngine()).getPolyglotEngine().getVersion();
|
||||
}
|
||||
|
||||
public List<String> getExtensions() {
|
||||
return extensions;
|
||||
}
|
||||
|
||||
public String getLanguageVersion() {
|
||||
return LANGUAGE_VERSION;
|
||||
}
|
||||
|
||||
public String getLanguageName() {
|
||||
return LANGUAGE;
|
||||
}
|
||||
|
||||
public List<String> getMimeTypes() {
|
||||
return mimeTypes;
|
||||
}
|
||||
|
||||
public List<String> getNames() {
|
||||
return names;
|
||||
}
|
||||
|
||||
public String getMethodCallSyntax(final String obj, final String method, final String... args) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getOutputStatement(final String toDisplay) {
|
||||
return "print(" + toDisplay + ")";
|
||||
}
|
||||
|
||||
public Object getParameter(String key) {
|
||||
switch (key) {
|
||||
case "javax.script.name":
|
||||
return "javascript";
|
||||
case "javax.script.engine":
|
||||
return this.getEngineName();
|
||||
case "javax.script.engine_version":
|
||||
return this.getEngineVersion();
|
||||
case "javax.script.language":
|
||||
return this.getLanguageName();
|
||||
case "javax.script.language_version":
|
||||
return this.getLanguageVersion();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String getProgram(final String... statements) {
|
||||
return null;
|
||||
}
|
||||
|
||||
static {
|
||||
List<String> nameList = Arrays.asList(NAMES);
|
||||
List<String> mimeTypeList = Arrays.asList(MIME_TYPES);
|
||||
List<String> extensionList = Arrays.asList(EXTENSIONS);
|
||||
names = Collections.unmodifiableList(nameList);
|
||||
mimeTypes = Collections.unmodifiableList(mimeTypeList);
|
||||
extensions = Collections.unmodifiableList(extensionList);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,300 @@
|
||||
/*
|
||||
* This is a replacement for the ScriptEngineManager which gets around and issue with the sun.misc.ServiceConfigurationError
|
||||
* which has been removed in Java 9+. If using the ScriptEngineManager from the script-api*.jar then the sun.misc throws
|
||||
* a ClassNotFoundException.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package net.sf.openrocket.scripting;
|
||||
|
||||
import javax.script.*;
|
||||
import java.util.*;
|
||||
import java.security.*;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.ServiceConfigurationError;
|
||||
|
||||
/**
|
||||
* The <code>ScriptEngineManager</code> implements a discovery and instantiation
|
||||
* mechanism for <code>ScriptEngine</code> classes and also maintains a
|
||||
* collection of key/value pairs storing state shared by all engines created
|
||||
* by the Manager. This class uses the service provider mechanism described in the
|
||||
* {@link java.util.ServiceLoader} class to enumerate all the
|
||||
* implementations of <code>ScriptEngineFactory</code>. <br><br>
|
||||
* The <code>ScriptEngineManager</code> provides a method to return a list of all these factories
|
||||
* as well as utility methods which look up factories on the basis of language name, file extension
|
||||
* and mime type.
|
||||
* <p>
|
||||
* The <code>Bindings</code> of key/value pairs, referred to as the "Global Scope" maintained
|
||||
* by the manager is available to all instances of <code>ScriptEngine</code> created
|
||||
* by the <code>ScriptEngineManager</code>. The values in the <code>Bindings</code> are
|
||||
* generally exposed in all scripts.
|
||||
*
|
||||
* @author Mike Grogan
|
||||
* @author A. Sundararajan
|
||||
* @since 1.6
|
||||
*/
|
||||
public class ScriptEngineManagerRedux {
|
||||
private static final boolean DEBUG = false;
|
||||
/**
|
||||
* The effect of calling this constructor is the same as calling
|
||||
* <code>ScriptEngineManager(Thread.currentThread().getContextClassLoader())</code>.
|
||||
*
|
||||
* @see java.lang.Thread#getContextClassLoader
|
||||
*/
|
||||
public ScriptEngineManagerRedux() {
|
||||
init(Thread.currentThread().getContextClassLoader());
|
||||
}
|
||||
|
||||
/**
|
||||
* This constructor loads the implementations of
|
||||
* <code>ScriptEngineFactory</code> visible to the given
|
||||
* <code>ClassLoader</code> using the service provider mechanism.<br><br>
|
||||
* If loader is <code>null</code>, the script engine factories that are
|
||||
* bundled with the platform are loaded. <br>
|
||||
*
|
||||
* @param loader ClassLoader used to discover script engine factories.
|
||||
*/
|
||||
public ScriptEngineManagerRedux(ClassLoader loader) {
|
||||
init(loader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value for the specified key in the Global Scope
|
||||
* @param key The key whose value is to be returned.
|
||||
* @return The value for the specified key.
|
||||
*/
|
||||
public Object get(String key) {
|
||||
return _globalScope.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* <code>getBindings</code> returns the value of the <code>globalScope</code> field.
|
||||
* ScriptEngineManager sets this <code>Bindings</code> as global bindings for
|
||||
* <code>ScriptEngine</code> objects created by it.
|
||||
*
|
||||
* @return The globalScope field.
|
||||
*/
|
||||
public Bindings getBindings() {
|
||||
return _globalScope;
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks up and creates a <code>ScriptEngine</code> for a given name.
|
||||
* The algorithm first searches for a <code>ScriptEngineFactory</code> that has been
|
||||
* registered as a handler for the specified name using the <code>registerEngineName</code>
|
||||
* method.
|
||||
* <br><br> If one is not found, it searches the set of <code>ScriptEngineFactory</code> instances
|
||||
* stored by the constructor for one with the specified name. If a <code>ScriptEngineFactory</code>
|
||||
* is found by either method, it is used to create instance of <code>ScriptEngine</code>.
|
||||
* @param shortName The short name of the <code>ScriptEngine</code> implementation.
|
||||
* returned by the <code>getNames</code> method of its <code>ScriptEngineFactory</code>.
|
||||
* @return A <code>ScriptEngine</code> created by the factory located in the search. Returns null
|
||||
* if no such factory was found. The <code>ScriptEngineManager</code> sets its own <code>globalScope</code>
|
||||
* <code>Bindings</code> as the <code>GLOBAL_SCOPE</code> <code>Bindings</code> of the newly
|
||||
* created <code>ScriptEngine</code>.
|
||||
* @throws NullPointerException if shortName is null.
|
||||
*/
|
||||
private Map<String, ScriptEngineFactory> _factoriesByName = new HashMap<>();
|
||||
public synchronized ScriptEngine getEngineByName(String shortName) {
|
||||
if (shortName == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
|
||||
String key = shortName.toLowerCase();
|
||||
if (_factoriesByName.containsKey(key)) {
|
||||
return getEngineByFactory(_factoriesByName.get(key));
|
||||
}
|
||||
|
||||
// Look for registered name first
|
||||
ScriptEngineFactory factoryNamed;
|
||||
if (null != (factoryNamed = _nameAssociations.get(key))) {
|
||||
try {
|
||||
_factoriesByName.put(key, factoryNamed);
|
||||
return getEngineByFactory(factoryNamed);
|
||||
} catch (Exception exp) {
|
||||
if (DEBUG) {
|
||||
exp.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Optional<String> factoryName;
|
||||
List<String> names;
|
||||
for (ScriptEngineFactory factory : _scriptEngineFactories) {
|
||||
try {
|
||||
factoryName = factory.getNames().stream().filter(l -> l.equalsIgnoreCase(shortName)).findFirst();
|
||||
if (factoryName.isPresent()) {
|
||||
_factoriesByName.put(key, factory);
|
||||
return getEngineByFactory(factory);
|
||||
}
|
||||
} catch (Exception exp) {
|
||||
if (DEBUG) {
|
||||
exp.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list whose elements are instances of all the <code>ScriptEngineFactory</code> classes
|
||||
* found by the discovery mechanism.
|
||||
* @return List of all discovered <code>ScriptEngineFactory</code>s.
|
||||
*/
|
||||
public synchronized List<ScriptEngineFactory> getEngineFactories() {
|
||||
return List.copyOf(_scriptEngineFactories);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the specified key/value pair in the Global Scope.
|
||||
* @param key Key to set
|
||||
* @param value Value to set.
|
||||
* @throws NullPointerException if key is null.
|
||||
* @throws IllegalArgumentException if key is empty string.
|
||||
*/
|
||||
public void put(String key, Object value) {
|
||||
_globalScope.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a <code>ScriptEngineFactory</code> to handle a language
|
||||
* name. Overrides any such association found using the Discovery mechanism.
|
||||
* @param name The name to be associated with the <code>ScriptEngineFactory</code>.
|
||||
* @param factory The class to associate with the given name.
|
||||
* @throws NullPointerException if any of the parameters is null.
|
||||
*/
|
||||
public void registerEngineName(String name, ScriptEngineFactory factory) {
|
||||
if (name == null || factory == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
|
||||
_nameAssociations.put(name.toLowerCase(), factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* <code>setBindings</code> stores the specified <code>Bindings</code>
|
||||
* in the <code>globalScope</code> field. ScriptEngineManager sets this
|
||||
* <code>Bindings</code> as global bindings for <code>ScriptEngine</code>
|
||||
* objects created by it.
|
||||
*
|
||||
* @param bindings The specified <code>Bindings</code>
|
||||
* @throws IllegalArgumentException if bindings is null.
|
||||
*/
|
||||
public void setBindings(Bindings bindings) {
|
||||
if (bindings == null) {
|
||||
throw new IllegalArgumentException("Global scope cannot be null.");
|
||||
}
|
||||
|
||||
_globalScope = bindings;
|
||||
}
|
||||
|
||||
private ScriptEngine getEngineByFactory(ScriptEngineFactory factory) {
|
||||
if (factory == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ScriptEngine engine = factory.getScriptEngine();
|
||||
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
|
||||
return engine;
|
||||
}
|
||||
|
||||
private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader loader) {
|
||||
if (loader != null) {
|
||||
return ServiceLoader.load(ScriptEngineFactory.class, loader);
|
||||
} else {
|
||||
return ServiceLoader.loadInstalled(ScriptEngineFactory.class);
|
||||
}
|
||||
}
|
||||
|
||||
private void init(final ClassLoader loader) {
|
||||
_scriptEngineFactories = new TreeSet<>(Comparator.comparing(
|
||||
ScriptEngineFactory::getEngineName,
|
||||
Comparator.nullsLast(Comparator.naturalOrder()))
|
||||
);
|
||||
initEngines(loader);
|
||||
}
|
||||
|
||||
private void initEngines(final ClassLoader loader) {
|
||||
Iterator<ScriptEngineFactory> itr;
|
||||
try {
|
||||
ServiceLoader<ScriptEngineFactory> loaders = AccessController.doPrivileged(
|
||||
new PrivilegedAction<ServiceLoader<ScriptEngineFactory>>() {
|
||||
@Override
|
||||
public ServiceLoader<ScriptEngineFactory> run() {
|
||||
return getServiceLoader(loader);
|
||||
}
|
||||
});
|
||||
|
||||
itr = loaders.iterator();
|
||||
} catch (ServiceConfigurationError err) {
|
||||
// } catch (Exception err) {
|
||||
System.err.println("Can't find ScriptEngineFactory providers: " + err.getMessage());
|
||||
if (DEBUG) {
|
||||
err.printStackTrace();
|
||||
}
|
||||
// do not throw any exception here. user may want to
|
||||
// manage his/her own factories using this manager
|
||||
// by explicit registration (by registerXXX) methods.
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
while (itr.hasNext()) {
|
||||
try {
|
||||
ScriptEngineFactory factory = itr.next();
|
||||
_scriptEngineFactories.add(factory);
|
||||
} catch (ServiceConfigurationError err) {
|
||||
// } catch (Exception err) {
|
||||
System.err.println("ScriptEngineManager providers.next(): " + err.getMessage());
|
||||
if (DEBUG) {
|
||||
err.printStackTrace();
|
||||
}
|
||||
// one factory failed, but check other factories...
|
||||
}
|
||||
}
|
||||
} catch (ServiceConfigurationError err) {
|
||||
// } catch (Exception err) {
|
||||
System.err.println("ScriptEngineManager providers.hasNext(): " + err.getMessage());
|
||||
if (DEBUG) {
|
||||
err.printStackTrace();
|
||||
}
|
||||
// do not throw any exception here. user may want to
|
||||
// manage his/her own factories using this manager
|
||||
// by explicit registratation (by registerXXX) methods.
|
||||
}
|
||||
}
|
||||
|
||||
/** Set of script engine factories discovered. */
|
||||
private TreeSet<ScriptEngineFactory> _scriptEngineFactories;
|
||||
|
||||
/** Map of engine name to script engine factory. */
|
||||
private HashMap<String, ScriptEngineFactory> _nameAssociations = new HashMap<>();
|
||||
|
||||
/** Global bindings associated with script engines created by this manager. */
|
||||
private Bindings _globalScope = new SimpleBindings();
|
||||
}
|
||||
@ -2,7 +2,6 @@ package net.sf.openrocket.simulation.extension.impl;
|
||||
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import net.sf.openrocket.aerodynamics.Warning;
|
||||
@ -91,8 +90,7 @@ public class ScriptingExtension extends AbstractSimulationExtension {
|
||||
|
||||
|
||||
SimulationListener getListener() throws SimulationException {
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
ScriptEngine engine = manager.getEngineByName(getLanguage());
|
||||
ScriptEngine engine = util.getEngineByName(getLanguage());
|
||||
if (engine == null) {
|
||||
throw new SimulationException("Your JRE does not support the scripting language '" + getLanguage() + "'");
|
||||
}
|
||||
|
||||
@ -4,14 +4,14 @@ import java.math.BigInteger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.prefs.BackingStoreException;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineFactory;
|
||||
import javax.script.ScriptEngineManager;
|
||||
|
||||
import net.sf.openrocket.scripting.ScriptEngineManagerRedux;
|
||||
import net.sf.openrocket.scripting.GraalJSScriptEngineFactory;
|
||||
import net.sf.openrocket.startup.Preferences;
|
||||
import net.sf.openrocket.util.ArrayList;
|
||||
import net.sf.openrocket.util.BugException;
|
||||
@ -22,22 +22,34 @@ import com.google.inject.Inject;
|
||||
* Utility class used by the scripting extension and its configurator.
|
||||
*/
|
||||
public class ScriptingUtil {
|
||||
|
||||
static final String NODE_ID = ScriptingExtension.class.getCanonicalName();
|
||||
|
||||
private static final List<String> DEFAULT_TRUSTED_HASHES = Arrays.asList(
|
||||
private static final List<String> DEFAULT_TRUSTED_HASHES = List.of(
|
||||
// Roll control script in roll control example file:
|
||||
"SHA-256:9bf364ce4d4a75f09b29178bf9d6872b232084f73dae20dc7b5b073e54e95a42"
|
||||
);
|
||||
);
|
||||
|
||||
/** The name to be chosen from a list of alternatives. If not found, will use the default name. */
|
||||
private static final List<String> PREFERRED_LANGUAGE_NAMES = Arrays.asList("JavaScript");
|
||||
private static final List<String> PREFERRED_LANGUAGE_NAMES = List.of("JavaScript");
|
||||
|
||||
private static ScriptEngineManagerRedux manager;
|
||||
|
||||
@Inject
|
||||
Preferences prefs;
|
||||
|
||||
|
||||
|
||||
|
||||
public ScriptingUtil() {
|
||||
if (manager == null) {
|
||||
// using the ScriptEngineManger from javax.script package pulls in the sun.misc.ServiceConfigurationError
|
||||
// which is removed in Java 9+ which causes a ClassNotFoundException to be thrown.
|
||||
manager = new ScriptEngineManagerRedux();
|
||||
|
||||
manager.registerEngineName("Javascript", new GraalJSScriptEngineFactory());
|
||||
}
|
||||
}
|
||||
|
||||
public ScriptEngine getEngineByName(String shortName) {
|
||||
return manager.getEngineByName(shortName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the preferred internal language name based on a script language name.
|
||||
@ -48,38 +60,23 @@ public class ScriptingUtil {
|
||||
if (language == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
|
||||
ScriptEngine engine = manager.getEngineByName(language);
|
||||
if (engine == null) {
|
||||
return null;
|
||||
}
|
||||
return getLanguage(engine.getFactory());
|
||||
|
||||
return getLanguageByFactory(engine.getFactory());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<String> getLanguages() {
|
||||
List<String> langs = new ArrayList<String>();
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
List<String> languages = new ArrayList<>();
|
||||
for (ScriptEngineFactory factory : manager.getEngineFactories()) {
|
||||
langs.add(getLanguage(factory));
|
||||
languages.add(getLanguageByFactory(factory));
|
||||
}
|
||||
return langs;
|
||||
return languages;
|
||||
}
|
||||
|
||||
|
||||
private String getLanguage(ScriptEngineFactory factory) {
|
||||
for (String name : factory.getNames()) {
|
||||
if (PREFERRED_LANGUAGE_NAMES.contains(name)) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
return factory.getLanguageName();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Test whether the user has indicated this script to be trusted,
|
||||
* or if it is an internally trusted script.
|
||||
@ -122,7 +119,16 @@ public class ScriptingUtil {
|
||||
throw new BugException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String getLanguageByFactory(ScriptEngineFactory factory) {
|
||||
for (String name : factory.getNames()) {
|
||||
if (PREFERRED_LANGUAGE_NAMES.contains(name)) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
return factory.getLanguageName();
|
||||
}
|
||||
|
||||
static String normalize(String script) {
|
||||
return script.replaceAll("\r", "").trim();
|
||||
@ -132,10 +138,8 @@ public class ScriptingUtil {
|
||||
/*
|
||||
* NOTE: Hash length must be max 80 chars, the max length of a key in a Properties object.
|
||||
*/
|
||||
|
||||
String output;
|
||||
MessageDigest digest;
|
||||
|
||||
try {
|
||||
digest = MessageDigest.getInstance("SHA-256");
|
||||
digest.update(language.getBytes(StandardCharsets.UTF_8));
|
||||
@ -152,5 +156,4 @@ public class ScriptingUtil {
|
||||
|
||||
return digest.getAlgorithm() + ":" + output;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -35,8 +35,7 @@ public class RollControlListener extends AbstractSimulationListener {
|
||||
|
||||
// Maximum control fin angle (rad)
|
||||
private static final double MAX_ANGLE = 15 * Math.PI / 180;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* PID parameters
|
||||
*
|
||||
@ -47,29 +46,22 @@ public class RollControlListener extends AbstractSimulationListener {
|
||||
private static final double KP = 0.007;
|
||||
private static final double KI = 0.2;
|
||||
|
||||
|
||||
|
||||
|
||||
private double rollrate;
|
||||
private double rollRate;
|
||||
|
||||
private double prevTime = 0;
|
||||
private double intState = 0;
|
||||
|
||||
private double finPosition = 0;
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public FlightConditions postFlightConditions(SimulationStatus status, FlightConditions flightConditions) {
|
||||
// Store the current roll rate for later use
|
||||
rollrate = flightConditions.getRollRate();
|
||||
rollRate = flightConditions.getRollRate();
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void postStep(SimulationStatus status) throws SimulationException {
|
||||
|
||||
// Activate PID controller only after a specific time
|
||||
if (status.getSimulationTime() < START_TIME) {
|
||||
prevTime = status.getSimulationTime();
|
||||
@ -87,23 +79,20 @@ public class RollControlListener extends AbstractSimulationListener {
|
||||
if (finset == null) {
|
||||
throw new SimulationException("A fin set with name '" + CONTROL_FIN_NAME + "' was not found");
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Determine time step
|
||||
double deltaT = status.getSimulationTime() - prevTime;
|
||||
prevTime = status.getSimulationTime();
|
||||
|
||||
|
||||
|
||||
// PID controller
|
||||
double error = SETPOINT - rollrate;
|
||||
double error = SETPOINT - rollRate;
|
||||
|
||||
double p = KP * error;
|
||||
intState += error * deltaT;
|
||||
double i = KI * intState;
|
||||
|
||||
double value = p + i;
|
||||
|
||||
|
||||
|
||||
// Clamp the fin angle between -MAX_ANGLE and MAX_ANGLE
|
||||
if (Math.abs(value) > MAX_ANGLE) {
|
||||
System.err.printf("Attempting to set angle %.1f at t=%.3f, clamping.\n",
|
||||
@ -111,7 +100,6 @@ public class RollControlListener extends AbstractSimulationListener {
|
||||
value = MathUtil.clamp(value, -MAX_ANGLE, MAX_ANGLE);
|
||||
}
|
||||
|
||||
|
||||
// Limit the fin turn rate
|
||||
if (finPosition < value) {
|
||||
finPosition = Math.min(finPosition + TURNRATE * deltaT, value);
|
||||
@ -122,6 +110,5 @@ public class RollControlListener extends AbstractSimulationListener {
|
||||
// Set the control fin cant and store the data
|
||||
finset.setCantAngle(finPosition);
|
||||
status.getFlightData().setValue(FIN_CANT_TYPE, finPosition);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -245,5 +245,50 @@
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MODULE_DIR$/../core/lib/icu4j-71.1.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MODULE_DIR$/../core/lib/graal-sdk-22.1.0.1.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MODULE_DIR$/../core/lib/js-22.1.0.1.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MODULE_DIR$/../core/lib/js-scriptengine-22.1.0.1.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MODULE_DIR$/../core/lib/truffle-api-22.1.0.1.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
||||
@ -99,6 +99,11 @@
|
||||
<zipfileset src="${core.dir}/lib/guice-4.2.3-no_aop.jar" />
|
||||
<zipfileset src="${core.dir}/lib/aopalliance.jar"/>
|
||||
<zipfileset src="${core.dir}/lib/commonmark-0.19.0.jar"/>
|
||||
<zipfileset src="${core.dir}/lib/icu4j-71.1.jar"/>
|
||||
<zipfileset src="${core.dir}/lib/js-22.1.0.1.jar"/>
|
||||
<zipfileset src="${core.dir}/lib/graal-sdk-22.1.0.1.jar"/>
|
||||
<zipfileset src="${core.dir}/lib/js-scriptengine-22.1.0.1.jar"/>
|
||||
<zipfileset src="${core.dir}/lib/truffle-api-22.1.0.1.jar"/>
|
||||
<zipfileset src="${core.dir}/lib/script-api-1.0.jar"/>
|
||||
<zipfileset src="${lib.dir}/iText-5.0.2.jar"/>
|
||||
<zipfileset src="${core.dir}/lib/istack-commons-runtime.jar"/>
|
||||
|
||||
Binary file not shown.
@ -282,38 +282,38 @@ public class RocketFigure extends AbstractScaleFigure {
|
||||
Color borderColor = ((SwingPreferences)Application.getPreferences()).getMotorBorderColor();
|
||||
|
||||
FlightConfiguration config = rocket.getSelectedConfiguration();
|
||||
for( MotorConfiguration curInstance : config.getActiveMotors()){
|
||||
for (MotorConfiguration curInstance : config.getActiveMotors()) {
|
||||
MotorMount mount = curInstance.getMount();
|
||||
Motor motor = curInstance.getMotor();
|
||||
double motorLength = motor.getLength();
|
||||
double motorRadius = motor.getDiameter() / 2;
|
||||
RocketComponent mountComponent = ((RocketComponent) mount);
|
||||
|
||||
|
||||
// <component>.getLocation() will return all the parent instances of this owning component, AND all of it's own instances as well.
|
||||
// so, just draw a motor once for each Coordinate returned...
|
||||
Coordinate[] mountLocations = mount.getLocations();
|
||||
|
||||
|
||||
double mountLength = mountComponent.getLength();
|
||||
// System.err.println("Drawing Motor: "+motor.getDesignation()+" (x"+mountLocations.length+")");
|
||||
for ( Coordinate curMountLocation : mountLocations ){
|
||||
Coordinate curMotorLocation = curMountLocation.add( mountLength - motorLength + mount.getMotorOverhang(), 0, 0);
|
||||
for (Coordinate curMountLocation : mountLocations) {
|
||||
Coordinate curMotorLocation = curMountLocation.add(mountLength - motorLength + mount.getMotorOverhang(), 0, 0);
|
||||
// System.err.println(String.format(" mount instance: %s => %s", curMountLocation.toString(), curMotorLocation.toString() ));
|
||||
|
||||
// rotate by figure's axial rotation:
|
||||
curMotorLocation = this.axialRotation.transform(curMotorLocation);
|
||||
|
||||
// rotate by figure's axial rotation:
|
||||
curMotorLocation = this.axialRotation.transform(curMotorLocation);
|
||||
|
||||
{
|
||||
Shape s;
|
||||
if (currentViewType == RocketPanel.VIEW_TYPE.SideView) {
|
||||
s = new Rectangle2D.Double( curMotorLocation.x,
|
||||
(curMotorLocation.y - motorRadius),
|
||||
motorLength,
|
||||
2 * motorRadius);
|
||||
s = new Rectangle2D.Double(curMotorLocation.x,
|
||||
(curMotorLocation.y - motorRadius),
|
||||
motorLength,
|
||||
2 * motorRadius);
|
||||
} else {
|
||||
s = new Ellipse2D.Double((curMotorLocation.z - motorRadius),
|
||||
(curMotorLocation.y - motorRadius),
|
||||
2 * motorRadius,
|
||||
2 * motorRadius);
|
||||
(curMotorLocation.y - motorRadius),
|
||||
2 * motorRadius,
|
||||
2 * motorRadius);
|
||||
}
|
||||
g2.setColor(fillColor);
|
||||
g2.fill(s);
|
||||
@ -378,8 +378,8 @@ public class RocketFigure extends AbstractScaleFigure {
|
||||
|
||||
// allShapes is an output buffer -- it stores all the generated shapes
|
||||
allShapes.clear();
|
||||
|
||||
for(Entry<RocketComponent, ArrayList<InstanceContext>> entry: config.getActiveInstances().entrySet() ) {
|
||||
|
||||
for (Entry<RocketComponent, ArrayList<InstanceContext>> entry : config.getActiveInstances().entrySet()) {
|
||||
final RocketComponent comp = entry.getKey();
|
||||
|
||||
// Only draw podsets when they are selected
|
||||
@ -395,14 +395,14 @@ public class RocketFigure extends AbstractScaleFigure {
|
||||
}
|
||||
if (!selected) continue;
|
||||
}
|
||||
|
||||
|
||||
final ArrayList<InstanceContext> contextList = entry.getValue();
|
||||
|
||||
for(InstanceContext context: contextList ) {
|
||||
for (InstanceContext context : contextList) {
|
||||
final Transformation currentTransform = this.axialRotation.applyTransformation(context.transform);
|
||||
allShapes = addThisShape( allShapes, this.currentViewType, comp, currentTransform);
|
||||
allShapes = addThisShape(allShapes, this.currentViewType, comp, currentTransform);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -8,7 +8,6 @@ import java.awt.event.FocusListener;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JComboBox;
|
||||
@ -33,7 +32,6 @@ import com.google.inject.Inject;
|
||||
|
||||
@Plugin
|
||||
public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfigurator<ScriptingExtension> {
|
||||
|
||||
@Inject
|
||||
private ScriptingUtil util;
|
||||
|
||||
@ -66,7 +64,6 @@ public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfi
|
||||
});
|
||||
panel.add(languageSelector, "wrap para");
|
||||
|
||||
|
||||
text = new RSyntaxTextArea(extension.getScript(), 20, 80);
|
||||
text.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT);
|
||||
text.setCodeFoldingEnabled(true);
|
||||
@ -91,7 +88,6 @@ public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfi
|
||||
RTextScrollPane scroll = new RTextScrollPane(text);
|
||||
panel.add(scroll, "spanx, grow, wrap para");
|
||||
|
||||
|
||||
BooleanModel enabled = new BooleanModel(extension, "Enabled");
|
||||
JCheckBox check = new JCheckBox(enabled);
|
||||
check.setText(trans.get("SimulationExtension.scripting.text.enabled"));
|
||||
@ -116,7 +112,6 @@ public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfi
|
||||
});
|
||||
panel.add(button, "wrap rel");
|
||||
|
||||
|
||||
StyledLabel label = new StyledLabel(trans.get("SimulationExtension.scripting.text.trusted.msg"), -1, Style.ITALIC);
|
||||
panel.add(label);
|
||||
|
||||
@ -130,7 +125,6 @@ public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfi
|
||||
util.setTrustedScript(extension.getLanguage(), extension.getScript(), trusted.isSelected());
|
||||
}
|
||||
|
||||
|
||||
private void setLanguage(String language) {
|
||||
if (language == null) {
|
||||
language = "";
|
||||
@ -144,8 +138,7 @@ public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfi
|
||||
}
|
||||
|
||||
private String findSyntaxLanguage(String language) {
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
ScriptEngine engine = manager.getEngineByName(language);
|
||||
ScriptEngine engine = util.getEngineByName(language);
|
||||
|
||||
if (engine != null) {
|
||||
Set<String> supported = TokenMakerFactory.getDefaultInstance().keySet();
|
||||
@ -163,5 +156,4 @@ public class ScriptingConfigurator extends AbstractSwingSimulationExtensionConfi
|
||||
|
||||
return SyntaxConstants.SYNTAX_STYLE_NONE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -5,16 +5,17 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.script.ScriptEngineFactory;
|
||||
import javax.script.ScriptEngineManager;
|
||||
|
||||
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
|
||||
|
||||
import net.sf.openrocket.scripting.ScriptEngineManagerRedux;
|
||||
|
||||
public class Scripting {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Scripting APIs:");
|
||||
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
ScriptEngineManagerRedux manager = new ScriptEngineManagerRedux();
|
||||
for (ScriptEngineFactory factory : manager.getEngineFactories()) {
|
||||
System.out.println(" engineName=" + factory.getEngineName() +
|
||||
" engineVersion=" + factory.getEngineVersion() +
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user