Skip to content

Commit a677042

Browse files
add closure extension regulation condition factories
1 parent 6915776 commit a677042

File tree

6 files changed

+201
-28
lines changed

6 files changed

+201
-28
lines changed

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

+26-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import uk.ac.ox.oxfish.regulations.conditions.*;
55
import uk.ac.ox.oxfish.utility.AlgorithmFactory;
66
import uk.ac.ox.oxfish.utility.parameters.IntegerParameter;
7+
import uk.ac.ox.oxfish.utility.parameters.StringParameter;
78
import uk.ac.ox.poseidon.regulations.api.Regulations;
89

910
import java.time.MonthDay;
@@ -12,21 +13,26 @@
1213
import static uk.ac.ox.oxfish.regulations.conditions.False.FALSE;
1314

1415
public class Closure implements RegulationFactory {
16+
private StringParameter agentTag;
1517
private IntegerParameter beginningDay;
1618
private IntegerParameter beginningMonth;
1719
private IntegerParameter endDay;
1820
private IntegerParameter endMonth;
1921
private IntegerParameter daysToForbidDeploymentsBefore;
2022

23+
@SuppressWarnings("unused")
2124
public Closure() {
2225
}
2326

27+
@SuppressWarnings({"WeakerAccess", "unused"})
2428
public Closure(
29+
final String agentTag,
2530
final MonthDay beginning,
2631
final MonthDay end,
2732
final int daysToForbidDeploymentsBefore
2833
) {
2934
this(
35+
new StringParameter(agentTag),
3036
new IntegerParameter(beginning.getDayOfMonth()),
3137
new IntegerParameter(beginning.getMonthValue()),
3238
new IntegerParameter(end.getDayOfMonth()),
@@ -35,20 +41,32 @@ public Closure(
3541
);
3642
}
3743

44+
@SuppressWarnings("WeakerAccess")
3845
public Closure(
46+
final StringParameter agentTag,
3947
final IntegerParameter beginningDay,
4048
final IntegerParameter beginningMonth,
4149
final IntegerParameter endDay,
4250
final IntegerParameter endMonth,
4351
final IntegerParameter daysToForbidDeploymentsBefore
4452
) {
53+
this.agentTag = agentTag;
4554
this.beginningDay = beginningDay;
4655
this.beginningMonth = beginningMonth;
4756
this.endDay = endDay;
4857
this.endMonth = endMonth;
4958
this.daysToForbidDeploymentsBefore = daysToForbidDeploymentsBefore;
5059
}
5160

61+
public StringParameter getAgentTag() {
62+
return agentTag;
63+
}
64+
65+
@SuppressWarnings("unused")
66+
public void setAgentTag(final StringParameter agentTag) {
67+
this.agentTag = agentTag;
68+
}
69+
5270
@SuppressWarnings("unused")
5371
public IntegerParameter getDaysToForbidDeploymentsBefore() {
5472
return daysToForbidDeploymentsBefore;
@@ -103,16 +121,13 @@ public void setEndMonth(final IntegerParameter endMonth) {
103121
public AlgorithmFactory<Regulations> get() {
104122
final MonthDay beginning = getBeginning();
105123
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-
)
124+
new AllOf(
125+
new AgentHasTag(agentTag.getValue()),
126+
new AnyOf(
127+
daysToForbidDeploymentsBefore.getIntValue() >= 1
128+
? forbidDeploymentsBefore(beginning, daysToForbidDeploymentsBefore.getIntValue())
129+
: FALSE,
130+
new BetweenYearlyDates(beginning, getEnd())
116131
)
117132
)
118133
);
@@ -122,9 +137,8 @@ public MonthDay getBeginning() {
122137
return makeMonthDay(beginningMonth, beginningDay);
123138
}
124139

125-
private AllOf forbidDeploymentsBefore(final MonthDay beginning, final int numDays) {
140+
static AllOf forbidDeploymentsBefore(final MonthDay beginning, final int numDays) {
126141
return new AllOf(
127-
new AgentHasTag("closure A"),
128142
new ActionCodeIs("DPL"),
129143
new BetweenYearlyDates(
130144
addDays(beginning, -numDays),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package uk.ac.ox.oxfish.fisher.purseseiner.regulations;
2+
3+
import uk.ac.ox.oxfish.regulations.conditions.AgentHasTag;
4+
import uk.ac.ox.oxfish.regulations.conditions.AllOf;
5+
import uk.ac.ox.oxfish.regulations.conditions.BetweenYearlyDates;
6+
import uk.ac.ox.oxfish.utility.AlgorithmFactory;
7+
import uk.ac.ox.oxfish.utility.parameters.IntegerParameter;
8+
import uk.ac.ox.poseidon.regulations.api.Condition;
9+
10+
import static com.google.common.base.Preconditions.checkArgument;
11+
import static uk.ac.ox.oxfish.fisher.purseseiner.regulations.DefaultEpoRegulations.addDays;
12+
13+
public class ClosureExtensionAfter implements ConditionFactory {
14+
private Closure originalClosure;
15+
private IntegerParameter numberOfDaysToExtend;
16+
17+
@SuppressWarnings("unused")
18+
public ClosureExtensionAfter() {
19+
}
20+
21+
public ClosureExtensionAfter(
22+
final Closure originalClosure,
23+
final int numberOfDaysToExtend
24+
) {
25+
this(originalClosure, new IntegerParameter(numberOfDaysToExtend));
26+
}
27+
28+
@SuppressWarnings("WeakerAccess")
29+
public ClosureExtensionAfter(
30+
final Closure originalClosure,
31+
final IntegerParameter numberOfDaysToExtend
32+
) {
33+
checkArgument(numberOfDaysToExtend.getIntValue() >= 1);
34+
this.originalClosure = originalClosure;
35+
this.numberOfDaysToExtend = numberOfDaysToExtend;
36+
}
37+
38+
@SuppressWarnings("unused")
39+
public Closure getOriginalClosure() {
40+
return originalClosure;
41+
}
42+
43+
@SuppressWarnings("unused")
44+
public void setOriginalClosure(final Closure originalClosure) {
45+
this.originalClosure = originalClosure;
46+
}
47+
48+
@SuppressWarnings("unused")
49+
public IntegerParameter getNumberOfDaysToExtend() {
50+
return numberOfDaysToExtend;
51+
}
52+
53+
@SuppressWarnings("unused")
54+
public void setNumberOfDaysToExtend(final IntegerParameter numberOfDaysToExtend) {
55+
checkArgument(numberOfDaysToExtend.getIntValue() >= 1);
56+
this.numberOfDaysToExtend = numberOfDaysToExtend;
57+
}
58+
59+
@Override
60+
public AlgorithmFactory<Condition> get() {
61+
return new AllOf(
62+
new AgentHasTag(originalClosure.getAgentTag().getValue()),
63+
new BetweenYearlyDates(
64+
addDays(originalClosure.getEnd(), 1),
65+
addDays(originalClosure.getEnd(), numberOfDaysToExtend.getIntValue())
66+
)
67+
);
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package uk.ac.ox.oxfish.fisher.purseseiner.regulations;
2+
3+
import uk.ac.ox.oxfish.regulations.conditions.AgentHasTag;
4+
import uk.ac.ox.oxfish.regulations.conditions.AllOf;
5+
import uk.ac.ox.oxfish.regulations.conditions.AnyOf;
6+
import uk.ac.ox.oxfish.regulations.conditions.BetweenYearlyDates;
7+
import uk.ac.ox.oxfish.utility.AlgorithmFactory;
8+
import uk.ac.ox.oxfish.utility.parameters.IntegerParameter;
9+
import uk.ac.ox.poseidon.regulations.api.Condition;
10+
11+
import java.time.MonthDay;
12+
13+
import static com.google.common.base.Preconditions.checkArgument;
14+
import static uk.ac.ox.oxfish.fisher.purseseiner.regulations.Closure.forbidDeploymentsBefore;
15+
import static uk.ac.ox.oxfish.fisher.purseseiner.regulations.DefaultEpoRegulations.addDays;
16+
import static uk.ac.ox.oxfish.regulations.conditions.False.FALSE;
17+
18+
public class ClosureExtensionBefore implements ConditionFactory {
19+
private Closure originalClosure;
20+
private IntegerParameter numberOfDaysToExtend;
21+
22+
@SuppressWarnings("unused")
23+
public ClosureExtensionBefore() {
24+
}
25+
26+
@SuppressWarnings("WeakerAccess")
27+
public ClosureExtensionBefore(
28+
final Closure originalClosure,
29+
final int numberOfDaysToExtend
30+
) {
31+
this(originalClosure, new IntegerParameter(numberOfDaysToExtend));
32+
}
33+
34+
@SuppressWarnings("WeakerAccess")
35+
public ClosureExtensionBefore(
36+
final Closure originalClosure,
37+
final IntegerParameter numberOfDaysToExtend
38+
) {
39+
checkArgument(numberOfDaysToExtend.getIntValue() >= 1);
40+
this.originalClosure = originalClosure;
41+
this.numberOfDaysToExtend = numberOfDaysToExtend;
42+
}
43+
44+
@SuppressWarnings("unused")
45+
public Closure getOriginalClosure() {
46+
return originalClosure;
47+
}
48+
49+
@SuppressWarnings("unused")
50+
public void setOriginalClosure(final Closure originalClosure) {
51+
this.originalClosure = originalClosure;
52+
}
53+
54+
@SuppressWarnings("unused")
55+
public IntegerParameter getNumberOfDaysToExtend() {
56+
return numberOfDaysToExtend;
57+
}
58+
59+
@SuppressWarnings("unused")
60+
public void setNumberOfDaysToExtend(final IntegerParameter numberOfDaysToExtend) {
61+
checkArgument(numberOfDaysToExtend.getIntValue() >= 1);
62+
this.numberOfDaysToExtend = numberOfDaysToExtend;
63+
}
64+
65+
@Override
66+
public AlgorithmFactory<Condition> get() {
67+
final MonthDay newBeginning = addDays(originalClosure.getBeginning(), -numberOfDaysToExtend.getIntValue());
68+
final int daysOfForbiddenDeployments = originalClosure.getDaysToForbidDeploymentsBefore().getIntValue();
69+
return new AllOf(
70+
new AgentHasTag(originalClosure.getAgentTag().getValue()),
71+
new AnyOf(
72+
daysOfForbiddenDeployments >= 1
73+
? forbidDeploymentsBefore(newBeginning, daysOfForbiddenDeployments)
74+
: FALSE,
75+
new BetweenYearlyDates(
76+
newBeginning,
77+
addDays(originalClosure.getBeginning(), -1)
78+
)
79+
)
80+
);
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package uk.ac.ox.oxfish.fisher.purseseiner.regulations;
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+
import java.util.function.Supplier;
8+
9+
public interface ConditionFactory extends Supplier<AlgorithmFactory<Condition>>, AlgorithmFactory<Condition> {
10+
@Override
11+
default Condition apply(final FishState fishState) {
12+
return get().apply(fishState);
13+
}
14+
}

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

+6-16
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public static AlgorithmFactory<Regulations> make(final InputPath inputFolder) {
4646

4747
final InputPath regions = inputFolder.path("regions");
4848

49+
final Closure closureA = new Closure("closure A", CLOSURE_A_START, CLOSURE_A_END, 15);
50+
final Closure closureB = new Closure("closure B", CLOSURE_B_START, CLOSURE_B_END, 15);
4951
return new NamedRegulations(
5052
ImmutableMap.of(
5153
"DEL licence", new ForbiddenIf(
@@ -55,8 +57,8 @@ public static AlgorithmFactory<Regulations> make(final InputPath inputFolder) {
5557
)
5658
),
5759
"Active-FAD limits", new ActiveFadLimits(ACTIVE_FAD_LIMITS),
58-
"Closure A", new Closure(CLOSURE_A_START, CLOSURE_A_END, 15),
59-
"Closure B", new Closure(CLOSURE_B_START, CLOSURE_B_END, 15),
60+
"Closure A", closureA,
61+
"Closure B", closureB,
6062
"El Corralito", new ForbiddenIf(
6163
new AllOf(
6264
new BetweenYearlyDates(
@@ -76,20 +78,8 @@ public static AlgorithmFactory<Regulations> make(final InputPath inputFolder) {
7678
new AllOf(
7779
new AgentHasTag("extended_2022_closure"),
7880
new AnyOf(
79-
new AllOf(
80-
new AgentHasTag("closure A"),
81-
new BetweenDates(
82-
CLOSURE_A_START.atYear(2022).minusDays(8),
83-
CLOSURE_A_START.atYear(2022).minusDays(1)
84-
)
85-
),
86-
new AllOf(
87-
new AgentHasTag("closure B"),
88-
new BetweenDates(
89-
CLOSURE_B_END.atYear(2023).plusDays(1),
90-
CLOSURE_B_END.atYear(2023).plusDays(8)
91-
)
92-
)
81+
new ClosureExtensionBefore(closureA, 8),
82+
new ClosureExtensionAfter(closureB, 8)
9383
)
9484
)
9585
),

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

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
import uk.ac.ox.oxfish.fisher.purseseiner.planner.factories.*;
7272
import uk.ac.ox.oxfish.fisher.purseseiner.regulations.ActiveFadLimits;
7373
import uk.ac.ox.oxfish.fisher.purseseiner.regulations.Closure;
74+
import uk.ac.ox.oxfish.fisher.purseseiner.regulations.ClosureExtensionAfter;
75+
import uk.ac.ox.oxfish.fisher.purseseiner.regulations.ClosureExtensionBefore;
7476
import uk.ac.ox.oxfish.fisher.purseseiner.samplers.*;
7577
import uk.ac.ox.oxfish.fisher.purseseiner.strategies.departing.PurseSeinerDepartingStrategyFactory;
7678
import uk.ac.ox.oxfish.fisher.purseseiner.strategies.destination.GravityDestinationStrategyFactory;
@@ -690,6 +692,8 @@ public class AlgorithmFactories {
690692
Below.class,
691693
BetweenDates.class,
692694
BetweenYearlyDates.class,
695+
ClosureExtensionAfter.class,
696+
ClosureExtensionBefore.class,
693697
InRectangularArea.class,
694698
InYear.class,
695699
Not.class,

0 commit comments

Comments
 (0)