Accessing Properties and Labels on Vertices and Edges

Setting and Getting Property Values

PGX offers the following methods for setting and getting property values in the PgxProperty class:

1prop[key]

You can either pass in a PgxVertex/PgxEdge object or directly assign the vertex/edge identifier values. Another way to get vertex/edge property values is to pass vertex/edge property name to PgxVertex.get_property()/PgxEdge.get_property methods.

Examples:

 1 v_prop = graph.get_vertex_property('prop1')
 2 e_prop = graph.get_edge_property('cost')
 3
 4 v = graph.get_vertex(1908)
 5 e = graph.get_edge(0)
 6
 7 v_prop.get(128)
 8 e_prop.get(2)
 9
10 # You can get a single property value through the following method of PgxVertex and PgxEdge
11 prop1 = v.get_property('prop1')
12 cost = e.get_property('cost')

Similarly, the client can set the value of properties by applying one of the following methods in PgxProperty:

1property.property(key, value)

Property values may be set in a number of ways. A single property value can be set directly (first method), or the property values for several vertices and edges can be set all at once. Using the following fill() method, the client can set the same value for a property in every vertex or edge of the graph:

1 # Using following fill() method, the user can set the same value
2 # for a property in every vertex or edge of the graph
3 property = graph.create_edge_property('integer')
4 property.fill(5)

You can set a single property value through the following method of PgxVertex and PgxEdge

1edge.get_property(property_name)
2vertex.get_property(property_name)

Ordered Iteration over the values of a Property

To iterate the values of a property, the client can invoke the following methods in the PgxProperty class:

1 cost = graph.get_edge_property("cost")
2 cost.get_values()
3 cost.get_top_k_values(2)
4 cost.get_bottom_k_values(2)

The method get_values() returns an iterator of key-value pairs; the key is the unique PgxVertex and the value is the value of this property for that vertex or edge. The iteration does not necessarily follow a specific order.

The next two methods (get_top_k_values() and get_bottom_k_values()) return similar iterators. However, these iterators are ordered by the value of this property, in descending or ascending order up to K elements.

Getting Label Values

PGX provides VertexLabels and EdgeLabel for multiple labels on a vertex and a single label on an edge. The property graph model that PGX support defines such label concepts.

Similar to the property access, to access vertex labels, PGX offers the following methods of VertexLabels:

1vertices.get(key)

To access an edge label, PGX offers the following methods of EdgeLabel:

1edges.get(key)

You can either pass in a PgxVertex / PgxEdge object or directly assign the vertex/edge identifier values and a boxed Java object with the appropriate type that contains the requested value is returned. Another way to get vertex/edge labels is to directly call PgxVertex.labels() / PgxEdge.labels() methods.

Examples:

 1 # Load a graph
 2 graph = session.read_graph_with_properties(self.graph_config)
 3
 4 vertex_labels = graph.get_vertex_labels()
 5 edge_label = graph.get_edge_label()
 6
 7 vertex = graph.get_vertex(3)
 8 edge = graph.get_edge(6)
 9
10 # Accessing a label is done with PgxVertex/PgxEdge objects
11 v_labels = vertex_labels.get(vertex)
12 e_label = edge_label.get(edge)
13
14 # Or directly accessible via vertex/edge identifiers
15 v_labels = vertex_labels.get(3)
16 e_label = edge_label.get(6)
17
18 # Or accessing directly from PgxVertex/PgxEdge objects
19 v_labels = vertex.labels
20 e_label = edge.label