MATSIM
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Private Attributes | List of all members
tutorial.programming.reflectiveConfigGroup.MyConfigGroup Class Reference
Inheritance diagram for tutorial.programming.reflectiveConfigGroup.MyConfigGroup:
Inheritance graph
[legend]

Public Member Functions

 MyConfigGroup ()
 
final void addParam (final String param_name, final String value)
 
final String getValue (final String param_name)
 
final Map< String, String > getParams ()
 
Map< String, String > getComments ()
 
final String getName ()
 
final String toString ()
 
ConfigGroup createParameterSet (final String type)
 
void addParameterSet (final ConfigGroup set)
 
boolean removeParameterSet (final ConfigGroup set)
 
final Collection<?extends
ConfigGroup
getParameterSets (final String type)
 
final Map< String,?extends
Collection<?extends
ConfigGroup > > 
getParameterSets ()
 
final boolean isLocked ()
 
void setLocked ()
 
final void testForLocked ()
 

Static Public Member Functions

static URL getInputFileURL (URL context, String filename)
 

Static Public Attributes

static final String GROUP_NAME = "testModule"
 

Protected Member Functions

void addParameterToMap (final Map< String, String > map, final String paramName)
 
void checkConsistency ()
 
void checkParameterSet (final ConfigGroup set)
 
final Collection<?extends
ConfigGroup
clearParameterSetsForType (final String type)
 

Private Attributes

double doubleField = Double.NaN
 
Id< LinkidField = null
 
Coord coordField = null
 
MyEnum enumField = null
 
String nonNull = "some arbitrary default value."
 

Detailed Description

Demonstrate how to use ReflectiveModule to easily create typed config groups. Please do not modify this class: it is used from unit tests!

Definition at line 32 of file MyConfigGroup.java.

Constructor & Destructor Documentation

tutorial.programming.reflectiveConfigGroup.MyConfigGroup.MyConfigGroup ( )

Definition at line 49 of file MyConfigGroup.java.

49  {
50  super( GROUP_NAME );
51  }

Member Function Documentation

final void org.matsim.core.config.ReflectiveConfigGroup.addParam ( final String  param_name,
final String  value 
)
inherited

Definition at line 215 of file ReflectiveConfigGroup.java.

References org.matsim.core.config.ConfigGroup.getName(), and org.matsim.core.config.ReflectiveConfigGroup.invokeSetter().

Referenced by org.matsim.core.config.groups.PlanCalcScoreConfigGroup.addParam(), and org.matsim.core.config.groups.StrategyConfigGroup.addParam().

217  {
218  final Method setter = setters.get( param_name );
219 
220  if (setter == null) {
221  if ( !storeUnknownParameters ) {
222  throw new IllegalArgumentException(
223  "Module "+getName()+" of type "+getClass().getName()+
224  " doesn't accept unkown parameters. Parameter "+param_name+
225  " is not part of the valid parameters: "+setters.keySet() );
226  }
227  log.warn( "unknown parameter "+param_name+" for group "+getName()+". Here are the valid parameter names: "+setters.keySet() );
228  log.warn( "Only the string value will be remembered" );
229  super.addParam( param_name , value );
230  return;
231  }
232 
233  try {
234  invokeSetter( setter , value );
235  log.trace( "value "+value+" successfully set for field "+param_name+" for group "+getName() );
236  }
237  catch (InvocationTargetException e) {
238  // this exception wraps Throwables intercepted in the invocation of the setter.
239  // Avoid multiple wrappings (exception wrapped in InvocationTargetException
240  // itself wrapped in a RuntimeException), as it makes error messages
241  // messy.
242  final Throwable cause = e.getCause();
243  if ( cause instanceof RuntimeException ) {
244  throw (RuntimeException) cause;
245  }
246 
247  if ( cause instanceof Error ) {
248  throw (Error) cause;
249  }
250 
251  throw new RuntimeException( cause );
252  }
253  catch (IllegalAccessException e) {
254  throw new RuntimeException( e );
255  }
256  }
void invokeSetter(final Method setter, final String value)

Here is the call graph for this function:

final String org.matsim.core.config.ReflectiveConfigGroup.getValue ( final String  param_name)
inherited

Definition at line 339 of file ReflectiveConfigGroup.java.

References org.matsim.core.config.ConfigGroup.getName().

Referenced by org.matsim.core.scoring.functions.CharyparNagelScoringParameters.Builder.Builder(), and org.matsim.core.config.groups.StrategyConfigGroup.getValue().

339  {
340  final Method getter = stringGetters.get( param_name );
341 
342  try {
343  if (getter != null) {
344  // do not care about access modifier:
345  // if a method is tagged with the StringGetter
346  // annotation, we are supposed to access it.
347  // This *is* safe.
348  final boolean accessible = getter.isAccessible();
349  getter.setAccessible( true );
350  final Object result = getter.invoke( this );
351  getter.setAccessible( accessible );
352 
353  if ( result == null ) {
354  if ( getter.isAnnotationPresent( DoNotConvertNull.class ) ) {
355  log.error( "getter for parameter "+param_name+" of module "+getName()+" returned null." );
356  log.error( "This is not allowed for this getter." );
357 
358  throw new NullPointerException( "getter for parameter "+param_name+" of module "+getClass().getName()+" ("+getName()+") returned null." );
359  }
360  return null;
361  }
362 
363  final String value = ""+result;
364 
365  if ( value.equals( "null" ) && !getter.isAnnotationPresent( DoNotConvertNull.class ) ) {
366  throw new RuntimeException( "parameter "+param_name+" understands null pointers for IO. As a consequence, the \"null\" String is not a valid value for "+getter.getName() );
367  }
368 
369  return value;
370  }
371  }
372  catch (InvocationTargetException e) {
373  // this exception wraps Throwables intercepted in the invocation of the getter.
374  // Avoid multiple wrappings (exception wrapped in InvocationTargetException
375  // itself wrapped in a RuntimeException), as it makes error messages
376  // messy.
377  final Throwable cause = e.getCause();
378  if ( cause instanceof RuntimeException ) {
379  throw (RuntimeException) cause;
380  }
381 
382  if ( cause instanceof Error ) {
383  throw (Error) cause;
384  }
385 
386  throw new RuntimeException( cause );
387  }
388  catch (IllegalAccessException e) {
389  throw new RuntimeException( e );
390  }
391 
392  if ( !storeUnknownParameters ) {
393  throw new IllegalArgumentException(
394  "Module "+getName()+" of type "+getClass().getName()+
395  " doesn't store unkown parameters. Parameter "+param_name+
396  " is not part of the valid parameters: "+stringGetters.keySet() );
397  }
398 
399  log.warn( "no getter found for param "+param_name+": trying parent method" );
400  return super.getValue( param_name );
401  }

Here is the call graph for this function:

final Map<String, String> org.matsim.core.config.ReflectiveConfigGroup.getParams ( )
inherited

Definition at line 404 of file ReflectiveConfigGroup.java.

References org.matsim.core.config.ConfigGroup.addParameterToMap().

Referenced by org.matsim.core.config.groups.PlanCalcScoreConfigGroup.getParams(), and org.matsim.core.config.groups.StrategyConfigGroup.getParams().

404  {
405  final Map<String, String> map = super.getParams();
406 
407  for (String f : setters.keySet()) {
408  addParameterToMap( map , f );
409  }
410 
411  return map;
412  }
void addParameterToMap(final Map< String, String > map, final String paramName)

Here is the call graph for this function:

Map<String, String> org.matsim.core.config.ReflectiveConfigGroup.getComments ( )
inherited

Comments for parameters which setter get an enum type are automatically generated, containing a list of possible values. They can be overriden by subclasses without problems.


it is recommended for subclasses to get this map using super.getComments() and fill it with additional comments, rather than generate an empty map.

Definition at line 424 of file ReflectiveConfigGroup.java.

References org.matsim.core.config.ConfigGroup.params, and org.matsim.core.config.ConfigGroup.toString().

424  {
425  // generate some default comments.
426  final Map<String, String> comments = super.getComments();
427 
428  for ( Map.Entry<String, Method> entry : setters.entrySet() ) {
429  final String paramName = entry.getKey();
430  if ( comments.containsKey( paramName ) ) {
431  // at the time of implementation, this is not possible,
432  // but who knows? Do not override something already there.
433  continue;
434  }
435 
436  final Method setter = entry.getValue();
437 
438  final Class<?>[] params = setter.getParameterTypes();
439  assert params.length == 1; // already checked at constr.
440 
441  final Class<?> type = params[ 0 ];
442 
443  if ( type.isEnum() ) {
444  // generate an automatic comment containing the possible values for enum types
445  final StringBuilder comment = new StringBuilder( "Possible values: " );
446  final Object[] consts = type.getEnumConstants();
447 
448  for ( int i = 0; i < consts.length; i++ ) {
449  comment.append( consts[ i ].toString() );
450  if ( i < consts.length - 1 ) comment.append( ", " );
451  }
452 
453  comments.put( paramName , comment.toString() );
454  }
455  }
456 
457  return comments;
458  }
final TreeMap< String, String > params

Here is the call graph for this function:

void org.matsim.core.config.ConfigGroup.addParameterToMap ( final Map< String, String >  map,
final String  paramName 
)
protectedinherited

Little helper for subclasses (i.e. the ConfigGroups). This method adds the value of the parameter to the given map only if the getValue() method of this Module doesn't return null (Java null-type) or the String representation of null, i.e. "null" or "NULL". If the value is null, the string "null" is added to the map to document the parameter.

Parameters
map
paramName

Definition at line 77 of file ConfigGroup.java.

References org.matsim.core.config.ConfigGroup.getValue().

Referenced by org.matsim.core.config.ReflectiveConfigGroup.getParams().

77  {
78  String value = this.getValue(paramName);
79  if (!((value == null) || value.equalsIgnoreCase("null"))) {
80  map.put(paramName, value);
81  } else {
82  map.put(paramName, "null");
83  }
84  }
String getValue(final String paramName)

Here is the call graph for this function:

void org.matsim.core.config.ConfigGroup.checkConsistency ( )
protectedinherited

Check if the set values go well together. This method is usually called after reading the configuration from a file. If an inconsistency is found, a warning or error should be issued and (optionally) a RuntimeException being thrown.

Definition at line 90 of file ConfigGroup.java.

References org.matsim.core.config.ConfigGroup.getParameterSets().

90  {
91  // default: just call this method on parameter sets
92  for ( Collection<? extends ConfigGroup> sets : getParameterSets().values() ) {
93  for ( ConfigGroup set : sets ) set.checkConsistency();
94  }
95  }
final Map< String,?extends Collection<?extends ConfigGroup > > getParameterSets()

Here is the call graph for this function:

final String org.matsim.core.config.ConfigGroup.getName ( )
inherited
final String org.matsim.core.config.ConfigGroup.toString ( )
inherited

Definition at line 118 of file ConfigGroup.java.

References org.matsim.core.config.ConfigGroup.getName(), and org.matsim.core.config.ConfigGroup.getParams().

Referenced by org.matsim.core.config.ConfigGroup.addParam(), org.matsim.core.config.ReflectiveConfigGroup.getComments(), and org.matsim.core.config.ReflectiveConfigGroup.invokeSetter().

118  {
119  String str = "" ;
120  for ( Entry<String, String> entry : this.getParams().entrySet() ) {
121  str += "[" + entry.getKey() + "=" + entry.getValue() + "]" ;
122  }
123  return "[name=" + this.getName() + "]" +
124  "[nOfParams=" + this.getParams().size() + "]" + str ;
125  }
Map< String, String > getParams()

Here is the call graph for this function:

ConfigGroup org.matsim.core.config.ConfigGroup.createParameterSet ( final String  type)
inherited

Override if parameter sets of a certain type need a special implementation

Definition at line 136 of file ConfigGroup.java.

References org.matsim.core.config.ConfigGroup.ConfigGroup().

136  {
137  return new ConfigGroup( type );
138  }

Here is the call graph for this function:

void org.matsim.core.config.ConfigGroup.addParameterSet ( final ConfigGroup  set)
inherited

Definition at line 152 of file ConfigGroup.java.

References org.matsim.core.config.ConfigGroup.checkParameterSet(), and org.matsim.core.config.ConfigGroup.getName().

Referenced by org.matsim.core.config.groups.PlansCalcRouteConfigGroup.addParameterSet(), and org.matsim.core.config.groups.StrategyConfigGroup.addStrategySettings().

152  {
153  checkParameterSet( set );
154  Collection<ConfigGroup> parameterSets = parameterSetsPerType.get( set.getName() );
155 
156  if ( parameterSets == null ) {
157  parameterSets = new ArrayList<>();
158  parameterSetsPerType.put( set.getName() , parameterSets );
159  }
160 
161  parameterSets.add( set );
162  }
final Map< String, Collection< ConfigGroup > > parameterSetsPerType
void checkParameterSet(final ConfigGroup set)

Here is the call graph for this function:

boolean org.matsim.core.config.ConfigGroup.removeParameterSet ( final ConfigGroup  set)
inherited

Definition at line 164 of file ConfigGroup.java.

References org.matsim.core.config.ConfigGroup.getName().

Referenced by org.matsim.core.config.groups.PlanCalcScoreConfigGroup.addParam(), and org.matsim.core.config.groups.PlanCalcScoreConfigGroup.addScoringParameters().

164  {
165  final Collection<ConfigGroup> parameterSets = parameterSetsPerType.get( set.getName() );
166  return parameterSets != null ?
167  parameterSets.remove( set ) :
168  false;
169  }
final Map< String, Collection< ConfigGroup > > parameterSetsPerType

Here is the call graph for this function:

void org.matsim.core.config.ConfigGroup.checkParameterSet ( final ConfigGroup  set)
protectedinherited

Method called on parameter sets added by the add methods. Can be extended if there are consistency checks to makes, for instance if parameter sets of a given type should be instances of a particular class.

Parameters
set

Definition at line 178 of file ConfigGroup.java.

Referenced by org.matsim.core.config.ConfigGroup.addParameterSet().

178  {
179  // empty for inheritance
180  }
final Collection<? extends ConfigGroup> org.matsim.core.config.ConfigGroup.clearParameterSetsForType ( final String  type)
protectedinherited

Useful for instance if default values are provided but should be cleared if user provides values.

Definition at line 186 of file ConfigGroup.java.

Referenced by org.matsim.core.config.groups.PlansCalcRouteConfigGroup.addParameterSet(), and org.matsim.core.config.groups.StrategyConfigGroup.clearStrategySettings().

186  {
187  return parameterSetsPerType.remove( type );
188  }
final Map< String, Collection< ConfigGroup > > parameterSetsPerType
final Collection<? extends ConfigGroup> org.matsim.core.config.ConfigGroup.getParameterSets ( final String  type)
inherited

Definition at line 190 of file ConfigGroup.java.

190  {
191  final Collection<ConfigGroup> sets = parameterSetsPerType.get( type );
192  return sets == null ?
193  Collections.<ConfigGroup>emptySet() :
194  Collections.unmodifiableCollection( sets );
195  }
final Map< String, Collection< ConfigGroup > > parameterSetsPerType
final Map<String, ? extends Collection<? extends ConfigGroup> > org.matsim.core.config.ConfigGroup.getParameterSets ( )
inherited

Definition at line 197 of file ConfigGroup.java.

Referenced by org.matsim.core.config.ConfigGroup.checkConsistency(), org.matsim.core.config.groups.PlansCalcRouteConfigGroup.getModeRoutingParams(), org.matsim.core.config.groups.PlanCalcScoreConfigGroup.getScoringParametersPerSubpopulation(), and org.matsim.core.config.groups.StrategyConfigGroup.getStrategySettings().

197  {
198  // TODO: immutabilize (including lists)
199  // maybe done with what I did below? kai, sep'16
200 
201  // return parameterSetsPerType;
202 
203  Map<String, Collection<ConfigGroup>> parameterSetsPerType2 = new TreeMap<>() ;
204  for ( Entry<String, Collection<ConfigGroup>> entry : parameterSetsPerType.entrySet() ) {
205  parameterSetsPerType2.put( entry.getKey(), Collections.unmodifiableCollection(entry.getValue()) ) ;
206  }
207  return Collections.unmodifiableMap( parameterSetsPerType2 ) ;
208  }
final Map< String, Collection< ConfigGroup > > parameterSetsPerType
final boolean org.matsim.core.config.ConfigGroup.isLocked ( )
inherited
void org.matsim.core.config.ConfigGroup.setLocked ( )
inherited

Definition at line 214 of file ConfigGroup.java.

Referenced by org.matsim.core.config.groups.PlansCalcRouteConfigGroup.getOrCreateModeRoutingParams(), org.matsim.core.config.groups.PlanCalcScoreConfigGroup.setLocked(), and org.matsim.pt.router.TransitRouterConfig.TransitRouterConfig().

214  {
215  // need to have this non-final to be able to override in order to set delegates. kai, jun'15
216  this.locked = true ;
217  for ( Collection<ConfigGroup> parameterSets : this.parameterSetsPerType.values() ) {
218  for ( ConfigGroup parameterSet : parameterSets ) {
219  parameterSet.setLocked();
220  }
221  }
222  }
final Map< String, Collection< ConfigGroup > > parameterSetsPerType
final void org.matsim.core.config.ConfigGroup.testForLocked ( )
inherited
static URL org.matsim.core.config.ConfigGroup.getInputFileURL ( URL  context,
String  filename 
)
staticinherited

Definition at line 230 of file ConfigGroup.java.

References org.matsim.core.utils.io.IOUtils.newUrl().

230  {
231  if (filename.startsWith("~" + File.separator)) {
232  filename = System.getProperty("user.home") + filename.substring(1);
233  }
234  return IOUtils.newUrl(context, filename);
235  }

Here is the call graph for this function:

Member Data Documentation

final String tutorial.programming.reflectiveConfigGroup.MyConfigGroup.GROUP_NAME = "testModule"
static
double tutorial.programming.reflectiveConfigGroup.MyConfigGroup.doubleField = Double.NaN
private

Definition at line 37 of file MyConfigGroup.java.

Id<Link> tutorial.programming.reflectiveConfigGroup.MyConfigGroup.idField = null
private

Definition at line 41 of file MyConfigGroup.java.

Coord tutorial.programming.reflectiveConfigGroup.MyConfigGroup.coordField = null
private

Definition at line 43 of file MyConfigGroup.java.

MyEnum tutorial.programming.reflectiveConfigGroup.MyConfigGroup.enumField = null
private

Definition at line 45 of file MyConfigGroup.java.

String tutorial.programming.reflectiveConfigGroup.MyConfigGroup.nonNull = "some arbitrary default value."
private

Definition at line 47 of file MyConfigGroup.java.


The documentation for this class was generated from the following file: