Skip to content

Commit fef078f

Browse files
use standard bean addresses for calibrated parameters
1 parent 6915290 commit fef078f

File tree

11 files changed

+124
-312
lines changed

11 files changed

+124
-312
lines changed

POSEIDON/src/main/java/uk/ac/ox/oxfish/maximization/generic/LegacyParameterAddressBuilder.java

-75
This file was deleted.

POSEIDON/src/main/java/uk/ac/ox/oxfish/maximization/generic/OptimizationParameter.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static void navigateAndSet(
2929
final String address,
3030
final Object value
3131
) {
32-
new ParameterAddress(address).getSetter(scenario).accept(value);
32+
new ParameterAddress(address).setValue(scenario, value);
3333
}
3434

3535
/**
@@ -46,7 +46,10 @@ static void navigateAndSet(
4646
* @param inputs the numerical values of the parameters to set
4747
* @return
4848
*/
49-
String parametrize(Scenario scenario, double[] inputs);
49+
String parametrize(
50+
Scenario scenario,
51+
double[] inputs
52+
);
5053

5154
String getName();
5255
}

POSEIDON/src/main/java/uk/ac/ox/oxfish/maximization/generic/ParameterAddress.java

+17-148
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package uk.ac.ox.oxfish.maximization.generic;
22

3-
import com.google.common.collect.ImmutableList;
43
import org.apache.commons.beanutils.PropertyUtils;
54
import uk.ac.ox.oxfish.model.scenario.Scenario;
65

76
import java.lang.reflect.InvocationTargetException;
8-
import java.util.List;
9-
import java.util.Map.Entry;
10-
import java.util.function.Consumer;
11-
import java.util.function.Supplier;
12-
import java.util.regex.Pattern;
137

148
import static com.google.common.base.Preconditions.checkArgument;
159
import static com.google.common.base.Preconditions.checkNotNull;
16-
import static uk.ac.ox.oxfish.utility.FishStateUtilities.entry;
1710

1811
public class ParameterAddress {
1912
private final String address;
@@ -23,150 +16,26 @@ public ParameterAddress(final String address) {
2316
this.address = address;
2417
}
2518

26-
private static Entry<String, String> splitBy(final String s, final String sep) {
27-
final String[] strings = s.split(Pattern.quote(sep));
28-
checkArgument(
29-
strings.length == 2,
30-
"There should be one and only one % in the string %", sep, s
31-
);
32-
return entry(strings[0], strings[1]);
33-
}
34-
35-
public Supplier<Object> getGetter(final Scenario scenario) {
36-
return getProperty(scenario).getGetter();
37-
}
38-
39-
public Property getProperty(final Scenario scenario) {
40-
return getProperty(scenario, ImmutableList.copyOf(this.address.split("\\.")));
41-
}
42-
43-
private Property getProperty(final Object object, final List<String> address) {
44-
checkArgument(!address.isEmpty());
45-
final String head = address.get(0);
46-
final Property property;
47-
if (head.contains("$")) {
48-
final Entry<String, String> entry = splitBy(head, "$");
49-
property = new IndexedProperty(object, entry.getKey(), Integer.parseInt(entry.getValue()));
50-
} else if (head.contains("~")) {
51-
final Entry<String, String> entry = splitBy(head, "~");
52-
property = new MappedProperty(object, entry.getKey(), entry.getValue());
53-
} else {
54-
property = new Property(object, head);
19+
public Object getValue(final Scenario scenario) {
20+
try {
21+
return PropertyUtils.getProperty(scenario, address);
22+
} catch (
23+
final IllegalAccessException | InvocationTargetException | NoSuchMethodException e
24+
) {
25+
throw new RuntimeException(e);
5526
}
56-
return address.size() == 1
57-
? property
58-
: getProperty(property.getGetter().get(), address.subList(1, address.size()));
59-
}
60-
61-
public Consumer<Object> getSetter(final Scenario scenario) {
62-
return getProperty(scenario).getSetter();
6327
}
6428

65-
private static class Property {
66-
private final Object bean;
67-
private final String name;
68-
69-
private Property(final Object bean, final String name) {
70-
this.bean = bean;
71-
this.name = name;
72-
}
73-
74-
public Object getBean() {
75-
return bean;
76-
}
77-
78-
public String getName() {
79-
return name;
80-
}
81-
82-
public Supplier<Object> getGetter() {
83-
return () -> {
84-
try {
85-
return PropertyUtils.getProperty(bean, name);
86-
} catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
87-
throw new RuntimeException(e);
88-
}
89-
};
90-
}
91-
92-
public Consumer<Object> getSetter() {
93-
return (value) -> {
94-
try {
95-
PropertyUtils.setProperty(bean, name, value);
96-
} catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
97-
throw new RuntimeException(e);
98-
}
99-
};
100-
}
101-
}
102-
103-
private static class IndexedProperty extends Property {
104-
private final int index;
105-
106-
private IndexedProperty(final Object bean, final String name, final int index) {
107-
super(bean, name);
108-
this.index = index;
109-
}
110-
111-
public int getIndex() {
112-
return index;
113-
}
114-
115-
@Override
116-
public Supplier<Object> getGetter() {
117-
return () -> {
118-
try {
119-
return PropertyUtils.getIndexedProperty(getBean(), getName(), index);
120-
} catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
121-
throw new RuntimeException(e);
122-
}
123-
};
124-
}
125-
126-
@Override
127-
public Consumer<Object> getSetter() {
128-
return (value) -> {
129-
try {
130-
PropertyUtils.setIndexedProperty(getBean(), getName(), index, value);
131-
} catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
132-
throw new RuntimeException(e);
133-
}
134-
};
135-
}
136-
}
137-
138-
private static class MappedProperty extends Property {
139-
private final String key;
140-
141-
private MappedProperty(final Object bean, final String name, final String key) {
142-
super(bean, name);
143-
this.key = key;
144-
}
145-
146-
public String getKey() {
147-
return key;
148-
}
149-
150-
@Override
151-
public Supplier<Object> getGetter() {
152-
return () -> {
153-
try {
154-
return PropertyUtils.getMappedProperty(getBean(), getName(), key);
155-
} catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
156-
throw new RuntimeException(e);
157-
}
158-
};
159-
}
160-
161-
@Override
162-
public Consumer<Object> getSetter() {
163-
return (value) -> {
164-
try {
165-
PropertyUtils.setMappedProperty(getBean(), getName(), key, value);
166-
} catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
167-
throw new RuntimeException(e);
168-
}
169-
};
29+
public void setValue(
30+
final Scenario scenario,
31+
final Object value
32+
) {
33+
try {
34+
PropertyUtils.setProperty(scenario, address, value);
35+
} catch (
36+
final IllegalAccessException | InvocationTargetException | NoSuchMethodException e
37+
) {
38+
throw new RuntimeException(e);
17039
}
17140
}
17241

0 commit comments

Comments
 (0)