From c2917609e15bc763878a09723ff69f68a6a08168 Mon Sep 17 00:00:00 2001 From: SeH <1s1e1h1@gmail.com> Date: Fri, 19 Jun 2015 00:01:42 -0400 Subject: [PATCH] fixes for blueprints compliance and removal of non-relevant code Change-Id: Ife05660ff8ae7285264fb9281cf01f18d17c2ff0 --- pom.xml | 5 - .../spangraph/ConcurrentHashMapGraph.java | 14 + .../com/syncleus/spangraph/HashMapGraph.java | 14 + .../com/syncleus/spangraph/InfiniPeer.java | 10 +- .../java/com/syncleus/spangraph/MapGraph.java | 71 ++- .../com/syncleus/spangraph/SpanGraph.java | 11 + .../spangraph/spacetime/EarthMap.java | 29 -- .../syncleus/spangraph/spacetime/OctBox.java | 479 ------------------ .../syncleus/spangraph/spacetime/OctMap.java | 177 ------- .../spangraph/spacetime/OctMapGraph.java | 50 -- .../syncleus/spangraph/spacetime/OctSet.java | 115 ----- .../spangraph/spacetime/PlanetMap.java | 31 -- .../spangraph/MapGraphBlueprintsTest.java | 4 +- .../spangraph/SpanGraphBlueprintsTest.java | 4 +- .../com/syncleus/spangraph/SpanGraphTest.java | 6 +- .../syncleus/spangraph/geom/OctMapTest.java | 109 ---- .../syncleus/spangraph/geom/OctreeTest.java | 86 ---- 17 files changed, 107 insertions(+), 1108 deletions(-) delete mode 100644 src/main/java/com/syncleus/spangraph/spacetime/EarthMap.java delete mode 100644 src/main/java/com/syncleus/spangraph/spacetime/OctBox.java delete mode 100644 src/main/java/com/syncleus/spangraph/spacetime/OctMap.java delete mode 100644 src/main/java/com/syncleus/spangraph/spacetime/OctMapGraph.java delete mode 100644 src/main/java/com/syncleus/spangraph/spacetime/OctSet.java delete mode 100644 src/main/java/com/syncleus/spangraph/spacetime/PlanetMap.java delete mode 100644 src/test/java/com/syncleus/spangraph/geom/OctMapTest.java delete mode 100644 src/test/java/com/syncleus/spangraph/geom/OctreeTest.java diff --git a/pom.xml b/pom.xml index 904b33e..0cace42 100644 --- a/pom.xml +++ b/pom.xml @@ -189,11 +189,6 @@ <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> - <dependency> - <groupId>com.syncleus.toxi</groupId> - <artifactId>toxi</artifactId> - <version>1.0.0-SNAPSHOT</version> - </dependency> <dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-embedded</artifactId> diff --git a/src/main/java/com/syncleus/spangraph/ConcurrentHashMapGraph.java b/src/main/java/com/syncleus/spangraph/ConcurrentHashMapGraph.java index 5c9d012..8bab610 100644 --- a/src/main/java/com/syncleus/spangraph/ConcurrentHashMapGraph.java +++ b/src/main/java/com/syncleus/spangraph/ConcurrentHashMapGraph.java @@ -16,7 +16,11 @@ */ package com.syncleus.spangraph; +import com.tinkerpop.blueprints.Edge; +import org.infinispan.util.concurrent.ConcurrentHashSet; + import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** @@ -43,4 +47,14 @@ public class ConcurrentHashMapGraph extends MapGraph { protected Map newVertexMap() { return new ConcurrentHashMap<>(); } + + @Override + protected Map<String, Set<Edge>> newVertexEdgeMap() { + return new ConcurrentHashMap<>(); + } + + @Override + protected Set<Edge> newEdgeSet(int size) { + return new ConcurrentHashSet<>(size); + } } diff --git a/src/main/java/com/syncleus/spangraph/HashMapGraph.java b/src/main/java/com/syncleus/spangraph/HashMapGraph.java index 3cd2ec6..d545a53 100644 --- a/src/main/java/com/syncleus/spangraph/HashMapGraph.java +++ b/src/main/java/com/syncleus/spangraph/HashMapGraph.java @@ -16,8 +16,12 @@ */ package com.syncleus.spangraph; +import com.tinkerpop.blueprints.Edge; + import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.Map; +import java.util.Set; /** * Uses java.util.LinkedHashMap to implement adjacency @@ -43,4 +47,14 @@ public class HashMapGraph extends MapGraph { protected Map newVertexMap() { return new LinkedHashMap<>(); } + + @Override + protected Map<String, Set<Edge>> newVertexEdgeMap() { + return new LinkedHashMap(); + } + + @Override + protected Set<Edge> newEdgeSet(int size) { + return new LinkedHashSet(size); + } } diff --git a/src/main/java/com/syncleus/spangraph/InfiniPeer.java b/src/main/java/com/syncleus/spangraph/InfiniPeer.java index 2880db8..1dd8576 100644 --- a/src/main/java/com/syncleus/spangraph/InfiniPeer.java +++ b/src/main/java/com/syncleus/spangraph/InfiniPeer.java @@ -44,7 +44,7 @@ import java.util.logging.Logger; /** * Peer-to-peer node manager : wraps CacheManager functionality * - * @see https://github.com/belaban/JGroups/tree/master/conf + * see: https://github.com/belaban/JGroups/tree/master/conf */ @Listener(sync = true) public class InfiniPeer extends DefaultCacheManager { @@ -66,11 +66,9 @@ public class InfiniPeer extends DefaultCacheManager { addListener(this); - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - InfiniPeer.this.stop(); - } - }); + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + InfiniPeer.this.stop(); + })); } diff --git a/src/main/java/com/syncleus/spangraph/MapGraph.java b/src/main/java/com/syncleus/spangraph/MapGraph.java index f513fe2..73ce7cc 100644 --- a/src/main/java/com/syncleus/spangraph/MapGraph.java +++ b/src/main/java/com/syncleus/spangraph/MapGraph.java @@ -35,7 +35,10 @@ import java.util.concurrent.ConcurrentHashMap; */ abstract public class MapGraph<X extends Serializable> implements Graph { - private static Map<String,MapGraph> global = new WeakValueHashMap<>(); + final private static Map<String,MapGraph> global = new WeakValueHashMap<>(); + + /** blank string used if null labels are allowed and none is specified */ + public static final String DEFAULT_EDGE_LABEL = ""; /** name of the graph combined with the peerID */ public final String id; @@ -89,6 +92,8 @@ abstract public class MapGraph<X extends Serializable> implements Graph { } + private boolean requireEdgeLabels = false; + public int vertexCount() { return vertexCollection().size(); } @@ -144,6 +149,10 @@ abstract public class MapGraph<X extends Serializable> implements Graph { return r; } + public void setRequireEdgeLabels(boolean requireEdgeLabels) { + this.requireEdgeLabels = requireEdgeLabels; + } + public MVertex<X> addVertex(Object id) { @@ -156,7 +165,7 @@ abstract public class MapGraph<X extends Serializable> implements Graph { } } - MVertex<X> vertex = new MVertex<X>((X) id, this); + MVertex<X> vertex = new MVertex<X>((X) id, this, newVertexEdgeMap(), newVertexEdgeMap()); this.vertices.put(vertex.id, vertex); return vertex; } @@ -277,9 +286,9 @@ abstract public class MapGraph<X extends Serializable> implements Graph { return addEdge(edgeID, o, i, null); } - public Edge addEdge(Object id, final Vertex outVertex, final Vertex inVertex, final String label) { - /*if (label == null) - throw ExceptionFactory.edgeLabelCanNotBeNull();*/ + public Edge addEdge(Object id, final Vertex outVertex, final Vertex inVertex, String label) { + if (requireEdgeLabels && label == null) + throw ExceptionFactory.edgeLabelCanNotBeNull(); @@ -288,9 +297,13 @@ abstract public class MapGraph<X extends Serializable> implements Graph { throw ExceptionFactory.edgeWithIdAlreadyExist(id); } } else { - id = newID(); + String nid = newID(); + id = nid; } + if (label == null) + label = DEFAULT_EDGE_LABEL; + MEdge<X> edge = new MEdge<X>((X)id, (MVertex)outVertex, (MVertex)inVertex, @@ -306,7 +319,7 @@ abstract public class MapGraph<X extends Serializable> implements Graph { public void removeEdge(final Edge edge) { - if (null == this.edges.remove(edge.getId().toString())) { + if (null == this.edges.remove(edge.getId())) { return; } @@ -314,12 +327,14 @@ abstract public class MapGraph<X extends Serializable> implements Graph { MVertex<X> outVertex = ((MEdge) edge).outVertex; MVertex<X> inVertex = ((MEdge) edge).inVertex; - Object edgeID = edge.getId(); + if (null != outVertex && null != outVertex.outEdges) { - outVertex.remove(edgeID, false); + if (!outVertex.remove(edge, true)) + throw new RuntimeException("edge " + edge + " unable not removed from outVertex + " + outVertex); } if (null != inVertex && null != inVertex.inEdges) { - inVertex.remove(edgeID, true); + if (!inVertex.remove(edge, false)) + throw new RuntimeException("edge " + edge + " unable not removed from inVertex + " + outVertex); } @@ -559,13 +574,20 @@ abstract public class MapGraph<X extends Serializable> implements Graph { } + abstract protected Map<String, Set<Edge>> newVertexEdgeMap(); + abstract protected Set<Edge> newEdgeSet(int size); + public static class MVertex<X extends Serializable> extends MElement<X> implements Vertex, Serializable { - public final Map<String, Set<Edge>> outEdges = new LinkedHashMap(); - public final Map<String, Set<Edge>> inEdges = new LinkedHashMap(); + public final Map<String, Set<Edge>> outEdges; + public final Map<String, Set<Edge>> inEdges; - protected MVertex(final X id, final MapGraph<X> graph) { + protected MVertex(final X id, final MapGraph<X> graph, + Map<String, Set<Edge>> iEdges, + Map<String, Set<Edge>> oEdges) { super(id, graph); + this.outEdges = oEdges; + this.inEdges = iEdges; } public Iterable<Edge> getEdges(final Direction direction, final String... labels) { @@ -632,7 +654,8 @@ abstract public class MapGraph<X extends Serializable> implements Graph { private Set<Edge> newEdgeSet(int size) { - return new LinkedHashSet<Edge>(size); + + return graph().newEdgeSet(size); } @@ -655,9 +678,13 @@ abstract public class MapGraph<X extends Serializable> implements Graph { } - public boolean remove(Object edgeID, boolean incoming) { + public boolean remove(Edge edge, boolean incoming) { Map<String, Set<Edge>> target = incoming ? outEdges : inEdges; - if (target.remove(edgeID)!=null) { + + Set<Edge> ss = target.get(edge.getLabel()); + if (ss == null) return false; + + if (ss.remove(edge)) { graph().update(this); return true; } @@ -705,6 +732,10 @@ abstract public class MapGraph<X extends Serializable> implements Graph { protected MEdge(final X id, final MVertex<X> outVertex, final MVertex<X> inVertex, final String label, final MapGraph<X> graph) { super(id, graph); + + if (label == null) + throw new RuntimeException("edge label can not be null"); + this.label = label; this.outVertex = outVertex; this.inVertex = inVertex; @@ -760,6 +791,14 @@ abstract public class MapGraph<X extends Serializable> implements Graph { return sb.append("->").append(getVertex(Direction.IN).getId()).append(']').toString(); } + @Override + public boolean hasProperty(String k, Object value) { + if (k.equals(StringFactory.LABEL)) + return label.equals(value); + + return super.hasProperty(k, value); + } + @Override protected void afterAdd(String key, Serializable value) { //this.graph.edgeKeyIndex.autoUpdate(key, value, oldValue, (InfiniEdge) this);*/ diff --git a/src/main/java/com/syncleus/spangraph/SpanGraph.java b/src/main/java/com/syncleus/spangraph/SpanGraph.java index 7300caf..26524bb 100644 --- a/src/main/java/com/syncleus/spangraph/SpanGraph.java +++ b/src/main/java/com/syncleus/spangraph/SpanGraph.java @@ -18,9 +18,12 @@ package com.syncleus.spangraph; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; +import org.infinispan.util.concurrent.ConcurrentHashSet; import java.io.Serializable; import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; /** * Blueprints Graph interface with adjacency implemented by Infinispan collections @@ -51,5 +54,13 @@ public class SpanGraph<X extends Serializable> extends MapGraph<X> { return newElementMap("v"); } + @Override + protected Map<String, Set<Edge>> newVertexEdgeMap() { + return new ConcurrentHashMap<>(); + } + @Override + protected Set<Edge> newEdgeSet(int size) { + return new ConcurrentHashSet(size); + } } diff --git a/src/main/java/com/syncleus/spangraph/spacetime/EarthMap.java b/src/main/java/com/syncleus/spangraph/spacetime/EarthMap.java deleted file mode 100644 index 27be638..0000000 --- a/src/main/java/com/syncleus/spangraph/spacetime/EarthMap.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Copyright: (c) Syncleus, Inc. - * - * You may redistribute and modify this source code under the terms and - * conditions of the Open Source Community License - Type C version 1.0 - * or any later version as published by Syncleus, Inc. at www.syncleus.com. - * There should be a copy of the license included with this file. If a copy - * of the license is not included you are granted no right to distribute or - * otherwise use this file except through a legal and valid license. You - * should also contact Syncleus, Inc. at the information below if you cannot - * find a license: - * - * Syncleus, Inc. - * 2604 South 12th Street - * Philadelphia, PA 19148 - */ -package com.syncleus.spangraph.spacetime; - -import com.syncleus.spangraph.InfiniPeer; -import com.syncleus.toxi.geom.Vec3D; -import com.syncleus.toxi.geom.XYZ; - -/** - * PlanetMap preconfigured for Earth's known dimensions and irregular sphericity */ -public class EarthMap<K extends XYZ, V> extends PlanetMap<K,V> { - public EarthMap(InfiniPeer p, String id, Vec3D center, Vec3D radius, Vec3D resolution) { - super(p, id, center, radius, resolution); - } -} diff --git a/src/main/java/com/syncleus/spangraph/spacetime/OctBox.java b/src/main/java/com/syncleus/spangraph/spacetime/OctBox.java deleted file mode 100644 index 56e54bb..0000000 --- a/src/main/java/com/syncleus/spangraph/spacetime/OctBox.java +++ /dev/null @@ -1,479 +0,0 @@ -/** - * Copyright: (c) Syncleus, Inc. - * - * You may redistribute and modify this source code under the terms and - * conditions of the Open Source Community License - Type C version 1.0 - * or any later version as published by Syncleus, Inc. at www.syncleus.com. - * There should be a copy of the license included with this file. If a copy - * of the license is not included you are granted no right to distribute or - * otherwise use this file except through a legal and valid license. You - * should also contact Syncleus, Inc. at the information below if you cannot - * find a license: - * - * Syncleus, Inc. - * 2604 South 12th Street - * Philadelphia, PA 19148 - */ -package com.syncleus.spangraph.spacetime; - -import com.syncleus.toxi.geom.*; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.function.Consumer; - - -public class OctBox<V extends XYZ> extends BB implements Shape3D { - - /** - * alternative tree recursion limit, number of world units when cells are - * not subdivided any further - */ - protected Vec3D resolution; - - public final OctBox parent; - - protected OctBox[] children; - - protected Collection<V> points; - - - /** - * Constructs a new AbstractOctree node within the AABB cube volume: {o.x, o.y, - * o.z} ... {o.x+size, o.y+size, o.z+size} - * - * @param p - * parent node - * @param o - * tree origin - * @param halfSize - * half length of the tree volume along a single axis - */ - private OctBox(OctBox p, Vec3D o, Vec3D halfSize) { - super(o.plus(halfSize), new Vec3D(halfSize)); - this.parent = p; - if (parent != null) { - resolution = parent.resolution; - } - - - } - - public int depth() { - if (parent == null) return 0; - return parent.depth() + 1; - } - - /** - * Constructs a new AbstractOctree node within the AABB cube volume: {o.x, o.y, - * o.z} ... {o.x+size, o.y+size, o.z+size} - * - * @param o - * tree origin - * size of the tree volume along a single axis - */ - public OctBox(Vec3D o, Vec3D extents, Vec3D resolution) { - this(null, o, extents); - this.resolution = resolution; - } - - - /** - * Adds all points of the collection to the octree. - * - * @param points - * point collection - * @return how many points were added - */ - public int putAll(final Collection<? extends V> points) { - int count = 0; - for (final V p : points) { - if (ADD(p)!=null) count++; - } - return count; - } - - //TODO memoize this result in a special leaf subclass - public boolean belowResolution() { - return extent.x() <= resolution.x || - extent.y() <= resolution.y || - extent.z() <= resolution.z; - } - /** - * Adds a new point/particle to the tree structure. All points are stored - * within leaf nodes only. The tree implementation is using lazy - * instantiation for all intermediate tree levels. - * - * @param p - * @return the box it was inserted to, or null if wasn't - */ - public OctBox<V> ADD(final V p) { - - - // check if point is inside cube - if (containsPoint(p)) { - // only add points to leaves for now - if (belowResolution()) { - if (points == null) { - points = newPointsCollection(); - } - points.add(p); - return this; - } else { - if (children == null) { - children = new OctBox[8]; - } - int octant = getOctantID(p); - if (children[octant] == null) { - Vec3D off = new Vec3D( - minX() + ((octant & 1) != 0 ? extent.x() : 0), - minY() + ((octant & 2) != 0 ? extent.y() : 0), - minZ() + ((octant & 4) != 0 ? extent.z() : 0)); - children[octant] = new OctBox(this, off, - extent.scale(0.5f)); - } - return children[octant].ADD(p); - } - } - return null; - } - - protected Collection<V> newPointsCollection() { - return new ArrayList(); - } - - - /** - * Applies the given {@link OctreeVisitor} implementation to this node and - * all of its children. - */ - public void forEachInBox(Consumer<OctBox> visitor) { - visitor.accept(this); - if (children!=null) { - for (OctBox c : children) { - if (c != null) { - c.forEachInBox(visitor); - } - } - } - } - - - - public boolean containsPoint(XYZ p) { - return p.isInAABB(this); - } - - public void clear() { - zero(); - children = null; - points = null; - } - - /** - * @return a copy of the child nodes array - */ - public OctBox[] getChildrenCopy() { - if (children != null) { - OctBox[] clones = new OctBox[8]; - System.arraycopy(children, 0, clones, 0, 8); - return clones; - } - return null; - } - - - - /** - * Finds the leaf node which spatially relates to the given point - * - * @return leaf node or null if point is outside the tree dimensions - */ - public OctBox getLeafForPoint(XYZ p) { - // if not a leaf node... - if (p.isInAABB(this)) { - if (children!=null) { - int octant = getOctantID(p); - if (children[octant] != null) { - return children[octant].getLeafForPoint(p); - } - } else if (points != null) { - return this; - } - } - return null; - } - - - - /** - * Returns the minimum size of nodes (in world units). This value acts as - * tree recursion limit since nodes smaller than this size are not - * subdivided further. Leaf node are always smaller or equal to this size. - * - * @return the minimum size of tree nodes - */ - public Vec3D getResolution() { - return resolution; - } - - - /** - * Computes the local child octant/cube index for the given point - * - * @param plocal - * point in the node-local coordinate system - * @return octant index - */ - protected final int getOctantID(final Vec3D plocal) { - final XYZ h = this.extent; - - return (plocal.x >= h.x() ? 1 : 0) + (plocal.y >= h.y() ? 2 : 0) - + (plocal.z >= h.z() ? 4 : 0); - } - - /** computes getOctantID for the point subtracted by another point, - * without needing to allocate a temporary object - - */ - private int getOctantID(final XYZ p) { - //final XYZ h = this.extent; - return ((p.x() - x) >= 0 ? 1 : 0) + ((p.y() - y) >= 0 ? 2 : 0) - + ((p.z() - z) >= 0 ? 4 : 0); - } - - - - /** - * @return the parent - */ - public OctBox getParent() { - return parent; - } - - public Collection<V> getPoints() { - if (points == null) return Collections.EMPTY_LIST; - return points; - } - - public int countPointsRecursively() { - final int[] x = {0}; - forEachInBox(n -> x[0] += n.countPoints()); - return x[0]; - } - - public int countPoints() { - if (points == null) return 0; - return points.size(); - } - - public List<V> getPointsRecursively() { - return getPointsRecursively(new ArrayList()); - } - - /** - * @return the points - */ - public List<V> getPointsRecursively(List<V> results) { - if (points != null) { - results.addAll(points); - } else if (children!=null) { - for (int i = 0; i < 8; i++) { - if (children[i] != null) { - children[i].getPointsRecursively(results); - } - } - } - return results; - } - - /** - * Selects all stored points within the given axis-aligned bounding box. - * - * @param b - * AABB - * @return all points with the box volume - */ - @Deprecated public List<XYZ> getPointsWithinBox(BB b) { - ArrayList<XYZ> results = null; - if (this.intersectsBox(b)) { - if (points != null) { - for (XYZ q : points) { - if (q.isInAABB(b)) { - if (results == null) { - results = new ArrayList<XYZ>(); - } - results.add(q); - } - } - } else if (children!=null) { - for (int i = 0; i < 8; i++) { - if (children[i] != null) { - List<XYZ> points = children[i].getPointsWithinBox(b); - if (points != null) { - if (results == null) { - results = new ArrayList(); - } - results.addAll(points); - } - } - } - } - } - return results; - } - - public void forEachInBox(BB b, Consumer<XYZ> c) { - if (this.intersectsBox(b)) { - if (points != null) { - for (XYZ q : points) { - if (q.isInAABB(b)) { - c.accept(q); - } - } - } else if (children!=null) { - for (int i = 0; i < 8; i++) { - if (children[i] != null) { - children[i].forEachInBox(b, c); - } - } - } - } - } - - public void forEachNeighbor(V item, XYZ boxRadius, Consumer<OctBox> visitor) { - //SOON - throw new UnsupportedOperationException(); - } - - public void forEachInSphere(Sphere s, Consumer<XYZ> c) { - - if (this.intersectsSphere(s)) { - if (points != null) { - for (XYZ q : points) { - if (s.containsPoint(q)) { - c.accept(q); - } - } - } else if (children!=null) { - for (int i = 0; i < 8; i++) { - OctBox cc = children[i]; - if (cc != null) { - cc.forEachInSphere(s, c); - } - } - } - } - } - - - /** - * Selects all stored points within the given sphere volume - * - * @param s - * sphere - * @return selected points - */ - @Deprecated public List<XYZ> getPointsWithinSphere(Sphere s) { - ArrayList<XYZ> results = null; - if (this.intersectsSphere(s)) { - if (points != null) { - for (XYZ q : points) { - if (s.containsPoint(q)) { - if (results == null) { - results = new ArrayList(); - } - results.add(q); - } - } - } else if (children!=null) { - for (int i = 0; i < 8; i++) { - if (children[i] != null) { - List<XYZ> points = children[i].getPointsWithinSphere(s); - if (points != null) { - if (results == null) { - results = new ArrayList(); - } - results.addAll(points); - } - } - } - } - } - return results; - } - - - - /** - * Selects all stored points within the given sphere volume - * - * @param sphereOrigin - * @param clipRadius - * @return selected points - */ - public void forEachInSphere(Vec3D sphereOrigin, float clipRadius, Consumer<XYZ> c) { - forEachInSphere(new Sphere(sphereOrigin, clipRadius), c); - } - - - - private void reduceBranch() { - if (points != null && points.size() == 0) { - points = null; - } - if (children!=null) { - for (int i = 0; i < 8; i++) { - if (children[i] != null && children[i].points == null) { - children[i] = null; - } - } - } - if (parent != null) { - parent.reduceBranch(); - } - } - - /** - * Removes a point from the tree and (optionally) tries to release memory by - * reducing now empty sub-branches. - * - * @return true, if the point was found & removed - */ - public boolean remove(Object _p) { - boolean found = false; - V p = (V)_p; - OctBox leaf = getLeafForPoint(p); - if (leaf != null) { - if (leaf.points.remove(p)) { - found = true; - if (leaf.points.size() == 0) { - leaf.reduceBranch(); - } - } - } - return found; - } - - public boolean removeAll(Collection<?> points) { - boolean allRemoved = true; - for (Object p : points) { - allRemoved &= remove(p); - } - return allRemoved; - } - - /* - * (non-Javadoc) - * - * @see toxi.geom.AABB#toString() - */ - public String toString() { - String x = "<OctBox @" + super.toString() + '>'; - if (points!=null) - x += "=" + points.toString(); - return x; - } -} diff --git a/src/main/java/com/syncleus/spangraph/spacetime/OctMap.java b/src/main/java/com/syncleus/spangraph/spacetime/OctMap.java deleted file mode 100644 index 9d16464..0000000 --- a/src/main/java/com/syncleus/spangraph/spacetime/OctMap.java +++ /dev/null @@ -1,177 +0,0 @@ -/** - * Copyright: (c) Syncleus, Inc. - * - * You may redistribute and modify this source code under the terms and - * conditions of the Open Source Community License - Type C version 1.0 - * or any later version as published by Syncleus, Inc. at www.syncleus.com. - * There should be a copy of the license included with this file. If a copy - * of the license is not included you are granted no right to distribute or - * otherwise use this file except through a legal and valid license. You - * should also contact Syncleus, Inc. at the information below if you cannot - * find a license: - * - * Syncleus, Inc. - * 2604 South 12th Street - * Philadelphia, PA 19148 - */ -package com.syncleus.spangraph.spacetime; - - -import com.syncleus.spangraph.InfiniPeer; -import org.infinispan.Cache; -import com.syncleus.toxi.geom.Vec3D; -import com.syncleus.toxi.geom.XYZ; - -import java.util.Collection; -import java.util.Map; -import java.util.Set; -import java.util.logging.Logger; - -/** TODO extract infinispan to subclass allowing any Map impl for index */ -public class OctMap<K extends XYZ, V> implements Map<K,V> { - - private final Logger logger; - - /** holder for _oct for infinispan persistence */ - protected final Cache<Long, OctBox<K>> _oct; - - protected final Cache<K, V> map; - protected final OctBox<K> box; - - boolean startupCheck = true; - - public OctMap(InfiniPeer p, String id, Vec3D center, Vec3D radius, Vec3D resolution) { - - this.logger = Logger.getLogger(OctMap.class.getSimpleName() + ":" + id); - - this.map = p.the(id); - this._oct = p.the(id + ".oct"); - - if (_oct.isEmpty()) { - OctBox newBox = this.box = new OctBox(center, radius, resolution); - _oct.put(0L, newBox); - logger.info("new octree created: " + box); - } - else { - this.box = _oct.get(0L); - if (this.box == null) { - throw new RuntimeException("Unable to load persisted OctBox:" + this._oct); - } - logger.info("existing octbox loaded: " + box); - - - if (startupCheck) { - if (!validate()) { - reindex(); - flush(); - } - } - } - } - - - @Override - public int size() { - return map.size(); - } - - @Override - public boolean isEmpty() { - return map.isEmpty(); - } - - @Override - public boolean containsKey(Object key) { - return map.containsKey(key); - } - - @Override - public boolean containsValue(Object value) { - return map.containsValue(value); - } - - @Override - public V get(Object key) { - return map.get(key); - } - - @Override - public V put(K key, V value) { - V removed = map.put(key, value); - if (removed!=null) { - octRemove(key, removed); - } - if (box.ADD(key)==null) { - throw new RuntimeException("Octree rejected value=" + value + ", key=" + key ); - } - return removed; - } - - private void octRemove(K key, V v) { - if (!box.remove(key)) { - throw new RuntimeException("Octree inconsistency detected on removal key=" + key + ", value=" + v); - } - } - - @Override - public V remove(Object key) { - V v = map.remove(key); - if (v!=null) { - octRemove((K)key, v); - } - return v; - } - - @Override - public void putAll(Map<? extends K, ? extends V> m) { - map.putAll(m); - box.putAll(m.keySet()); - } - - @Override - public void clear() { - map.clear(); - box.zero(); - } - - @Override - public Set<K> keySet() { - return map.keySet(); - } - - @Override - public Collection<V> values() { - return map.values(); - } - - @Override - public Set<Entry<K, V>> entrySet() { - return map.entrySet(); - } - - public void reindex() { - logger.info("re-indexing " + map.size() + " items"); - box.zero(); - box.putAll(map.keySet()); - - validate(); - } - - /** manually flush the octree to persistence */ - public boolean flush() { - _oct.put(0L, box); - return validate(); - } - - public boolean validate() { - int e = box.countPointsRecursively(); - int msize = map.size(); - boolean consistent = (e == msize); - logger.info("octbox contains " + e + " entries. consistent with map=" + msize + " is " + consistent); - return consistent; - } - - public OctBox box() { - return box; - } -} diff --git a/src/main/java/com/syncleus/spangraph/spacetime/OctMapGraph.java b/src/main/java/com/syncleus/spangraph/spacetime/OctMapGraph.java deleted file mode 100644 index 82fbea1..0000000 --- a/src/main/java/com/syncleus/spangraph/spacetime/OctMapGraph.java +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright: (c) Syncleus, Inc. - * - * You may redistribute and modify this source code under the terms and - * conditions of the Open Source Community License - Type C version 1.0 - * or any later version as published by Syncleus, Inc. at www.syncleus.com. - * There should be a copy of the license included with this file. If a copy - * of the license is not included you are granted no right to distribute or - * otherwise use this file except through a legal and valid license. You - * should also contact Syncleus, Inc. at the information below if you cannot - * find a license: - * - * Syncleus, Inc. - * 2604 South 12th Street - * Philadelphia, PA 19148 - */ -package com.syncleus.spangraph.spacetime; - -import com.syncleus.spangraph.MapGraph; -import com.tinkerpop.blueprints.Edge; -import com.tinkerpop.blueprints.Vertex; -import com.syncleus.toxi.geom.XYZ; - -/** - * OctMapped indexes vertices in a Graph - */ -abstract public class OctMapGraph<X extends XYZ> extends MapGraph<X> { - - public interface DistanceFunction3 { - - public float get(XYZ a, XYZ b); - - - /** allows an implementation to use early termination when - * exceeding a minimum value than completely calculating the result - */ - default public boolean lessThan(XYZ a, XYZ b, float minimum) { - return get(a, b) < minimum; - } - - } - - abstract protected OctMap<X, Edge> newEdgeMap(); - - abstract protected OctMap<X, Vertex> newVertexMap(); - - - - -} diff --git a/src/main/java/com/syncleus/spangraph/spacetime/OctSet.java b/src/main/java/com/syncleus/spangraph/spacetime/OctSet.java deleted file mode 100644 index a3164a4..0000000 --- a/src/main/java/com/syncleus/spangraph/spacetime/OctSet.java +++ /dev/null @@ -1,115 +0,0 @@ -/** - * Copyright: (c) Syncleus, Inc. - * - * You may redistribute and modify this source code under the terms and - * conditions of the Open Source Community License - Type C version 1.0 - * or any later version as published by Syncleus, Inc. at www.syncleus.com. - * There should be a copy of the license included with this file. If a copy - * of the license is not included you are granted no right to distribute or - * otherwise use this file except through a legal and valid license. You - * should also contact Syncleus, Inc. at the information below if you cannot - * find a license: - * - * Syncleus, Inc. - * 2604 South 12th Street - * Philadelphia, PA 19148 - */ -package com.syncleus.spangraph.spacetime; - -import com.syncleus.toxi.geom.Vec3D; -import com.syncleus.toxi.geom.XYZ; - -import java.util.*; - - -public class OctSet<V extends XYZ> extends OctBox<V> implements Set<V> { - - - final Map<V, OctBox<V>> data = new HashMap(); - - public OctSet(Vec3D o, Vec3D extents, Vec3D resolution) { - super(o, extents, resolution); - } - - @Override - public OctBox<V> ADD(V p) { - OctBox<V> target = super.ADD(p); - if (target!=null) { - data.put(p, target); - return target; - } - return null; - } - - @Override - public boolean remove(Object p) { - //TODO use the value in data for fast access - if (data.remove(p)!=null) { - super.remove(p); - return true; - } - return false; - } - - @Override - public int size() { - return data.size(); - } - - @Override - public boolean isEmpty() { - return data.isEmpty(); - } - - @Override - public boolean contains(Object o) { - return data.containsKey(o); - } - - @Override - public Iterator<V> iterator() { - return data.keySet().iterator(); - } - - @Override - public Object[] toArray() { - return data.keySet().toArray(); - } - - @Override - public <T> T[] toArray(T[] a) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean add(V v) { - if (ADD(v)!=null) { - return true; - } - return false; - } - - - - @Override - public boolean containsAll(Collection<?> c) { - return data.keySet().containsAll(c); - } - - @Override - public boolean addAll(Collection<? extends V> c) { - return putAll(c) == c.size(); - } - - @Override - public boolean retainAll(Collection<?> c) { - //SOON - throw new UnsupportedOperationException(); - } - - @Override - public boolean removeAll(Collection<?> points) { - //SOON - throw new UnsupportedOperationException(); - } -} diff --git a/src/main/java/com/syncleus/spangraph/spacetime/PlanetMap.java b/src/main/java/com/syncleus/spangraph/spacetime/PlanetMap.java deleted file mode 100644 index fa44443..0000000 --- a/src/main/java/com/syncleus/spangraph/spacetime/PlanetMap.java +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Copyright: (c) Syncleus, Inc. - * - * You may redistribute and modify this source code under the terms and - * conditions of the Open Source Community License - Type C version 1.0 - * or any later version as published by Syncleus, Inc. at www.syncleus.com. - * There should be a copy of the license included with this file. If a copy - * of the license is not included you are granted no right to distribute or - * otherwise use this file except through a legal and valid license. You - * should also contact Syncleus, Inc. at the information below if you cannot - * find a license: - * - * Syncleus, Inc. - * 2604 South 12th Street - * Philadelphia, PA 19148 - */ -package com.syncleus.spangraph.spacetime; - -import com.syncleus.spangraph.InfiniPeer; -import com.syncleus.toxi.geom.Vec3D; -import com.syncleus.toxi.geom.XYZ; - -/** - * OctMap with special handling for planetary coordinates and spheroid geometry. - * 1st dimensions is Latitude, 2nd dimension is Longitude, 3rd dimension is time */ -public class PlanetMap<K extends XYZ, V> extends OctMap<K,V> { - - public PlanetMap(InfiniPeer p, String id, Vec3D center, Vec3D radius, Vec3D resolution) { - super(p, id, center, radius, resolution); - } -} diff --git a/src/test/java/com/syncleus/spangraph/MapGraphBlueprintsTest.java b/src/test/java/com/syncleus/spangraph/MapGraphBlueprintsTest.java index 9661c6f..cdc4565 100644 --- a/src/test/java/com/syncleus/spangraph/MapGraphBlueprintsTest.java +++ b/src/test/java/com/syncleus/spangraph/MapGraphBlueprintsTest.java @@ -81,7 +81,9 @@ public class MapGraphBlueprintsTest extends GraphTest { // } public Graph generateGraph() { - return new ConcurrentHashMapGraph(); + ConcurrentHashMapGraph g = new ConcurrentHashMapGraph(); + g.setRequireEdgeLabels(true); //for compliance + return g; } @Override diff --git a/src/test/java/com/syncleus/spangraph/SpanGraphBlueprintsTest.java b/src/test/java/com/syncleus/spangraph/SpanGraphBlueprintsTest.java index f2253f7..b5558d3 100644 --- a/src/test/java/com/syncleus/spangraph/SpanGraphBlueprintsTest.java +++ b/src/test/java/com/syncleus/spangraph/SpanGraphBlueprintsTest.java @@ -34,7 +34,9 @@ public class SpanGraphBlueprintsTest extends MapGraphBlueprintsTest { public static int n = 0; public Graph generateGraph() { - return new SpanGraph("test" + (n++), local); + MapGraph g = new SpanGraph("test" + (n++), local); + g.setRequireEdgeLabels(true); //for compliance + return g; } } diff --git a/src/test/java/com/syncleus/spangraph/SpanGraphTest.java b/src/test/java/com/syncleus/spangraph/SpanGraphTest.java index f54873b..1a9540a 100644 --- a/src/test/java/com/syncleus/spangraph/SpanGraphTest.java +++ b/src/test/java/com/syncleus/spangraph/SpanGraphTest.java @@ -43,7 +43,7 @@ public class SpanGraphTest { ); } - public void testVertexPropagation(Function<String, SpanGraph> graph) throws InterruptedException { + public static void testVertexPropagation(Function<String, SpanGraph> graph) throws InterruptedException { final AtomicReference<SpanGraph> bRef = new AtomicReference(null); @@ -54,7 +54,7 @@ public class SpanGraphTest { Vertex vy = a.addVertex("y"); assertEquals("correct vertex id", vx.getId(), "x"); assertEquals("correct vertex id", vy.getId(), "y"); - assertEquals("non-string vertex id", ((MapGraph.MVertex) a.addVertex(17)).getId(), 17); + assertEquals("non-string vertex id", a.addVertex(17).getId(), 17); assertEquals(3, a.vertexCount()); Thread x = new Thread(() -> { @@ -90,7 +90,7 @@ public class SpanGraphTest { assertEquals(1, a.edgeCount()); assertEquals(0, a.differentEdges(b).size()); assertEquals(0, b.differentEdges(a).size()); - assertEquals("Graphs:\n" + a.toString() + "\n" + b.toString() + "\n", a, b); + assertEquals("Graphs:\n" + a.toString() + '\n' + b.toString() + '\n', a, b); } diff --git a/src/test/java/com/syncleus/spangraph/geom/OctMapTest.java b/src/test/java/com/syncleus/spangraph/geom/OctMapTest.java deleted file mode 100644 index 529f5d4..0000000 --- a/src/test/java/com/syncleus/spangraph/geom/OctMapTest.java +++ /dev/null @@ -1,109 +0,0 @@ -/** - * Copyright: (c) Syncleus, Inc. - * - * You may redistribute and modify this source code under the terms and - * conditions of the Open Source Community License - Type C version 1.0 - * or any later version as published by Syncleus, Inc. at www.syncleus.com. - * There should be a copy of the license included with this file. If a copy - * of the license is not included you are granted no right to distribute or - * otherwise use this file except through a legal and valid license. You - * should also contact Syncleus, Inc. at the information below if you cannot - * find a license: - * - * Syncleus, Inc. - * 2604 South 12th Street - * Philadelphia, PA 19148 - */ -package com.syncleus.spangraph.geom; - -import com.syncleus.spangraph.InfiniPeer; -import com.syncleus.spangraph.spacetime.OctMap; -import org.junit.Test; -import com.syncleus.toxi.geom.Vec3D; -import com.syncleus.toxi.geom.XYZ; - -import java.io.Serializable; - -import static org.junit.Assert.assertTrue; - -/** - * Created by me on 6/14/15. - */ -public class OctMapTest { - - public static class Event implements XYZ, Serializable { - - public final String name; - private final float lon, lat, year; - - public Event(String name, float lon, float lat, float year) { - this.name = name; - this.lon = lon; - this.lat = lat; - this.year = year; - } - - @Override - public boolean equals(Object that) { - if (that == this) return true; - return name.equals(((Event)that).name); - } - - @Override - public int hashCode() { - return name.hashCode(); - } - - @Override - public String toString() { - return "Event[\'" + name + "\']"; - } - - @Override - public float x() { - return lon; - } - - @Override - public float y() { - return lat; - } - - @Override - public float z() { - return year; - } - } - - @Test - public void test1() { - InfiniPeer p = InfiniPeer.local("i", "/tmp/t", 1024); - OctMap<Event,String> o = new OctMap<>(p, "octmap", - new Vec3D(-180f,-90f,0f ), /* AD */ - new Vec3D(180f,90f,2100f ), /* 0AD .. 2100AD */ - new Vec3D(1f,0.75f,2f) - ); - - o.clear(); - - Event[] ee = new Event[] { - new Event("Beginning of Common Era", 75f, -20f, 0), - new Event("Invasion of Western Hemisphere", 65f, 34f, 1492f), - new Event("Monolith Discovered", -50f, 40f, 2001f), - new Event("Earth Destroyed", 0, 10f, 2015f)}; - - for (Event e : ee) { - o.put(e, ""); - } - - System.out.println(o); - o.box().forEachInBox(x -> System.out.println(x)); - - - assertTrue(o.validate()); - - - o.flush(); - p.stop(); - } -} diff --git a/src/test/java/com/syncleus/spangraph/geom/OctreeTest.java b/src/test/java/com/syncleus/spangraph/geom/OctreeTest.java deleted file mode 100644 index cbd4d87..0000000 --- a/src/test/java/com/syncleus/spangraph/geom/OctreeTest.java +++ /dev/null @@ -1,86 +0,0 @@ -/** - * Copyright: (c) Syncleus, Inc. - * - * You may redistribute and modify this source code under the terms and - * conditions of the Open Source Community License - Type C version 1.0 - * or any later version as published by Syncleus, Inc. at www.syncleus.com. - * There should be a copy of the license included with this file. If a copy - * of the license is not included you are granted no right to distribute or - * otherwise use this file except through a legal and valid license. You - * should also contact Syncleus, Inc. at the information below if you cannot - * find a license: - * - * Syncleus, Inc. - * 2604 South 12th Street - * Philadelphia, PA 19148 - */ -package com.syncleus.spangraph.geom; - -import com.syncleus.spangraph.spacetime.OctBox; -import org.junit.Test; -import com.syncleus.toxi.geom.AABB; -import com.syncleus.toxi.geom.BB; -import com.syncleus.toxi.geom.Vec3D; - -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * Created by me on 6/13/15. - */ -public class OctreeTest { - @Test - public void test1() { - OctBox o = new OctBox( - new Vec3D(-2f, -2f, -2f), - new Vec3D(4f, 4f, 4f), - new Vec3D(0.05f, 0.05f, 0.05f)); - - assertEquals(0, o.countPointsRecursively()); - - OctBox block = o.ADD(new Vec3D(3, 3, 3)); - assertTrue(block!=null); - assertEquals(1, o.countPointsRecursively()); - - o.ADD(new Vec3D(0, 1, 0)); - o.ADD(new Vec3D(0, 1, 0)); - o.ADD(new Vec3D(0, 0, 1)); - o.ADD(new Vec3D(0, 0, 1.25f)); - o.ADD(new Vec3D(0, 0, 1.5f)); - o.ADD(new Vec3D(0, 0, -1)); - o.ADD(new Vec3D(0, 0, -1.25f)); - o.ADD(new Vec3D(0, 0, -1.50f)); - o.ADD(new Vec3D(0, 0, -1.55f)); - o.ADD(new Vec3D(0, 0, -1.575f)); - - o.forEachInBox(x -> { - - List p = (((OctBox) x).getPointsRecursively()); - //if (!p.isEmpty()) - //System.out.println(x + " " + p); - }); - - //System.out.println("size: " + o.getNumChildren()); - - assertEquals(o.countPointsRecursively(), 11); - - int[] sphereCount = new int[1]; - o.forEachInSphere(new Vec3D(0, 0, -0.75f), 0.5f, x -> { - sphereCount[0]++; - }); - assertEquals(2, sphereCount[0]); - - int[] boxCount = new int[1]; - - BB BB = new AABB(new Vec3D(0f, -0.5f, -2.0f), new Vec3D(0.5f, 0.5f, 0.5f)); - o.forEachInBox(BB, x -> { - boxCount[0]++; - }); - assertEquals(3, boxCount[0]); - - //o.print_tree(); - - } -} -- GitLab