11package org .eqasim .core .scenario .config ;
22
3- import java .util .HashMap ;
4- import java .util .HashSet ;
5- import java .util .Map ;
6- import java .util .Set ;
3+ import java .util .*;
74import java .util .stream .Collectors ;
85
96import org .eqasim .core .simulation .EqasimConfigurator ;
2320 * <li>`{@value CMD_OUTPUT_PATH}`: required</li>
2421 * <li>optional arguments in the form '{@value CMD_ADD_MODULE_PREFIX}:moduleName' add the given module to the config. Module config groups matching the provided names are searched in the core modules and the optional modules specified by an {@link EqasimConfigurator} </li>
2522 * <li>optional arguments in the form `{@value CMD_REMOVE_MODULE_PREFIX}:moduleName` remove the given module from the config </li>
26- * <li>`{@value CMD_EQASIM_CONFIGURATOR}` the full name of a class extending the {@link EqasimConfigurator} class from which optional modules config groups are retrieved </li>
2723 * </ul>
2824 * After adding and removing modules, configuration elements can still be adapted using the `config:` prefix.
2925 * @author Tarek Chouaki (tkchouaki)
@@ -34,19 +30,41 @@ public class EditConfig {
3430 public static final String CMD_OUTPUT_PATH = "output-path" ;
3531 public static final String CMD_ADD_MODULE_PREFIX = "add-module" ;
3632 public static final String CMD_REMOVE_MODULE_PREFIX = "remove-module" ;
33+ public static final String CMD_ADD_PARAMETERSET_PREFIX = "add-parameterset" ;
34+ public static final String CMD_REMOVE_PARAMETERSET_PREFIX = "remove-parameterset" ;
35+
36+ public static ConfigGroup getParameterSet (ConfigGroup parent , String parameterSetName ) {
37+ String [] components = parameterSetName .split ("\\ ." );
38+ Collection <? extends ConfigGroup > parameterSets = parent .getParameterSets (components [0 ]);
39+ if (parameterSets .isEmpty ()) {
40+ return null ;
41+ }
42+ if (parameterSets .size () > 1 ) {
43+ throw new IllegalArgumentException (String .format ("Multiple parameter sets '%s' found" , components [0 ]));
44+ }
45+ ConfigGroup parameterSet = parameterSets .iterator ().next ();
46+ if (components .length == 1 ) {
47+ return parameterSet ;
48+ }
49+ return getParameterSet (parameterSet , String .join ("." , Arrays .copyOfRange (components , 1 , components .length )));
50+ }
3751
3852 public static void main (String [] args ) throws CommandLine .ConfigurationException {
3953 CommandLine cmd = new CommandLine .Builder (args )
4054 .requireOptions (CMD_INPUT_PATH , CMD_OUTPUT_PATH )
4155 .allowPrefixes (CMD_ADD_MODULE_PREFIX )
4256 .allowPrefixes (CMD_REMOVE_MODULE_PREFIX )
57+ .allowPrefixes (CMD_ADD_PARAMETERSET_PREFIX )
58+ .allowPrefixes (CMD_REMOVE_PARAMETERSET_PREFIX )
4359 .allowOptions (EqasimConfigurator .CONFIGURATOR )
4460 .build ();
4561
4662 Config config = ConfigUtils .loadConfig (cmd .getOptionStrict (CMD_INPUT_PATH ));
4763
4864 Set <String > addedModules = new HashSet <>();
4965 Set <String > removedModules = new HashSet <>();
66+ Set <String > addedParameterSets = new HashSet <>();
67+ Set <String > removedParameterSets = new HashSet <>();
5068
5169 for (String option : cmd .getAvailableOptions ()) {
5270 if (option .startsWith (CMD_ADD_MODULE_PREFIX )) {
@@ -55,6 +73,10 @@ public static void main(String[] args) throws CommandLine.ConfigurationException
5573 } else if (option .startsWith (CMD_REMOVE_MODULE_PREFIX )) {
5674 Verify .verify (cmd .getOptionStrict (option ).equals ("true" ), String .format ("Options prefix with --%s must be used without values" , CMD_REMOVE_MODULE_PREFIX ));
5775 removedModules .add (option .substring (CMD_REMOVE_MODULE_PREFIX .length () + 1 ));
76+ } else if (option .startsWith (CMD_ADD_PARAMETERSET_PREFIX )) {
77+ addedParameterSets .add (option .substring (CMD_ADD_PARAMETERSET_PREFIX .length () + 1 ));
78+ } else if (option .startsWith (CMD_REMOVE_PARAMETERSET_PREFIX )) {
79+ removedParameterSets .add (option .substring (CMD_REMOVE_PARAMETERSET_PREFIX .length () + 1 ));
5880 }
5981 }
6082
@@ -77,6 +99,45 @@ public static void main(String[] args) throws CommandLine.ConfigurationException
7799 config .addModule (configGroup );
78100 }
79101
102+ for (String parameterSet : removedParameterSets .stream ().sorted (Comparator .comparingInt (s -> -s .length ())).toList ()) {
103+ int firstDotIndex = parameterSet .indexOf ('.' );
104+ int lastDotIndex = parameterSet .lastIndexOf ('.' );
105+ Verify .verify (firstDotIndex >= 0 , "parameterSet to remove must be of the form module.(parentParameterSet.)*targetParameterSet" );
106+ ConfigGroup module = config .getModules ().get (parameterSet .substring (0 , firstDotIndex ));
107+ if (module == null ) {
108+ continue ;
109+ }
110+ ConfigGroup parentParameterSet ;
111+ if (firstDotIndex == lastDotIndex ) {
112+ parentParameterSet = module ;
113+ } else {
114+ parentParameterSet = getParameterSet (module , parameterSet .substring (firstDotIndex +1 , lastDotIndex ));
115+ }
116+ if (parentParameterSet != null ) {
117+ ConfigGroup targetParameterSet = getParameterSet (parentParameterSet , parameterSet .substring (lastDotIndex +1 ));
118+ if (targetParameterSet != null ) {
119+ parentParameterSet .removeParameterSet (targetParameterSet );
120+ }
121+ }
122+ }
123+
124+ for (String parameterSet : addedParameterSets .stream ().sorted (Comparator .comparingInt (String ::length )).toList ()) {
125+ String [] components = parameterSet .split ("\\ ." );
126+ Verify .verify (components .length > 1 , "parameterSet to remove must be of the form module.(parentParameterSet.)*targetParameterSet" );
127+ ConfigGroup module = config .getModules ().get (components [0 ]);
128+ Verify .verify (module != null , String .format ("Cannot find module %s to add a parameter set into, please add it first" , components [0 ]));
129+ ConfigGroup currentParent = module ;
130+ for (int i =1 ; i <components .length -1 ; i ++) {
131+ ConfigGroup currentParameterSet = getParameterSet (currentParent , components [i ]);
132+ if (currentParameterSet == null ) {
133+ currentParameterSet = currentParent .createParameterSet (components [i ]);
134+ currentParent .addParameterSet (currentParameterSet );
135+ }
136+ currentParent = currentParameterSet ;
137+ }
138+ currentParent .addParameterSet (currentParent .createParameterSet (components [components .length -1 ]));
139+ }
140+
80141 cmd .applyConfiguration (config );
81142
82143 ConfigUtils .writeConfig (config , cmd .getOptionStrict (CMD_OUTPUT_PATH ));
0 commit comments