-
Notifications
You must be signed in to change notification settings - Fork 344
/
CommandOption.java
782 lines (694 loc) · 26.6 KB
/
CommandOption.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/* Copyright (C) 2002 Univ. of Massachusetts Amherst, Computer Science Dept.
This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).
http://www.cs.umass.edu/~mccallum/mallet
This software is provided under the terms of the Common Public License,
version 1.0, as published by http://www.opensource.org. For further
information, see the file `LICENSE' included with this distribution. */
/**
@author Andrew McCallum <a href="mailto:[email protected]">[email protected]</a>
*/
package cc.mallet.util;
import java.io.FileInputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;
import com.google.errorprone.annotations.Var;
public abstract class CommandOption
{
static BshInterpreter interpreter;
/** Maps a Java class to the array of CommandOption objects that are owned by it. */
static HashMap class2options = new HashMap ();
Class owner;
/** The name of the argument, eg "input" */
java.lang.String name;
/** The display name of the argument type, eg "[TRUE|FALSE]" or "FILE" */
java.lang.String argName;
/** The type of the argument, if present */
Class argType;
boolean argRequired;
java.lang.String shortdoc;
java.lang.String longdoc;
java.lang.String fullName;
/** did this command option get processed, or do we just have default value */
boolean invoked = false;
public CommandOption (Class owner, java.lang.String name, java.lang.String argName,
Class argType, boolean argRequired,
java.lang.String shortdoc, java.lang.String longdoc)
{
this.owner = owner;
this.name = name;
this.argName = argName;
this.argType = argType;
this.argRequired = argRequired;
this.shortdoc = shortdoc;
this.longdoc = longdoc;
Package p = owner.getPackage();
this.fullName = (p != null ? p.toString() : "") + name;
if (interpreter == null)
interpreter = new BshInterpreter ();
if (owner != CommandOption.class) {
@Var
CommandOption.List options = (CommandOption.List) class2options.get (owner);
if (options == null) {
options = new CommandOption.List ("");
class2options.put (owner, options);
}
options.add (this);
}
}
/** @deprecated */
@Deprecated
public CommandOption (Class owner, java.lang.String name, java.lang.String argName,
Class argType, boolean argRequired,
java.lang.String shortdoc)
{
this (owner, name, argName, argType, argRequired, shortdoc, null);
}
/** Give this CommandOption the opportunity to process the index'th argument in args.
Return the next unprocessed index. */
public int process (java.lang.String[] args, @Var int index) {
//System.out.println (name + " processing arg " + args[index]);
if (args.length == 0)
return index;
// System.out.println(index + ": " + args[index]);
// Is there anything to process?
if (index >= args.length ||
args[index] == null || args[index].length() < 2 ||
args[index].charAt(0) != '-' || args[index].charAt(1) != '-')
return index;
// Determine what the command name is
java.lang.String optFullName = args[index].substring(2);
@Var
int dotIndex = optFullName.lastIndexOf('.');
@Var
java.lang.String optName = optFullName;
// Commands may have a package prefix
if (dotIndex != -1) {
java.lang.String optPackageName = optFullName.substring (0, dotIndex);
if (owner.getPackage() != null &&
! owner.getPackage().toString().endsWith(optPackageName))
return index;
optName = optFullName.substring (dotIndex+1);
}
// Does the option name match the name of this CommandOption?
if (! name.equals(optName))
return index;
// We have now determined that this CommandOption is the correct one
this.invoked = true;
index++;
if (args.length > index && (args[index].length() < 2
|| (args[index].charAt(0) != '-' && args[index].charAt(1) != '-'))) {
index = parseArg (args, index);
}
else {
if (argRequired) {
throw new IllegalArgumentException ("Missing argument for option " + optName);
}
else {
// xxx This is not parallel behavior to the above parseArg(String[],int) method.
parseArg (args, -index); // xxx was ""
}
//index++;
}
return index;
}
public static BshInterpreter getInterpreter ()
{
return interpreter;
}
public static java.lang.String[] process (java.lang.Class owner, java.lang.String[] args)
{
CommandOption.List options = (CommandOption.List) class2options.get (owner);
if (options == null)
throw new IllegalArgumentException ("No CommandOptions registered for class "+owner);
return options.process (args);
}
public static List getList (java.lang.Class owner)
{
CommandOption.List options = (CommandOption.List) class2options.get (owner);
if (options == null)
throw new IllegalArgumentException ("No CommandOptions registered for class "+owner);
return options;
}
public static void setSummary (java.lang.Class owner, java.lang.String summary)
{
CommandOption.List options = (CommandOption.List) class2options.get (owner);
if (options == null)
throw new IllegalArgumentException ("No CommandOption.List registered for class "+owner);
options.setSummary (summary);
}
public java.lang.String getFullName () {
return fullName;
}
public java.lang.String getName () {
return name;
}
public abstract java.lang.String defaultValueToString ();
public abstract java.lang.String valueToString ();
/** Return true is this CommandOption was matched by one of the processed arguments. */
public boolean wasInvoked () {
return invoked;
}
/** Called after this CommandOption matches an argument.
The default implementation simply calls parseArg(String), and
returns index+1; unless index is negative, in which case it calls
parseArg((String)null) and returns index. */
public int parseArg (java.lang.String args[], int index) {
if (index < 0) {
parseArg ((java.lang.String)null);
return index;
}
else {
parseArg (args[index]);
return index+1;
}
}
public void parseArg (java.lang.String arg) {}
/** To be overridden by subclasses;
"list" is the the CommandOption.List that called this option */
public void postParsing (CommandOption.List list) {}
/** For objects that can provide CommandOption.List's (which can be merged into other lists. */
public static interface ListProviding {
public CommandOption.List getCommandOptionList ();
}
public static void printOptionValues(Class owner) {
CommandOption.List options = (CommandOption.List) class2options.get (owner);
for (int i=0; i < options.size(); i++) {
CommandOption option = options.getCommandOption(i);
System.out.println(option.getName() + "\t=\t" + option.valueToString());
}
}
public static class List {
ArrayList options;
HashMap map;
java.lang.String summary;
private List (java.lang.String summary) {
this.options = new ArrayList ();
this.map = new HashMap ();
this.summary = summary;
add (new Boolean (CommandOption.class, "help", "TRUE|FALSE", false, false,
"Print this command line option usage information. "+
"Give argument of TRUE for longer documentation", null)
{ public void postParsing(CommandOption.List list) { printUsage(value); System.exit(-1); } });
add (new Object (CommandOption.class, "prefix-code", "'JAVA CODE'", true, null,
"Java code you want run before any other interpreted code. Note that the text "+
"is interpreted without modification, so unlike some other Java code options, "+
"you need to include any necessary 'new's when creating objects.", null));
add (new File (CommandOption.class, "config", "FILE", false, null,
"Read command option values from a file", null)
{ public void postParsing(CommandOption.List list) { readFromFile(value); } } );
}
public List (java.lang.String summary, CommandOption[] options) {
this(summary);
add(options);
}
public void setSummary (java.lang.String s)
{
this.summary = s;
}
public int size ()
{
return options.size();
}
public CommandOption getCommandOption (int index)
{
return (CommandOption) options.get(index);
}
public void add (CommandOption opt) {
options.add (opt);
map.put (opt.getFullName(), opt);
}
public void add (CommandOption[] opts) {
for (int i = 0; i < opts.length; i++)
add (opts[i]);
}
public void add (CommandOption.List opts) {
for (int i = 0; i < opts.size(); i++)
add (opts.getCommandOption(i));
}
public void add (Class owner) {
CommandOption.List options = (CommandOption.List) class2options.get (owner);
if (options == null)
throw new IllegalArgumentException ("No CommandOptions registered for class "+owner);
add (options);
}
/**
* Load configuration information from a file. If the filename ends
* with ".xml", the file is interpreted as a Java XML configuration file.
* Otherwise it is interpreted as a text config file (eg "key = value" on each line).
* Note that text files can only use Latin 1 (en-us) characters, while
* XML files can be UTF-8.
*/
public void readFromFile(java.io.File configurationFile) {
try {
Properties properties = new Properties();
if (configurationFile.getName().endsWith(".xml")) {
properties.loadFromXML(new FileInputStream(configurationFile));
}
else {
properties.load(new FileInputStream(configurationFile));
}
Enumeration keys = properties.propertyNames();
while (keys.hasMoreElements()) {
java.lang.String key = (java.lang.String) keys.nextElement();
java.lang.String[] values = properties.getProperty(key).split("\\s+");
@Var
boolean foundValue = false;
for (int i = 0; i < options.size(); i++) {
CommandOption option = (CommandOption) options.get(i);
if (option.name.equals(key)) {
foundValue = true;
option.parseArg(values, 0);
option.invoked = true;
break;
}
}
}
} catch (Exception e) {
System.err.println("Unable to process configuration file: " + e.getMessage());
}
}
/** Parse and process command-line options in args. Return sub-array of
args occurring after first non-recognized arg that doesn't begin with a dash. */
public java.lang.String[] process (java.lang.String[] args)
{
@Var
int index = 0;
while (index < args.length) {
@Var
int newIndex = index;
for (int i = 0; i < options.size(); i++) {
CommandOption o = (CommandOption)options.get(i);
newIndex = o.process (args, index);
if (newIndex != index) {
o.postParsing(this);
break;
}
}
if (newIndex == index) {
// All of the CommandOptions had their chance to claim the index'th option,,
// but none of them did.
printUsage(false);
throw new IllegalArgumentException ("Unrecognized option " + index + ": " +args[index]);
}
index = newIndex;
}
return new java.lang.String[0];
}
public int processOptions (java.lang.String[] args)
{
for (int index = 0; index < args.length;) {
@Var
int newIndex = index;
for (int i = 0; i < options.size(); i++) {
CommandOption o = (CommandOption)options.get(i);
newIndex = o.process (args, index);
if (newIndex != index) {
o.postParsing(this);
break;
}
}
if (newIndex == index) {
if (index < args.length && args[index].length() > 1 &&
args[index].charAt(0) == '-' && args[index].charAt(1) == '-') {
printUsage(false);
throw new IllegalArgumentException ("Unrecognized option "+args[index]);
}
return index;
}
index = newIndex;
}
return args.length;
}
public void printUsage (boolean printLongDoc)
{
// xxx Fix this to have nicer formatting later.
System.err.println (summary);
for (int i = 0; i < options.size(); i++) {
CommandOption o = (CommandOption) options.get(i);
System.err.println ("--"+ o.name + " " + o.argName + "\n " + o.shortdoc);
if (o.longdoc != null && printLongDoc)
System.err.println (" "+o.longdoc);
System.err.println (" Default is "+o.defaultValueToString());
}
}
public void logOptions (java.util.logging.Logger logger)
{
for (int i = 0; i < options.size(); i++) {
CommandOption o = (CommandOption) options.get(i);
logger.info (o.name+" = "+o.valueToString ());
}
}
}
public static class Boolean extends CommandOption
{
public boolean value, defaultValue;;
public Boolean (Class owner, java.lang.String name, java.lang.String argName,
boolean argRequired, boolean defaultValue,
java.lang.String shortdoc, java.lang.String longdoc)
{
super (owner, name, argName, Boolean.class, argRequired, shortdoc, longdoc);
this.defaultValue = value = defaultValue;
}
public boolean value () { return value; }
public void parseArg (java.lang.String arg) {
if (arg == null || arg.equalsIgnoreCase("true") || arg.equals("1"))
value = true;
else if (arg.equalsIgnoreCase("false") || arg.equals("0"))
value = false;
else
throw new IllegalArgumentException ("Boolean option should be true|false|0|1. Instead found "+arg);
}
public java.lang.String defaultValueToString() { return java.lang.Boolean.toString(defaultValue); }
public java.lang.String valueToString () { return java.lang.Boolean.toString (value); }
}
public static class Integer extends CommandOption
{
public int value, defaultValue;
public Integer (Class owner, java.lang.String name, java.lang.String argName,
boolean argRequired, int defaultValue,
java.lang.String shortdoc, java.lang.String longdoc)
{
super (owner, name, argName, Integer.class, argRequired, shortdoc, longdoc);
this.defaultValue = value = defaultValue;
}
public int value () { return value; }
public void parseArg (java.lang.String arg) { value = java.lang.Integer.parseInt(arg); }
public java.lang.String defaultValueToString() { return java.lang.Integer.toString(defaultValue); }
public java.lang.String valueToString () { return java.lang.Integer.toString (value); }
}
public static class IntegerArray extends CommandOption
{
public int[] value, defaultValue;
public IntegerArray (Class owner, java.lang.String name, java.lang.String argName,
boolean argRequired, int[] defaultValue,
java.lang.String shortdoc, java.lang.String longdoc)
{
super (owner, name, argName, IntegerArray.class, argRequired, shortdoc, longdoc);
this.defaultValue = value = defaultValue;
}
public int[] value () { return value; }
public void parseArg (java.lang.String arg) {
java.lang.String elts[] = arg.split(",");
value = new int[elts.length];
for (int i = 0; i < elts.length; i++)
value[i] = java.lang.Integer.parseInt(elts[i]);
}
public java.lang.String defaultValueToString() {
StringBuffer b = new StringBuffer();
@Var
java.lang.String sep = "";
for (int i = 0; i < defaultValue.length; i++) {
b.append(sep).append(java.lang.Integer.toString(defaultValue[i]));
sep = ",";
}
return b.toString();
}
public java.lang.String valueToString() {
StringBuffer b = new StringBuffer();
@Var
java.lang.String sep = "";
for (int i = 0; i < value.length; i++) {
b.append(sep).append(java.lang.Integer.toString(value[i]));
sep = ",";
}
return b.toString();
}
}
public static class Double extends CommandOption
{
public double value, defaultValue;
public Double (Class owner, java.lang.String name, java.lang.String argName,
boolean argRequired, double defaultValue,
java.lang.String shortdoc, java.lang.String longdoc)
{
super (owner, name, argName, Double.class, argRequired, shortdoc, longdoc);
this.defaultValue = value = defaultValue;
}
public double value () { return value; }
public void parseArg (java.lang.String arg) { value = java.lang.Double.parseDouble(arg); }
public java.lang.String defaultValueToString() { return java.lang.Double.toString(defaultValue); }
public java.lang.String valueToString () { return java.lang.Double.toString (value); }
}
public static class DoubleArray extends CommandOption
{
public double[] value, defaultValue;
public DoubleArray (Class owner, java.lang.String name, java.lang.String argName,
boolean argRequired, double[] defaultValue,
java.lang.String shortdoc, java.lang.String longdoc)
{
super (owner, name, argName, IntegerArray.class, argRequired, shortdoc, longdoc);
this.defaultValue = value = defaultValue;
}
public double[] value () { return value; }
public void parseArg (java.lang.String arg) {
java.lang.String elts[] = arg.split(",");
value = new double[elts.length];
for (int i = 0; i < elts.length; i++)
value[i] = java.lang.Double.parseDouble(elts[i]);
}
public java.lang.String defaultValueToString() {
StringBuffer b = new StringBuffer();
@Var
java.lang.String sep = "";
for (int i = 0; i < defaultValue.length; i++) {
b.append(sep).append(java.lang.Double.toString(defaultValue[i]));
sep = ",";
}
return b.toString();
}
public java.lang.String valueToString() {
StringBuffer b = new StringBuffer();
@Var
java.lang.String sep = "";
for (int i = 0; i < value.length; i++) {
b.append(sep).append(java.lang.Double.toString(value[i]));
sep = ",";
}
return b.toString();
}
}
public static class String extends CommandOption
{
public java.lang.String value, defaultValue;
public String (Class owner, java.lang.String name, java.lang.String argName,
boolean argRequired, java.lang.String defaultValue,
java.lang.String shortdoc, java.lang.String longdoc)
{
super (owner, name, argName, java.lang.String.class, argRequired, shortdoc, longdoc);
this.defaultValue = value = defaultValue;
}
public java.lang.String value () { return value; }
public void parseArg (java.lang.String arg) { value = arg; }
public java.lang.String defaultValueToString() { return defaultValue; }
public java.lang.String valueToString() { return value; }
}
public static class SpacedStrings extends CommandOption
{
public java.lang.String[] value, defaultValue;
public SpacedStrings (Class owner, java.lang.String name, java.lang.String argName,
boolean argRequired, java.lang.String[] defaultValue,
java.lang.String shortdoc, java.lang.String longdoc)
{
super (owner, name, argName, java.lang.String.class, argRequired, shortdoc, longdoc);
this.defaultValue = value = defaultValue;
}
public java.lang.String[] value () { return value; }
public int parseArg (java.lang.String args[], @Var int index)
{
@Var
int count = 0;
this.value = null;
while (index < args.length
&& (args[index].length() < 2
|| (args[index].charAt(0) != '-' && args[index].charAt(1) != '-'))) {
count++;
java.lang.String[] oldValue = value;
value = new java.lang.String[count];
if (oldValue != null)
System.arraycopy (oldValue, 0, value, 0, oldValue.length);
value[count-1] = args[index];
index++;
}
return index;
}
public java.lang.String defaultValueToString() {
if (defaultValue == null)
return "(null)";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < defaultValue.length; i++) {
sb.append (defaultValue[i]);
if (i < defaultValue.length-1)
sb.append (" ");
}
return sb.toString();
}
public java.lang.String valueToString () {
if (value == null)
return "(null)";
@Var
java.lang.String val = "";
for (int i = 0; i < value.length; i++) {
val += value [i] + " ";
}
return val;
}
}
public static class File extends CommandOption
{
public java.io.File value, defaultValue;
public File (Class owner, java.lang.String name, java.lang.String argName,
boolean argRequired, java.io.File defaultValue,
java.lang.String shortdoc, java.lang.String longdoc)
{
super (owner, name, argName, java.io.File.class, argRequired, shortdoc, longdoc);
this.defaultValue = value = defaultValue;
}
public java.io.File value () { return value; }
public void parseArg (java.lang.String arg) { value = new java.io.File(arg); }
public java.lang.String defaultValueToString() { return defaultValue == null ? null : defaultValue.toString(); }
public java.lang.String valueToString () { return value == null ? null : value.toString(); };
}
// Value is a string that can take on only a limited set of values
public static class Set extends CommandOption
{
public java.lang.String value, defaultValue;
java.lang.String[] setContents;
java.lang.String contentsString;
public Set (Class owner, java.lang.String name, java.lang.String argName,
boolean argRequired, java.lang.String[] setContents, int defaultIndex,
java.lang.String shortdoc, java.lang.String longdoc)
{
super (owner, name, argName, java.io.File.class, argRequired, shortdoc, longdoc);
this.defaultValue = this.value = setContents[defaultIndex];
this.setContents = setContents;
StringBuffer sb = new StringBuffer ();
for (int i = 0; i < setContents.length; i++) {
sb.append (setContents[i]);
sb.append (",");
}
this.contentsString = sb.toString();
}
public java.lang.String value () { return value; }
public void parseArg (java.lang.String arg)
{
value = null;
for (int i = 0; i < setContents.length; i++)
if (setContents[i].equals(arg))
value = setContents[i];
if (value == null)
throw new IllegalArgumentException ("Unrecognized option argument \""+arg+"\" not in set "+contentsString);
}
public java.lang.String defaultValueToString() { return defaultValue; }
public java.lang.String valueToString() { return value; }
}
public static class Object extends CommandOption
{
public java.lang.Object value, defaultValue;
public Object (Class owner, java.lang.String name, java.lang.String argName,
boolean argRequired, java.lang.Object defaultValue,
java.lang.String shortdoc, java.lang.String longdoc)
{
super (owner, name, argName, java.lang.Object.class, argRequired, shortdoc, longdoc);
this.defaultValue = value = defaultValue;
}
public java.lang.Object value () { return value; }
public void parseArg (java.lang.String arg) {
try {
value = interpreter.eval (arg);
} catch (bsh.EvalError e) {
throw new IllegalArgumentException ("Java interpreter eval error\n"+e);
}
}
public java.lang.String defaultValueToString() { return defaultValue == null ? null : defaultValue.toString(); }
public java.lang.String valueToString() { return value == null ? null : value.toString(); }
}
public static class ObjectFromBean extends Object
{
public ObjectFromBean (Class owner, java.lang.String name, java.lang.String argName,
boolean argRequired, java.lang.Object defValue,
java.lang.String shortdoc, java.lang.String longdoc)
{
super (owner, name, argName, argRequired, java.lang.Object.class, shortdoc, longdoc);
defaultValue = value = defValue;
}
public java.lang.Object value () { return value; }
public void parseArg (java.lang.String arg) {
// parse something like MaxEntTrainer,gaussianPriorVariance=10,numIterations=20
//System.out.println("Arg = " + arg);
// First, split the argument at commas.
java.lang.String fields[] = arg.split(",");
//Massage constructor name, so that MaxEnt, MaxEntTrainer, new MaxEntTrainer()
// all call new MaxEntTrainer()
java.lang.String constructorName = fields[0];
if (constructorName.contains("(") || constructorName.contains(";")) // if contains ( or a ;, pass it though
super.parseArg(arg);
else {
super.parseArg("new " + constructorName + "()"); // use default constructor to make the object
}
// Call the appropriate set... methods that appeared comma-separated
// find methods associated with the class we just built
Method methods[] = this.value.getClass().getMethods();
// find setters corresponding to parameter names.
for (int i=1; i<fields.length; i++){
java.lang.String nameValuePair[] = fields[i].split("=");
java.lang.String parameterName = nameValuePair[0];
java.lang.String parameterValue = nameValuePair[1]; //todo: check for val present!
java.lang.Object parameterValueObject;
try {
parameterValueObject = getInterpreter().eval(parameterValue);
} catch (bsh.EvalError e) {
throw new IllegalArgumentException ("Java interpreter eval error on parameter "+
parameterName + "\n"+e);
}
@Var
boolean foundSetter = false;
for (int j=0; j<methods.length; j++){
// System.out.println("method " + j + " name is " + methods[j].getName());
// System.out.println("set" + Character.toUpperCase(parameterName.charAt(0)) + parameterName.substring(1));
if ( ("set" + Character.toUpperCase(parameterName.charAt(0)) + parameterName.substring(1)).equals(methods[j].getName()) &&
methods[j].getParameterTypes().length == 1){
// System.out.println("Matched method " + methods[j].getName());
// Class[] ptypes = methods[j].getParameterTypes();
// System.out.println("Parameter types:");
// for (int k=0; k<ptypes.length; k++){
// System.out.println("class " + k + " = " + ptypes[k].getName());
// }
try {
java.lang.Object[] parameterList = new java.lang.Object[]{parameterValueObject};
// System.out.println("Argument types:");
// for (int k=0; k<parameterList.length; k++){
// System.out.println("class " + k + " = " + parameterList[k].getClass().getName());
// }
methods[j].invoke(this.value, parameterList);
} catch ( IllegalAccessException e) {
System.out.println("IllegalAccessException " + e);
throw new IllegalArgumentException ("Java access error calling setter\n"+e);
} catch ( InvocationTargetException e) {
System.out.println("IllegalTargetException " + e);
throw new IllegalArgumentException ("Java target error calling setter\n"+e);
}
foundSetter = true;
break;
}
}
if (!foundSetter){
System.out.println("Parameter " + parameterName + " not found on trainer " + constructorName);
System.out.println("Available parameters for " + constructorName);
for (int j=0; j<methods.length; j++){
if ( methods[j].getName().startsWith("set") && methods[j].getParameterTypes().length == 1){
System.out.println(Character.toLowerCase(methods[j].getName().charAt(3)) +
methods[j].getName().substring(4));
}
}
throw new IllegalArgumentException ("no setter found for parameter " + parameterName);
}
}
}
public java.lang.String defaultValueToString() { return defaultValue == null ? null : defaultValue.toString(); }
public java.lang.String valueToString() { return value == null ? null : value.toString(); }
}
}