Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
augustearth committed Mar 21, 2023
1 parent 6c699fb commit fe237d8
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,32 @@ import org.apache.tinkerpop.gremlin.structure.Element
@Slf4j
class PropertyValuesHolder<T> {

/** */
/** A map of property definitions to property values */
Map<PropertyDefinition,Object> propertyValues = new HashMap<PropertyDefinition,Object>()

/** */
/** If true, then properties must be defined to have a value */
Boolean propertiesMustBeDefined = true


/** */
/**
* Set the provided property to the provided enum value.
* @param propDef The property to set
* @param propValue The enum value to use
* @return This object
*/
public T withProperty(PropertyDefinition propDef, Enum propValue) {
assert propDef != null
assert propValue != null
withProperty(propDef, propValue.name())
}


/** */
/**
* Set the provided property to the provided value.
* @param propDef The property to set
* @param prooValue The value to use
* @return This object
*/
public T withProperty(PropertyDefinition propDef, Object propValue) {
assert this.respondsTo("getElementDef")
ElementDefinition eDef = getElementDef()
Expand All @@ -51,7 +61,12 @@ class PropertyValuesHolder<T> {
}


/** */
/**
* Set the provided property pairs of properties and values, ex.
* .setProperty(p1, v1, p2, v2).
* @param args Pairs of property definitions and values
* @return This object
*/
public T withProperties(Object... args) {
if (args.size() == 1) {
assert args[0] instanceof Map
Expand All @@ -61,7 +76,12 @@ class PropertyValuesHolder<T> {
}


/** */
/**
* Set all the properties specified in the provided map of property
* definitions to values ex. .setProperty([p1:v1, p2:v2]).
* @param args Map of property definitions to property values
* @return This object
*/
public T withProperties(Map args) {
assert this.respondsTo("getElementDef")
ElementDefinition eDef = getElementDef()
Expand All @@ -75,7 +95,13 @@ class PropertyValuesHolder<T> {
}


/** */
/**
* Set all the properties in the provided list where the list elements
* alternate between property definitions and property values ex.
* .setProperty([p1, v1, p2, v2]).
* @paran args A list of property definitions and values
* @return This object
*/
public T withProperties(List args) {
def numArgs = args.size()
assert numArgs >= 2
Expand All @@ -88,13 +114,23 @@ class PropertyValuesHolder<T> {
}


/** */
/**
* Set the pairs of property definitions and property values when the value
* is not null ex. .withNonNullProperties(p1, v1, p2, v2)
* @param args Pairs of property definitions and values
* @return This object
*/
public T withNonNullProperties(Object... args) {
withNonNullProperties(args.toList())
}


/** */
/**
* Set the pairs of property definitions and property values when the value
* is not null ex. .withNonNullProperties([p1, v1, p2, v2]).
* @paran args A list of property definitions and values
* @return This object
*/
public T withNonNullProperties(List args) {
def numArgs = args.size()
assert numArgs >= 2
Expand All @@ -110,7 +146,8 @@ class PropertyValuesHolder<T> {
/**
* Matches a map of data against the properties of this element by name and
* assignes the property value on match.
*
* @param args A map of property definitions to values
* @return This object
*/
public T withMatchingProperties(Map args) {
assert args != null
Expand All @@ -129,7 +166,8 @@ class PropertyValuesHolder<T> {
* Matches a map of data against the properties of this element by name and
* assignes the property value on match ignoring data records where the
* value is null.
*
* @param args A map of property definitions to values
* @return This object
*/
public T withNonNullMatchingProperties(Map args) {
assert args != null
Expand All @@ -138,7 +176,12 @@ class PropertyValuesHolder<T> {
}


/** */
/**
* Utility method to split a list of pairs into property definition and
* value and then call withProperty on each pair.
* @param pairs A list of two element lists where each two element list
* is a pair of property definition and property value.
*/
private void holdPropertyPairs(List<List> pairs) {
Map<PropertyDefinition,Object> props = new HashMap<PropertyDefinition,Object>()
pairs.each { p ->
Expand All @@ -152,7 +195,10 @@ class PropertyValuesHolder<T> {
}


/** */
/**
* Returns a map of all property definitions and their value.
* @return A map of property definition to value
*/
public Map<PropertyDefinition,Object> allPropertyValues() {
assert this.respondsTo("getElementDef")
ElementDefinition eDef = getElementDef()
Expand All @@ -172,7 +218,11 @@ class PropertyValuesHolder<T> {
}


/** */
/**
* Set the properties of the provided graph element to the property values
* held by this object.
* @param el The target graph element
*/
public Element setElementProperties(Element el) {
def pvs = allPropertyValues()
pvs.each { PropertyDefinition vp, Object val ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ class VertexBuilder extends PropertyValuesHolder<VertexBuilder> {
///////////////////////////////////////////////////////////////////////////

/**
* Object that owns this builder.
* */
* The source vertex definition of this builder.
*/
VertexDefinition vertexDef


///////////////////////////////////////////////////////////////////////////
// CONSTRUCTOR
///////////////////////////////////////////////////////////////////////////

/** */
/**
* Constructor
* @param The source vertex definition of this builder.
*/
public VertexBuilder(VertexDefinition vertexDef) {
assert vertexDef
this.vertexDef = vertexDef
Expand All @@ -48,7 +51,9 @@ class VertexBuilder extends PropertyValuesHolder<VertexBuilder> {
// GETTERS
///////////////////////////////////////////////////////////////////////////

/** */
/**
* Return the source vertex definition as an element definition.
*/
public ElementDefinition getElementDef() { vertexDef }


Expand Down Expand Up @@ -104,7 +109,13 @@ class VertexBuilder extends PropertyValuesHolder<VertexBuilder> {
}


/** */
/**
* Returns a graph traversal that starts with vertices that match this
* vertex builder.
* @param graph The property graph
* @param g A graph traversal source to use
* @return A graph traversal
*/
public Traversal traversal(Graph graph, GraphTraversalSource g) {
assert graph
assert g
Expand Down Expand Up @@ -174,7 +185,10 @@ class VertexBuilder extends PropertyValuesHolder<VertexBuilder> {
}


/** */
/**
* Assert that all the properties required by the source vertex definition
* have been set in this builder.
*/
void assertRequiredProperties() {
vertexDef.requiredProperties.each { requiredPropDef ->
boolean found = allPropertyValues().find { k, v ->
Expand Down
Loading

0 comments on commit fe237d8

Please sign in to comment.