NebulaGraph Java Client  release-3.8
PathWrapper.java
1 /* Copyright (c) 2020 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 package com.vesoft.nebula.client.graph.data;
7 
8 import com.vesoft.nebula.Edge;
9 import com.vesoft.nebula.Path;
10 import com.vesoft.nebula.Step;
11 import com.vesoft.nebula.Value;
12 import com.vesoft.nebula.client.graph.exception.InvalidValueException;
13 import java.io.UnsupportedEncodingException;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Objects;
18 
19 public class PathWrapper extends BaseDataObject {
20  private List<Segment> segments = new ArrayList<>();
21  private List<Node> nodes = new ArrayList<>();
22  private List<Relationship> relationships = new ArrayList<>();
23  private Path path = null;
24 
25  public static class Segment {
26  Node startNode;
27  Relationship relationShip;
28  Node endNode;
29 
39  public Segment(Node startNode, Relationship relationShip, Node endNode) {
40  this.startNode = startNode;
41  this.relationShip = relationShip;
42  this.endNode = endNode;
43  }
44 
48  public Node getStartNode() {
49  return startNode;
50  }
51 
55  public Relationship getRelationShip() {
56  return relationShip;
57  }
58 
62  public Node getEndNode() {
63  return endNode;
64  }
65 
66  @Override
67  public boolean equals(Object o) {
68  if (this == o) {
69  return true;
70  }
71  if (o == null || getClass() != o.getClass()) {
72  return false;
73  }
74  Segment segment = (Segment) o;
75  return Objects.equals(startNode, segment.startNode)
76  && Objects.equals(relationShip, segment.relationShip)
77  && Objects.equals(endNode, segment.endNode);
78  }
79 
80  @Override
81  public int hashCode() {
82  return Objects.hash(startNode, relationShip, endNode);
83  }
84 
85  @Override
86  public String toString() {
87  return "Segment{"
88  + "startNode=" + startNode
89  + ", relationShip=" + relationShip
90  + ", endNode=" + endNode
91  + '}';
92  }
93  }
94 
99  public Node getStartNode() {
100  if (nodes == null || nodes.isEmpty()) {
101  return null;
102  }
103  return nodes.get(0);
104  }
105 
110  public Node getEndNode() {
111  if (nodes == null || nodes.isEmpty()) {
112  return null;
113  }
114  return nodes.get(nodes.size() - 1);
115  }
116 
122  public boolean containNode(Node node) {
123  return nodes.contains(node);
124  }
125 
131  public boolean containRelationship(Relationship relationship) {
132  return relationships.contains(relationship);
133  }
134 
139  public List<Node> getNodes() {
140  return nodes;
141  }
142 
147  public List<Relationship> getRelationships() {
148  return relationships;
149  }
150 
155  public List<Segment> getSegments() {
156  return segments;
157  }
158 
163  public int length() {
164  return segments.size();
165  }
166 
173  public PathWrapper(Path path) throws InvalidValueException, UnsupportedEncodingException {
174  if (path == null) {
175  this.nodes = new ArrayList<>();
176  this.relationships = new ArrayList<>();
177  this.segments = new ArrayList<>();
178  return;
179  }
180  this.path = path;
181  nodes.add((Node) new Node(path.src)
182  .setDecodeType(getDecodeType())
183  .setTimezoneOffset(getTimezoneOffset()));
184  List<Value> vids = new ArrayList<>();
185  vids.add(path.src.vid);
186  Value srcId;
187  Value dstId;
188  for (Step step : path.steps) {
189  Node startNode;
190  Node endNode;
191  int type = step.type;
192  if (step.type > 0) {
193  startNode = nodes.get(nodes.size() - 1);
194  endNode = (Node) new Node(step.dst)
195  .setDecodeType(getDecodeType())
196  .setTimezoneOffset(getTimezoneOffset());
197  nodes.add(endNode);
198  srcId = vids.get(vids.size() - 1);
199  dstId = step.dst.vid;
200  } else {
201  type = -type;
202  startNode = (Node) new Node(step.dst)
203  .setDecodeType(getDecodeType())
204  .setTimezoneOffset(getTimezoneOffset());
205  endNode = nodes.get(nodes.size() - 1);
206  nodes.add(startNode);
207  dstId = vids.get(vids.size() - 1);
208  srcId = step.dst.vid;
209  }
210  vids.add(step.dst.vid);
211  Edge edge = new Edge(srcId,
212  dstId,
213  type,
214  step.name,
215  step.ranking,
216  step.props);
217  Relationship relationShip = (Relationship) new Relationship(edge)
218  .setDecodeType(getDecodeType())
219  .setTimezoneOffset(getTimezoneOffset());
220  relationships.add(relationShip);
221  Segment segment = new Segment(startNode, relationShip, endNode);
222  if (segment.getStartNode() != nodes.get(nodes.size() - 1)
223  && segment.getEndNode() != nodes.get(nodes.size() - 1)) {
224  throw new InvalidValueException(
225  String.format("Relationship [%s] does not connect to the last node",
226  relationShip.toString()));
227  }
228  segments.add(segment);
229  }
230  }
231 
232  @Override
233  public String toString() {
234  try {
235  List<String> edgeStrs = new ArrayList<>();
236  for (int i = 0; i < relationships.size(); i++) {
237  Relationship relationship = relationships.get(i);
238  List<String> propStrs = new ArrayList<>();
239  Map<String, ValueWrapper> props = relationship.properties();
240  for (String key : props.keySet()) {
241  propStrs.add(key + ": " + props.get(key).toString());
242  }
243  Step step = path.steps.get(i);
244  Node node = (Node) new Node(step.dst)
245  .setDecodeType(getDecodeType())
246  .setTimezoneOffset(getTimezoneOffset());
247  if (step.type > 0) {
248  edgeStrs.add(String.format("-[:%s@%d{%s}]->%s",
249  relationship.edgeName(),
250  relationship.ranking(),
251  String.join(", ", propStrs),
252  node.toString()));
253  } else {
254  edgeStrs.add(String.format("<-[:%s@%d{%s}]-%s",
255  relationship.edgeName(),
256  relationship.ranking(),
257  String.join(", ", propStrs),
258  node.toString()));
259  }
260  }
261  return String.format("%s%s",
262  getStartNode().toString(), String.join("", edgeStrs));
263  } catch (UnsupportedEncodingException e) {
264  return e.getMessage();
265  }
266  }
267 
268  @Override
269  public boolean equals(Object o) {
270  if (this == o) {
271  return true;
272  }
273  if (o == null || getClass() != o.getClass()) {
274  return false;
275  }
276  PathWrapper segments1 = (PathWrapper) o;
277  return Objects.equals(segments, segments1.segments)
278  && Objects.equals(nodes, segments1.nodes)
279  && Objects.equals(relationships, segments1.relationships);
280  }
281 
282  @Override
283  public int hashCode() {
284  return Objects.hash(segments, nodes, relationships);
285  }
286 }
List< Relationship > getRelationships()
get all relationship from the path
List< Node > getNodes()
get all nodes from the path
List< Segment > getSegments()
get all segments from the path
int length()
get the length of the path
Node getEndNode()
get the end node from the path
PathWrapper(Path path)
PathWrapper is a wrapper around the Path type returned by nebula-graph.
boolean containRelationship(Relationship relationship)
determine if path contains the given relationShip
Node getStartNode()
get the start node from the path
boolean containNode(Node node)
determine if path contains the given node
long ranking()
get ranking from the relationship
HashMap< String, ValueWrapper > properties()
get property names and values from the relationship
String edgeName()
get edge name from the relationship