Skip to content

Commit

Permalink
add dummy gRPC client
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Nov 27, 2024
1 parent 5882675 commit 6dfe878
Show file tree
Hide file tree
Showing 12 changed files with 630 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 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;

public GridAction(
final LocalDateTime start,
final Duration duration,
final Vessel vessel,
final Int2D cell
) {
super(start, duration, vessel);
this.cell = cell;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private Fishing(
final Duration duration,
final Vessel vessel
) {
super(start, duration, vessel);
super(start, duration, vessel, vessel.getCurrentCell());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@

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

import uk.ac.ox.poseidon.agents.behaviours.Action;
import sim.util.Int2D;
import uk.ac.ox.poseidon.agents.behaviours.GridAction;
import uk.ac.ox.poseidon.agents.vessels.Vessel;

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

public abstract class FishingAction extends Action {
public abstract class FishingAction extends GridAction {

public FishingAction(
final LocalDateTime start,
final Duration duration,
final Vessel vessel
final Vessel vessel,
final Int2D cell
) {
super(start, duration, vessel);
super(start, duration, vessel, cell);
}

public abstract double getTotalBiomassCaught();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,3 @@ public Bucket<Biomass> extract(final Bucket<Biomass> fishToExtract) {
}
}
}

12 changes: 12 additions & 0 deletions core/src/main/java/uk/ac/ox/poseidon/core/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.Serial;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -46,6 +47,7 @@ public class Simulation extends SimState {
private final TemporalSchedule temporalSchedule;
private final Scenario scenario;
private final long id = idCounter.getAndIncrement();
private final List<Runnable> finalProcesses = new ArrayList<>();
private boolean started = false;
private List<?> components;

Expand Down Expand Up @@ -91,6 +93,16 @@ public void start() {
started = true;
}

public void addFinalProcess(final Runnable process) {
finalProcesses.add(process);
}

@Override
public void finish() {
finalProcesses.forEach(Runnable::run);
super.finish();
}

@Override
public final int hashCode() {
return super.hashCode();
Expand Down
30 changes: 30 additions & 0 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,41 @@

plugins {
id("buildlogic.java-application-conventions")
id("com.google.protobuf") version "0.9.4"
}

dependencies {
implementation(project(":agents"))
implementation(project(":biology"))
implementation(project(":io"))
implementation(project(":gui"))

// The following are all necessary for gRPC
implementation("io.grpc:grpc-netty-shaded:1.68.1")
implementation("io.grpc:grpc-protobuf:1.68.1")
implementation("io.grpc:grpc-stub:1.68.1")
implementation("com.google.protobuf:protobuf-java:4.28.3")
implementation("javax.annotation:javax.annotation-api:1.3.2")
}

repositories {
mavenCentral()
}

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:4.28.3" // Specify Protoc compiler
}
plugins {
create("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:1.68.1" // Specify gRPC plugin
}
}
generateProtoTasks {
all().forEach { task ->
task.plugins {
create("grpc")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,6 @@ public static void main(final String[] args) {
) {
simulation.schedule.step(simulation);
}
simulation.finish();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.examples.external;

import com.google.common.collect.ImmutableList;
import fishing.BiomassGridServiceGrpc;
import fishing.Fishing;
import lombok.RequiredArgsConstructor;
import sim.engine.SimState;
import sim.engine.Steppable;
import sim.util.Int2D;
import uk.ac.ox.poseidon.agents.behaviours.fishing.FishingAction;
import uk.ac.ox.poseidon.biology.biomass.BiomassGrid;
import uk.ac.ox.poseidon.core.events.Listener;

import java.util.List;

@RequiredArgsConstructor
public class ExternalBiomassGridProcess implements Steppable, Listener<FishingAction> {

private final BiomassGrid internalBiomassGrid;
private final BiomassGridServiceGrpc.BiomassGridServiceBlockingStub stub;
private ImmutableList.Builder<FishingAction> accumulator = new ImmutableList.Builder<>();

@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public void step(final SimState simState) {
// report catches to external service
stub.applyCatches(
Fishing.Catches
.newBuilder()
.addAllCatches(accumulator.build().stream()
.map(fishingAction -> Fishing.Catches.Catch
.newBuilder()
.setX(fishingAction.getCell().x)
.setY(fishingAction.getCell().y)
.setBiomassCaught(fishingAction.getTotalBiomassCaught())
.build())
::iterator
)
.build()
);
// reset fishing action accumulator
accumulator = new ImmutableList.Builder<>();
// apply growth process and update our internal grid
final List<Fishing.Grid.Column> cols = stub
.applyGrowth(Fishing.Void.getDefaultInstance())
.getColumnsList();
for (int x = 0; x < cols.size(); x++) {
final List<Double> col = cols.get(x).getValuesList();
for (int y = 0; y < col.size(); y++) {
internalBiomassGrid.setBiomass(new Int2D(x, y), col.get(y));
}
}
}

@Override
public void receive(final FishingAction event) {
accumulator.add(event);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.examples.external;

import fishing.BiomassGridServiceGrpc;
import fishing.Fishing;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import sim.util.Int2D;
import uk.ac.ox.poseidon.agents.behaviours.fishing.FishingAction;
import uk.ac.ox.poseidon.biology.biomass.BiomassGrid;
import uk.ac.ox.poseidon.core.Factory;
import uk.ac.ox.poseidon.core.Simulation;
import uk.ac.ox.poseidon.core.SimulationScopeFactory;
import uk.ac.ox.poseidon.geography.grids.GridExtent;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ExternalBiomassGridProcessFactory
extends SimulationScopeFactory<ExternalBiomassGridProcess> {

private String serverName;
private int serverPort;
private Factory<? extends BiomassGrid> internalBiomassGrid;
private double carryingCapacity;
private double growthRate;

@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
protected ExternalBiomassGridProcess newInstance(final Simulation simulation) {

final BiomassGrid internalGrid = internalBiomassGrid.get(simulation);
final GridExtent gridExtent = internalGrid.getGridExtent();
final var gridBuilder = Fishing.Grid.newBuilder();
for (int x = 0; x < gridExtent.getGridWidth(); x++) {
final var columnBuilder = Fishing.Grid.Column.newBuilder();
for (int y = 0; y < gridExtent.getGridHeight(); y++) {
columnBuilder.addValues(internalGrid.getValue(new Int2D(x, y)));
}
gridBuilder.addColumns(columnBuilder);
}

final ManagedChannel channel = ManagedChannelBuilder
.forAddress(serverName, serverPort)
.usePlaintext()
.build();

simulation.addFinalProcess(channel::shutdown);

final BiomassGridServiceGrpc.BiomassGridServiceBlockingStub stub =
BiomassGridServiceGrpc.newBlockingStub(
channel
);

stub.init(
Fishing.GridDef
.newBuilder()
.setGrid(gridBuilder)
.setCarryingCapacity(carryingCapacity)
.setGrowthRate(growthRate)
.build()
);

final ExternalBiomassGridProcess externalBiomassGridProcess =
new ExternalBiomassGridProcess(
internalGrid,
stub
);
simulation.getEventManager().addListener(
FishingAction.class,
externalBiomassGridProcess
);
return externalBiomassGridProcess;
}
}
Loading

0 comments on commit 6dfe878

Please sign in to comment.