***************************************************** 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 :class:`PgxProperty` class: .. code-block:: python :linenos: prop[key] You can either pass in a :class:`PgxVertex`/:class:`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 :meth:`PgxVertex.get_property`/:class:`PgxEdge.get_property` methods. Examples: .. code-block:: python :linenos: v_prop = graph.get_vertex_property('prop1') e_prop = graph.get_edge_property('cost') v = graph.get_vertex(1908) e = graph.get_edge(0) v_prop.get(128) e_prop.get(2) # You can get a single property value through the following method of PgxVertex and PgxEdge prop1 = v.get_property('prop1') cost = e.get_property('cost') Similarly, the client can set the value of properties by applying one of the following methods in :class:`PgxProperty`: .. code-block:: python :linenos: property.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 :meth:`fill()` method, the client can set the same value for a property in every vertex or edge of the graph: .. code-block:: python :linenos: # Using following fill() method, the user can set the same value # for a property in every vertex or edge of the graph property = graph.create_edge_property('integer') property.fill(5) You can set a single property value through the following method of :class:`PgxVertex` and :class:`PgxEdge` .. code-block:: python :linenos: edge.get_property(property_name) vertex.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 :class:`PgxProperty` class: .. code-block:: python :linenos: cost = graph.get_edge_property("cost") cost.get_values() cost.get_top_k_values(2) cost.get_bottom_k_values(2) The method :meth:`get_values` returns an iterator of key-value pairs; the key is the unique :class:`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 (:meth:`get_top_k_values` and :meth:`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 :class:`VertexLabels` and :class:`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 :class:`VertexLabels`: .. code-block:: python :linenos: vertices.get(key) To access an edge label, PGX offers the following methods of :class:`EdgeLabel`: .. code-block:: python :linenos: edges.get(key) You can either pass in a :class:`PgxVertex` / :class:`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 :meth:`PgxVertex.labels` / :meth:`PgxEdge.labels` methods. Examples: .. code-block:: python :linenos: # Load a graph graph = session.read_graph_with_properties(self.graph_config) vertex_labels = graph.get_vertex_labels() edge_label = graph.get_edge_label() vertex = graph.get_vertex(3) edge = graph.get_edge(6) # Accessing a label is done with PgxVertex/PgxEdge objects v_labels = vertex_labels.get(vertex) e_label = edge_label.get(edge) # Or directly accessible via vertex/edge identifiers v_labels = vertex_labels.get(3) e_label = edge_label.get(6) # Or accessing directly from PgxVertex/PgxEdge objects v_labels = vertex.labels e_label = edge.label