Skip to content

Commit 6915776

Browse files
make factory class for closure regulations
1 parent c485aa3 commit 6915776

File tree

7 files changed

+216
-43
lines changed

7 files changed

+216
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package uk.ac.ox.oxfish.fisher.purseseiner.regulations;
2+
3+
import uk.ac.ox.oxfish.regulations.ForbiddenIf;
4+
import uk.ac.ox.oxfish.regulations.conditions.*;
5+
import uk.ac.ox.oxfish.utility.AlgorithmFactory;
6+
import uk.ac.ox.oxfish.utility.parameters.IntegerParameter;
7+
import uk.ac.ox.poseidon.regulations.api.Regulations;
8+
9+
import java.time.MonthDay;
10+
11+
import static uk.ac.ox.oxfish.fisher.purseseiner.regulations.DefaultEpoRegulations.addDays;
12+
import static uk.ac.ox.oxfish.regulations.conditions.False.FALSE;
13+
14+
public class Closure implements RegulationFactory {
15+
private IntegerParameter beginningDay;
16+
private IntegerParameter beginningMonth;
17+
private IntegerParameter endDay;
18+
private IntegerParameter endMonth;
19+
private IntegerParameter daysToForbidDeploymentsBefore;
20+
21+
public Closure() {
22+
}
23+
24+
public Closure(
25+
final MonthDay beginning,
26+
final MonthDay end,
27+
final int daysToForbidDeploymentsBefore
28+
) {
29+
this(
30+
new IntegerParameter(beginning.getDayOfMonth()),
31+
new IntegerParameter(beginning.getMonthValue()),
32+
new IntegerParameter(end.getDayOfMonth()),
33+
new IntegerParameter(end.getMonthValue()),
34+
new IntegerParameter(daysToForbidDeploymentsBefore)
35+
);
36+
}
37+
38+
public Closure(
39+
final IntegerParameter beginningDay,
40+
final IntegerParameter beginningMonth,
41+
final IntegerParameter endDay,
42+
final IntegerParameter endMonth,
43+
final IntegerParameter daysToForbidDeploymentsBefore
44+
) {
45+
this.beginningDay = beginningDay;
46+
this.beginningMonth = beginningMonth;
47+
this.endDay = endDay;
48+
this.endMonth = endMonth;
49+
this.daysToForbidDeploymentsBefore = daysToForbidDeploymentsBefore;
50+
}
51+
52+
@SuppressWarnings("unused")
53+
public IntegerParameter getDaysToForbidDeploymentsBefore() {
54+
return daysToForbidDeploymentsBefore;
55+
}
56+
57+
@SuppressWarnings("unused")
58+
public void setDaysToForbidDeploymentsBefore(final IntegerParameter daysToForbidDeploymentsBefore) {
59+
this.daysToForbidDeploymentsBefore = daysToForbidDeploymentsBefore;
60+
}
61+
62+
@SuppressWarnings("unused")
63+
public IntegerParameter getBeginningDay() {
64+
return beginningDay;
65+
}
66+
67+
@SuppressWarnings("unused")
68+
public void setBeginningDay(final IntegerParameter beginningDay) {
69+
this.beginningDay = beginningDay;
70+
}
71+
72+
@SuppressWarnings("unused")
73+
public IntegerParameter getBeginningMonth() {
74+
return beginningMonth;
75+
}
76+
77+
@SuppressWarnings("unused")
78+
public void setBeginningMonth(final IntegerParameter beginningMonth) {
79+
this.beginningMonth = beginningMonth;
80+
}
81+
82+
@SuppressWarnings("unused")
83+
public IntegerParameter getEndDay() {
84+
return endDay;
85+
}
86+
87+
@SuppressWarnings("unused")
88+
public void setEndDay(final IntegerParameter endDay) {
89+
this.endDay = endDay;
90+
}
91+
92+
@SuppressWarnings("unused")
93+
public IntegerParameter getEndMonth() {
94+
return endMonth;
95+
}
96+
97+
@SuppressWarnings("unused")
98+
public void setEndMonth(final IntegerParameter endMonth) {
99+
this.endMonth = endMonth;
100+
}
101+
102+
@Override
103+
public AlgorithmFactory<Regulations> get() {
104+
final MonthDay beginning = getBeginning();
105+
return new ForbiddenIf(
106+
new AnyOf(
107+
daysToForbidDeploymentsBefore.getIntValue() >= 1
108+
? forbidDeploymentsBefore(beginning, daysToForbidDeploymentsBefore.getIntValue())
109+
: FALSE,
110+
new AllOf(
111+
new AgentHasTag("closure A"),
112+
new BetweenYearlyDates(
113+
beginning,
114+
getEnd()
115+
)
116+
)
117+
)
118+
);
119+
}
120+
121+
public MonthDay getBeginning() {
122+
return makeMonthDay(beginningMonth, beginningDay);
123+
}
124+
125+
private AllOf forbidDeploymentsBefore(final MonthDay beginning, final int numDays) {
126+
return new AllOf(
127+
new AgentHasTag("closure A"),
128+
new ActionCodeIs("DPL"),
129+
new BetweenYearlyDates(
130+
addDays(beginning, -numDays),
131+
addDays(beginning, -1)
132+
)
133+
);
134+
}
135+
136+
public MonthDay getEnd() {
137+
return makeMonthDay(endMonth, endDay);
138+
}
139+
140+
private static MonthDay makeMonthDay(final IntegerParameter month, final IntegerParameter day) {
141+
return MonthDay.of(month.getIntValue(), day.getIntValue());
142+
}
143+
}

POSEIDON/src/main/java/uk/ac/ox/oxfish/fisher/purseseiner/regulations/DefaultEpoRegulations.java

+3-42
Original file line numberDiff line numberDiff line change
@@ -55,47 +55,8 @@ public static AlgorithmFactory<Regulations> make(final InputPath inputFolder) {
5555
)
5656
),
5757
"Active-FAD limits", new ActiveFadLimits(ACTIVE_FAD_LIMITS),
58-
// Forbid deployments 15 days before closure
59-
"Closure A", new ForbiddenIf(
60-
new AnyOf(
61-
new AllOf(
62-
// Forbid deployments 15 days before closure
63-
new AgentHasTag("closure A"),
64-
new ActionCodeIs("DPL"),
65-
new BetweenYearlyDates(
66-
addDays(CLOSURE_A_START, -15),
67-
addDays(CLOSURE_A_START, -1)
68-
)
69-
),
70-
new AllOf(
71-
new AgentHasTag("closure A"),
72-
new BetweenYearlyDates(
73-
CLOSURE_A_START,
74-
CLOSURE_A_END
75-
)
76-
)
77-
)
78-
),
79-
"Closure B", new ForbiddenIf(
80-
new AnyOf(
81-
// Forbid deployments 15 days before closure
82-
new AllOf(
83-
new AgentHasTag("closure B"),
84-
new ActionCodeIs("DPL"),
85-
new BetweenYearlyDates(
86-
addDays(CLOSURE_B_START, -15),
87-
addDays(CLOSURE_B_START, -1)
88-
)
89-
),
90-
new AllOf(
91-
new AgentHasTag("closure B"),
92-
new BetweenYearlyDates(
93-
CLOSURE_B_START,
94-
CLOSURE_B_END
95-
)
96-
)
97-
)
98-
),
58+
"Closure A", new Closure(CLOSURE_A_START, CLOSURE_A_END, 15),
59+
"Closure B", new Closure(CLOSURE_B_START, CLOSURE_B_END, 15),
9960
"El Corralito", new ForbiddenIf(
10061
new AllOf(
10162
new BetweenYearlyDates(
@@ -215,7 +176,7 @@ public static AlgorithmFactory<Regulations> make(final InputPath inputFolder) {
215176
}
216177

217178
@SuppressWarnings("SameParameterValue")
218-
private static MonthDay addDays(
179+
public static MonthDay addDays(
219180
final MonthDay monthDay,
220181
final long daysToAdd
221182
) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package uk.ac.ox.oxfish.regulations.conditions;
2+
3+
import uk.ac.ox.oxfish.model.FishState;
4+
import uk.ac.ox.oxfish.utility.AlgorithmFactory;
5+
import uk.ac.ox.poseidon.regulations.api.Condition;
6+
7+
public enum False implements AlgorithmFactory<Condition> {
8+
FALSE;
9+
10+
// needed for YAML initialization
11+
public False getInstance() {
12+
return FALSE;
13+
}
14+
15+
@Override
16+
public Condition apply(final FishState fishState) {
17+
return uk.ac.ox.poseidon.regulations.core.conditions.False.FALSE;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package uk.ac.ox.oxfish.regulations.conditions;
2+
3+
import uk.ac.ox.oxfish.model.FishState;
4+
import uk.ac.ox.oxfish.utility.AlgorithmFactory;
5+
import uk.ac.ox.poseidon.regulations.api.Condition;
6+
7+
public enum True implements AlgorithmFactory<Condition> {
8+
TRUE;
9+
10+
// needed for YAML initialization
11+
public True getInstance() {
12+
return TRUE;
13+
}
14+
15+
@Override
16+
public Condition apply(final FishState fishState) {
17+
return uk.ac.ox.poseidon.regulations.core.conditions.True.TRUE;
18+
}
19+
}

POSEIDON/src/main/java/uk/ac/ox/oxfish/utility/AlgorithmFactories.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import uk.ac.ox.oxfish.fisher.purseseiner.planner.PlanningModule;
7171
import uk.ac.ox.oxfish.fisher.purseseiner.planner.factories.*;
7272
import uk.ac.ox.oxfish.fisher.purseseiner.regulations.ActiveFadLimits;
73+
import uk.ac.ox.oxfish.fisher.purseseiner.regulations.Closure;
7374
import uk.ac.ox.oxfish.fisher.purseseiner.samplers.*;
7475
import uk.ac.ox.oxfish.fisher.purseseiner.strategies.departing.PurseSeinerDepartingStrategyFactory;
7576
import uk.ac.ox.oxfish.fisher.purseseiner.strategies.destination.GravityDestinationStrategyFactory;
@@ -693,14 +694,17 @@ public class AlgorithmFactories {
693694
InYear.class,
694695
Not.class,
695696
NotBelow.class,
696-
NotAbove.class
697+
NotAbove.class,
698+
True.class,
699+
False.class
697700
));
698701
addFactories(new Factories<>(
699702
Regulations.class,
700703
ImmutableMap.of(
701704
ActiveFadLimits.class, "Active-FAD limits"
702705
),
703706
NamedRegulations.class,
707+
Closure.class,
704708
EverythingForbidden.class,
705709
EverythingPermitted.class,
706710
ForbiddenIf.class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package uk.ac.ox.poseidon.regulations.core.conditions;
2+
3+
import uk.ac.ox.poseidon.agents.api.Action;
4+
import uk.ac.ox.poseidon.regulations.api.Condition;
5+
6+
public enum False implements Condition {
7+
FALSE;
8+
9+
@Override
10+
public boolean test(final Action action) {
11+
return false;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package uk.ac.ox.poseidon.regulations.core.conditions;
2+
3+
import uk.ac.ox.poseidon.agents.api.Action;
4+
import uk.ac.ox.poseidon.regulations.api.Condition;
5+
6+
public enum True implements Condition {
7+
8+
TRUE;
9+
10+
@Override
11+
public boolean test(final Action action) {
12+
return true;
13+
}
14+
}

0 commit comments

Comments
 (0)