Skip to content

Commit

Permalink
Allows preferences to be overriden if passed as VM arguments to GAMA
Browse files Browse the repository at this point in the history
e.g. -Dpref_use_pooling=true in the VM arguments of the run
configuration or gama.ini file will override whatever value is defined
in the preferences.
**WARNING**: It has the side effect of changing the value of the
preference for all instances if -Duse_global_preference_store=false is
not set at the same time (or if the other instances do not defined their
own override).
  • Loading branch information
AlexisDrogoul committed Aug 14, 2021
1 parent ca55bd3 commit 3aebaeb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
import msi.gaml.types.IType;
import ummisco.gama.dev.utils.FLAGS;

/**
* A store that acts as a gateway with either the JRE preference store (global) or configuration-specific preference
* stores (Eclipse). In addition, allows preferences to be overriden if they are passed as VM arguments to GAMA (e.g.
* "-Dpref_use_pooling=true"), enabling different instances to set different values even if the store used is global
*
* @author drogoul
*
* @param <T>
*/
@SuppressWarnings ({ "restriction", "unchecked", "rawtypes" })
abstract class GamaPreferenceStore<T> {

Expand All @@ -47,7 +56,7 @@ abstract class GamaPreferenceStore<T> {

public static GamaPreferenceStore getStore() {
if (STORE == null) {
STORE = FLAGS.USE_JRE_PREFERENCE_STORE ? new JRE(userRoot().node(NODE_NAME))
STORE = FLAGS.USE_GLOBAL_PREFERENCE_STORE ? new JRE(userRoot().node(NODE_NAME))
: new Configuration(ConfigurationScope.INSTANCE.getNode(NODE_NAME));
}
return STORE;
Expand Down Expand Up @@ -93,22 +102,22 @@ public void putBoolean(final String key, final Boolean value) {
}

@Override
public String get(final String key, final String def) {
public String getStringPreference(final String key, final String def) {
return store.get(key, def);
}

@Override
public Integer getInt(final String key, final Integer def) {
public Integer getIntPreference(final String key, final Integer def) {
return store.getInt(key, def);
}

@Override
public Double getDouble(final String key, final Double def) {
public Double getDoublePreference(final String key, final Double def) {
return store.getDouble(key, def);
}

@Override
public Boolean getBoolean(final String key, final Boolean def) {
public Boolean getBooleanPreference(final String key, final Boolean def) {
return store.getBoolean(key, def);
}

Expand Down Expand Up @@ -191,22 +200,22 @@ public void putBoolean(final String key, final Boolean value) {
}

@Override
public String get(final String key, final String def) {
public String getStringPreference(final String key, final String def) {
return store.get(key, def);
}

@Override
public Integer getInt(final String key, final Integer def) {
public Integer getIntPreference(final String key, final Integer def) {
return store.getInt(key, def);
}

@Override
public Double getDouble(final String key, final Double def) {
public Double getDoublePreference(final String key, final Double def) {
return store.getDouble(key, def);
}

@Override
public Boolean getBoolean(final String key, final Boolean def) {
public Boolean getBooleanPreference(final String key, final Boolean def) {
return store.getBoolean(key, def);
}

Expand Down Expand Up @@ -271,13 +280,65 @@ public List<String> getKeys() {

public abstract void putBoolean(final String key, final Boolean value);

public abstract String get(final String key, final String def);
/**
* First searches if the preference is overriden in the system/VM properties/arguments, then looks into the store if
* not
*
* @param key
* @param def
* @return
*/
public final String get(final String key, final String def) {
String result = System.getProperty(key);
return result == null ? getStringPreference(key, def) : result;
}

protected abstract String getStringPreference(String key, String def);

/**
* First searches if the preference is overriden in the system/VM properties/arguments, then looks into the store if
* not
*
* @param key
* @param def
* @return
*/
public final Integer getInt(final String key, final Integer def) {
String result = System.getProperty(key);
return result == null ? getIntPreference(key, def) : Integer.valueOf(result);
}

public abstract Integer getInt(final String key, final Integer def);
protected abstract Integer getIntPreference(String key, Integer def);

public abstract Double getDouble(final String key, final Double def);
/**
* First searches if the preference is overriden in the system/VM properties/arguments, then looks into the store if
* not
*
* @param key
* @param def
* @return
*/
public final Double getDouble(final String key, final Double def) {
String result = System.getProperty(key);
return result == null ? getDoublePreference(key, def) : Double.valueOf(result);
}

protected abstract Double getDoublePreference(String key, Double def);

/**
* First searches if the preference is overriden in the system/VM properties/arguments, then looks into the store if
* not
*
* @param key
* @param def
* @return
*/
public final Boolean getBoolean(final String key, final Boolean def) {
String result = System.getProperty(key);
return result == null ? getBooleanPreference(key, def) : Boolean.valueOf(result);
}

public abstract Boolean getBoolean(final String key, final Boolean def);
protected abstract Boolean getBooleanPreference(String key, Boolean def);

/**
* Makes sure preferences are kept in sync between GAMA runtime and the backend file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public static class Runtime {
create("pref_experiment_ask_closing", "Ask to close the previous experiment when launching a new one",
true, IType.BOOL, true).in(NAME, EXECUTION);
public static final Pref<Boolean> CORE_ASK_FULLSCREEN =
create("pref_experiment_ask_fullscreen", "Ask to go to fullscreen mode", false, IType.BOOL, true)
create("pref_experiment_ask_fullscreen", "Ask before entering fullscreen mode", false, IType.BOOL, true)
.in(NAME, EXECUTION);
// public static final Pref<Double> CORE_DELAY_STEP = create("pref_experiment_default_step",
// "Default step for the delay slider (in sec.)", 0.001, IType.FLOAT, true).in(NAME, EXECUTION).disabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private static boolean get(final String name, final boolean def) {
* Used in GamaPreferences, true to save the preferences in the global (managed by the JRE) preference store or
* false to save them in each GAMA instance preference store (like Eclipse, see #3115). True by default.
*/
public static final boolean USE_JRE_PREFERENCE_STORE = get("use_jre_preference_store", true);
public static final boolean USE_GLOBAL_PREFERENCE_STORE = get("use_global_preference_store", true);

/**
* Used in DEBUG, set to true to enable logging activities (which will follow the declaration ofD EBUG.ON() on the
Expand Down

0 comments on commit 3aebaeb

Please sign in to comment.