From 1be64083c2c38eedeffc7cea9f51afbed4d46d28 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Mon, 26 Sep 2022 01:42:58 +0200 Subject: refactor: remove viatra dependency from store --- .../query/view/AbstractFilteredRelationView.java | 44 ++++++++++++++++++ .../store/query/view/FilteredRelationView.java | 52 ++++++++-------------- .../store/query/view/FunctionalRelationView.java | 35 ++++++++------- .../store/query/view/KeyOnlyRelationView.java | 14 +++--- .../refinery/store/query/view/RelationView.java | 52 ++++++++-------------- 5 files changed, 109 insertions(+), 88 deletions(-) create mode 100644 subprojects/store/src/main/java/tools/refinery/store/query/view/AbstractFilteredRelationView.java (limited to 'subprojects/store/src') diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/view/AbstractFilteredRelationView.java b/subprojects/store/src/main/java/tools/refinery/store/query/view/AbstractFilteredRelationView.java new file mode 100644 index 00000000..a21a38ba --- /dev/null +++ b/subprojects/store/src/main/java/tools/refinery/store/query/view/AbstractFilteredRelationView.java @@ -0,0 +1,44 @@ +package tools.refinery.store.query.view; + +import tools.refinery.store.model.Model; +import tools.refinery.store.model.Tuple; +import tools.refinery.store.model.Tuple.Tuple1; +import tools.refinery.store.model.representation.Relation; + +public abstract class AbstractFilteredRelationView extends RelationView { + protected AbstractFilteredRelationView(Relation representation, String name) { + super(representation, name); + } + + protected AbstractFilteredRelationView(Relation representation) { + super(representation); + } + + @Override + public Object[] forwardMap(Tuple key, D value) { + return toTuple1Array(key); + } + + @Override + public boolean get(Model model, Object[] tuple) { + int[] content = new int[tuple.length]; + for (int i = 0; i < tuple.length; i++) { + content[i] = ((Tuple1) tuple[i]).get(0); + } + Tuple key = Tuple.of(content); + D value = model.get(getRepresentation(), key); + return filter(key, value); + } + + public int getArity() { + return this.getRepresentation().getArity(); + } + + private static Object[] toTuple1Array(Tuple t) { + Object[] result = new Object[t.getSize()]; + for (int i = 0; i < t.getSize(); i++) { + result[i] = Tuple.of(t.get(i)); + } + return result; + } +} diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/view/FilteredRelationView.java b/subprojects/store/src/main/java/tools/refinery/store/query/view/FilteredRelationView.java index 3531195a..bb5d0237 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/query/view/FilteredRelationView.java +++ b/subprojects/store/src/main/java/tools/refinery/store/query/view/FilteredRelationView.java @@ -1,48 +1,34 @@ package tools.refinery.store.query.view; -import java.util.function.BiPredicate; - -import tools.refinery.store.model.Model; import tools.refinery.store.model.Tuple; -import tools.refinery.store.model.Tuple.Tuple1; import tools.refinery.store.model.representation.Relation; -public class FilteredRelationView extends RelationView{ - private final BiPredicate predicate; +import java.util.function.BiPredicate; +import java.util.function.Predicate; - public FilteredRelationView(Relation representation, BiPredicate predicate) { - super(representation); +public class FilteredRelationView extends AbstractFilteredRelationView { + private final BiPredicate predicate; + + public FilteredRelationView(Relation representation, String name, BiPredicate predicate) { + super(representation, name); this.predicate = predicate; } - @Override - protected Object[] forwardMap(Tuple key, D value) { - return toTuple1Array(key); - } - @Override - public boolean get(Model model, Object[] tuple) { - int[] content = new int[tuple.length]; - for(int i = 0; i representation, BiPredicate predicate) { + super(representation); + this.predicate = predicate; } - - public static Object[] toTuple1Array(Tuple t) { - Object[] result = new Object[t.getSize()]; - for(int i = 0; i representation, String name, Predicate predicate) { + this(representation, name, (k, v) -> predicate.test(v)); } - - @Override - public int getArity() { - return this.representation.getArity(); + + public FilteredRelationView(Relation representation, Predicate predicate) { + this(representation, (k, v) -> predicate.test(v)); } + @Override - protected boolean filter(Tuple key, D value) { + public boolean filter(Tuple key, D value) { return this.predicate.test(key, value); } } diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/view/FunctionalRelationView.java b/subprojects/store/src/main/java/tools/refinery/store/query/view/FunctionalRelationView.java index db9ba4b8..54baa050 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/query/view/FunctionalRelationView.java +++ b/subprojects/store/src/main/java/tools/refinery/store/query/view/FunctionalRelationView.java @@ -6,45 +6,48 @@ import tools.refinery.store.model.Tuple.Tuple1; import tools.refinery.store.model.representation.Relation; public class FunctionalRelationView extends RelationView { + public FunctionalRelationView(Relation representation, String name) { + super(representation, name); + } public FunctionalRelationView(Relation representation) { super(representation); } @Override - protected boolean filter(Tuple key, D value) { + public boolean filter(Tuple key, D value) { return true; } @Override - protected Object[] forwardMap(Tuple key, D value) { + public Object[] forwardMap(Tuple key, D value) { return toTuple1ArrayPlusValue(key, value); } @Override public boolean get(Model model, Object[] tuple) { - int[] content = new int[tuple.length-1]; - for(int i = 0; i Object[] toTuple1ArrayPlusValue(Tuple t, D value) { - Object[] result = new Object[t.getSize()+1]; - for(int i = 0; i Object[] toTuple1ArrayPlusValue(Tuple t, D value) { + Object[] result = new Object[t.getSize() + 1]; + for (int i = 0; i < t.getSize(); i++) { result[i] = Tuple.of(t.get(i)); } result[t.getSize()] = value; return result; } - - @Override - public int getArity() { - return this.representation.getArity()+1; - } } diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/view/KeyOnlyRelationView.java b/subprojects/store/src/main/java/tools/refinery/store/query/view/KeyOnlyRelationView.java index 6fa387a1..b9a9ed9f 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/query/view/KeyOnlyRelationView.java +++ b/subprojects/store/src/main/java/tools/refinery/store/query/view/KeyOnlyRelationView.java @@ -3,14 +3,18 @@ package tools.refinery.store.query.view; import tools.refinery.store.model.Tuple; import tools.refinery.store.model.representation.Relation; -public class KeyOnlyRelationView extends FilteredRelationView{ +public class KeyOnlyRelationView extends AbstractFilteredRelationView { + public static final String VIEW_NAME = "key"; + + private final Boolean defaultValue; public KeyOnlyRelationView(Relation representation) { - super(representation, (k,v)->true); + super(representation, VIEW_NAME); + defaultValue = representation.getDefaultValue(); } + @Override - protected boolean filter(Tuple key, Boolean value) { - return !value.equals(representation.getDefaultValue()); + public boolean filter(Tuple key, Boolean value) { + return !value.equals(defaultValue); } - } diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/view/RelationView.java b/subprojects/store/src/main/java/tools/refinery/store/query/view/RelationView.java index fd55eed4..b8a54046 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/query/view/RelationView.java +++ b/subprojects/store/src/main/java/tools/refinery/store/query/view/RelationView.java @@ -1,66 +1,51 @@ package tools.refinery.store.query.view; -import java.util.Objects; - -import org.eclipse.viatra.query.runtime.matchers.context.common.BaseInputKeyWrapper; - import tools.refinery.store.map.CursorAsIterator; import tools.refinery.store.model.Model; import tools.refinery.store.model.Tuple; import tools.refinery.store.model.representation.Relation; +import java.util.Objects; +import java.util.UUID; + /** * Represents a view of a {@link Relation} that can be queried. - * - * @author Oszkar Semerath * * @param + * @author Oszkar Semerath */ -public abstract class RelationView extends BaseInputKeyWrapper> { - protected final Relation representation; +public abstract class RelationView { + private final Relation representation; - protected RelationView(Relation representation) { - super(null); - this.wrappedKey = this; + private final String name; + + protected RelationView(Relation representation, String name) { this.representation = representation; + this.name = name; } - @Override - public String getPrettyPrintableName() { - return representation.getName(); + protected RelationView(Relation representation) { + this(representation, UUID.randomUUID().toString()); } - @Override - public String getStringID() { - return representation.getName() + this.getClass().getName(); - } + public abstract int getArity(); public Relation getRepresentation() { return representation; } - @Override - public boolean isEnumerable() { - return true; + public String getName() { + return representation.getName() + "#" + name; } - protected abstract boolean filter(Tuple key, D value); + public abstract boolean filter(Tuple key, D value); - protected abstract Object[] forwardMap(Tuple key, D value); + public abstract Object[] forwardMap(Tuple key, D value); public abstract boolean get(Model model, Object[] tuple); - - @SuppressWarnings("squid:S1168") - public Object[] transform(Tuple tuple, D value) { - if (filter(tuple, value)) { - return forwardMap(tuple, value); - } else - return null; - } public Iterable getAll(Model model) { - return (() -> new CursorAsIterator<>(model.getAll(representation), (k, v) -> forwardMap(k, v), - (k, v) -> filter(k, v))); + return (() -> new CursorAsIterator<>(model.getAll(representation), this::forwardMap, this::filter)); } @Override @@ -81,5 +66,4 @@ public abstract class RelationView extends BaseInputKeyWrapper other = ((RelationView) obj); return Objects.equals(representation, other.representation); } - } -- cgit v1.2.3-70-g09d2