From eb12cceac0060e66171c00aa3d608c635c29ad47 Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Sat, 26 Mar 2016 04:12:37 +0100 Subject: [PATCH 01/20] WIP: rewriting everything with inner classes --- build.sbt | 4 +- src/main/java/com/bio4j/angulillos/Arity.java | 79 +++ .../com/bio4j/angulillos/GraphSchema.java | 121 +---- .../java/com/bio4j/angulillos/Property.java | 20 +- .../java/com/bio4j/angulillos/TypedEdge.java | 146 ++---- .../com/bio4j/angulillos/TypedElement.java | 60 +-- .../bio4j/angulillos/TypedElementIndex.java | 25 +- .../java/com/bio4j/angulillos/TypedGraph.java | 465 +++++++++--------- .../com/bio4j/angulillos/TypedVertex.java | 370 +++++++------- .../angulillos/TwitterGraphTestSuite.java | 64 ++- .../com/bio4j/angulillos/TwitterSchema.java | 6 +- 11 files changed, 627 insertions(+), 733 deletions(-) create mode 100644 src/main/java/com/bio4j/angulillos/Arity.java diff --git a/build.sbt b/build.sbt index 7018ec9..726f743 100644 --- a/build.sbt +++ b/build.sbt @@ -9,5 +9,5 @@ javaVersion := "1.8" excludeFilter in unmanagedSources := (excludeFilter in unmanagedSources).value || - "*Query.java" || - "*TwitterGraphTestSuite.java" + "*Index.java" || + "*Query.java" diff --git a/src/main/java/com/bio4j/angulillos/Arity.java b/src/main/java/com/bio4j/angulillos/Arity.java new file mode 100644 index 0000000..03b266a --- /dev/null +++ b/src/main/java/com/bio4j/angulillos/Arity.java @@ -0,0 +1,79 @@ +package com.bio4j.angulillos; + +interface HasArity { + + /* the arity for this edge. This corresponds to the edge between the two vertex types. */ + Arity arity(); +} + +/* + ### Arities + + We have six basic arities: three for in, three for out. +*/ +public enum Arity { + + oneToOne, + oneToAtMostOne, + oneToAtLeastOne, + oneToAny, + + atMostOneToOne, + atMostOneToAtMostOne, + atMostOneToAtLeastOne, + atMostOneToAny, + + atLeastOneToOne, + atLeastOneToAtMostOne, + atLeastOneToAtLeastOne, + atLeastOneToAny, + + anyToOne, + anyToAtMostOne, + anyToAtLeastOne, + anyToAny; +} + +/* #### In-arities */ + +/* An edge type `e` being _surjective_ implies that calling `inV(e)` will always return some, possibly several, vertices */ +interface FromAtLeastOne extends HasArity {} +/* An edge type `e` being _from many_ implies that calling `inV(e)` will in general return more than one vertex */ +interface FromOne extends HasArity {} +/* An edge type `e` being _from one_ implies that calling `inV(e)` will return at most one vertex */ +interface FromAtMostOne extends HasArity {} + +/* #### Out-arities */ + +/* That an edge type `e` being _always defined_ implies that calling `outV(e)` will always return some, possibly several, vertices */ +interface ToAtLeastOne extends HasArity {} +/* An edge type `e` being _to many_ implies that calling `outV(e)` will in general return more than one vertex */ +interface ToOne extends HasArity {} +/* An edge type `e` being _to one_ implies that calling `outV(e)` will return at most one vertex */ +interface ToAtMostOne extends HasArity {} + + +/* + #### Arity combinations + + These are all the possible combinations of the different arities. In the first line under `extends` you see those that correspond to `in`, and in the second one those that correspond to `out` +*/ +interface OneToOne extends FromOne, ToOne { default Arity arity() { return Arity.oneToOne; } } +interface OneToAtMostOne extends FromOne, ToAtMostOne { default Arity arity() { return Arity.oneToAtMostOne; } } +interface OneToAtLeastOne extends FromOne, ToAtLeastOne { default Arity arity() { return Arity.oneToAtLeastOne; } } +interface OneToAny extends FromOne { default Arity arity() { return Arity.oneToAny; } } + +interface AtMostOneToOne extends FromAtMostOne, ToOne { default Arity arity() { return Arity.atMostOneToOne; } } +interface AtMostOneToAtMostOne extends FromAtMostOne, ToAtMostOne { default Arity arity() { return Arity.atMostOneToAtMostOne; } } +interface AtMostOneToAtLeastOne extends FromAtMostOne, ToAtLeastOne { default Arity arity() { return Arity.atMostOneToAtLeastOne; } } +interface AtMostOneToAny extends FromAtMostOne { default Arity arity() { return Arity.atMostOneToAny; } } + +interface AtLeastOneToOne extends FromAtLeastOne, ToOne { default Arity arity() { return Arity.atLeastOneToOne; } } +interface AtLeastOneToAtMostOne extends FromAtLeastOne, ToAtMostOne { default Arity arity() { return Arity.atLeastOneToAtMostOne; } } +interface AtLeastOneToAtLeastOne extends FromAtLeastOne, ToAtLeastOne { default Arity arity() { return Arity.atLeastOneToAtLeastOne; } } +interface AtLeastOneToAny extends FromAtLeastOne { default Arity arity() { return Arity.atLeastOneToAny; } } + +interface AnyToOne extends ToOne { default Arity arity() { return Arity.anyToOne; } } +interface AnyToAtMostOne extends ToAtMostOne { default Arity arity() { return Arity.anyToAtMostOne; } } +interface AnyToAtLeastOne extends ToAtLeastOne { default Arity arity() { return Arity.anyToAtLeastOne; } } +interface AnyToAny extends HasArity { default Arity arity() { return Arity.anyToAny; } } diff --git a/src/main/java/com/bio4j/angulillos/GraphSchema.java b/src/main/java/com/bio4j/angulillos/GraphSchema.java index ba5eda4..520dd8b 100644 --- a/src/main/java/com/bio4j/angulillos/GraphSchema.java +++ b/src/main/java/com/bio4j/angulillos/GraphSchema.java @@ -13,129 +13,22 @@ public abstract class GraphSchema< protected GraphSchema(UntypedGraph raw) { this.raw = raw; } - /* ### Abstract helper classes - - These inner classes are implementations of the corresponding Typed* interfaces. - They bound raw vertex/edge types that the graph is parametrized by. - */ - public class Property< - FT extends ElementType, - X - > implements com.bio4j.angulillos.Property { - - private final FT elementType; - private final String nameSuffix; - private final Class valueClass; - - @Override public final FT elementType() { return this.elementType; } - @Override public final Class valueClass() { return this.valueClass; } - - protected Property(FT elementType, String nameSuffix, Class valueClass) { - this.elementType = elementType; - this.nameSuffix = nameSuffix; - this.valueClass = valueClass; - } - - // NOTE: if we don't override it, all properties will have the same name (FQN of this class) - @Override public String _label() { - return (elementType()._label() + "." + nameSuffix); - } - } - - - private abstract class Element< - F extends Element, - FT extends ElementType, - RF - > implements TypedElement { - - @Override public final SG graph() { return GraphSchema.this.self(); } - - private final RF raw; - private final FT type; - - @Override public final RF raw() { return this.raw; } - @Override public final FT type() { return this.type; } - - // NOTE: we cannot do the same to `self`, because `super()` constructor cannot refer to `this` - protected Element(RF raw, FT type) { - this.raw = raw; - this.type = type; - } - } - - public abstract class ElementType< - F extends Element, - FT extends ElementType, - RF - > implements TypedElement.Type { - - public abstract FT self(); - - public Property property(String nameSuffix, Class valueClass) { - return new Property(self(), nameSuffix, valueClass); - } - } - - - public class Vertex< - VT extends VertexType - > extends Element,VT, RV> - implements TypedVertex,VT, SG,RV,RE> { - - protected Vertex(RV raw, VT type) { super(raw, type); } - @Override public Vertex self() { return this; } - } - public abstract class VertexType< VT extends VertexType - > extends ElementType,VT, RV> - implements TypedVertex.Type,VT, SG,RV,RE> { + > extends com.bio4j.angulillos.VertexType { - @Override public Vertex fromRaw(RV raw) { return new Vertex(raw, self()); } - } - - - public class Edge< - ST extends VertexType, - ET extends EdgeType, - TT extends VertexType - > extends Element,ET, RE> - implements TypedEdge< - Vertex, ST, - Edge, ET, - Vertex, TT, - SG,RV,RE - > { - - protected Edge(RE raw, ET type) { super(raw, type); } - @Override public Edge self() { return this; } + @Override public final SG graph() { return GraphSchema.this.self(); } } public abstract class EdgeType< - ST extends VertexType, + ST extends com.bio4j.angulillos.VertexType, ET extends EdgeType, - TT extends VertexType - > extends ElementType,ET, RE> - implements TypedEdge.Type< - Vertex, ST, - Edge, ET, - Vertex, TT, - SG,RV,RE - > - { - private final ST sourceType; - private final TT targetType; + TT extends com.bio4j.angulillos.VertexType + > extends com.bio4j.angulillos.EdgeType { - @Override public final ST sourceType() { return this.sourceType; } - @Override public final TT targetType() { return this.targetType; } + public EdgeType(ST sourceType, TT targetType) { super(sourceType, targetType); } - protected EdgeType(ST sourceType, TT targetType) { - this.sourceType = sourceType; - this.targetType = targetType; - } - - @Override public Edge fromRaw(RE raw) { return new Edge(raw, self()); } + @Override public final SG graph() { return GraphSchema.this.self(); } } } diff --git a/src/main/java/com/bio4j/angulillos/Property.java b/src/main/java/com/bio4j/angulillos/Property.java index c6299e0..7151c12 100644 --- a/src/main/java/com/bio4j/angulillos/Property.java +++ b/src/main/java/com/bio4j/angulillos/Property.java @@ -5,18 +5,22 @@ A property of the [Element](TypedElement.java.md) of type `ET`, with value type `X`. */ -interface Property < - ElemType extends TypedElement.Type, +public class Property < + FT extends ElementType, X > { + // private final String nameSuffix; - /* the element type which has this property */ - ElemType elementType(); + public final String _label; - /* the class of the property value, so that implementing classes can create values of it */ - Class valueClass(); + public final FT elementType; + public final Class valueClass; - /* the name of the property. By default this is the canonical name of the implementing class */ - default String _label() { return getClass().getCanonicalName(); } + protected Property(FT elementType, String nameSuffix, Class valueClass) { + this.elementType = elementType; + // this.nameSuffix = nameSuffix; + this.valueClass = valueClass; + this._label = elementType._label + "." + nameSuffix; + } } diff --git a/src/main/java/com/bio4j/angulillos/TypedEdge.java b/src/main/java/com/bio4j/angulillos/TypedEdge.java index 15ea3f8..1833de2 100644 --- a/src/main/java/com/bio4j/angulillos/TypedEdge.java +++ b/src/main/java/com/bio4j/angulillos/TypedEdge.java @@ -9,140 +9,50 @@ - `E` the edge, `ET` the edge type - `T` the target TypedVertex, `TT` the target TypedVertex type */ -interface TypedEdge < - // source vertex - S extends TypedVertex, - ST extends TypedVertex.Type, - // edge - E extends TypedEdge, - ET extends TypedEdge.Type, - // target vertex - T extends TypedVertex, - TT extends TypedVertex.Type, +abstract class EdgeType < + ST extends VertexType, + ET extends EdgeType, + TT extends VertexType, // graph & raws G extends TypedGraph, RV,RE -> - extends TypedElement +> extends ElementType + implements HasArity { - /* the source vertex of this edge */ - default S source() { return graph().source( self() ); } + public final ST sourceType; + public final TT targetType; - /* the target vertex of this edge */ - default T target() { return graph().target( self() ); } - - - @Override default - X get(Property property) { return graph().getProperty(self(), property); } - - @Override default - E set(Property property, X value) { - - graph().setProperty(self(), property, value); - return self(); + protected EdgeType(ST sourceType, TT targetType) { + this.sourceType = sourceType; + this.targetType = targetType; } + @Override public Edge fromRaw(RE raw) { return this.new Edge(raw); } - interface HasArity { - /* the arity for this edge. This corresponds to the edge between the two vertex types. */ - Type.Arity arity(); - } - - interface Type < - // source vertex - S extends TypedVertex, - ST extends TypedVertex.Type, - // edge - E extends TypedEdge, - ET extends TypedEdge.Type, - // target vertex - T extends TypedVertex, - TT extends TypedVertex.Type, - // graph & raws - G extends TypedGraph, - RV,RE - > extends - TypedElement.Type, - HasArity - { - - ST sourceType(); - TT targetType(); - - - /* - ### Arities - - We have six basic arities: three for in, three for out. - */ - public enum Arity { - - oneToOne, - oneToAtMostOne, - oneToAtLeastOne, - oneToAny, - - atMostOneToOne, - atMostOneToAtMostOne, - atMostOneToAtLeastOne, - atMostOneToAny, - - atLeastOneToOne, - atLeastOneToAtMostOne, - atLeastOneToAtLeastOne, - atLeastOneToAny, - - anyToOne, - anyToAtMostOne, - anyToAtLeastOne, - anyToAny; - } + class Edge extends Element { - /* #### In-arities */ + public Edge(RE raw) { super(raw); } - /* An edge type `e` being _surjective_ implies that calling `inV(e)` will always return some, possibly several, vertices */ - interface FromAtLeastOne extends HasArity {} - /* An edge type `e` being _from many_ implies that calling `inV(e)` will in general return more than one vertex */ - interface FromOne extends HasArity {} - /* An edge type `e` being _from one_ implies that calling `inV(e)` will return at most one vertex */ - interface FromAtMostOne extends HasArity {} + /* the source vertex of this edge */ + // public final < + // SG extends TypedGraph + // > VertexType.Vertex source() { return graph.source( this ); } - /* #### Out-arities */ + // /* the target vertex of this edge */ + // public final T target() { return graph.target( self() ); } - /* That an edge type `e` being _always defined_ implies that calling `outV(e)` will always return some, possibly several, vertices */ - interface ToAtLeastOne extends HasArity {} - /* An edge type `e` being _to many_ implies that calling `outV(e)` will in general return more than one vertex */ - interface ToOne extends HasArity {} - /* An edge type `e` being _to one_ implies that calling `outV(e)` will return at most one vertex */ - interface ToAtMostOne extends HasArity {} + @Override public + X get(Property property) { return graph.getProperty(this, property); } + @Override public + Edge set(Property property, X value) { - /* - #### Arity combinations - - These are all the possible combinations of the different arities. In the first line under `extends` you see those that correspond to `in`, and in the second one those that correspond to `out` - */ - interface OneToOne extends FromOne, ToOne { default Arity arity() { return Arity.oneToOne; } } - interface OneToAtMostOne extends FromOne, ToAtMostOne { default Arity arity() { return Arity.oneToAtMostOne; } } - interface OneToAtLeastOne extends FromOne, ToAtLeastOne { default Arity arity() { return Arity.oneToAtLeastOne; } } - interface OneToAny extends FromOne { default Arity arity() { return Arity.oneToAny; } } - - interface AtMostOneToOne extends FromAtMostOne, ToOne { default Arity arity() { return Arity.atMostOneToOne; } } - interface AtMostOneToAtMostOne extends FromAtMostOne, ToAtMostOne { default Arity arity() { return Arity.atMostOneToAtMostOne; } } - interface AtMostOneToAtLeastOne extends FromAtMostOne, ToAtLeastOne { default Arity arity() { return Arity.atMostOneToAtLeastOne; } } - interface AtMostOneToAny extends FromAtMostOne { default Arity arity() { return Arity.atMostOneToAny; } } - - interface AtLeastOneToOne extends FromAtLeastOne, ToOne { default Arity arity() { return Arity.atLeastOneToOne; } } - interface AtLeastOneToAtMostOne extends FromAtLeastOne, ToAtMostOne { default Arity arity() { return Arity.atLeastOneToAtMostOne; } } - interface AtLeastOneToAtLeastOne extends FromAtLeastOne, ToAtLeastOne { default Arity arity() { return Arity.atLeastOneToAtLeastOne; } } - interface AtLeastOneToAny extends FromAtLeastOne { default Arity arity() { return Arity.atLeastOneToAny; } } - - interface AnyToOne extends ToOne { default Arity arity() { return Arity.anyToOne; } } - interface AnyToAtMostOne extends ToAtMostOne { default Arity arity() { return Arity.anyToAtMostOne; } } - interface AnyToAtLeastOne extends ToAtLeastOne { default Arity arity() { return Arity.anyToAtLeastOne; } } - interface AnyToAny extends HasArity { default Arity arity() { return Arity.anyToAny; } } + graph().setProperty(this, property, value); + return this; + } } + } diff --git a/src/main/java/com/bio4j/angulillos/TypedElement.java b/src/main/java/com/bio4j/angulillos/TypedElement.java index 001705c..6d91c77 100644 --- a/src/main/java/com/bio4j/angulillos/TypedElement.java +++ b/src/main/java/com/bio4j/angulillos/TypedElement.java @@ -13,49 +13,43 @@ `E` refers to the element itself, and `ET` its type. You cannot define one without defining the other. */ -interface TypedElement < - F extends TypedElement, - FT extends TypedElement.Type, - G extends TypedGraph, +abstract class ElementType < + FT extends ElementType, + G extends TypedGraph, RF > { + /* This is a unique label */ + public final String _label = getClass().getCanonicalName(); - /* - ### Element types - - Element types are also used as factories for constructing instances of the corresponding elements. - */ - interface Type < - F extends TypedElement, - FT extends TypedElement.Type, - G extends TypedGraph, - RF - > { - /* Constructs a value of the typed element of this type */ - F fromRaw(RF rawElem); - - // NOTE: this should be final, but interface cannot have final methods - default String _label() { return getClass().getCanonicalName(); } - } + /* The graph in which this element lives */ + public abstract G graph(); + + /* An abstract reference to the instance of the implementing class. This should return `this` in all cases; it just cannot be implemented at this level */ + public abstract FT self(); + /* Constructs a new element of this element type */ + public abstract Element fromRaw(RF raw); - /* The type of this element */ - FT type(); + /* Defines a new property on this element type */ + public final Property property(String nameSuffix, Class valueClass) { + return new Property(self(), nameSuffix, valueClass); + } - /* An abstract reference to the instance of the implementing class. This should return `this` in all cases; it just cannot be implemented at this level. */ - F self(); - /* `raw` should return a reference to the instance of the corresponding raw type underlying this element. The return type should be `RV + RE` but Java does not have sum types, and it will collapse anyway at the level of Vertex and Edge so `Object` is not that bad here. */ - RF raw(); + abstract class Element { + public final RF raw; - /* The graph in which this element lives. */ - G graph(); + public Element(RF raw) { this.raw = raw; } - /* The `get` method lets you get the value of a `property` which this element has. For that, you pass as an argument the [property](Property.java.md). Note that the type bounds only allow properties of this element. */ - X get(Property property); + public final FT type = ElementType.this.self(); + public final G graph = type.graph(); - /* `set` sets the value of a `property` for this element. Again, you can only set properties that this element has, using values of the corresponding property value type. */ - F set(Property property, X value); + /* The `get` method lets you get the value of a `property` which this element has. For that, you pass as an argument the [property](Property.java.md). Note that the type bounds only allow properties of this element. */ + public abstract X get(Property property); + + /* `set` sets the value of a `property` for this element. Again, you can only set properties that this element has, using values of the corresponding property value type. */ + public abstract Element set(Property property, X value); + } } diff --git a/src/main/java/com/bio4j/angulillos/TypedElementIndex.java b/src/main/java/com/bio4j/angulillos/TypedElementIndex.java index 6714223..9f21b4e 100644 --- a/src/main/java/com/bio4j/angulillos/TypedElementIndex.java +++ b/src/main/java/com/bio4j/angulillos/TypedElementIndex.java @@ -5,11 +5,8 @@ import java.util.Collection; interface TypedElementIndex < - // element - F extends TypedElement, - FT extends TypedElement.Type, - // property - P extends Property, + FT extends ElementType, + P extends Property, X > { @@ -35,14 +32,11 @@ interface TypedElementIndex < /* This interface declares that this index is over a property that uniquely classifies a element type for exact match queries */ interface Unique < - // element - F extends TypedElement, - FT extends TypedElement.Type, - // property - P extends Property, + FT extends ElementType, + P extends Property, X > - extends TypedElementIndex + extends TypedElementIndex { /* Get an element by providing a value of the indexed property */ @@ -54,14 +48,11 @@ default Optional getElement(X byValue) { /* This interface declares that this index is over a property that classifies lists of elements for exact match queries */ interface List < - // element - F extends TypedElement, - FT extends TypedElement.Type, - // property - P extends Property, + FT extends ElementType, + P extends Property, X > - extends TypedElementIndex + extends TypedElementIndex { /* Get a list of elements by providing a value of the property */ diff --git a/src/main/java/com/bio4j/angulillos/TypedGraph.java b/src/main/java/com/bio4j/angulillos/TypedGraph.java index 6d9e912..fad8217 100644 --- a/src/main/java/com/bio4j/angulillos/TypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/TypedGraph.java @@ -20,80 +20,76 @@ interface TypedGraph < UntypedGraph raw(); default < - V extends TypedVertex, - VT extends TypedVertex.Type + VT extends VertexType > - V addVertex(VT vertexType) { + VertexType.Vertex addVertex(VT vertexType) { return vertexType.fromRaw( - raw().addVertex( vertexType._label() ) + raw().addVertex( vertexType._label ) ); } /* adds an edge; note that this method does not set any properties. As it needs to be called by vertices in possibly different graphs, all the graph bounds are free with respect to G. */ default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type, - T extends TypedVertex, - TT extends TypedVertex.Type + ST extends VertexType, + ET extends EdgeType, + TT extends VertexType > - E addEdge(S from, ET edgeType, T to) { + EdgeType.Edge addEdge( + VertexType.Vertex source, + ET edgeType, + VertexType.Vertex target + ) { return edgeType.fromRaw( - raw().addEdge( from.raw(), edgeType._label(), to.raw() ) + raw().addEdge( source.raw, edgeType._label, target.raw ) ); } /* ### properties - foobarbuh + These methods are used for setting and getting properties on vertices and edges. */ default < - V extends TypedVertex, - VT extends TypedVertex.Type, + VT extends VertexType, X > - X getProperty(V vertex, Property property) { + X getProperty(VertexType.Vertex vertex, Property property) { - return raw().getPropertyV(vertex.raw(), property._label()); + return raw().getPropertyV(vertex.raw, property._label); } /* Get the value of a property from an edge of G. */ default < - E extends TypedEdge, - ET extends TypedEdge.Type, + ET extends EdgeType, X > - X getProperty(E edge, Property property) { + X getProperty(EdgeType.Edge edge, Property property) { - return raw().getPropertyE(edge.raw(), property._label()); + return raw().getPropertyE(edge.raw, property._label); } /* Sets the value of a property for a vertex of G. */ default < - V extends TypedVertex, - VT extends TypedVertex.Type, + VT extends VertexType, X > - G setProperty(V vertex, Property property, X value) { + G setProperty(VertexType.Vertex vertex, Property property, X value) { - raw().setPropertyV(vertex.raw(), property._label(), value); - return vertex.graph(); + raw().setPropertyV(vertex.raw, property._label, value); + return vertex.graph; } /* Sets the value of a property for an edge of G. */ default < - E extends TypedEdge, - ET extends TypedEdge.Type, + ET extends EdgeType, X > - G setProperty(E edge, Property property, X value) { + G setProperty(EdgeType.Edge edge, Property property, X value) { - raw().setPropertyE(edge.raw(), property._label(), value); - return edge.graph(); + raw().setPropertyE(edge.raw, property._label, value); + return edge.graph; } /* @@ -102,233 +98,236 @@ G setProperty(E edge, Property property, X value) { gets the source of an edge of G, which could be of a different graph. */ default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type + ST extends VertexType, + SG extends TypedGraph, + ET extends EdgeType > - S source(E edge) { + VertexType.Vertex source( + EdgeType.Edge edge + ) { - return edge.type().sourceType().fromRaw( - raw().source(edge.raw()) + return edge.type.sourceType.fromRaw( + raw().source(edge.raw) ); } - default < - E extends TypedEdge, - ET extends TypedEdge.Type, - T extends TypedVertex, - TT extends TypedVertex.Type - > - T target(E edge) { - - return edge.type().targetType().fromRaw( - raw().target(edge.raw()) - ); - } + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // T target(E edge) { + // + // return edge.type().targetType().fromRaw( + // raw().target(edge.raw()) + // ); + // } /* #### Outgoing edges */ default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type + ST extends VertexType, + ET extends EdgeType, + TT extends VertexType, + EG extends TypedGraph > - Stream outE(S source, ET edgeType) { + Stream< + EdgeType.Edge + > outE(VertexType.Vertex source, ET edgeType) { return raw().outE( - source.raw(), - edgeType._label() - ).map( - edgeType::fromRaw - ); - } - - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.ToAtLeastOne - > - Stream outAtLeastOneE(S source, ET edgeType) { - - return outE(source, edgeType); - } - - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.ToAtMostOne - > - Optional outAtMostOneE(S source, ET edgeType) { - - return outE(source, edgeType).findFirst(); - } - - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.ToOne - > - E outOneE(S source, ET edgeType) { - - return outE(source, edgeType).findFirst().get(); - } - - /* #### Incoming edges */ - default < - E extends TypedEdge, - ET extends TypedEdge.Type, - T extends TypedVertex, - TT extends TypedVertex.Type - > - Stream inE(T vertex, ET edgeType) { - - return raw().inE( - vertex.raw(), - edgeType._label() + source.raw, + edgeType._label ).map( edgeType::fromRaw ); } - default < - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.FromAtLeastOne, - T extends TypedVertex, - TT extends TypedVertex.Type - > - Stream inAtLeastOneE(T target, ET edgeType) { return inE(target, edgeType); } - - default < - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.FromAtMostOne, - T extends TypedVertex, - TT extends TypedVertex.Type - > - Optional inAtMostOneE(T target, ET edgeType) { return inE(target, edgeType).findFirst(); } - - default < - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.FromOne, - T extends TypedVertex, - TT extends TypedVertex.Type - > - E inOneE(T target, ET edgeType) { return inE(target, edgeType).findFirst().get(); } - + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.ToAtLeastOne + // > + // Stream outAtLeastOneE(S source, ET edgeType) { + // + // return outE(source, edgeType); + // } + // + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.ToAtMostOne + // > + // Optional outAtMostOneE(S source, ET edgeType) { + // + // return outE(source, edgeType).findFirst(); + // } + // + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.ToOne + // > + // E outOneE(S source, ET edgeType) { + // + // return outE(source, edgeType).findFirst().get(); + // } + // + // /* #### Incoming edges */ + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // Stream inE(T vertex, ET edgeType) { + // + // return raw().inE( + // vertex.raw(), + // edgeType._label() + // ).map( + // edgeType::fromRaw + // ); + // } + // + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.FromAtLeastOne, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // Stream inAtLeastOneE(T target, ET edgeType) { return inE(target, edgeType); } + // + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.FromAtMostOne, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // Optional inAtMostOneE(T target, ET edgeType) { return inE(target, edgeType).findFirst(); } + // + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.FromOne, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // E inOneE(T target, ET edgeType) { return inE(target, edgeType).findFirst().get(); } + // /* #### Outgoing vertices */ default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type, - T extends TypedVertex, - TT extends TypedVertex.Type + ST extends VertexType, + ET extends EdgeType, + TT extends VertexType, + TG extends TypedGraph > - Stream outV(S source, ET edgeType) { + Stream< + VertexType.Vertex + > outV(VertexType.Vertex source, ET edgeType) { return raw().outV( - source.raw(), - edgeType._label() - ).map( - edgeType.targetType()::fromRaw - ); - } - - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.ToAtLeastOne, - T extends TypedVertex, - TT extends TypedVertex.Type - > - Stream outAtLeastOneV(S source, ET edgeType) { return outV(source, edgeType); } - - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.ToAtMostOne, - T extends TypedVertex, - TT extends TypedVertex.Type - > - Optional outAtMostOneV(S source, ET edgeType) { return outV(source, edgeType).findFirst(); } - - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.ToOne, - T extends TypedVertex, - TT extends TypedVertex.Type - > - T outOneV(S source, ET edgeType) { return outV(source, edgeType).findFirst().get(); } - - - /* #### Incoming vertices */ - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type, - T extends TypedVertex, - TT extends TypedVertex.Type - > - Stream inV(T target, ET edgeType) { - - return raw().inV( - target.raw(), - edgeType._label() - ).map( - edgeType.sourceType()::fromRaw + source.raw, + edgeType._label + ).map( rawT -> + edgeType.targetType.fromRaw(rawT) ); } - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.FromAtLeastOne, - T extends TypedVertex, - TT extends TypedVertex.Type - > - Stream inAtLeastOneV(T target, ET edgeType) { return inV(target, edgeType); } - - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.FromAtMostOne, - T extends TypedVertex, - TT extends TypedVertex.Type - > - Optional inAtMostOneV(T target, ET edgeType) { return inV(target, edgeType).findFirst(); } - - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.FromOne, - T extends TypedVertex, - TT extends TypedVertex.Type - > - S inOneV(T target, ET edgeType) { return inV(target, edgeType).findFirst().get(); } + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.ToAtLeastOne, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // Stream outAtLeastOneV(S source, ET edgeType) { return outV(source, edgeType); } + // + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.ToAtMostOne, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // Optional outAtMostOneV(S source, ET edgeType) { return outV(source, edgeType).findFirst(); } + // + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.ToOne, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // T outOneV(S source, ET edgeType) { return outV(source, edgeType).findFirst().get(); } + // + // + // /* #### Incoming vertices */ + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // Stream inV(T target, ET edgeType) { + // + // return raw().inV( + // target.raw(), + // edgeType._label() + // ).map( + // edgeType.sourceType()::fromRaw + // ); + // } + // + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.FromAtLeastOne, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // Stream inAtLeastOneV(T target, ET edgeType) { return inV(target, edgeType); } + // + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.FromAtMostOne, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // Optional inAtMostOneV(T target, ET edgeType) { return inV(target, edgeType).findFirst(); } + // + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.FromOne, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // S inOneV(T target, ET edgeType) { return inV(target, edgeType).findFirst().get(); } } diff --git a/src/main/java/com/bio4j/angulillos/TypedVertex.java b/src/main/java/com/bio4j/angulillos/TypedVertex.java index aaee2b2..ca595bf 100644 --- a/src/main/java/com/bio4j/angulillos/TypedVertex.java +++ b/src/main/java/com/bio4j/angulillos/TypedVertex.java @@ -9,194 +9,194 @@ A typed vertex. A vertex and its type need to be defined at the same time. The vertex keeps a reference of its type, while the type works as a factory for creating vertices with that type. */ -interface TypedVertex < - V extends TypedVertex, - VT extends TypedVertex.Type, +abstract class VertexType < + VT extends VertexType, G extends TypedGraph, RV,RE -> - extends TypedElement -{ - - interface Type < - V extends TypedVertex, - VT extends TypedVertex.Type, - G extends TypedGraph, - RV,RE - > extends TypedElement.Type {} - - - /* - ### Create edges in/out of this vertex - - There are two methods for creating new edges, into and out of this vertex respectively. Their implementation delegates to the typed graph methods. Note that all graphs are in principle different. - */ - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type - > - E addInEdge(S from, ET edgeType) { return graph().addEdge( from, edgeType, self() ); } - - default < - E extends TypedEdge, - ET extends TypedEdge.Type, - T extends TypedVertex, - TT extends TypedVertex.Type - > - E addOutEdge(ET edgeType, T to) { return graph().addEdge( self(), edgeType, to ); } - - - /* ### Properties */ - @Override default - X get(Property property) { return graph().getProperty(self(), property); } - - - @Override default - V set(Property property, X value) { - - graph().setProperty(self(), property, value); - return self(); +> extends ElementType { + + @Override public Vertex fromRaw(RV raw) { return this.new Vertex(raw); } + + public final class Vertex extends Element { + + public Vertex(RV raw) { super(raw); } + + + /* ### Properties */ + @Override public + X get(Property property) { return graph().getProperty(this, property); } + + @Override public + Vertex set(Property property, X value) { + + graph().setProperty(this, property, value); + return this; + } + + /* #### outE */ + public final < + ET extends EdgeType, + TT extends VertexType, + EG extends TypedGraph + > + Stream< + EdgeType.Edge + > outE(ET edgeType) { return graph().outE(this, edgeType); } + + + /* #### outV */ + public final < + ET extends EdgeType, + TT extends VertexType, + TG extends TypedGraph + > + Stream< + VertexType.Vertex + > outV(ET edgeType) { return graph().outV(this, edgeType); } + } - /* - ### Getting incoming and outgoing edges - - For when you don't know anything about the arity, we have unbounded in/out methods which return `Stream`s - */ - - /* #### outE */ - default < - E extends TypedEdge, - ET extends TypedEdge.Type - > - Stream outE(ET edgeType) { return graph().outE(self(), edgeType); } - - default < - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.ToAtLeastOne - > - Stream outAtLeastOneE(ET edgeType) { return graph().outAtLeastOneE(self(), edgeType); } - - default < - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.ToAtMostOne - > - Optional outAtMostOneE(ET edgeType) { return graph().outAtMostOneE(self(), edgeType); } - - default < - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.ToOne - > - E outOneE(ET edgeType) { return graph().outOneE(self(), edgeType); } - - - /* #### inE */ - default < - E extends TypedEdge, - ET extends TypedEdge.Type - > - Stream inE(ET edgeType) { return graph().inE(self(), edgeType); } - - default < - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.FromAtLeastOne - > - Stream inAtLeastOneE(ET edgeType) { return graph().inAtLeastOneE(self(), edgeType); } - - default < - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.FromAtMostOne - > - Optional inAtMostOneE(ET edgeType) { return graph().inAtMostOneE(self(), edgeType); } - - default < - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.FromOne - > - E inOneE(ET edgeType) { return graph().inOneE(self(), edgeType); } - - ///////////////////////////////////////////////////////////////////////////////////////////////////// - - - /* #### outV */ - default < - E extends TypedEdge, - ET extends TypedEdge.Type, - T extends TypedVertex, - TT extends TypedVertex.Type - > - Stream outV(ET edgeType) { return graph().outV(self(), edgeType); } - - default < - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.ToAtLeastOne, - T extends TypedVertex, - TT extends TypedVertex.Type - > - Stream outAtLeastOneV(ET edgeType) { return graph().outAtLeastOneV(self(), edgeType); } - - default < - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.ToAtMostOne, - T extends TypedVertex, - TT extends TypedVertex.Type - > - Optional outAtMostOneV(ET edgeType) { return graph().outAtMostOneV(self(), edgeType); } - - default < - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.ToOne, - T extends TypedVertex, - TT extends TypedVertex.Type - > - T outOneV(ET edgeType) { return graph().outOneV(self(), edgeType); } - - - /* #### inV */ - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type - > - Stream inV(ET edgeType) { return graph().inV(self(), edgeType); } - - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.FromAtLeastOne - > - Stream inAtLeastOneV(ET edgeType) { return graph().inAtLeastOneV(self(), edgeType); } - - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.FromAtMostOne - > - Optional inAtMostOneV(ET edgeType) { return graph().inAtMostOneV(self(), edgeType); } - - default < - S extends TypedVertex, - ST extends TypedVertex.Type, - E extends TypedEdge, - ET extends TypedEdge.Type - & TypedEdge.Type.FromOne - > - S inOneV(ET edgeType) { return graph().inOneV(self(), edgeType); } + // /* + // ### Create edges in/out of this vertex + // + // There are two methods for creating new edges, into and out of this vertex respectively. Their implementation delegates to the typed graph methods. Note that all graphs are in principle different. + // */ + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type + // > + // E addInEdge(S from, ET edgeType) { return graph().addEdge( from, edgeType, self() ); } + // + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // E addOutEdge(ET edgeType, T to) { return graph().addEdge( self(), edgeType, to ); } + + + // /* + // ### Getting incoming and outgoing edges + // + // For when you don't know anything about the arity, we have unbounded in/out methods which return `Stream`s + // */ + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.ToAtLeastOne + // > + // Stream outAtLeastOneE(ET edgeType) { return graph().outAtLeastOneE(self(), edgeType); } + // + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.ToAtMostOne + // > + // Optional outAtMostOneE(ET edgeType) { return graph().outAtMostOneE(self(), edgeType); } + // + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.ToOne + // > + // E outOneE(ET edgeType) { return graph().outOneE(self(), edgeType); } + // + // + // /* #### inE */ + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type + // > + // Stream inE(ET edgeType) { return graph().inE(self(), edgeType); } + // + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.FromAtLeastOne + // > + // Stream inAtLeastOneE(ET edgeType) { return graph().inAtLeastOneE(self(), edgeType); } + // + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.FromAtMostOne + // > + // Optional inAtMostOneE(ET edgeType) { return graph().inAtMostOneE(self(), edgeType); } + // + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.FromOne + // > + // E inOneE(ET edgeType) { return graph().inOneE(self(), edgeType); } + // + // ///////////////////////////////////////////////////////////////////////////////////////////////////// + // + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.ToAtLeastOne, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // Stream outAtLeastOneV(ET edgeType) { return graph().outAtLeastOneV(self(), edgeType); } + // + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.ToAtMostOne, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // Optional outAtMostOneV(ET edgeType) { return graph().outAtMostOneV(self(), edgeType); } + // + // default < + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.ToOne, + // T extends TypedVertex, + // TT extends TypedVertex.Type + // > + // T outOneV(ET edgeType) { return graph().outOneV(self(), edgeType); } + // + // + // /* #### inV */ + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type + // > + // Stream inV(ET edgeType) { return graph().inV(self(), edgeType); } + // + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.FromAtLeastOne + // > + // Stream inAtLeastOneV(ET edgeType) { return graph().inAtLeastOneV(self(), edgeType); } + // + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.FromAtMostOne + // > + // Optional inAtMostOneV(ET edgeType) { return graph().inAtMostOneV(self(), edgeType); } + // + // default < + // S extends TypedVertex, + // ST extends TypedVertex.Type, + // E extends TypedEdge, + // ET extends TypedEdge.Type + // & TypedEdge.Type.FromOne + // > + // S inOneV(ET edgeType) { return graph().inOneV(self(), edgeType); } } diff --git a/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java b/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java index 425c7ee..078d6c1 100644 --- a/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java +++ b/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java @@ -1,38 +1,60 @@ package com.bio4j.angulillos; -import com.bio4j.angulillos.TwitterGraph.*; +// import com.bio4j.angulillos.TwitterSchema.*; import java.util.stream.Stream; -public abstract class TwitterGraphTestSuite,V,VT,E,ET> { +public abstract class TwitterGraphTestSuite< + G extends TwitterSchema, + RV,RE +> { - protected TwitterGraph g; + protected G g; - public void doSomething(TwitterGraph.User user) { + public TwitterSchema.User.Vertex addUser(String name, Integer age) { - Stream.Tweet> tweets = user.outV(g.Posted()); + return g.addVertex(g.user) + .set(g.user.name, name) + .set(g.user.age, age); } - public Stream.User> tweetedTheTweetsThatTweeted(TwitterGraph.User user) { + public void doSomething(TwitterSchema.User.Vertex user) { - return user.outV(g.Posted()).flatMap( - tw -> tw.inV(g.Posted()) - ); - } - - /* This uses arity-specific methods to return **the** user that tweeted a tweet. */ - public TwitterGraph.User tweeted(TwitterGraph.Tweet tweet) { + System.out.println(user.get(g.user.age)); - return tweet.inOneV(g.Posted()); - } + TwitterSchema.User.Vertex bob = addUser("Bob", 25); - public Stream.User> repliedToSomeTweetFrom(TwitterGraph.User user) { + TwitterSchema.Follows.Edge edge = + g.addEdge( + bob, + g.follows, + addUser("Alice", 34) + ); - return user - .outV( g.Posted() ) - .flatMap( tw -> tw.inV( g.RepliesTo() ) ) - .map( tw -> tw.inOneV( g.Posted() ) ) - .distinct(); + Stream.Tweet.Vertex> tweets = user.outV(g.posted); + Stream.User.Vertex> followees = bob.outV(g.follows); } + + // public Stream.User> tweetedTheTweetsThatTweeted(TwitterSchema.User user) { + // + // return user.outV(g.Posted()).flatMap( + // tw -> tw.inV(g.Posted()) + // ); + // } + // + // /* This uses arity-specific methods to return **the** user that tweeted a tweet. */ + // public TwitterSchema.User tweeted(TwitterSchema.Tweet tweet) { + // + // return tweet.inOneV(g.Posted()); + // } + // + // public Stream.User> repliedToSomeTweetFrom(TwitterSchema.User user) { + // + // return user + // .outV( g.Posted() ) + // .flatMap( tw -> tw.inV( g.RepliesTo() ) ) + // .map( tw -> tw.inOneV( g.Posted() ) ) + // .distinct(); + // } } diff --git a/src/test/java/com/bio4j/angulillos/TwitterSchema.java b/src/test/java/com/bio4j/angulillos/TwitterSchema.java index a50e657..32171cc 100644 --- a/src/test/java/com/bio4j/angulillos/TwitterSchema.java +++ b/src/test/java/com/bio4j/angulillos/TwitterSchema.java @@ -1,14 +1,16 @@ package com.bio4j.angulillos; -import com.bio4j.angulillos.TypedEdge.Type.*; +// import com.bio4j.angulillos.TypedEdge.Type.*; import java.net.URL; import java.util.Date; -public abstract class TwitterSchema +public class TwitterSchema extends GraphSchema, RV,RE> { + public final TwitterSchema self() { return this; } + public TwitterSchema(UntypedGraph raw) { super(raw); } From bbec14aeda6912628d4e4d5c9b4eb2a70cdf5fcc Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Sun, 27 Mar 2016 22:53:29 +0200 Subject: [PATCH 02/20] Added source and target graphs to the edge parameters; otherwise source/target methods won't work (now wee need to bind all parameters :-1:) --- .../com/bio4j/angulillos/GraphSchema.java | 25 +++++--- .../java/com/bio4j/angulillos/TypedEdge.java | 25 +++++--- .../java/com/bio4j/angulillos/TypedGraph.java | 63 ++++++++++--------- .../com/bio4j/angulillos/TypedVertex.java | 12 ++-- .../angulillos/TwitterGraphTestSuite.java | 4 ++ .../com/bio4j/angulillos/TwitterSchema.java | 30 +++++---- 6 files changed, 94 insertions(+), 65 deletions(-) diff --git a/src/main/java/com/bio4j/angulillos/GraphSchema.java b/src/main/java/com/bio4j/angulillos/GraphSchema.java index 520dd8b..d2b75ec 100644 --- a/src/main/java/com/bio4j/angulillos/GraphSchema.java +++ b/src/main/java/com/bio4j/angulillos/GraphSchema.java @@ -2,11 +2,11 @@ public abstract class GraphSchema< - SG extends GraphSchema, + G extends GraphSchema, RV,RE -> implements TypedGraph { +> implements TypedGraph { - public abstract SG self(); + public abstract G self(); private final UntypedGraph raw; @Override public final UntypedGraph raw() { return this.raw; } @@ -15,20 +15,25 @@ public abstract class GraphSchema< public abstract class VertexType< VT extends VertexType - > extends com.bio4j.angulillos.VertexType { + > extends com.bio4j.angulillos.VertexType { - @Override public final SG graph() { return GraphSchema.this.self(); } + @Override public final G graph() { return GraphSchema.this.self(); } } public abstract class EdgeType< - ST extends com.bio4j.angulillos.VertexType, - ET extends EdgeType, - TT extends com.bio4j.angulillos.VertexType - > extends com.bio4j.angulillos.EdgeType { + // source + ST extends com.bio4j.angulillos.VertexType, + SG extends TypedGraph, + // edge itself + ET extends EdgeType, + // target + TT extends com.bio4j.angulillos.VertexType, + TG extends TypedGraph + > extends com.bio4j.angulillos.EdgeType { public EdgeType(ST sourceType, TT targetType) { super(sourceType, targetType); } - @Override public final SG graph() { return GraphSchema.this.self(); } + @Override public final G graph() { return GraphSchema.this.self(); } } } diff --git a/src/main/java/com/bio4j/angulillos/TypedEdge.java b/src/main/java/com/bio4j/angulillos/TypedEdge.java index 1833de2..0932414 100644 --- a/src/main/java/com/bio4j/angulillos/TypedEdge.java +++ b/src/main/java/com/bio4j/angulillos/TypedEdge.java @@ -10,13 +10,18 @@ - `T` the target TypedVertex, `TT` the target TypedVertex type */ abstract class EdgeType < - ST extends VertexType, - ET extends EdgeType, - TT extends VertexType, - // graph & raws - G extends TypedGraph, + // source + ST extends VertexType, + SG extends TypedGraph, + // edge itself + ET extends EdgeType, + EG extends TypedGraph, + // target + TT extends VertexType, + TG extends TypedGraph, + // raws RV,RE -> extends ElementType +> extends ElementType implements HasArity { @@ -36,12 +41,12 @@ class Edge extends Element { public Edge(RE raw) { super(raw); } /* the source vertex of this edge */ - // public final < - // SG extends TypedGraph - // > VertexType.Vertex source() { return graph.source( this ); } + public final + VertexType.Vertex source() { return graph.source( this ); } // /* the target vertex of this edge */ - // public final T target() { return graph.target( self() ); } + public final + VertexType.Vertex target() { return graph.target( this ); } @Override public X get(Property property) { return graph.getProperty(this, property); } diff --git a/src/main/java/com/bio4j/angulillos/TypedGraph.java b/src/main/java/com/bio4j/angulillos/TypedGraph.java index fad8217..fb95419 100644 --- a/src/main/java/com/bio4j/angulillos/TypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/TypedGraph.java @@ -31,14 +31,16 @@ VertexType.Vertex addVertex(VT vertexType) { /* adds an edge; note that this method does not set any properties. As it needs to be called by vertices in possibly different graphs, all the graph bounds are free with respect to G. */ default < - ST extends VertexType, - ET extends EdgeType, - TT extends VertexType + ST extends VertexType, + SG extends TypedGraph, + ET extends EdgeType, + TT extends VertexType, + TG extends TypedGraph > - EdgeType.Edge addEdge( - VertexType.Vertex source, + EdgeType.Edge addEdge( + VertexType.Vertex source, ET edgeType, - VertexType.Vertex target + VertexType.Vertex target ) { return edgeType.fromRaw( @@ -62,10 +64,10 @@ X getProperty(VertexType.Vertex vertex, Property property) { /* Get the value of a property from an edge of G. */ default < - ET extends EdgeType, + ET extends EdgeType, X > - X getProperty(EdgeType.Edge edge, Property property) { + X getProperty(EdgeType.Edge edge, Property property) { return raw().getPropertyE(edge.raw, property._label); } @@ -83,10 +85,10 @@ G setProperty(VertexType.Vertex vertex, Property property, X v /* Sets the value of a property for an edge of G. */ default < - ET extends EdgeType, + ET extends EdgeType, X > - G setProperty(EdgeType.Edge edge, Property property, X value) { + G setProperty(EdgeType.Edge edge, Property property, X value) { raw().setPropertyE(edge.raw, property._label, value); return edge.graph; @@ -100,10 +102,10 @@ G setProperty(EdgeType.Edge edge, Property property, X va default < ST extends VertexType, SG extends TypedGraph, - ET extends EdgeType + ET extends EdgeType > VertexType.Vertex source( - EdgeType.Edge edge + EdgeType.Edge edge ) { return edge.type.sourceType.fromRaw( @@ -111,29 +113,31 @@ VertexType.Vertex source( ); } - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // T target(E edge) { - // - // return edge.type().targetType().fromRaw( - // raw().target(edge.raw()) - // ); - // } + default < + ET extends EdgeType, + TT extends VertexType, + TG extends TypedGraph + > + VertexType.Vertex target( + EdgeType.Edge edge + ) { + + return edge.type.targetType.fromRaw( + raw().target(edge.raw) + ); + } /* #### Outgoing edges */ default < ST extends VertexType, - ET extends EdgeType, - TT extends VertexType, - EG extends TypedGraph + ET extends EdgeType, + EG extends TypedGraph, + TT extends VertexType, + TG extends TypedGraph > Stream< - EdgeType.Edge + EdgeType.Edge > outE(VertexType.Vertex source, ET edgeType) { return raw().outE( @@ -228,7 +232,8 @@ > outE(VertexType.Vertex source, ET edgeType) { /* #### Outgoing vertices */ default < ST extends VertexType, - ET extends EdgeType, + ET extends EdgeType, + EG extends TypedGraph, TT extends VertexType, TG extends TypedGraph > diff --git a/src/main/java/com/bio4j/angulillos/TypedVertex.java b/src/main/java/com/bio4j/angulillos/TypedVertex.java index ca595bf..57a3243 100644 --- a/src/main/java/com/bio4j/angulillos/TypedVertex.java +++ b/src/main/java/com/bio4j/angulillos/TypedVertex.java @@ -35,18 +35,20 @@ Vertex set(Property property, X value) { /* #### outE */ public final < - ET extends EdgeType, - TT extends VertexType, - EG extends TypedGraph + ET extends EdgeType, + EG extends TypedGraph, + TT extends VertexType, + TG extends TypedGraph > Stream< - EdgeType.Edge + EdgeType.Edge > outE(ET edgeType) { return graph().outE(this, edgeType); } /* #### outV */ public final < - ET extends EdgeType, + ET extends EdgeType, + EG extends TypedGraph, TT extends VertexType, TG extends TypedGraph > diff --git a/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java b/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java index 078d6c1..b66bdd0 100644 --- a/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java +++ b/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java @@ -32,8 +32,12 @@ public void doSomething(TwitterSchema.User.Vertex user) { addUser("Alice", 34) ); + Stream.Follows.Edge> followEdges = bob.outE(g.follows); Stream.Tweet.Vertex> tweets = user.outV(g.posted); Stream.User.Vertex> followees = bob.outV(g.follows); + + // TwitterSchema.User.Vertex source = g.source(edge); + TwitterSchema.User.Vertex source = edge.source(); } // public Stream.User> tweetedTheTweetsThatTweeted(TwitterSchema.User user) { diff --git a/src/test/java/com/bio4j/angulillos/TwitterSchema.java b/src/test/java/com/bio4j/angulillos/TwitterSchema.java index 32171cc..3008c6a 100644 --- a/src/test/java/com/bio4j/angulillos/TwitterSchema.java +++ b/src/test/java/com/bio4j/angulillos/TwitterSchema.java @@ -37,7 +37,11 @@ public final class Tweet extends VertexType { /* ### Edges and their types */ public final Follows follows = new Follows(); - public final class Follows extends EdgeType + public final class Follows extends EdgeType< + User, TwitterSchema, + Follows, + User, TwitterSchema + > implements AnyToAny { @Override public Follows self() { return this; } Follows() { super(user, user); } @@ -47,7 +51,11 @@ public final class Follows extends EdgeType // Any tweet is posted by exactly one user, but user may post any number of tweets (incl. 0) public final Posted posted = new Posted(); - public final class Posted extends EdgeType + public final class Posted extends EdgeType< + User, TwitterSchema, + Posted, + Tweet, TwitterSchema + > implements OneToAny { @Override public Posted self() { return this; } Posted() { super(user, tweet); } @@ -55,14 +63,14 @@ public final class Posted extends EdgeType public Property date = property("date", Date.class); } - // A reply addresses exactly one tweet, but a tweet may not have any replies - public final Replies replies = new Replies(); - public final class Replies extends EdgeType - implements AnyToOne { - @Override public Replies self() { return this; } - Replies() { super(tweet, tweet); } - - public Property date = property("date", Date.class); - } + // // A reply addresses exactly one tweet, but a tweet may not have any replies + // public final Replies replies = new Replies(); + // public final class Replies extends EdgeType + // implements AnyToOne { + // @Override public Replies self() { return this; } + // Replies() { super(tweet, tweet); } + // + // public Property date = property("date", Date.class); + // } } From 758be8430f7fbdc80dabaff8fc2ffb3a8ff6e16c Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Mon, 28 Mar 2016 23:42:08 +0200 Subject: [PATCH 03/20] Returning VT.Vertex-like types; using .new Vertex() for that; .fromRaw() doesn't work --- build.sbt | 4 ++ .../java/com/bio4j/angulillos/TypedEdge.java | 8 ++- .../com/bio4j/angulillos/TypedElement.java | 2 +- .../java/com/bio4j/angulillos/TypedGraph.java | 65 ++++++++----------- .../com/bio4j/angulillos/TypedVertex.java | 12 ++-- 5 files changed, 42 insertions(+), 49 deletions(-) diff --git a/build.sbt b/build.sbt index 726f743..7902d59 100644 --- a/build.sbt +++ b/build.sbt @@ -11,3 +11,7 @@ excludeFilter in unmanagedSources := (excludeFilter in unmanagedSources).value || "*Index.java" || "*Query.java" + +javacOptions ++= Seq( + "-Xdiags:verbose" +) diff --git a/src/main/java/com/bio4j/angulillos/TypedEdge.java b/src/main/java/com/bio4j/angulillos/TypedEdge.java index 0932414..c81c880 100644 --- a/src/main/java/com/bio4j/angulillos/TypedEdge.java +++ b/src/main/java/com/bio4j/angulillos/TypedEdge.java @@ -33,7 +33,9 @@ protected EdgeType(ST sourceType, TT targetType) { this.targetType = targetType; } - @Override public Edge fromRaw(RE raw) { return this.new Edge(raw); } + // @Override + // @SuppressWarnings("unchecked") + // public final ET.Edge fromRaw(RE raw) { return self().new Edge(raw); } class Edge extends Element { @@ -42,11 +44,11 @@ class Edge extends Element { /* the source vertex of this edge */ public final - VertexType.Vertex source() { return graph.source( this ); } + ST.Vertex source() { return graph.source( this ); } // /* the target vertex of this edge */ public final - VertexType.Vertex target() { return graph.target( this ); } + TT.Vertex target() { return graph.target( this ); } @Override public X get(Property property) { return graph.getProperty(this, property); } diff --git a/src/main/java/com/bio4j/angulillos/TypedElement.java b/src/main/java/com/bio4j/angulillos/TypedElement.java index 6d91c77..7ee8041 100644 --- a/src/main/java/com/bio4j/angulillos/TypedElement.java +++ b/src/main/java/com/bio4j/angulillos/TypedElement.java @@ -29,7 +29,7 @@ abstract class ElementType < public abstract FT self(); /* Constructs a new element of this element type */ - public abstract Element fromRaw(RF raw); + // public abstract Element fromRaw(RF raw); /* Defines a new property on this element type */ public final Property property(String nameSuffix, Class valueClass) { diff --git a/src/main/java/com/bio4j/angulillos/TypedGraph.java b/src/main/java/com/bio4j/angulillos/TypedGraph.java index fb95419..351c1bd 100644 --- a/src/main/java/com/bio4j/angulillos/TypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/TypedGraph.java @@ -22,28 +22,26 @@ interface TypedGraph < default < VT extends VertexType > - VertexType.Vertex addVertex(VT vertexType) { + VT.Vertex addVertex(VT vertexType) { - return vertexType.fromRaw( + return vertexType.new Vertex( raw().addVertex( vertexType._label ) ); } /* adds an edge; note that this method does not set any properties. As it needs to be called by vertices in possibly different graphs, all the graph bounds are free with respect to G. */ default < - ST extends VertexType, - SG extends TypedGraph, - ET extends EdgeType, - TT extends VertexType, - TG extends TypedGraph + ST extends VertexType, + ET extends EdgeType, + TT extends VertexType > - EdgeType.Edge addEdge( - VertexType.Vertex source, + ET.Edge addEdge( + VertexType.Vertex source, ET edgeType, - VertexType.Vertex target + VertexType.Vertex target ) { - return edgeType.fromRaw( + return edgeType.new Edge( raw().addEdge( source.raw, edgeType._label, target.raw ) ); } @@ -100,29 +98,27 @@ G setProperty(EdgeType.Edge edge, Property property gets the source of an edge of G, which could be of a different graph. */ default < - ST extends VertexType, - SG extends TypedGraph, - ET extends EdgeType + ST extends VertexType, + ET extends EdgeType > - VertexType.Vertex source( - EdgeType.Edge edge + ST.Vertex source( + EdgeType.Edge edge ) { - return edge.type.sourceType.fromRaw( + return edge.type.sourceType.new Vertex( raw().source(edge.raw) ); } default < - ET extends EdgeType, - TT extends VertexType, - TG extends TypedGraph + ET extends EdgeType, + TT extends VertexType > - VertexType.Vertex target( - EdgeType.Edge edge + TT.Vertex target( + EdgeType.Edge edge ) { - return edge.type.targetType.fromRaw( + return edge.type.targetType.new Vertex( raw().target(edge.raw) ); } @@ -131,20 +127,15 @@ VertexType.Vertex target( /* #### Outgoing edges */ default < ST extends VertexType, - ET extends EdgeType, - EG extends TypedGraph, - TT extends VertexType, - TG extends TypedGraph + ET extends EdgeType > - Stream< - EdgeType.Edge - > outE(VertexType.Vertex source, ET edgeType) { + Stream outE(VertexType.Vertex source, ET edgeType) { return raw().outE( source.raw, edgeType._label - ).map( - edgeType::fromRaw + ).map( re -> + edgeType.new Edge(re) ); } @@ -232,20 +223,18 @@ > outE(VertexType.Vertex source, ET edgeType) { /* #### Outgoing vertices */ default < ST extends VertexType, - ET extends EdgeType, - EG extends TypedGraph, - TT extends VertexType, - TG extends TypedGraph + ET extends EdgeType, + TT extends VertexType > Stream< - VertexType.Vertex + TT.Vertex > outV(VertexType.Vertex source, ET edgeType) { return raw().outV( source.raw, edgeType._label ).map( rawT -> - edgeType.targetType.fromRaw(rawT) + edgeType.targetType.new Vertex(rawT) ); } diff --git a/src/main/java/com/bio4j/angulillos/TypedVertex.java b/src/main/java/com/bio4j/angulillos/TypedVertex.java index 57a3243..913654c 100644 --- a/src/main/java/com/bio4j/angulillos/TypedVertex.java +++ b/src/main/java/com/bio4j/angulillos/TypedVertex.java @@ -15,7 +15,9 @@ abstract class VertexType < RV,RE > extends ElementType { - @Override public Vertex fromRaw(RV raw) { return this.new Vertex(raw); } + // @Override + // @SuppressWarnings("unchecked") + // public final VT.Vertex fromRaw(RV raw) { return self().new Vertex(raw); } public final class Vertex extends Element { @@ -40,9 +42,7 @@ Vertex set(Property property, X value) { TT extends VertexType, TG extends TypedGraph > - Stream< - EdgeType.Edge - > outE(ET edgeType) { return graph().outE(this, edgeType); } + Stream outE(ET edgeType) { return graph().outE(this, edgeType); } /* #### outV */ @@ -52,9 +52,7 @@ Vertex set(Property property, X value) { TT extends VertexType, TG extends TypedGraph > - Stream< - VertexType.Vertex - > outV(ET edgeType) { return graph().outV(this, edgeType); } + Stream outV(ET edgeType) { return graph().outV(this, edgeType); } } From e9bb8a1e3f553c6b7983f0e36f3373ee07f6353c Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Tue, 29 Mar 2016 01:10:48 +0200 Subject: [PATCH 04/20] Cleaned build well, now .fromRaw() works as expected; added a note about the SuppressWarnings annotation --- src/main/java/com/bio4j/angulillos/TypedEdge.java | 9 +++++---- src/main/java/com/bio4j/angulillos/TypedElement.java | 3 --- src/main/java/com/bio4j/angulillos/TypedGraph.java | 12 ++++++------ src/main/java/com/bio4j/angulillos/TypedVertex.java | 9 +++++---- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/bio4j/angulillos/TypedEdge.java b/src/main/java/com/bio4j/angulillos/TypedEdge.java index c81c880..b5614a5 100644 --- a/src/main/java/com/bio4j/angulillos/TypedEdge.java +++ b/src/main/java/com/bio4j/angulillos/TypedEdge.java @@ -33,14 +33,15 @@ protected EdgeType(ST sourceType, TT targetType) { this.targetType = targetType; } - // @Override - // @SuppressWarnings("unchecked") - // public final ET.Edge fromRaw(RE raw) { return self().new Edge(raw); } + // NOTE: this call is typesafe, but the compiler cannot check it here, because the RV type in self() is not bound to be the same as we use from the enclousing class context + @SuppressWarnings("unchecked") + /* This method should be used for constructing _all_ instances of the Edge inner class to get the precise return type */ + public final ET.Edge fromRaw(RE raw) { return self().new Edge(raw); } class Edge extends Element { - public Edge(RE raw) { super(raw); } + private Edge(RE raw) { super(raw); } /* the source vertex of this edge */ public final diff --git a/src/main/java/com/bio4j/angulillos/TypedElement.java b/src/main/java/com/bio4j/angulillos/TypedElement.java index 7ee8041..0a36875 100644 --- a/src/main/java/com/bio4j/angulillos/TypedElement.java +++ b/src/main/java/com/bio4j/angulillos/TypedElement.java @@ -28,9 +28,6 @@ abstract class ElementType < /* An abstract reference to the instance of the implementing class. This should return `this` in all cases; it just cannot be implemented at this level */ public abstract FT self(); - /* Constructs a new element of this element type */ - // public abstract Element fromRaw(RF raw); - /* Defines a new property on this element type */ public final Property property(String nameSuffix, Class valueClass) { return new Property(self(), nameSuffix, valueClass); diff --git a/src/main/java/com/bio4j/angulillos/TypedGraph.java b/src/main/java/com/bio4j/angulillos/TypedGraph.java index 351c1bd..0d8d82a 100644 --- a/src/main/java/com/bio4j/angulillos/TypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/TypedGraph.java @@ -24,7 +24,7 @@ interface TypedGraph < > VT.Vertex addVertex(VT vertexType) { - return vertexType.new Vertex( + return vertexType.fromRaw( raw().addVertex( vertexType._label ) ); } @@ -41,7 +41,7 @@ ET.Edge addEdge( VertexType.Vertex target ) { - return edgeType.new Edge( + return edgeType.fromRaw( raw().addEdge( source.raw, edgeType._label, target.raw ) ); } @@ -105,7 +105,7 @@ ST.Vertex source( EdgeType.Edge edge ) { - return edge.type.sourceType.new Vertex( + return edge.type.sourceType.fromRaw( raw().source(edge.raw) ); } @@ -118,7 +118,7 @@ TT.Vertex target( EdgeType.Edge edge ) { - return edge.type.targetType.new Vertex( + return edge.type.targetType.fromRaw( raw().target(edge.raw) ); } @@ -135,7 +135,7 @@ Stream outE(VertexType.Vertex source, ET edgeType) { source.raw, edgeType._label ).map( re -> - edgeType.new Edge(re) + edgeType.fromRaw(re) ); } @@ -234,7 +234,7 @@ > outV(VertexType.Vertex source, ET edgeType) { source.raw, edgeType._label ).map( rawT -> - edgeType.targetType.new Vertex(rawT) + edgeType.targetType.fromRaw(rawT) ); } diff --git a/src/main/java/com/bio4j/angulillos/TypedVertex.java b/src/main/java/com/bio4j/angulillos/TypedVertex.java index 913654c..2086b87 100644 --- a/src/main/java/com/bio4j/angulillos/TypedVertex.java +++ b/src/main/java/com/bio4j/angulillos/TypedVertex.java @@ -15,13 +15,14 @@ abstract class VertexType < RV,RE > extends ElementType { - // @Override - // @SuppressWarnings("unchecked") - // public final VT.Vertex fromRaw(RV raw) { return self().new Vertex(raw); } + // NOTE: this call is typesafe, but the compiler cannot check it here, because the RV type in self() is not bound to be the same as we use from the enclousing class context + @SuppressWarnings("unchecked") + /* This method should be used for constructing _all_ instances of the Vertex inner class to get the precise return type */ + public final VT.Vertex fromRaw(RV raw) { return self().new Vertex(raw); } public final class Vertex extends Element { - public Vertex(RV raw) { super(raw); } + private Vertex(RV raw) { super(raw); } /* ### Properties */ From fbbaffdd6109db94b434ba4d14caa04fda76394e Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Tue, 29 Mar 2016 01:17:15 +0200 Subject: [PATCH 05/20] Added arity-specific methods in/out-E/V methods with default implementations --- .../java/com/bio4j/angulillos/UntypedGraph.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/com/bio4j/angulillos/UntypedGraph.java b/src/main/java/com/bio4j/angulillos/UntypedGraph.java index 600445a..c16970e 100644 --- a/src/main/java/com/bio4j/angulillos/UntypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/UntypedGraph.java @@ -1,6 +1,7 @@ package com.bio4j.angulillos; import java.util.stream.Stream; +import java.util.Optional; /* @@ -26,13 +27,28 @@ interface UntypedGraph { /* - Get the edges of type `edgeType` _out_ of `vertex` */ Stream outE(RV vertex, String edgeLabel); + default Stream outAtLeastOneE(RV vertex, String edgeLabel) { return outE(vertex, edgeLabel); } + default Optional outAtMostOneE(RV vertex, String edgeLabel) { return outE(vertex, edgeLabel).findFirst(); } + default RE outOneE(RV vertex, String edgeLabel) { return outE(vertex, edgeLabel).findFirst().get(); } + /* - Get the _target_ vertices of the edges of type `edgeType` _out_ of `vertex` */ Stream outV(RV vertex, String edgeLabel); + default Stream outAtLeastOneV(RV vertex, String edgeLabel) { return outV(vertex, edgeLabel); } + default Optional outAtMostOneV(RV vertex, String edgeLabel) { return outV(vertex, edgeLabel).findFirst(); } + default RV outOneV(RV vertex, String edgeLabel) { return outV(vertex, edgeLabel).findFirst().get(); } + /* - Get the edges of type `edgeType` _into_ `vertex` */ Stream inE(RV vertex, String edgeLabel); + default Stream inAtLeastOneE(RV vertex, String edgeLabel) { return inE(vertex, edgeLabel); } + default Optional inAtMostOneE(RV vertex, String edgeLabel) { return inE(vertex, edgeLabel).findFirst(); } + default RE inOneE(RV vertex, String edgeLabel) { return inE(vertex, edgeLabel).findFirst().get(); } + /* - Get the _source_ vertices of the edges of type `edgeType` _into_ `vertex` */ Stream inV(RV vertex, String edgeLabel); + default Stream inAtLeastOneV(RV vertex, String edgeLabel) { return inV(vertex, edgeLabel); } + default Optional inAtMostOneV(RV vertex, String edgeLabel) { return inV(vertex, edgeLabel).findFirst(); } + default RV inOneV(RV vertex, String edgeLabel) { return inV(vertex, edgeLabel).findFirst().get(); } /* #### Methods on edges */ From 7bb50cf6ddff34de8d6b5fd6d638bb417e028fab Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Tue, 29 Mar 2016 01:39:50 +0200 Subject: [PATCH 06/20] Moved all in/out methods from graph to Vertex class --- .../java/com/bio4j/angulillos/TypedGraph.java | 201 ----------- .../com/bio4j/angulillos/TypedVertex.java | 318 +++++++++--------- 2 files changed, 159 insertions(+), 360 deletions(-) diff --git a/src/main/java/com/bio4j/angulillos/TypedGraph.java b/src/main/java/com/bio4j/angulillos/TypedGraph.java index 0d8d82a..6d23307 100644 --- a/src/main/java/com/bio4j/angulillos/TypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/TypedGraph.java @@ -123,205 +123,4 @@ TT.Vertex target( ); } - - /* #### Outgoing edges */ - default < - ST extends VertexType, - ET extends EdgeType - > - Stream outE(VertexType.Vertex source, ET edgeType) { - - return raw().outE( - source.raw, - edgeType._label - ).map( re -> - edgeType.fromRaw(re) - ); - } - - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.ToAtLeastOne - // > - // Stream outAtLeastOneE(S source, ET edgeType) { - // - // return outE(source, edgeType); - // } - // - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.ToAtMostOne - // > - // Optional outAtMostOneE(S source, ET edgeType) { - // - // return outE(source, edgeType).findFirst(); - // } - // - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.ToOne - // > - // E outOneE(S source, ET edgeType) { - // - // return outE(source, edgeType).findFirst().get(); - // } - // - // /* #### Incoming edges */ - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // Stream inE(T vertex, ET edgeType) { - // - // return raw().inE( - // vertex.raw(), - // edgeType._label() - // ).map( - // edgeType::fromRaw - // ); - // } - // - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.FromAtLeastOne, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // Stream inAtLeastOneE(T target, ET edgeType) { return inE(target, edgeType); } - // - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.FromAtMostOne, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // Optional inAtMostOneE(T target, ET edgeType) { return inE(target, edgeType).findFirst(); } - // - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.FromOne, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // E inOneE(T target, ET edgeType) { return inE(target, edgeType).findFirst().get(); } - // - - /* #### Outgoing vertices */ - default < - ST extends VertexType, - ET extends EdgeType, - TT extends VertexType - > - Stream< - TT.Vertex - > outV(VertexType.Vertex source, ET edgeType) { - - return raw().outV( - source.raw, - edgeType._label - ).map( rawT -> - edgeType.targetType.fromRaw(rawT) - ); - } - - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.ToAtLeastOne, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // Stream outAtLeastOneV(S source, ET edgeType) { return outV(source, edgeType); } - // - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.ToAtMostOne, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // Optional outAtMostOneV(S source, ET edgeType) { return outV(source, edgeType).findFirst(); } - // - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.ToOne, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // T outOneV(S source, ET edgeType) { return outV(source, edgeType).findFirst().get(); } - // - // - // /* #### Incoming vertices */ - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // Stream inV(T target, ET edgeType) { - // - // return raw().inV( - // target.raw(), - // edgeType._label() - // ).map( - // edgeType.sourceType()::fromRaw - // ); - // } - // - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.FromAtLeastOne, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // Stream inAtLeastOneV(T target, ET edgeType) { return inV(target, edgeType); } - // - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.FromAtMostOne, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // Optional inAtMostOneV(T target, ET edgeType) { return inV(target, edgeType).findFirst(); } - // - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.FromOne, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // S inOneV(T target, ET edgeType) { return inV(target, edgeType).findFirst().get(); } - } diff --git a/src/main/java/com/bio4j/angulillos/TypedVertex.java b/src/main/java/com/bio4j/angulillos/TypedVertex.java index 2086b87..e0b665b 100644 --- a/src/main/java/com/bio4j/angulillos/TypedVertex.java +++ b/src/main/java/com/bio4j/angulillos/TypedVertex.java @@ -27,177 +27,177 @@ public final class Vertex extends Element { /* ### Properties */ @Override public - X get(Property property) { return graph().getProperty(this, property); } + X get(Property property) { return graph.getProperty(this, property); } @Override public Vertex set(Property property, X value) { - graph().setProperty(this, property, value); + graph.setProperty(this, property, value); return this; } - /* #### outE */ - public final < - ET extends EdgeType, - EG extends TypedGraph, - TT extends VertexType, - TG extends TypedGraph + + /* #### Outgoing edges */ + + public < + ET extends EdgeType > - Stream outE(ET edgeType) { return graph().outE(this, edgeType); } + Stream outE(ET edgeType) { + return graph.raw() + .outE(this.raw, edgeType._label) + .map( edgeType::fromRaw ); + } + public < + ET extends EdgeType & ToAtLeastOne + > + Stream outAtLeastOneE(ET edgeType) { + return graph.raw() + .outAtLeastOneE(this.raw, edgeType._label) + .map( edgeType::fromRaw ); + } - /* #### outV */ - public final < - ET extends EdgeType, - EG extends TypedGraph, - TT extends VertexType, - TG extends TypedGraph + public < + ET extends EdgeType & ToAtMostOne > - Stream outV(ET edgeType) { return graph().outV(this, edgeType); } + Optional outAtMostOneE(ET edgeType) { + return graph.raw() + .outAtMostOneE(this.raw, edgeType._label) + .map( edgeType::fromRaw ); + } - } + public < + ET extends EdgeType & ToOne + > + ET.Edge outOneE(ET edgeType) { + return edgeType.fromRaw( + graph.raw().outOneE(this.raw, edgeType._label) + ); + } + + /* #### Incoming edges */ + + public < + ET extends EdgeType + > + Stream inE(ET edgeType) { + return graph.raw() + .inE(this.raw, edgeType._label) + .map( edgeType::fromRaw ); + } + + public < + ET extends EdgeType & FromAtLeastOne + > + Stream inAtLeastOneE(ET edgeType) { + return graph.raw() + .inAtLeastOneE(this.raw, edgeType._label) + .map( edgeType::fromRaw ); + } + + public < + ET extends EdgeType & FromAtMostOne + > + Optional inAtMostOneE(ET edgeType) { + return graph.raw() + .inAtMostOneE(this.raw, edgeType._label) + .map( edgeType::fromRaw ); + } + + public < + ET extends EdgeType & FromOne + > + ET.Edge inOneE(ET edgeType) { + return edgeType.fromRaw( + graph.raw().inOneE(this.raw, edgeType._label) + ); + } + + + /* #### Outgoing vertices */ + + public < + ET extends EdgeType, + TT extends VertexType + > + Stream outV(ET edgeType) { + return graph.raw() + .outV(this.raw, edgeType._label) + .map( edgeType.targetType::fromRaw ); + } - // /* - // ### Create edges in/out of this vertex - // - // There are two methods for creating new edges, into and out of this vertex respectively. Their implementation delegates to the typed graph methods. Note that all graphs are in principle different. - // */ - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type - // > - // E addInEdge(S from, ET edgeType) { return graph().addEdge( from, edgeType, self() ); } - // - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // E addOutEdge(ET edgeType, T to) { return graph().addEdge( self(), edgeType, to ); } - - - // /* - // ### Getting incoming and outgoing edges - // - // For when you don't know anything about the arity, we have unbounded in/out methods which return `Stream`s - // */ - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.ToAtLeastOne - // > - // Stream outAtLeastOneE(ET edgeType) { return graph().outAtLeastOneE(self(), edgeType); } - // - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.ToAtMostOne - // > - // Optional outAtMostOneE(ET edgeType) { return graph().outAtMostOneE(self(), edgeType); } - // - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.ToOne - // > - // E outOneE(ET edgeType) { return graph().outOneE(self(), edgeType); } - // - // - // /* #### inE */ - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type - // > - // Stream inE(ET edgeType) { return graph().inE(self(), edgeType); } - // - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.FromAtLeastOne - // > - // Stream inAtLeastOneE(ET edgeType) { return graph().inAtLeastOneE(self(), edgeType); } - // - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.FromAtMostOne - // > - // Optional inAtMostOneE(ET edgeType) { return graph().inAtMostOneE(self(), edgeType); } - // - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.FromOne - // > - // E inOneE(ET edgeType) { return graph().inOneE(self(), edgeType); } - // - // ///////////////////////////////////////////////////////////////////////////////////////////////////// - // - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.ToAtLeastOne, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // Stream outAtLeastOneV(ET edgeType) { return graph().outAtLeastOneV(self(), edgeType); } - // - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.ToAtMostOne, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // Optional outAtMostOneV(ET edgeType) { return graph().outAtMostOneV(self(), edgeType); } - // - // default < - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.ToOne, - // T extends TypedVertex, - // TT extends TypedVertex.Type - // > - // T outOneV(ET edgeType) { return graph().outOneV(self(), edgeType); } - // - // - // /* #### inV */ - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type - // > - // Stream inV(ET edgeType) { return graph().inV(self(), edgeType); } - // - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.FromAtLeastOne - // > - // Stream inAtLeastOneV(ET edgeType) { return graph().inAtLeastOneV(self(), edgeType); } - // - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.FromAtMostOne - // > - // Optional inAtMostOneV(ET edgeType) { return graph().inAtMostOneV(self(), edgeType); } - // - // default < - // S extends TypedVertex, - // ST extends TypedVertex.Type, - // E extends TypedEdge, - // ET extends TypedEdge.Type - // & TypedEdge.Type.FromOne - // > - // S inOneV(ET edgeType) { return graph().inOneV(self(), edgeType); } + public < + ET extends EdgeType & ToAtLeastOne, + TT extends VertexType + > + Stream outAtLeastOneV(ET edgeType) { + return graph.raw() + .outAtLeastOneV(this.raw, edgeType._label) + .map( edgeType.targetType::fromRaw ); + } + + public < + ET extends EdgeType & ToAtMostOne, + TT extends VertexType + > + Optional outAtMostOneV(ET edgeType) { + return graph.raw() + .outAtMostOneV(this.raw, edgeType._label) + .map( edgeType.targetType::fromRaw ); + } + + public < + ET extends EdgeType & ToOne, + TT extends VertexType + > + TT.Vertex outOneV(ET edgeType) { + return edgeType.targetType.fromRaw( + graph.raw().outOneV(this.raw, edgeType._label) + ); + } + + /* #### Incoming vertices */ + + public < + ST extends VertexType, + ET extends EdgeType + > + Stream inV(ET edgeType) { + return graph.raw() + .inV(this.raw, edgeType._label) + .map( edgeType.sourceType::fromRaw ); + } + + public < + ST extends VertexType, + ET extends EdgeType & FromAtLeastOne + > + Stream inAtLeastOneV(ET edgeType) { + return graph.raw() + .inAtLeastOneV(this.raw, edgeType._label) + .map( edgeType.sourceType::fromRaw ); + } + + public < + ST extends VertexType, + ET extends EdgeType & FromAtMostOne + > + Optional inAtMostOneV(ET edgeType) { + return graph.raw() + .inAtMostOneV(this.raw, edgeType._label) + .map( edgeType.sourceType::fromRaw ); + } + + public < + ST extends VertexType, + ET extends EdgeType & FromOne + > + ST.Vertex inOneV(ET edgeType) { + return edgeType.sourceType.fromRaw( + graph.raw().inOneV(this.raw, edgeType._label) + ); + } + + } } From 23ebbf083c0b3834b73a9de756c9bba3785510ef Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Tue, 29 Mar 2016 01:51:10 +0200 Subject: [PATCH 07/20] Moved source/target, all propeties-related methods and addVertex/addEdge to the corresponding classes; TypedGraph is empty now --- .../java/com/bio4j/angulillos/TypedEdge.java | 40 +++++-- .../java/com/bio4j/angulillos/TypedGraph.java | 104 ------------------ .../com/bio4j/angulillos/TypedVertex.java | 15 ++- 3 files changed, 44 insertions(+), 115 deletions(-) diff --git a/src/main/java/com/bio4j/angulillos/TypedEdge.java b/src/main/java/com/bio4j/angulillos/TypedEdge.java index b5614a5..0bc2d99 100644 --- a/src/main/java/com/bio4j/angulillos/TypedEdge.java +++ b/src/main/java/com/bio4j/angulillos/TypedEdge.java @@ -38,29 +38,51 @@ protected EdgeType(ST sourceType, TT targetType) { /* This method should be used for constructing _all_ instances of the Edge inner class to get the precise return type */ public final ET.Edge fromRaw(RE raw) { return self().new Edge(raw); } + /* This method adds a new edge of this type to the graph */ + public ET.Edge addEdge( + VertexType.Vertex source, + ET edgeType, + VertexType.Vertex target + ) { + return fromRaw( + graph().raw().addEdge( source.raw, this._label, target.raw ) + ); + } + class Edge extends Element { private Edge(RE raw) { super(raw); } - /* the source vertex of this edge */ - public final - ST.Vertex source() { return graph.source( this ); } - - // /* the target vertex of this edge */ - public final - TT.Vertex target() { return graph.target( this ); } + /* ### Properties */ @Override public - X get(Property property) { return graph.getProperty(this, property); } + X get(Property property) { + return graph.raw().getPropertyE(this.raw, property._label); + } @Override public Edge set(Property property, X value) { - graph().setProperty(this, property, value); + graph.raw().setPropertyE(this.raw, property._label, value); return this; } + + /* the source vertex of this edge */ + public ST.Vertex source() { + return sourceType.fromRaw( + graph.raw().source( this.raw ) + ); + } + + /* the target vertex of this edge */ + public TT.Vertex target() { + return targetType.fromRaw( + graph.raw().target( this.raw ) + ); + } + } } diff --git a/src/main/java/com/bio4j/angulillos/TypedGraph.java b/src/main/java/com/bio4j/angulillos/TypedGraph.java index 6d23307..687f112 100644 --- a/src/main/java/com/bio4j/angulillos/TypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/TypedGraph.java @@ -19,108 +19,4 @@ interface TypedGraph < UntypedGraph raw(); - default < - VT extends VertexType - > - VT.Vertex addVertex(VT vertexType) { - - return vertexType.fromRaw( - raw().addVertex( vertexType._label ) - ); - } - - /* adds an edge; note that this method does not set any properties. As it needs to be called by vertices in possibly different graphs, all the graph bounds are free with respect to G. */ - default < - ST extends VertexType, - ET extends EdgeType, - TT extends VertexType - > - ET.Edge addEdge( - VertexType.Vertex source, - ET edgeType, - VertexType.Vertex target - ) { - - return edgeType.fromRaw( - raw().addEdge( source.raw, edgeType._label, target.raw ) - ); - } - - /* - ### properties - - These methods are used for setting and getting properties on vertices and edges. - */ - default < - VT extends VertexType, - X - > - X getProperty(VertexType.Vertex vertex, Property property) { - - return raw().getPropertyV(vertex.raw, property._label); - } - - /* Get the value of a property from an edge of G. */ - default < - ET extends EdgeType, - X - > - X getProperty(EdgeType.Edge edge, Property property) { - - return raw().getPropertyE(edge.raw, property._label); - } - - /* Sets the value of a property for a vertex of G. */ - default < - VT extends VertexType, - X - > - G setProperty(VertexType.Vertex vertex, Property property, X value) { - - raw().setPropertyV(vertex.raw, property._label, value); - return vertex.graph; - } - - /* Sets the value of a property for an edge of G. */ - default < - ET extends EdgeType, - X - > - G setProperty(EdgeType.Edge edge, Property property, X value) { - - raw().setPropertyE(edge.raw, property._label, value); - return edge.graph; - } - - /* - ### source and target - - gets the source of an edge of G, which could be of a different graph. - */ - default < - ST extends VertexType, - ET extends EdgeType - > - ST.Vertex source( - EdgeType.Edge edge - ) { - - return edge.type.sourceType.fromRaw( - raw().source(edge.raw) - ); - } - - default < - ET extends EdgeType, - TT extends VertexType - > - TT.Vertex target( - EdgeType.Edge edge - ) { - - return edge.type.targetType.fromRaw( - raw().target(edge.raw) - ); - } - } diff --git a/src/main/java/com/bio4j/angulillos/TypedVertex.java b/src/main/java/com/bio4j/angulillos/TypedVertex.java index e0b665b..6945708 100644 --- a/src/main/java/com/bio4j/angulillos/TypedVertex.java +++ b/src/main/java/com/bio4j/angulillos/TypedVertex.java @@ -20,6 +20,15 @@ abstract class VertexType < /* This method should be used for constructing _all_ instances of the Vertex inner class to get the precise return type */ public final VT.Vertex fromRaw(RV raw) { return self().new Vertex(raw); } + + /* This method adds a new vertex of this type to the graph */ + public VT.Vertex addVertex() { + return fromRaw( + graph().raw().addVertex( this._label ) + ); + } + + public final class Vertex extends Element { private Vertex(RV raw) { super(raw); } @@ -27,12 +36,14 @@ public final class Vertex extends Element { /* ### Properties */ @Override public - X get(Property property) { return graph.getProperty(this, property); } + X get(Property property) { + return graph.raw().getPropertyV(this.raw, property._label); + } @Override public Vertex set(Property property, X value) { - graph.setProperty(this, property, value); + graph.raw().setPropertyV(this.raw, property._label, value); return this; } From f17a4e75e505b784884c60d2a4f1875fd70d8b9f Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Tue, 29 Mar 2016 01:59:03 +0200 Subject: [PATCH 08/20] Just moved things from GraphSchema to TypedGraph (now it's one class) --- .../com/bio4j/angulillos/GraphSchema.java | 39 ------------------- .../java/com/bio4j/angulillos/TypedGraph.java | 38 ++++++++++++++++-- 2 files changed, 34 insertions(+), 43 deletions(-) delete mode 100644 src/main/java/com/bio4j/angulillos/GraphSchema.java diff --git a/src/main/java/com/bio4j/angulillos/GraphSchema.java b/src/main/java/com/bio4j/angulillos/GraphSchema.java deleted file mode 100644 index d2b75ec..0000000 --- a/src/main/java/com/bio4j/angulillos/GraphSchema.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.bio4j.angulillos; - - -public abstract class GraphSchema< - G extends GraphSchema, - RV,RE -> implements TypedGraph { - - public abstract G self(); - - private final UntypedGraph raw; - @Override public final UntypedGraph raw() { return this.raw; } - - protected GraphSchema(UntypedGraph raw) { this.raw = raw; } - - public abstract class VertexType< - VT extends VertexType - > extends com.bio4j.angulillos.VertexType { - - @Override public final G graph() { return GraphSchema.this.self(); } - } - - public abstract class EdgeType< - // source - ST extends com.bio4j.angulillos.VertexType, - SG extends TypedGraph, - // edge itself - ET extends EdgeType, - // target - TT extends com.bio4j.angulillos.VertexType, - TG extends TypedGraph - > extends com.bio4j.angulillos.EdgeType { - - public EdgeType(ST sourceType, TT targetType) { super(sourceType, targetType); } - - @Override public final G graph() { return GraphSchema.this.self(); } - } - -} diff --git a/src/main/java/com/bio4j/angulillos/TypedGraph.java b/src/main/java/com/bio4j/angulillos/TypedGraph.java index 687f112..aca2c7b 100644 --- a/src/main/java/com/bio4j/angulillos/TypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/TypedGraph.java @@ -11,12 +11,42 @@ A `TypedGraph` is, unsurprisingly, the typed version of [UntypedGraph](UntypedGraph.java.md). */ -interface TypedGraph < +public abstract class TypedGraph < G extends TypedGraph, RV,RE -> -{ +> { - UntypedGraph raw(); + public abstract G self(); + + private final UntypedGraph raw; + public final UntypedGraph raw() { return this.raw; } + + protected TypedGraph(UntypedGraph raw) { this.raw = raw; } + + + /* Refined graph element classes */ + + public abstract class VertexType< + VT extends VertexType + > extends com.bio4j.angulillos.VertexType { + + @Override public final G graph() { return TypedGraph.this.self(); } + } + + public abstract class EdgeType< + // source + ST extends com.bio4j.angulillos.VertexType, + SG extends TypedGraph, + // edge itself + ET extends EdgeType, + // target + TT extends com.bio4j.angulillos.VertexType, + TG extends TypedGraph + > extends com.bio4j.angulillos.EdgeType { + + protected EdgeType(ST sourceType, TT targetType) { super(sourceType, targetType); } + + @Override public final G graph() { return TypedGraph.this.self(); } + } } From 1a8b15a991b8a0cefd74cc798cd95d6e9c24c18e Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Tue, 29 Mar 2016 02:04:07 +0200 Subject: [PATCH 09/20] Made top-level classes public, renamed files --- .../com/bio4j/angulillos/{TypedEdge.java => EdgeType.java} | 6 +----- .../angulillos/{TypedElement.java => ElementType.java} | 0 src/main/java/com/bio4j/angulillos/Property.java | 2 +- src/main/java/com/bio4j/angulillos/TypedGraph.java | 5 ----- .../bio4j/angulillos/{TypedVertex.java => VertexType.java} | 6 +++--- 5 files changed, 5 insertions(+), 14 deletions(-) rename src/main/java/com/bio4j/angulillos/{TypedEdge.java => EdgeType.java} (91%) rename src/main/java/com/bio4j/angulillos/{TypedElement.java => ElementType.java} (100%) rename src/main/java/com/bio4j/angulillos/{TypedVertex.java => VertexType.java} (95%) diff --git a/src/main/java/com/bio4j/angulillos/TypedEdge.java b/src/main/java/com/bio4j/angulillos/EdgeType.java similarity index 91% rename from src/main/java/com/bio4j/angulillos/TypedEdge.java rename to src/main/java/com/bio4j/angulillos/EdgeType.java index 0bc2d99..69cd0da 100644 --- a/src/main/java/com/bio4j/angulillos/TypedEdge.java +++ b/src/main/java/com/bio4j/angulillos/EdgeType.java @@ -4,12 +4,8 @@ ## Edges A typed edge with explicit source and target. - - - `S` the source TypedVertex, `ST` the source TypedVertex type - - `E` the edge, `ET` the edge type - - `T` the target TypedVertex, `TT` the target TypedVertex type */ -abstract class EdgeType < +public abstract class EdgeType < // source ST extends VertexType, SG extends TypedGraph, diff --git a/src/main/java/com/bio4j/angulillos/TypedElement.java b/src/main/java/com/bio4j/angulillos/ElementType.java similarity index 100% rename from src/main/java/com/bio4j/angulillos/TypedElement.java rename to src/main/java/com/bio4j/angulillos/ElementType.java diff --git a/src/main/java/com/bio4j/angulillos/Property.java b/src/main/java/com/bio4j/angulillos/Property.java index 7151c12..6b5e7be 100644 --- a/src/main/java/com/bio4j/angulillos/Property.java +++ b/src/main/java/com/bio4j/angulillos/Property.java @@ -3,7 +3,7 @@ /* ## Properties - A property of the [Element](TypedElement.java.md) of type `ET`, with value type `X`. + A property of the of type `FT`, with value type `X`. */ public class Property < FT extends ElementType, diff --git a/src/main/java/com/bio4j/angulillos/TypedGraph.java b/src/main/java/com/bio4j/angulillos/TypedGraph.java index aca2c7b..62b7478 100644 --- a/src/main/java/com/bio4j/angulillos/TypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/TypedGraph.java @@ -1,10 +1,5 @@ package com.bio4j.angulillos; -import static com.bio4j.angulillos.conversions.*; - -import java.util.stream.Stream; -import java.util.Optional; - /* ## Typed graphs diff --git a/src/main/java/com/bio4j/angulillos/TypedVertex.java b/src/main/java/com/bio4j/angulillos/VertexType.java similarity index 95% rename from src/main/java/com/bio4j/angulillos/TypedVertex.java rename to src/main/java/com/bio4j/angulillos/VertexType.java index 6945708..888daf4 100644 --- a/src/main/java/com/bio4j/angulillos/TypedVertex.java +++ b/src/main/java/com/bio4j/angulillos/VertexType.java @@ -5,11 +5,11 @@ /* - ## Typed Vertices + ## Vertex Type - A typed vertex. A vertex and its type need to be defined at the same time. The vertex keeps a reference of its type, while the type works as a factory for creating vertices with that type. + The vertex keeps a reference of its type, while the type works as a factory for creating vertices with that type. */ -abstract class VertexType < +public abstract class VertexType < VT extends VertexType, G extends TypedGraph, RV,RE From a41459edc3ff47a3b37d6d8ee35ae7e63f0e0571 Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Tue, 29 Mar 2016 13:20:22 +0200 Subject: [PATCH 10/20] Added TypedLinkGraph (not the best name, but will work for now) --- .../java/com/bio4j/angulillos/TypedGraph.java | 17 +++++------- .../com/bio4j/angulillos/TypedLinkGraph.java | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/bio4j/angulillos/TypedLinkGraph.java diff --git a/src/main/java/com/bio4j/angulillos/TypedGraph.java b/src/main/java/com/bio4j/angulillos/TypedGraph.java index 62b7478..f84e283 100644 --- a/src/main/java/com/bio4j/angulillos/TypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/TypedGraph.java @@ -21,23 +21,20 @@ public abstract class TypedGraph < /* Refined graph element classes */ + /* Defines an vertex of _this_ graph with fixed raw types */ public abstract class VertexType< - VT extends VertexType + VT extends G.VertexType > extends com.bio4j.angulillos.VertexType { @Override public final G graph() { return TypedGraph.this.self(); } } + /* Defines an edge between two vertices of _this_ graph */ public abstract class EdgeType< - // source - ST extends com.bio4j.angulillos.VertexType, - SG extends TypedGraph, - // edge itself - ET extends EdgeType, - // target - TT extends com.bio4j.angulillos.VertexType, - TG extends TypedGraph - > extends com.bio4j.angulillos.EdgeType { + ST extends G.VertexType, + ET extends G.EdgeType, + TT extends G.VertexType + > extends com.bio4j.angulillos.EdgeType { protected EdgeType(ST sourceType, TT targetType) { super(sourceType, targetType); } diff --git a/src/main/java/com/bio4j/angulillos/TypedLinkGraph.java b/src/main/java/com/bio4j/angulillos/TypedLinkGraph.java new file mode 100644 index 0000000..4cfd282 --- /dev/null +++ b/src/main/java/com/bio4j/angulillos/TypedLinkGraph.java @@ -0,0 +1,27 @@ +package com.bio4j.angulillos; + +public abstract class TypedLinkGraph < + SG extends TypedGraph, + G extends TypedLinkGraph, + TG extends TypedGraph, + RV,RE +> extends TypedGraph { + + // NOTE: this raw has to be the same as the SG.raw and TG.raw + protected TypedLinkGraph(UntypedGraph raw) { super(raw); } + + + /* Refined graph element classes */ + + public abstract class LinkEdgeType< + ST extends com.bio4j.angulillos.VertexType, + ET extends G.LinkEdgeType, + TT extends com.bio4j.angulillos.VertexType + > extends com.bio4j.angulillos.EdgeType { + + protected LinkEdgeType(ST sourceType, TT targetType) { super(sourceType, targetType); } + + @Override public final G graph() { return TypedLinkGraph.this.self(); } + } + +} From 86b61457fa1f8beddc6c18e0aedf8bae13088157 Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Tue, 29 Mar 2016 13:30:04 +0200 Subject: [PATCH 11/20] Updated tests code; discovered that .fromRaw() doesn't return the expected type --- .../java/com/bio4j/angulillos/EdgeType.java | 1 - .../angulillos/TwitterGraphTestSuite.java | 31 +++++++++---------- .../com/bio4j/angulillos/TwitterSchema.java | 14 ++------- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/bio4j/angulillos/EdgeType.java b/src/main/java/com/bio4j/angulillos/EdgeType.java index 69cd0da..5c24176 100644 --- a/src/main/java/com/bio4j/angulillos/EdgeType.java +++ b/src/main/java/com/bio4j/angulillos/EdgeType.java @@ -37,7 +37,6 @@ protected EdgeType(ST sourceType, TT targetType) { /* This method adds a new edge of this type to the graph */ public ET.Edge addEdge( VertexType.Vertex source, - ET edgeType, VertexType.Vertex target ) { return fromRaw( diff --git a/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java b/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java index b66bdd0..d932ec3 100644 --- a/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java +++ b/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java @@ -14,8 +14,9 @@ public abstract class TwitterGraphTestSuite< public TwitterSchema.User.Vertex addUser(String name, Integer age) { - return g.addVertex(g.user) - .set(g.user.name, name) + TwitterSchema.User.Vertex u = g.user.addVertex(); + + return u.set(g.user.name, name) .set(g.user.age, age); } @@ -23,21 +24,17 @@ public void doSomething(TwitterSchema.User.Vertex user) { System.out.println(user.get(g.user.age)); - TwitterSchema.User.Vertex bob = addUser("Bob", 25); - - TwitterSchema.Follows.Edge edge = - g.addEdge( - bob, - g.follows, - addUser("Alice", 34) - ); - - Stream.Follows.Edge> followEdges = bob.outE(g.follows); - Stream.Tweet.Vertex> tweets = user.outV(g.posted); - Stream.User.Vertex> followees = bob.outV(g.follows); - - // TwitterSchema.User.Vertex source = g.source(edge); - TwitterSchema.User.Vertex source = edge.source(); + // TwitterSchema.User.Vertex bob = addUser("Bob", 25); + // + // TwitterSchema.Follows.Edge edge = + // g.follows.addEdge( bob, addUser("Alice", 34) ); + // + // Stream.Follows.Edge> followEdges = bob.outE(g.follows); + // Stream.Tweet.Vertex> tweets = user.outV(g.posted); + // Stream.User.Vertex> followees = bob.outV(g.follows); + // + // // TwitterSchema.User.Vertex source = g.source(edge); + // TwitterSchema.User.Vertex source = edge.source(); } // public Stream.User> tweetedTheTweetsThatTweeted(TwitterSchema.User user) { diff --git a/src/test/java/com/bio4j/angulillos/TwitterSchema.java b/src/test/java/com/bio4j/angulillos/TwitterSchema.java index 3008c6a..890de7a 100644 --- a/src/test/java/com/bio4j/angulillos/TwitterSchema.java +++ b/src/test/java/com/bio4j/angulillos/TwitterSchema.java @@ -6,7 +6,7 @@ public class TwitterSchema extends - GraphSchema, RV,RE> + TypedGraph, RV,RE> { public final TwitterSchema self() { return this; } @@ -37,11 +37,7 @@ public final class Tweet extends VertexType { /* ### Edges and their types */ public final Follows follows = new Follows(); - public final class Follows extends EdgeType< - User, TwitterSchema, - Follows, - User, TwitterSchema - > + public final class Follows extends EdgeType implements AnyToAny { @Override public Follows self() { return this; } Follows() { super(user, user); } @@ -51,11 +47,7 @@ public final class Follows extends EdgeType< // Any tweet is posted by exactly one user, but user may post any number of tweets (incl. 0) public final Posted posted = new Posted(); - public final class Posted extends EdgeType< - User, TwitterSchema, - Posted, - Tweet, TwitterSchema - > + public final class Posted extends EdgeType implements OneToAny { @Override public Posted self() { return this; } Posted() { super(user, tweet); } From 0f3f829a2b10d9d14edf3704b157a56f243fbc91 Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Tue, 29 Mar 2016 22:54:06 +0200 Subject: [PATCH 12/20] Rewrote index-related code; added two classes refining the interface in the TypedGraph --- build.sbt | 1 - .../com/bio4j/angulillos/TypedEdgeIndex.java | 46 ------------- .../bio4j/angulillos/TypedElementIndex.java | 64 ------------------- .../java/com/bio4j/angulillos/TypedGraph.java | 20 ++++++ .../java/com/bio4j/angulillos/TypedIndex.java | 63 ++++++++++++++++++ .../bio4j/angulillos/TypedVertexIndex.java | 52 --------------- .../angulillos/TwitterGraphTestSuite.java | 14 ++-- 7 files changed, 90 insertions(+), 170 deletions(-) delete mode 100644 src/main/java/com/bio4j/angulillos/TypedEdgeIndex.java delete mode 100644 src/main/java/com/bio4j/angulillos/TypedElementIndex.java create mode 100644 src/main/java/com/bio4j/angulillos/TypedIndex.java delete mode 100644 src/main/java/com/bio4j/angulillos/TypedVertexIndex.java diff --git a/build.sbt b/build.sbt index 7902d59..91ef1de 100644 --- a/build.sbt +++ b/build.sbt @@ -9,7 +9,6 @@ javaVersion := "1.8" excludeFilter in unmanagedSources := (excludeFilter in unmanagedSources).value || - "*Index.java" || "*Query.java" javacOptions ++= Seq( diff --git a/src/main/java/com/bio4j/angulillos/TypedEdgeIndex.java b/src/main/java/com/bio4j/angulillos/TypedEdgeIndex.java deleted file mode 100644 index e37a1a7..0000000 --- a/src/main/java/com/bio4j/angulillos/TypedEdgeIndex.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.bio4j.angulillos; - -import java.util.Optional; -import java.util.stream.Stream; - -interface TypedEdgeIndex < - E extends TypedEdge, - ET extends TypedEdge.Type, - P extends Property, - X -> extends - TypedElementIndex -{ - - default ET edgeType() { return elementType(); } - - interface Unique < - E extends TypedEdge, - ET extends TypedEdge.Type, - P extends Property, - X - > extends - TypedEdgeIndex, - TypedElementIndex.Unique - { - - /* get a node by providing a value of the indexed property. */ - default Optional getEdge(X byValue) { return getElement(byValue); } - } - - interface List < - E extends TypedEdge, - ET extends TypedEdge.Type, - P extends Property, - X - > extends - TypedEdgeIndex, - TypedElementIndex.List - { - - /* get a list of nodes by providing a value of the indexed property. */ - default Stream getEdges(X byValue) { return getElements(byValue); } - } - - -} diff --git a/src/main/java/com/bio4j/angulillos/TypedElementIndex.java b/src/main/java/com/bio4j/angulillos/TypedElementIndex.java deleted file mode 100644 index 9f21b4e..0000000 --- a/src/main/java/com/bio4j/angulillos/TypedElementIndex.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.bio4j.angulillos; - -import java.util.stream.Stream; -import java.util.Optional; -import java.util.Collection; - -interface TypedElementIndex < - FT extends ElementType, - P extends Property, - X -> -{ - - /* Index name */ - String name(); - - // /* The graph */ - // G graph(); - - /* Get the indexed property. */ - P property(); - - default FT elementType() { return property().elementType(); } - - - /* Query this index by comparing the property value with the given one */ - Stream query(QueryPredicate.Compare predicate, X value); - - /* Query this index by checking whether the property value is in/not in the given collection */ - Stream query(QueryPredicate.Contain predicate, Collection values); - - - /* This interface declares that this index is over a property that uniquely classifies a element type for exact match queries */ - interface Unique < - FT extends ElementType, - P extends Property, - X - > - extends TypedElementIndex - { - - /* Get an element by providing a value of the indexed property */ - default Optional getElement(X byValue) { - - return query(QueryPredicate.Compare.EQUAL, byValue).findFirst(); - } - } - - /* This interface declares that this index is over a property that classifies lists of elements for exact match queries */ - interface List < - FT extends ElementType, - P extends Property, - X - > - extends TypedElementIndex - { - - /* Get a list of elements by providing a value of the property */ - default Stream getElements(X byValue) { - - return query(QueryPredicate.Compare.EQUAL, byValue); - } - } -} diff --git a/src/main/java/com/bio4j/angulillos/TypedGraph.java b/src/main/java/com/bio4j/angulillos/TypedGraph.java index f84e283..21228c3 100644 --- a/src/main/java/com/bio4j/angulillos/TypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/TypedGraph.java @@ -41,4 +41,24 @@ public abstract class EdgeType< @Override public final G graph() { return TypedGraph.this.self(); } } + + public abstract class VertexIndex< + VT extends G.VertexType, + P extends Property, + X + > implements TypedIndex.Vertex, G,P,X,RV> { + + @Override public final G graph() { return TypedGraph.this.self(); } + } + + public abstract class EdgeIndex< + ST extends G.VertexType, + ET extends G.EdgeType, + TT extends G.VertexType, + P extends Property, + X + > implements TypedIndex.Edge, G,P,X,RE> { + + @Override public final G graph() { return TypedGraph.this.self(); } + } } diff --git a/src/main/java/com/bio4j/angulillos/TypedIndex.java b/src/main/java/com/bio4j/angulillos/TypedIndex.java new file mode 100644 index 0000000..a6a5cd7 --- /dev/null +++ b/src/main/java/com/bio4j/angulillos/TypedIndex.java @@ -0,0 +1,63 @@ +package com.bio4j.angulillos; + +import java.util.stream.Stream; +import java.util.Optional; +import java.util.Collection; + +interface TypedIndex< + FT extends ElementType, + F extends ElementType.Element, + G extends TypedGraph, + P extends Property, + X, RF +> { + + /* Index name */ + String _label(); + + /* The graph */ + G graph(); + + /* The indexed property. */ + P property(); + + default FT elementType() { return property().elementType; } + + + /* Query this index by comparing the property value with the given one */ + Stream query(QueryPredicate.Compare predicate, X value); + + default Stream query(X value) { return query(QueryPredicate.Compare.EQUAL, value); } + + + /* Query this index by checking whether the property value is in/not in the given collection */ + Stream query(QueryPredicate.Contain predicate, Collection values); + + default Stream query(Collection values) { return query(QueryPredicate.Contain.IN, values); } + +} + +// NOTE: no much point in these two interfaces, because they don't introduce anything new and +// in the TypedGraph there are classes doing the same types refinement + +// interface VertexIndex< +// VT extends VertexType, +// G extends TypedGraph, +// P extends Property, +// X, RV,RE +// > extends TypedIndex.Vertex, G,P,X,RV> {} +// +// +// interface EdgeIndex< +// ST extends VertexType, +// SG extends TypedGraph, +// ET extends EdgeType, +// EG extends TypedGraph, +// TT extends VertexType, +// TG extends TypedGraph, +// P extends Property, +// X, RV,RE +// > extends TypedIndex.Edge, EG,P,X,RE> {} + + +// TODO: make Unique/List into property's arity diff --git a/src/main/java/com/bio4j/angulillos/TypedVertexIndex.java b/src/main/java/com/bio4j/angulillos/TypedVertexIndex.java deleted file mode 100644 index c5dd256..0000000 --- a/src/main/java/com/bio4j/angulillos/TypedVertexIndex.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.bio4j.angulillos; - -import java.util.stream.Stream; -import java.util.Optional; - -/* - ## Vertex Indices - - A vertex index indexes vertices of a given type through values of one of its properties. This just adds a bound on the indexed type to be a TypedVertex; see `TypedElementIndex` -*/ -interface TypedVertexIndex < - V extends TypedVertex, - VT extends TypedVertex.Type, - P extends Property, - X -> extends - TypedElementIndex -{ - - default VT vertexType() { return elementType(); } - - /* This interface declares that this index is over a property that uniquely classifies a vertex type for exact match queries; it adds the method `getTypedVertex` for that. */ - interface Unique < - V extends TypedVertex, - VT extends TypedVertex.Type, - P extends Property, - X - > extends - TypedVertexIndex, - TypedElementIndex.Unique - { - - /* get a vertex by providing a value of the indexed property. The default implementation relies on `query`. */ - default Optional getVertex(X byValue) { return getElement(byValue); } - } - - /* This interface declares that this index is over a property that classifies lists of vertices for exact match queries; it adds the method `getTypedVertexs` for that. */ - interface List < - V extends TypedVertex, - VT extends TypedVertex.Type, - P extends Property, - X - > extends - TypedVertexIndex, - TypedElementIndex.List - { - - /* get a list of vertices by providing a value of the property. The default */ - default Stream getVertices(X byValue) { return getElements(byValue); } - } - -} diff --git a/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java b/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java index d932ec3..333cb0f 100644 --- a/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java +++ b/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java @@ -12,13 +12,13 @@ public abstract class TwitterGraphTestSuite< protected G g; - public TwitterSchema.User.Vertex addUser(String name, Integer age) { - - TwitterSchema.User.Vertex u = g.user.addVertex(); - - return u.set(g.user.name, name) - .set(g.user.age, age); - } + // public TwitterSchema.User.Vertex addUser(String name, Integer age) { + // + // TwitterSchema.User.Vertex u = g.user.addVertex(); + // + // return u.set(g.user.name, name) + // .set(g.user.age, age); + // } public void doSomething(TwitterSchema.User.Vertex user) { From f9dc5249bffc9ebf222055a010f7cf47b75cba44 Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Wed, 30 Mar 2016 01:51:38 +0200 Subject: [PATCH 13/20] Made top-level entities explicitly public --- src/main/java/com/bio4j/angulillos/ElementType.java | 2 +- src/main/java/com/bio4j/angulillos/QueryPredicate.java | 2 +- src/main/java/com/bio4j/angulillos/TypedIndex.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/bio4j/angulillos/ElementType.java b/src/main/java/com/bio4j/angulillos/ElementType.java index 0a36875..10b3a12 100644 --- a/src/main/java/com/bio4j/angulillos/ElementType.java +++ b/src/main/java/com/bio4j/angulillos/ElementType.java @@ -13,7 +13,7 @@ `E` refers to the element itself, and `ET` its type. You cannot define one without defining the other. */ -abstract class ElementType < +public abstract class ElementType < FT extends ElementType, G extends TypedGraph, RF diff --git a/src/main/java/com/bio4j/angulillos/QueryPredicate.java b/src/main/java/com/bio4j/angulillos/QueryPredicate.java index 68c7f45..2bfa1ee 100644 --- a/src/main/java/com/bio4j/angulillos/QueryPredicate.java +++ b/src/main/java/com/bio4j/angulillos/QueryPredicate.java @@ -1,6 +1,6 @@ package com.bio4j.angulillos; -interface QueryPredicate { +public interface QueryPredicate { /* This is the same as - http://thinkaurelius.github.io/titan/javadoc/current/com/thinkaurelius/titan/core/attribute/Cmp.html diff --git a/src/main/java/com/bio4j/angulillos/TypedIndex.java b/src/main/java/com/bio4j/angulillos/TypedIndex.java index a6a5cd7..45ec16a 100644 --- a/src/main/java/com/bio4j/angulillos/TypedIndex.java +++ b/src/main/java/com/bio4j/angulillos/TypedIndex.java @@ -4,7 +4,7 @@ import java.util.Optional; import java.util.Collection; -interface TypedIndex< +public interface TypedIndex< FT extends ElementType, F extends ElementType.Element, G extends TypedGraph, From 32269935316a61ce257fcc8733d60b8d7b7e2eee Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Wed, 30 Mar 2016 02:10:23 +0200 Subject: [PATCH 14/20] Added methods for all members instead of fields (except of the _label); changed self() to protected; made Vertex inner class non-final --- .../java/com/bio4j/angulillos/EdgeType.java | 20 +++-- .../com/bio4j/angulillos/ElementType.java | 14 ++-- .../java/com/bio4j/angulillos/Property.java | 14 ++-- .../java/com/bio4j/angulillos/TypedGraph.java | 2 +- .../java/com/bio4j/angulillos/TypedIndex.java | 2 +- .../java/com/bio4j/angulillos/VertexType.java | 79 ++++++++++--------- 6 files changed, 71 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/bio4j/angulillos/EdgeType.java b/src/main/java/com/bio4j/angulillos/EdgeType.java index 5c24176..a15b97d 100644 --- a/src/main/java/com/bio4j/angulillos/EdgeType.java +++ b/src/main/java/com/bio4j/angulillos/EdgeType.java @@ -21,8 +21,11 @@ public abstract class EdgeType < implements HasArity { - public final ST sourceType; - public final TT targetType; + private final ST sourceType; + public final ST sourceType() { return this.sourceType; } + + private final TT targetType; + public final TT targetType() { return this.targetType; } protected EdgeType(ST sourceType, TT targetType) { this.sourceType = sourceType; @@ -40,26 +43,27 @@ public ET.Edge addEdge( VertexType.Vertex target ) { return fromRaw( - graph().raw().addEdge( source.raw, this._label, target.raw ) + graph().raw().addEdge( source.raw(), this._label, target.raw() ) ); } - class Edge extends Element { + public class Edge extends Element { + // NOTE: this constructor is private to enforce the usage of the factory method fromRaw() private Edge(RE raw) { super(raw); } /* ### Properties */ @Override public X get(Property property) { - return graph.raw().getPropertyE(this.raw, property._label); + return graph().raw().getPropertyE(this.raw(), property._label); } @Override public Edge set(Property property, X value) { - graph.raw().setPropertyE(this.raw, property._label, value); + graph().raw().setPropertyE(this.raw(), property._label, value); return this; } @@ -67,14 +71,14 @@ Edge set(Property property, X value) { /* the source vertex of this edge */ public ST.Vertex source() { return sourceType.fromRaw( - graph.raw().source( this.raw ) + graph().raw().source( this.raw() ) ); } /* the target vertex of this edge */ public TT.Vertex target() { return targetType.fromRaw( - graph.raw().target( this.raw ) + graph().raw().target( this.raw() ) ); } diff --git a/src/main/java/com/bio4j/angulillos/ElementType.java b/src/main/java/com/bio4j/angulillos/ElementType.java index 10b3a12..db5f537 100644 --- a/src/main/java/com/bio4j/angulillos/ElementType.java +++ b/src/main/java/com/bio4j/angulillos/ElementType.java @@ -22,12 +22,16 @@ public abstract class ElementType < /* This is a unique label */ public final String _label = getClass().getCanonicalName(); + /* An abstract reference to the instance of the implementing class. + _This has to be **always** implemented in a non-abstract inheritor as `return this`._ + It just cannot be implemented abstractly. + */ + protected abstract FT self(); + + /* The graph in which this element lives */ public abstract G graph(); - /* An abstract reference to the instance of the implementing class. This should return `this` in all cases; it just cannot be implemented at this level */ - public abstract FT self(); - /* Defines a new property on this element type */ public final Property property(String nameSuffix, Class valueClass) { return new Property(self(), nameSuffix, valueClass); @@ -35,12 +39,12 @@ public final Property property(String nameSuffix, Class valueClass) abstract class Element { - public final RF raw; + private final RF raw; + public final RF raw() { return this.raw; } public Element(RF raw) { this.raw = raw; } public final FT type = ElementType.this.self(); - public final G graph = type.graph(); /* The `get` method lets you get the value of a `property` which this element has. For that, you pass as an argument the [property](Property.java.md). Note that the type bounds only allow properties of this element. */ public abstract X get(Property property); diff --git a/src/main/java/com/bio4j/angulillos/Property.java b/src/main/java/com/bio4j/angulillos/Property.java index 6b5e7be..c58ab04 100644 --- a/src/main/java/com/bio4j/angulillos/Property.java +++ b/src/main/java/com/bio4j/angulillos/Property.java @@ -8,18 +8,20 @@ public class Property < FT extends ElementType, X -> -{ - // private final String nameSuffix; +> { public final String _label; - public final FT elementType; - public final Class valueClass; + private final FT elementType; + public final FT elementType() { return this.elementType; } + + private final Class valueClass; + public final Class valueClass() { return this.valueClass; } + protected Property(FT elementType, String nameSuffix, Class valueClass) { + this.elementType = elementType; - // this.nameSuffix = nameSuffix; this.valueClass = valueClass; this._label = elementType._label + "." + nameSuffix; } diff --git a/src/main/java/com/bio4j/angulillos/TypedGraph.java b/src/main/java/com/bio4j/angulillos/TypedGraph.java index 21228c3..978a57c 100644 --- a/src/main/java/com/bio4j/angulillos/TypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/TypedGraph.java @@ -11,7 +11,7 @@ public abstract class TypedGraph < RV,RE > { - public abstract G self(); + protected abstract G self(); private final UntypedGraph raw; public final UntypedGraph raw() { return this.raw; } diff --git a/src/main/java/com/bio4j/angulillos/TypedIndex.java b/src/main/java/com/bio4j/angulillos/TypedIndex.java index 45ec16a..d1110b9 100644 --- a/src/main/java/com/bio4j/angulillos/TypedIndex.java +++ b/src/main/java/com/bio4j/angulillos/TypedIndex.java @@ -21,7 +21,7 @@ public interface TypedIndex< /* The indexed property. */ P property(); - default FT elementType() { return property().elementType; } + default FT elementType() { return property().elementType(); } /* Query this index by comparing the property value with the given one */ diff --git a/src/main/java/com/bio4j/angulillos/VertexType.java b/src/main/java/com/bio4j/angulillos/VertexType.java index 888daf4..bd09be1 100644 --- a/src/main/java/com/bio4j/angulillos/VertexType.java +++ b/src/main/java/com/bio4j/angulillos/VertexType.java @@ -29,21 +29,22 @@ public VT.Vertex addVertex() { } - public final class Vertex extends Element { + public class Vertex extends Element { + // NOTE: this constructor is private to enforce the usage of the factory method fromRaw() private Vertex(RV raw) { super(raw); } /* ### Properties */ @Override public X get(Property property) { - return graph.raw().getPropertyV(this.raw, property._label); + return graph().raw().getPropertyV(this.raw(), property._label); } @Override public Vertex set(Property property, X value) { - graph.raw().setPropertyV(this.raw, property._label, value); + graph().raw().setPropertyV(this.raw(), property._label, value); return this; } @@ -54,8 +55,8 @@ Vertex set(Property property, X value) { ET extends EdgeType > Stream outE(ET edgeType) { - return graph.raw() - .outE(this.raw, edgeType._label) + return graph().raw() + .outE(this.raw(), edgeType._label) .map( edgeType::fromRaw ); } @@ -63,8 +64,8 @@ Stream outE(ET edgeType) { ET extends EdgeType & ToAtLeastOne > Stream outAtLeastOneE(ET edgeType) { - return graph.raw() - .outAtLeastOneE(this.raw, edgeType._label) + return graph().raw() + .outAtLeastOneE(this.raw(), edgeType._label) .map( edgeType::fromRaw ); } @@ -72,8 +73,8 @@ Stream outAtLeastOneE(ET edgeType) { ET extends EdgeType & ToAtMostOne > Optional outAtMostOneE(ET edgeType) { - return graph.raw() - .outAtMostOneE(this.raw, edgeType._label) + return graph().raw() + .outAtMostOneE(this.raw(), edgeType._label) .map( edgeType::fromRaw ); } @@ -82,7 +83,7 @@ Optional outAtMostOneE(ET edgeType) { > ET.Edge outOneE(ET edgeType) { return edgeType.fromRaw( - graph.raw().outOneE(this.raw, edgeType._label) + graph().raw().outOneE(this.raw(), edgeType._label) ); } @@ -92,8 +93,8 @@ ET.Edge outOneE(ET edgeType) { ET extends EdgeType > Stream inE(ET edgeType) { - return graph.raw() - .inE(this.raw, edgeType._label) + return graph().raw() + .inE(this.raw(), edgeType._label) .map( edgeType::fromRaw ); } @@ -101,8 +102,8 @@ Stream inE(ET edgeType) { ET extends EdgeType & FromAtLeastOne > Stream inAtLeastOneE(ET edgeType) { - return graph.raw() - .inAtLeastOneE(this.raw, edgeType._label) + return graph().raw() + .inAtLeastOneE(this.raw(), edgeType._label) .map( edgeType::fromRaw ); } @@ -110,8 +111,8 @@ Stream inAtLeastOneE(ET edgeType) { ET extends EdgeType & FromAtMostOne > Optional inAtMostOneE(ET edgeType) { - return graph.raw() - .inAtMostOneE(this.raw, edgeType._label) + return graph().raw() + .inAtMostOneE(this.raw(), edgeType._label) .map( edgeType::fromRaw ); } @@ -120,7 +121,7 @@ Optional inAtMostOneE(ET edgeType) { > ET.Edge inOneE(ET edgeType) { return edgeType.fromRaw( - graph.raw().inOneE(this.raw, edgeType._label) + graph().raw().inOneE(this.raw(), edgeType._label) ); } @@ -132,9 +133,9 @@ ET.Edge inOneE(ET edgeType) { TT extends VertexType > Stream outV(ET edgeType) { - return graph.raw() - .outV(this.raw, edgeType._label) - .map( edgeType.targetType::fromRaw ); + return graph().raw() + .outV(this.raw(), edgeType._label) + .map( edgeType.targetType()::fromRaw ); } public < @@ -142,9 +143,9 @@ Stream outV(ET edgeType) { TT extends VertexType > Stream outAtLeastOneV(ET edgeType) { - return graph.raw() - .outAtLeastOneV(this.raw, edgeType._label) - .map( edgeType.targetType::fromRaw ); + return graph().raw() + .outAtLeastOneV(this.raw(), edgeType._label) + .map( edgeType.targetType()::fromRaw ); } public < @@ -152,9 +153,9 @@ Stream outAtLeastOneV(ET edgeType) { TT extends VertexType > Optional outAtMostOneV(ET edgeType) { - return graph.raw() - .outAtMostOneV(this.raw, edgeType._label) - .map( edgeType.targetType::fromRaw ); + return graph().raw() + .outAtMostOneV(this.raw(), edgeType._label) + .map( edgeType.targetType()::fromRaw ); } public < @@ -162,8 +163,8 @@ Optional outAtMostOneV(ET edgeType) { TT extends VertexType > TT.Vertex outOneV(ET edgeType) { - return edgeType.targetType.fromRaw( - graph.raw().outOneV(this.raw, edgeType._label) + return edgeType.targetType().fromRaw( + graph().raw().outOneV(this.raw(), edgeType._label) ); } @@ -174,9 +175,9 @@ TT.Vertex outOneV(ET edgeType) { ET extends EdgeType > Stream inV(ET edgeType) { - return graph.raw() - .inV(this.raw, edgeType._label) - .map( edgeType.sourceType::fromRaw ); + return graph().raw() + .inV(this.raw(), edgeType._label) + .map( edgeType.sourceType()::fromRaw ); } public < @@ -184,9 +185,9 @@ Stream inV(ET edgeType) { ET extends EdgeType & FromAtLeastOne > Stream inAtLeastOneV(ET edgeType) { - return graph.raw() - .inAtLeastOneV(this.raw, edgeType._label) - .map( edgeType.sourceType::fromRaw ); + return graph().raw() + .inAtLeastOneV(this.raw(), edgeType._label) + .map( edgeType.sourceType()::fromRaw ); } public < @@ -194,9 +195,9 @@ Stream inAtLeastOneV(ET edgeType) { ET extends EdgeType & FromAtMostOne > Optional inAtMostOneV(ET edgeType) { - return graph.raw() - .inAtMostOneV(this.raw, edgeType._label) - .map( edgeType.sourceType::fromRaw ); + return graph().raw() + .inAtMostOneV(this.raw(), edgeType._label) + .map( edgeType.sourceType()::fromRaw ); } public < @@ -204,8 +205,8 @@ Optional inAtMostOneV(ET edgeType) { ET extends EdgeType & FromOne > ST.Vertex inOneV(ET edgeType) { - return edgeType.sourceType.fromRaw( - graph.raw().inOneV(this.raw, edgeType._label) + return edgeType.sourceType().fromRaw( + graph().raw().inOneV(this.raw(), edgeType._label) ); } From 604ddad7ec7b23745dae220a1898eac5d163d279 Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Wed, 30 Mar 2016 04:39:46 +0200 Subject: [PATCH 15/20] Added to ElementType properties set and made the properties factory method to add them to this set --- src/main/java/com/bio4j/angulillos/ElementType.java | 13 +++++++++++-- src/main/java/com/bio4j/angulillos/Property.java | 3 ++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/bio4j/angulillos/ElementType.java b/src/main/java/com/bio4j/angulillos/ElementType.java index db5f537..b53acb3 100644 --- a/src/main/java/com/bio4j/angulillos/ElementType.java +++ b/src/main/java/com/bio4j/angulillos/ElementType.java @@ -1,5 +1,8 @@ package com.bio4j.angulillos; +import java.util.Set; +import java.util.HashSet; + /* ## Elements @@ -32,9 +35,15 @@ public abstract class ElementType < /* The graph in which this element lives */ public abstract G graph(); - /* Defines a new property on this element type */ + + private Set> properties = new HashSet<>(); + public final Set> properties() { return this.properties; } + + /* Defines a new property on this element type and adds it to the `properties` set */ public final Property property(String nameSuffix, Class valueClass) { - return new Property(self(), nameSuffix, valueClass); + Property p = new Property(self(), nameSuffix, valueClass); + this.properties.add(p); + return p; } diff --git a/src/main/java/com/bio4j/angulillos/Property.java b/src/main/java/com/bio4j/angulillos/Property.java index c58ab04..3f1a161 100644 --- a/src/main/java/com/bio4j/angulillos/Property.java +++ b/src/main/java/com/bio4j/angulillos/Property.java @@ -19,7 +19,8 @@ public class Property < public final Class valueClass() { return this.valueClass; } - protected Property(FT elementType, String nameSuffix, Class valueClass) { + // NOTE: this constructor is package-private to enforce usage of the factory method in the ElementType class + Property(FT elementType, String nameSuffix, Class valueClass) { this.elementType = elementType; this.valueClass = valueClass; From d83494e2df60dfd9f4a710bafcddedf01e88f792 Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Wed, 30 Mar 2016 05:04:49 +0200 Subject: [PATCH 16/20] Added a set containing all vertex types of a graph (which are automatically added there once instantiated) --- src/main/java/com/bio4j/angulillos/TypedGraph.java | 12 ++++++++++++ src/main/java/com/bio4j/angulillos/VertexType.java | 5 +++++ .../com/bio4j/angulillos/TwitterGraphTestSuite.java | 5 +++-- .../java/com/bio4j/angulillos/TwitterSchema.java | 5 ++++- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/bio4j/angulillos/TypedGraph.java b/src/main/java/com/bio4j/angulillos/TypedGraph.java index 978a57c..1dc9f97 100644 --- a/src/main/java/com/bio4j/angulillos/TypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/TypedGraph.java @@ -1,5 +1,8 @@ package com.bio4j.angulillos; +import java.util.Set; +import java.util.HashSet; + /* ## Typed graphs @@ -19,12 +22,21 @@ public abstract class TypedGraph < protected TypedGraph(UntypedGraph raw) { this.raw = raw; } + /* This set will store all vertex types defined for this graph */ + private Set> vertexTypes = new HashSet<>(); + public final Set> vertexTypes() { return this.vertexTypes; } + + /* Refined graph element classes */ /* Defines an vertex of _this_ graph with fixed raw types */ public abstract class VertexType< VT extends G.VertexType > extends com.bio4j.angulillos.VertexType { + // NOTE: this initializer block will be inherited and will add each vertex type to the set + { + TypedGraph.this.vertexTypes.add(self()); + } @Override public final G graph() { return TypedGraph.this.self(); } } diff --git a/src/main/java/com/bio4j/angulillos/VertexType.java b/src/main/java/com/bio4j/angulillos/VertexType.java index bd09be1..95b8112 100644 --- a/src/main/java/com/bio4j/angulillos/VertexType.java +++ b/src/main/java/com/bio4j/angulillos/VertexType.java @@ -15,6 +15,11 @@ public abstract class VertexType < RV,RE > extends ElementType { + // NOTE: this is suuposed to enforce defining a non-public constructor for each vertex type to prevent creating instances of them outside of the graph + // But if it has no parameters, it will be overriden by default constructor + protected VertexType() {}; + + // NOTE: this call is typesafe, but the compiler cannot check it here, because the RV type in self() is not bound to be the same as we use from the enclousing class context @SuppressWarnings("unchecked") /* This method should be used for constructing _all_ instances of the Vertex inner class to get the precise return type */ diff --git a/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java b/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java index 333cb0f..81fc382 100644 --- a/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java +++ b/src/test/java/com/bio4j/angulillos/TwitterGraphTestSuite.java @@ -1,6 +1,6 @@ -package com.bio4j.angulillos; +package com.bio4j.angulillos.test; -// import com.bio4j.angulillos.TwitterSchema.*; +import com.bio4j.angulillos.*; import java.util.stream.Stream; @@ -19,6 +19,7 @@ public abstract class TwitterGraphTestSuite< // return u.set(g.user.name, name) // .set(g.user.age, age); // } + G.Tweet t = g.new Tweet(); public void doSomething(TwitterSchema.User.Vertex user) { diff --git a/src/test/java/com/bio4j/angulillos/TwitterSchema.java b/src/test/java/com/bio4j/angulillos/TwitterSchema.java index 890de7a..7388d0a 100644 --- a/src/test/java/com/bio4j/angulillos/TwitterSchema.java +++ b/src/test/java/com/bio4j/angulillos/TwitterSchema.java @@ -1,6 +1,7 @@ package com.bio4j.angulillos; +// FIXME: this should be another package, this will reveal problems with the non-public classes -// import com.bio4j.angulillos.TypedEdge.Type.*; +import com.bio4j.angulillos.*; import java.net.URL; import java.util.Date; @@ -19,6 +20,7 @@ public class TwitterSchema public final User user = new User(); public final class User extends VertexType { @Override public User self() { return this; } + // private User() {}; public Property name = property("name", String.class); public Property age = property("age", Integer.class); @@ -28,6 +30,7 @@ public final class User extends VertexType { public final Tweet tweet = new Tweet(); public final class Tweet extends VertexType { @Override public Tweet self() { return this; } + // private Tweet() {}; public Property text = property("text", String.class); public Property url = property("url", URL.class); From 0a8be61554cfb0e9a02b497749f04ee0a4c2c221 Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Wed, 30 Mar 2016 06:29:45 +0200 Subject: [PATCH 17/20] Added a check on the set construction for distinct labels (if you try to define a propety/vertex type with an existing label, on the graph instantiation you will get an exception) --- .../java/com/bio4j/angulillos/ElementType.java | 16 +++++++++++++--- .../java/com/bio4j/angulillos/TypedGraph.java | 7 +++++++ .../java/com/bio4j/angulillos/TwitterSchema.java | 5 +++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/bio4j/angulillos/ElementType.java b/src/main/java/com/bio4j/angulillos/ElementType.java index b53acb3..49d04df 100644 --- a/src/main/java/com/bio4j/angulillos/ElementType.java +++ b/src/main/java/com/bio4j/angulillos/ElementType.java @@ -41,9 +41,19 @@ public abstract class ElementType < /* Defines a new property on this element type and adds it to the `properties` set */ public final Property property(String nameSuffix, Class valueClass) { - Property p = new Property(self(), nameSuffix, valueClass); - this.properties.add(p); - return p; + Property newProp = new Property(self(), nameSuffix, valueClass); + + if ( + this.properties.removeIf( (Property p) -> + p._label.equals( newProp._label ) + ) + ) { + throw new IllegalArgumentException("Element type [" +this._label+ "] contains duplicate property: " + newProp._label); + } else { + this.properties.add(newProp); + } + + return newProp; } diff --git a/src/main/java/com/bio4j/angulillos/TypedGraph.java b/src/main/java/com/bio4j/angulillos/TypedGraph.java index 1dc9f97..061b983 100644 --- a/src/main/java/com/bio4j/angulillos/TypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/TypedGraph.java @@ -35,6 +35,13 @@ public abstract class VertexType< > extends com.bio4j.angulillos.VertexType { // NOTE: this initializer block will be inherited and will add each vertex type to the set { + if ( + TypedGraph.this.vertexTypes.removeIf( (G.VertexType vt) -> + vt._label.equals( self()._label ) + ) + ) { + throw new IllegalArgumentException("The graph contains duplicate vertex type: " + self()._label); + } TypedGraph.this.vertexTypes.add(self()); } diff --git a/src/test/java/com/bio4j/angulillos/TwitterSchema.java b/src/test/java/com/bio4j/angulillos/TwitterSchema.java index 7388d0a..a910057 100644 --- a/src/test/java/com/bio4j/angulillos/TwitterSchema.java +++ b/src/test/java/com/bio4j/angulillos/TwitterSchema.java @@ -34,8 +34,13 @@ public final class Tweet extends VertexType { public Property text = property("text", String.class); public Property url = property("url", URL.class); + + // NOTE: Try to uncomment it and instantiate TwitterSchema + // public Property date = property("text", Date.class); } + // NOTE: Try to uncomment it and instantiate TwitterSchema + // public final Tweet tweet2 = new Tweet(); /* ### Edges and their types */ From e89f450177aeb24eed1dcf392862533635a2493a Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Wed, 30 Mar 2016 15:22:45 +0200 Subject: [PATCH 18/20] Added inner class Property to the ElementType --- .../java/com/bio4j/angulillos/EdgeType.java | 4 +- .../com/bio4j/angulillos/ElementType.java | 58 +++++++++++-------- .../java/com/bio4j/angulillos/VertexType.java | 4 +- .../com/bio4j/angulillos/TwitterSchema.java | 16 ++--- 4 files changed, 47 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/bio4j/angulillos/EdgeType.java b/src/main/java/com/bio4j/angulillos/EdgeType.java index a15b97d..09e0e41 100644 --- a/src/main/java/com/bio4j/angulillos/EdgeType.java +++ b/src/main/java/com/bio4j/angulillos/EdgeType.java @@ -56,12 +56,12 @@ public class Edge extends Element { /* ### Properties */ @Override public - X get(Property property) { + X get(ET.Property property) { return graph().raw().getPropertyE(this.raw(), property._label); } @Override public - Edge set(Property property, X value) { + Edge set(ET.Property property, X value) { graph().raw().setPropertyE(this.raw(), property._label, value); return this; diff --git a/src/main/java/com/bio4j/angulillos/ElementType.java b/src/main/java/com/bio4j/angulillos/ElementType.java index 49d04df..cf8d7a9 100644 --- a/src/main/java/com/bio4j/angulillos/ElementType.java +++ b/src/main/java/com/bio4j/angulillos/ElementType.java @@ -36,27 +36,6 @@ public abstract class ElementType < public abstract G graph(); - private Set> properties = new HashSet<>(); - public final Set> properties() { return this.properties; } - - /* Defines a new property on this element type and adds it to the `properties` set */ - public final Property property(String nameSuffix, Class valueClass) { - Property newProp = new Property(self(), nameSuffix, valueClass); - - if ( - this.properties.removeIf( (Property p) -> - p._label.equals( newProp._label ) - ) - ) { - throw new IllegalArgumentException("Element type [" +this._label+ "] contains duplicate property: " + newProp._label); - } else { - this.properties.add(newProp); - } - - return newProp; - } - - abstract class Element { private final RF raw; public final RF raw() { return this.raw; } @@ -66,10 +45,43 @@ abstract class Element { public final FT type = ElementType.this.self(); /* The `get` method lets you get the value of a `property` which this element has. For that, you pass as an argument the [property](Property.java.md). Note that the type bounds only allow properties of this element. */ - public abstract X get(Property property); + public abstract X get(FT.Property property); /* `set` sets the value of a `property` for this element. Again, you can only set properties that this element has, using values of the corresponding property value type. */ - public abstract Element set(Property property, X value); + public abstract Element set(FT.Property property, X value); } + + /* This set stores all properties that are defined on this element type */ + private Set> properties = new HashSet<>(); + public final Set> properties() { return this.properties; } + + /* Defines a new property on this element type and adds it to the `properties` set */ + public final Property property(String nameSuffix, Class valueClass) { + return new Property(nameSuffix, valueClass); + } + + /* This inner class fixes the element type */ + public final class Property extends com.bio4j.angulillos.Property { + + // NOTE: this constructor is private to enforce usage of the factory method `property` + private Property(String nameSuffix, Class valueClass) { + super(ElementType.this.self(), nameSuffix, valueClass); + + if ( + ElementType.this.properties.removeIf( (Property p) -> + p._label.equals( this._label ) + ) + ) { + throw new IllegalArgumentException( + "Element type [" + + ElementType.this._label + + "] contains duplicate property: " + + this._label + ); + } else { + ElementType.this.properties.add(this); + } + } + } } diff --git a/src/main/java/com/bio4j/angulillos/VertexType.java b/src/main/java/com/bio4j/angulillos/VertexType.java index 95b8112..201b6d1 100644 --- a/src/main/java/com/bio4j/angulillos/VertexType.java +++ b/src/main/java/com/bio4j/angulillos/VertexType.java @@ -42,12 +42,12 @@ public class Vertex extends Element { /* ### Properties */ @Override public - X get(Property property) { + X get(VT.Property property) { return graph().raw().getPropertyV(this.raw(), property._label); } @Override public - Vertex set(Property property, X value) { + Vertex set(VT.Property property, X value) { graph().raw().setPropertyV(this.raw(), property._label, value); return this; diff --git a/src/test/java/com/bio4j/angulillos/TwitterSchema.java b/src/test/java/com/bio4j/angulillos/TwitterSchema.java index a910057..86bd8bb 100644 --- a/src/test/java/com/bio4j/angulillos/TwitterSchema.java +++ b/src/test/java/com/bio4j/angulillos/TwitterSchema.java @@ -22,8 +22,8 @@ public final class User extends VertexType { @Override public User self() { return this; } // private User() {}; - public Property name = property("name", String.class); - public Property age = property("age", Integer.class); + public final Property name = property("name", String.class); + public final Property age = property("age", Integer.class); } @@ -32,11 +32,11 @@ public final class Tweet extends VertexType { @Override public Tweet self() { return this; } // private Tweet() {}; - public Property text = property("text", String.class); - public Property url = property("url", URL.class); + public final Property text = property("text", String.class); + public final Property url = property("url", URL.class); // NOTE: Try to uncomment it and instantiate TwitterSchema - // public Property date = property("text", Date.class); + // public final Property date = property("text", Date.class); } // NOTE: Try to uncomment it and instantiate TwitterSchema @@ -50,7 +50,7 @@ public final class Follows extends EdgeType @Override public Follows self() { return this; } Follows() { super(user, user); } - public Property since = property("since", Date.class); + public final Property since = property("since", Date.class); } // Any tweet is posted by exactly one user, but user may post any number of tweets (incl. 0) @@ -60,7 +60,7 @@ public final class Posted extends EdgeType @Override public Posted self() { return this; } Posted() { super(user, tweet); } - public Property date = property("date", Date.class); + public final Property date = property("date", Date.class); } // // A reply addresses exactly one tweet, but a tweet may not have any replies @@ -70,7 +70,7 @@ public final class Posted extends EdgeType // @Override public Replies self() { return this; } // Replies() { super(tweet, tweet); } // - // public Property date = property("date", Date.class); + // public final Property date = property("date", Date.class); // } } From 663541931fd68a5ec5c7fcaaeca359983ab90c16 Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Wed, 30 Mar 2016 19:12:50 +0200 Subject: [PATCH 19/20] Added UntypedGraphSchema interface for schema creation; separated transaction-related methods --- .../com/bio4j/angulillos/UntypedGraph.java | 18 +++++--- .../bio4j/angulillos/UntypedGraphSchema.java | 46 +++++++++++++++++++ 2 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/bio4j/angulillos/UntypedGraphSchema.java diff --git a/src/main/java/com/bio4j/angulillos/UntypedGraph.java b/src/main/java/com/bio4j/angulillos/UntypedGraph.java index c16970e..c80e797 100644 --- a/src/main/java/com/bio4j/angulillos/UntypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/UntypedGraph.java @@ -4,6 +4,13 @@ import java.util.Optional; +interface UntypedTransactionalGraph { + + void commit(); + void rollback(); + void shutdown(); +} + /* ## Untyped graph @@ -16,7 +23,10 @@ Properties are represented using `String`s. What the methods are supposed to do is I think pretty obvious from their names; there is anyway a short explanation for each. */ -interface UntypedGraph { +public interface UntypedGraph +extends + UntypedTransactionalGraph +{ /* #### Methods on vertices */ @@ -71,10 +81,4 @@ interface UntypedGraph { /* - Returns a new vertex of type `vertexType` */ RV addVertex(String vertexLabel); - - /* These two methods are here at this level just for convenience; - they should be moved to `UntypedTransactionalGraph` or something like that. */ - void commit(); - void shutdown(); - } diff --git a/src/main/java/com/bio4j/angulillos/UntypedGraphSchema.java b/src/main/java/com/bio4j/angulillos/UntypedGraphSchema.java new file mode 100644 index 0000000..1569326 --- /dev/null +++ b/src/main/java/com/bio4j/angulillos/UntypedGraphSchema.java @@ -0,0 +1,46 @@ +package com.bio4j.angulillos; + + +public interface UntypedGraphSchema { + + < + VT extends VertexType + > SM createVertexType(SM schemaManager, VT vertexType); + + + < + ET extends EdgeType + > SM createEdgeType(SM schemaManager, ET edgeType); + + + < + FT extends ElementType, + P extends Property, + X + > SM createProperty(SM schemaManager, P property); + + + default < + VT extends VertexType + > SM createVertexTypeWithProperties(SM schemaManager, VT vertexType) { + SM sm = createVertexType(schemaManager, vertexType); + // TODO: some kind of fold here: + vertexType.properties().forEach( p -> + createProperty(sm, p) + ); + return sm; + } + + default < + G extends TypedGraph + > SM createAllVertexTypes(SM schemaManager, G g) { + // TODO: some kind of fold here: + g.vertexTypes().forEach( (VertexType vt) -> + createVertexTypeWithProperties(schemaManager, vt) + ); + return schemaManager; + } + + // etc. for edges and all together + +} From e3b59aec6a83141c68758b99ed1e8c22500b7764 Mon Sep 17 00:00:00 2001 From: Alexey Alekhin Date: Thu, 31 Mar 2016 00:21:47 +0200 Subject: [PATCH 20/20] Added sets for holding all graph's edge types and indexes --- .../java/com/bio4j/angulillos/TypedGraph.java | 60 ++++++++++++++++--- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/bio4j/angulillos/TypedGraph.java b/src/main/java/com/bio4j/angulillos/TypedGraph.java index 061b983..3184708 100644 --- a/src/main/java/com/bio4j/angulillos/TypedGraph.java +++ b/src/main/java/com/bio4j/angulillos/TypedGraph.java @@ -26,11 +26,8 @@ public abstract class TypedGraph < private Set> vertexTypes = new HashSet<>(); public final Set> vertexTypes() { return this.vertexTypes; } - - /* Refined graph element classes */ - - /* Defines an vertex of _this_ graph with fixed raw types */ - public abstract class VertexType< + /* Defines a vertex of _this_ graph with fixed raw types */ + protected abstract class VertexType< VT extends G.VertexType > extends com.bio4j.angulillos.VertexType { // NOTE: this initializer block will be inherited and will add each vertex type to the set @@ -48,12 +45,28 @@ public abstract class VertexType< @Override public final G graph() { return TypedGraph.this.self(); } } + + /* This set will store all edge types defined for this graph */ + private Set> edgeTypes = new HashSet<>(); + public final Set> edgeTypes() { return this.edgeTypes; } + /* Defines an edge between two vertices of _this_ graph */ - public abstract class EdgeType< + protected abstract class EdgeType< ST extends G.VertexType, ET extends G.EdgeType, TT extends G.VertexType > extends com.bio4j.angulillos.EdgeType { + // NOTE: this initializer block will be inherited and will add each edge type to the set + { + if ( + TypedGraph.this.edgeTypes.removeIf( (G.EdgeType et) -> + et._label.equals( self()._label ) + ) + ) { + throw new IllegalArgumentException("The graph contains duplicate edge type: " + self()._label); + } + TypedGraph.this.edgeTypes.add(self()); + } protected EdgeType(ST sourceType, TT targetType) { super(sourceType, targetType); } @@ -61,22 +74,53 @@ public abstract class EdgeType< } - public abstract class VertexIndex< + /* This set will store all vertex indexes defined for this graph */ + private Set> vertexIndexes = new HashSet<>(); + public final Set> vertexIndexes() { return this.vertexIndexes; } + + protected abstract class VertexIndex< VT extends G.VertexType, P extends Property, X > implements TypedIndex.Vertex, G,P,X,RV> { + // NOTE: this initializer block will be inherited and will add each vertex index to the set + { + if ( + TypedGraph.this.vertexIndexes.removeIf( (VertexIndex vi) -> + vi._label().equals( this._label() ) + ) + ) { + throw new IllegalArgumentException("The graph contains duplicate vertex index: " + this._label()); + } + TypedGraph.this.vertexIndexes.add(this); + } @Override public final G graph() { return TypedGraph.this.self(); } } - public abstract class EdgeIndex< + + /* This set will store all edge indexes defined for this graph */ + private Set> edgeIndexes = new HashSet<>(); + public final Set> edgeIndexes() { return this.edgeIndexes; } + + protected abstract class EdgeIndex< ST extends G.VertexType, ET extends G.EdgeType, TT extends G.VertexType, P extends Property, X > implements TypedIndex.Edge, G,P,X,RE> { + // NOTE: this initializer block will be inherited and will add each edge index to the set + { + if ( + TypedGraph.this.edgeIndexes.removeIf( (EdgeIndex ei) -> + ei._label().equals( this._label() ) + ) + ) { + throw new IllegalArgumentException("The graph contains duplicate edge index: " + this._label()); + } + TypedGraph.this.edgeIndexes.add(this); + } @Override public final G graph() { return TypedGraph.this.self(); } }