Skip to content

Commit

Permalink
wip on behaviour classes refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Jan 29, 2025
1 parent ed4ae35 commit 619f377
Show file tree
Hide file tree
Showing 38 changed files with 501 additions and 20,820 deletions.
2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules/examples/POSEIDON.examples.main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* POSEIDON: an agent-based model of fisheries
* Copyright (c) 2025 CoHESyS Lab [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package uk.ac.ox.poseidon.agents.behaviours;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.Data;
import uk.ac.ox.poseidon.agents.vessels.Vessel;

import java.time.Duration;
import java.time.LocalDateTime;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

@Data
public abstract class AbstractAction implements Action {
protected final Vessel vessel;
protected final LocalDateTime start;
protected final Duration duration;

@SuppressFBWarnings("EI_EXPOSE_REP2")
public AbstractAction(
final Vessel vessel,
final LocalDateTime start,
final Duration duration
) {
checkArgument(
duration.isPositive(),
"Duration must be positive but was %s.",
duration
);
this.start = checkNotNull(start);
this.duration = checkNotNull(duration);
this.vessel = checkNotNull(vessel);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* POSEIDON: an agent-based model of fisheries
* Copyright (c) 2024 CoHESyS Lab [email protected]
* Copyright (c) 2025 CoHESyS Lab [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -19,59 +19,20 @@

package uk.ac.ox.poseidon.agents.behaviours;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.Data;
import sim.engine.SimState;
import sim.engine.Steppable;
import uk.ac.ox.poseidon.agents.vessels.Vessel;
import uk.ac.ox.poseidon.core.schedule.TemporalSchedule;

import java.time.Duration;
import java.time.LocalDateTime;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
public interface Action {

@Data
public abstract class Action implements Steppable {

private final LocalDateTime start;
private final Duration duration;
private final Vessel vessel;

@SuppressFBWarnings("EI_EXPOSE_REP2")
public Action(
final LocalDateTime start,
final Duration duration,
final Vessel vessel
) {
checkArgument(
duration.isPositive(),
"Duration must be positive but was %s.",
duration
);
this.start = checkNotNull(start);
this.duration = checkNotNull(duration);
this.vessel = checkNotNull(vessel);
default LocalDateTime getEnd() {
return getStart().plus(getDuration());
}

@Override
public final void step(final SimState simState) {
checkArgument(simState.schedule instanceof TemporalSchedule);
final var schedule = (TemporalSchedule) simState.schedule;
final var nextAction = complete(schedule.getDateTime());
vessel.getEventManager().broadcast(this);
if (nextAction != null) {
schedule.scheduleOnceIn(nextAction.getDuration(), nextAction);
}
}
Vessel getVessel();

protected abstract Action complete(
LocalDateTime dateTime
);

public LocalDateTime getEnd() {
return start.plus(duration);
}
LocalDateTime getStart();

Duration getDuration();
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import java.time.LocalDateTime;

public interface Behaviour {
Action newAction(
LocalDateTime dateTime,
Vessel vessel
SteppableAction nextAction(
Vessel vessel,
LocalDateTime dateTime
);
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* POSEIDON: an agent-based model of fisheries
* Copyright (c) 2024 CoHESyS Lab [email protected]
* Copyright (c) 2025 CoHESyS Lab [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -19,29 +19,8 @@

package uk.ac.ox.poseidon.agents.behaviours;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.Getter;
import sim.util.Int2D;
import uk.ac.ox.poseidon.agents.vessels.Vessel;

import java.time.Duration;
import java.time.LocalDateTime;

@Getter
public abstract class GridAction extends Action {
private final Int2D cell;

@SuppressFBWarnings(
value = "EI2",
justification = "Int2D is immutable even if SpotBugs thinks otherwise."
)
public GridAction(
final LocalDateTime start,
final Duration duration,
final Vessel vessel,
final Int2D cell
) {
super(start, duration, vessel);
this.cell = cell;
}
public interface GridAction extends Action {
Int2D getCell();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* POSEIDON: an agent-based model of fisheries
* Copyright (c) 2024 CoHESyS Lab [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package uk.ac.ox.poseidon.agents.behaviours;

import sim.engine.SimState;
import sim.engine.Steppable;
import uk.ac.ox.poseidon.agents.vessels.Vessel;
import uk.ac.ox.poseidon.core.schedule.TemporalSchedule;

import java.time.Duration;
import java.time.LocalDateTime;

public abstract class SteppableAction extends AbstractAction implements Steppable {

public SteppableAction(
final Vessel vessel,
final LocalDateTime start,
final Duration duration
) {
super(vessel, start, duration);
}

public void init() {}

protected abstract void complete(
LocalDateTime dateTime
);

@Override
public final void step(final SimState simState) {
if (simState.schedule instanceof final TemporalSchedule schedule) {
complete(schedule.getDateTime());
vessel.getEventManager().broadcast(this);
vessel.scheduleNextAction(schedule);
} else throw new IllegalStateException(
"Simulation schedule type must be " + TemporalSchedule.class.getName()
);
}

}
Loading

0 comments on commit 619f377

Please sign in to comment.