// ユーザーを示すPersonクラスを定義
@Vertex
public class Person extends Entity {
String name;
public Person(String name) {
this.name = name;
}
}
// 2人のユーザーの関係性を示すFriendshipクラスを定義
@Edge
public class Friendship extends Entity {
Date startDate;
public Friendship(Date startDate) {
this.startDate = startDate;
}
}
GraphFactory factory = GraphFactory.getInstance();
GraphDatabase graphDB = factory.getGraphDatabase("myGraphDB");
Transaction tx = graphDB.beginTransaction(AccessMode.READ_WRITE);
// Personノードを作成
Person hanako = new Person("Hanako");
Person taro = new Person("Taro");
Person bob = new Person("Bob");
// Friendshipエッジを作成
Friendship aliceBob = new Friendship(new Date());
Friendship bobCharlie = new Friendship(new Date());
// エッジにノードを接続
aliceBob.connect(hanako, taro);
bobCharlie.connect(taro, bob);
tx.commit();
// トランザクションを開始
Transaction tx = graphDB.beginTransaction(AccessMode.READ_ONLY);
// Hanakoから交友関係を探索
Navigator navigator = hanako.navigate().depthFirst().adjacentVertices();
while (navigator.hasNext()) {
Person friend = (Person) navigator.next();
System.out.println("Hanako is friends with: " + friend.name);
}
tx.commit();