Skip to content

Commit

Permalink
add predicate operators for regulations
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Feb 5, 2025
1 parent 98535b5 commit bf1f0df
Show file tree
Hide file tree
Showing 15 changed files with 940 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.regulations;

import lombok.RequiredArgsConstructor;
import uk.ac.ox.poseidon.agents.behaviours.Action;

import java.util.function.Predicate;

@RequiredArgsConstructor
public class PermittedIf implements Regulations {

private final Predicate<Action> actionPredicate;

@Override
public boolean isPermitted(final Action action) {
return actionPredicate.test(action);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.regulations;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import uk.ac.ox.poseidon.agents.behaviours.Action;
import uk.ac.ox.poseidon.core.GlobalScopeFactory;
import uk.ac.ox.poseidon.core.Simulation;

import java.util.function.Predicate;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class PermittedIfFactory extends GlobalScopeFactory<PermittedIf> {

private GlobalScopeFactory<? extends Predicate<Action>> actionPredicate;

@Override
protected PermittedIf newInstance(final Simulation simulation) {
return new PermittedIf(actionPredicate.get(simulation));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@

@FunctionalInterface
public interface Regulations {

boolean isPermitted(Action action);

default boolean isForbidden(final Action action) {
return !isPermitted(action);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.regulations.predicates.operators;

import com.google.common.collect.ImmutableList;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import uk.ac.ox.poseidon.agents.behaviours.Action;

import java.util.function.Predicate;

@Getter
@RequiredArgsConstructor
public class AllOf implements Predicate<Action> {

@NonNull
private final ImmutableList<Predicate<Action>> predicates;

@Override
public boolean test(final Action action) {
return predicates.stream().allMatch(predicate -> predicate.test(action));
}
}
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.regulations.predicates.operators;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import uk.ac.ox.poseidon.agents.behaviours.Action;
import uk.ac.ox.poseidon.core.Factory;
import uk.ac.ox.poseidon.core.GlobalScopeFactory;
import uk.ac.ox.poseidon.core.Simulation;

import java.util.List;
import java.util.function.Predicate;

import static com.google.common.collect.ImmutableList.toImmutableList;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class AllOfFactory extends GlobalScopeFactory<AllOf> {

List<Factory<? extends Predicate<Action>>> predicates;

@Override
protected AllOf newInstance(final Simulation simulation) {
return new AllOf(
predicates
.stream()
.map(p -> p.get(simulation))
.collect(toImmutableList())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.regulations.predicates.operators;

import com.google.common.collect.ImmutableList;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import uk.ac.ox.poseidon.agents.behaviours.Action;

import java.util.function.Predicate;

@Getter
@RequiredArgsConstructor
public class AnyOf implements Predicate<Action> {

@NonNull
private final ImmutableList<Predicate<Action>> predicates;

@Override
public boolean test(final Action action) {
return predicates.stream().anyMatch(predicate -> predicate.test(action));
}
}
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.regulations.predicates.operators;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import uk.ac.ox.poseidon.agents.behaviours.Action;
import uk.ac.ox.poseidon.core.Factory;
import uk.ac.ox.poseidon.core.GlobalScopeFactory;
import uk.ac.ox.poseidon.core.Simulation;

import java.util.List;
import java.util.function.Predicate;

import static com.google.common.collect.ImmutableList.toImmutableList;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class AnyOfFactory extends GlobalScopeFactory<AnyOf> {

List<Factory<? extends Predicate<Action>>> predicates;

@Override
protected AnyOf newInstance(final Simulation simulation) {
return new AnyOf(
predicates
.stream()
.map(p -> p.get(simulation))
.collect(toImmutableList())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.regulations.predicates.operators;

import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import uk.ac.ox.poseidon.agents.behaviours.Action;

import java.util.function.Predicate;

@Getter
@RequiredArgsConstructor
public class Not implements Predicate<Action> {

@NonNull
private final Predicate<Action> predicate;

@Override
public boolean test(final Action action) {
return !predicate.test(action);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.regulations.predicates.operators;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import uk.ac.ox.poseidon.agents.behaviours.Action;
import uk.ac.ox.poseidon.core.Factory;
import uk.ac.ox.poseidon.core.GlobalScopeFactory;
import uk.ac.ox.poseidon.core.Simulation;

import java.util.function.Predicate;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class NotFactory extends GlobalScopeFactory<Not> {

private Factory<? extends Predicate<Action>> predicate;

@Override
protected Not newInstance(final Simulation simulation) {
return new Not(predicate.get(simulation));
}
}
Loading

0 comments on commit bf1f0df

Please sign in to comment.