Java源码示例:org.neo4j.driver.v1.Record
示例1
public RDElement createFromMethod(Record result, double height, double ringWidth, double ringWidthAD,
double transparency, double minArea, String color) {
long id = result.get("node").asNode().id();
long typeID = result.get("tID").asLong();
if (methodTypeMode) {
if (result.get("node").asNode().hasLabel(Labels.Constructor.name())) {
return new DiskSegment(id, typeID, height, transparency, minArea, color, result.get("line").asInt(0));
} else {
return new SubDisk(id, typeID, ringWidth, height, transparency, color);
}
} else {
if (methodDisks) {
return new SubDisk(id, typeID, ringWidthAD, height, transparency, color);
} else {
return new DiskSegment(id, typeID, height, transparency, minArea, color, result.get("line").asInt(0));
}
}
}
示例2
private void calculateFloors(Node building) {
Node position = connector.getPosition(building.id());
double bHeight = building.get("height").asDouble();
double bWidth = building.get("width").asDouble();
double bLength = building.get("length").asDouble();
double bPosX = position.get("x").asDouble();
double bPosY = position.get("y").asDouble();
double bPosZ = position.get("z").asDouble();
StatementResult floors = connector.executeRead("MATCH (n)-[:CONTAINS]->(f:Floor) WHERE ID(n) = " + building.id() +
" RETURN f");
int floorNumberValue = connector.executeRead("MATCH (n)-[:CONTAINS]->(f:Floor) WHERE ID(n) = " + building.id() +
" RETURN COUNT(f) as floorNumber").single().get("floorNumber").asInt();
int floorCounter = 0;
while (floors.hasNext()) {
Record record = floors.next();
long floor = record.get("f").asNode().id();
floorCounter++;
String statement = cypherSetBuildingSegmentAttributes(floor, bWidth * 1.1, bLength * 1.1,
bHeight / (floorNumberValue + 2 ) * 0.80, floorColor);
statement +=
String.format("CREATE (n)-[:HAS]->(p:City:Position {x: %f, y: %f, z: %f})", bPosX,
(bPosY - ( bHeight / 2) ) + bHeight / ( floorNumberValue + 2 ) * floorCounter, bPosZ);
connector.executeWrite(statement);
}
}
示例3
private void enhanceTranslationUnit(Record record) {
Node node = record.get("n").asNode();
String fileName = record.get("f.fileName").asString();
if(fileName.endsWith(".ast")) {
fileName = fileName.replaceAll(".ast", "");
if(!fileName.endsWith(".h")) {
fileName = fileName + ".c";
}
}
this.fileNameTranslationUnit = fileName;
connector.executeWrite(
"MATCH (n) WHERE ID(n) = " + node.id() + " SET n.name = '" + fileName + "', n.fqn = '" + fileName + "', n.hash = '" + createHash(fileName) + "'"
);
}
示例4
public static SubGraph run(SubGraph searchResult1, ApiLocatorContext context){
SubGraph r = new SubGraph();
r.getNodes().addAll(searchResult1.getNodes());
r.cost = searchResult1.cost;
for (long node : searchResult1.getNodes()) {
Session session = context.connection.session();
String stat = "match p=(n1)-[:" + CodeInDocxFileExtractor.API_EXPLAINED_BY + "]->(n2) where id(n1)=" + node + " unwind relationships(p) as r return id(r), id(startNode(r)), id(endNode(r))";
StatementResult rs = session.run(stat);
while (rs.hasNext()) {
Record item=rs.next();
long node1 = item.get("id(startNode(r))").asLong();
long node2 = item.get("id(endNode(r))").asLong();
long rel = item.get("id(r)").asLong();
r.getNodes().add(node1);
r.getNodes().add(node2);
r.getEdges().add(rel);
}
session.close();
}
return r;
}
示例5
public static SubGraph run(String query, SubGraph searchResult1, SubGraph linkedSearchResult1, ApiLocatorContext context){
SubGraph r = new SubGraph();
r.getNodes().addAll(searchResult1.getNodes());
r.cost = searchResult1.cost;
List<LuceneSearchResult> luceneSearchResults=context.getLuceneSearcher().query(query);
luceneSearchResults.sort((LuceneSearchResult a, LuceneSearchResult b)->{
Double aDist=new Double(dist(a.nodeSet,searchResult1.getNodes(),context));
Double bDist=new Double(dist(b.nodeSet,searchResult1.getNodes(),context));
return aDist.compareTo(bDist);
});
for (int i=0;i<3&&i<luceneSearchResults.size();i++) {
r.getNodes().add(luceneSearchResults.get(i).id);
for (long node:linkedSearchResult1.getNodes()){
Session session=context.connection.session();
StatementResult rs=session.run("match (a)-[r]-(b) where id(a)="+node+" and id(b)="+luceneSearchResults.get(i).id+" return id(r)");
while (rs.hasNext()){
Record item=rs.next();
r.getEdges().add(item.get("id(r)").asLong());
}
session.close();
}
}
return r;
}
示例6
public Map<String, String> getLabels(boolean refresh) {
if (labels == null || refresh) {
Map<String, String> old = labels == null ?
new LinkedHashMap<String, String>() : new LinkedHashMap<>(labels);
labels = new LinkedHashMap<>();
StatementResult result = this.neo4jConnectionManager.execute("CALL db.labels()");
Set<String> colors = new HashSet<>();
while (result.hasNext()) {
Record record = result.next();
String label = record.get("label").asString();
String color = old.get(label);
while (color == null || colors.contains(color)) {
color = Neo4jConversionUtils.getRandomLabelColor();
}
colors.add(color);
labels.put(label, color);
}
}
return labels;
}
示例7
private Entity getPropertiesObject(long id, int rowIndex, ElemType typ) {
Record rec = this.records.get(rowIndex);
List<Pair<String, Value>> flds = rec.fields();
for (Pair<String, Value> pair : flds) {
if (typ == ElemType.NODE && pair.value() instanceof NodeValue) {
Node nd = pair.value().asNode();
if (nd.id() == id)
return nd;
} else if (typ == ElemType.RELATION && pair.value() instanceof RelationshipValue) {
Relationship rel = pair.value().asRelationship();
if (rel.id() == id)
return rel;
}
}
// element with id may not have been loaded
return this.reloaded.getEntity(id, typ);
}
示例8
@Override
public Map<String, String> executeQuery(String query, Map<String, Object> parameters, GraphQueryResultCallback handler) {
try (Session session = gremlinDriver.session()) {
StatementResult result = session.run(query, parameters);
long count = 0;
while (result.hasNext()) {
Record record = result.next();
Map<String, Object> asMap = handleInternalNode(record.asMap());
handler.process(asMap, result.hasNext());
count++;
}
Map<String,String> resultAttributes = new HashMap<>();
resultAttributes.put(NODES_CREATED, NOT_SUPPORTED);
resultAttributes.put(RELATIONS_CREATED, NOT_SUPPORTED);
resultAttributes.put(LABELS_ADDED, NOT_SUPPORTED);
resultAttributes.put(NODES_DELETED, NOT_SUPPORTED);
resultAttributes.put(RELATIONS_DELETED, NOT_SUPPORTED);
resultAttributes.put(PROPERTIES_SET, NOT_SUPPORTED);
resultAttributes.put(ROWS_RETURNED, String.valueOf(count));
return resultAttributes;
} catch (Exception ex) {
throw new ProcessException(ex);
}
}
示例9
public RDElement createFromField(Record result, double ringWidthAD, double height, double transparency, String color) {
long id = result.get("node").asNode().id();
long typeID = result.get("tID").asLong();
if (methodTypeMode) {
return new SubDisk(id, typeID, ringWidthAD, height, transparency, color);
} else {
if (dataDisks) {
return new SubDisk(id, typeID, ringWidthAD, height, transparency, color);
} else {
return new DiskSegment(id, typeID, height, transparency, color);
}
}
}
示例10
public RDElement createFromFunction(Record result, double height, double ringWidthAD,
double transparency, double minArea, String color) {
long id = result.get("node").asNode().id();
long typeID = result.get("tID").asLong();
if(methodDisks) {
return new SubDisk(id, typeID, ringWidthAD, height, transparency, color);
} else {
return new DiskSegment(id, typeID, height, transparency, minArea, color, result.get("lineCount").asInt(0));
}
}
示例11
public RDElement createFromVariable(Record result, double height, double ringWidthAD, double transparency, String color) {
long id = result.get("node").asNode().id();
long typeID = result.get("tID").asLong();
if (dataDisks) {
return new SubDisk(id, typeID, ringWidthAD, height, transparency, color);
} else {
return new DiskSegment(id, typeID, height, transparency, color);
}
}
示例12
private static void adjustPositions(List<Node> children, double parentX, double parentY, double parentZ) {
for (Node child : children) {
Record record = connector.executeRead(
"MATCH (p:Position)<-[:HAS]-(n)-[:VISUALIZES]->(e) WHERE ID(n) = " + child.id() + " RETURN e, p")
.single();
Node entity = record.get("e").asNode();
Node position = record.get("p").asNode();
double centerX = position.get("x").asDouble();
double centerZ = position.get("z").asDouble();
double centerY = position.get("y").asDouble();
double setX = centerX + parentX + config.getBuildingHorizontalMargin();
double setZ = centerZ + parentZ + config.getBuildingHorizontalMargin();
double setY = centerY + parentY + config.getBuildingVerticalMargin();
double width = properties.get(child.id())[0];
double length = properties.get(child.id())[1];
connector.executeWrite(String.format(
"MATCH (c),(p) WHERE ID(c) = %d AND ID(p) = %d SET c.width = %f, c.length = %f, p.x = %f, p.y = %f, p.z = %f",
child.id(), position.id(), width, length, setX, setY, setZ));
double height = child.get("height").asDouble();
if (entity.hasLabel(Labels.Package.name())) {
double newUpperLeftX = setX - width / 2;
double newUpperLeftZ = setZ - length / 2;
double newUpperLeftY = setY - height / 2;
adjustPositions(getChildren(child.id()), newUpperLeftX, newUpperLeftY, newUpperLeftZ);
}
}
}
示例13
private void enhanceNode(Record record) {
Node node = record.get("e").asNode();
Node declaringParent = record.get("p").asNode();
fileNameTranslationUnit = connector.executeRead("MATCH (t:TranslationUnit)-[:DECLARES*]->(e) WHERE ID(e) = " + node.id() + " RETURN t.fileName").single().get("t.fileName").asString();
// this was included to exclude C elements declared outside the translation unit (usage of #include)
// but this does not work with the current jqassistant setup, since the files gets downloaded and thereby the filename is changed which is crucial
//if (fileNameChild.equals(fileNameTranslationUnit)) {
//If variable/constant is part of a struct, union or enum it could have the same name as another variable/constant therefore add parent name to name.
String fqn = "";
if ((node.hasLabel(Labels.Variable.name()) || node.hasLabel(Labels.EnumConstant.name())) && declaringParent != null
&& (declaringParent.hasLabel(Labels.Struct.name()) || declaringParent.hasLabel(Labels.Union.name()) || declaringParent.hasLabel(Labels.Enum.name()))) {
fqn = fileNameTranslationUnit + "_" + declaringParent.get("name") + "_" + node.get("name");
} else if (node.get("fqn").isNull()) {
fqn = fileNameTranslationUnit + "_" + node.get("name");
}
if (node.get("hash").isNull()) {
String hash = createHash(node.get("fqn").toString());
connector.executeWrite(
"MATCH (n) WHERE ID(n) = " + node.id() + " SET n.hash = '" + hash + "', n.fqn = '" + fqn + "'");
} else {
connector.executeWrite(
"MATCH (n) WHERE ID(n) = " + node.id() + " SET n.fqn = '" + fqn + "'");
}
}
示例14
private void enhanceCondition(Record record) {
Node node = record.get("n").asNode();
if(node.get("hash").isNull()){
String hash = createHash(Long.toString(node.id()));
connector.executeWrite(
"MATCH (n) WHERE ID(n) = " + node.id() + " SET n.hash = '" + hash + "'"
);
}
}
示例15
private void enhanceNode(Record record) {
Node node = record.get("n").asNode();
Value fqnValue = node.get("fqn");
String fqn = fqnValue.asString();
if (fqnValue.isNull()) {
Node container = connector.executeRead(
"MATCH (n)<-[:DECLARES]-(container) " +
"WHERE ID(n) = " + node.id() + " " +
"RETURN container"
).single().get("container").asNode();
String containerFqn = container.get("fqn").asString();
String name = node.get("name").asString();
String signature = node.get("signature").asString();
int index = signature.indexOf(" ") + 1;
if (node.hasLabel("Method")) {
int indexOfBracket = signature.indexOf("(");
if (name.isEmpty()) {
name = signature.substring(index, indexOfBracket);
}
fqn = containerFqn + "." + signature.substring(index);
} else {
if (name.isEmpty()) {
name = signature.substring(index);
}
fqn = containerFqn + "." + name;
}
connector.executeWrite(
"MATCH (n) WHERE ID(n) = " + node.id() + " SET n.name = '" + name + "', n.fqn = '" + fqn + "'");
}
connector.executeWrite(
"MATCH (n) WHERE ID(n) = " + node.id() + " SET n.hash = '" + createHash(fqn) + "'"
);
}
示例16
@Test
void numberOfMainDisks() {
Record result = connector
.executeRead("MATCH (disk:MainDisk)-[:VISUALIZES]->(n) RETURN count(disk) AS result").single();
int numberOfDisks = result.get("result").asInt();
assertEquals(3, numberOfDisks);
}
示例17
@Test
void numberOfSubDisks() {
Record result = connector
.executeRead("MATCH (disk:SubDisk)-[:VISUALIZES]->(n) RETURN count(disk) AS result").single();
int numberOfDisks = result.get("result").asInt();
assertEquals(7, numberOfDisks);
}
示例18
@Test
void numberOfDiskSegments() {
Record result = connector
.executeRead("MATCH (disk:DiskSegment)-[:VISUALIZES]->(n) RETURN count(disk) AS result").single();
int numberOfDisksSegments = result.get("result").asInt();
assertEquals(35, numberOfDisksSegments);
}
示例19
@Test
void parentChildRelationTest () {
long diskID = disk.getID();
long diskSegmentID = diskSegment.getID();
Record result = connector.executeRead("MATCH (n)-[r]->(m) WHERE ID(n) = " + diskID + " AND ID(m) = " +
diskSegmentID + " RETURN type(r) AS result").single();
String relation = result.get("result").asString();
assertEquals("CONTAINS", relation);
}
示例20
@Test
void layoutAlgorithmPackage() {
String hash = "ID_4481fcdc97864a546f67c76536e0308a3058f75d";
Record result = connector.executeRead(
"MATCH (:Package {hash: '" + hash + "'})<-[:VISUALIZES]-(:District)-[:HAS]->(position:Position) "
+ "RETURN position.x as x, position.y as y, position.z as z")
.single();
double x = result.get("x").asDouble();
double y = result.get("y").asDouble();
double z = result.get("z").asDouble();
assertEquals(11.0, x);
assertEquals(2.5, y);
assertEquals(14.0, z);
}
示例21
@Test
void layoutAlgorithmClass() {
String hash = "ID_26f25e4da4c82dc2370f3bde0201e612dd88c04c";
Record result = connector.executeRead(
"MATCH (:Type {hash: '" + hash + "'})<-[:VISUALIZES]-(:Building)-[:HAS]->(position:Position) "
+ "RETURN position.x as x, position.y as y, position.z as z")
.single();
double x = result.get("x").asDouble();
double y = result.get("y").asDouble();
double z = result.get("z").asDouble();
assertEquals(10.5, x);
assertEquals(5, y);
assertEquals(17.5, z);
}
示例22
@Test
void classMembers() {
String hash = "ID_26f25e4da4c82dc2370f3bde0201e612dd88c04c";
Record result = connector.executeRead(
"MATCH (building:Building)-[:VISUALIZES]->(:Type {hash: '" + hash + "'}) "
+ "RETURN building.height as height, building.length as length, building.width as width")
.single();
double height = result.get("height").asDouble();
double length = result.get("length").asDouble();
double width = result.get("width").asDouble();
assertEquals(4.0, height);
assertEquals(3.0, length);
assertEquals(3.0, width);
}
示例23
@Test
void districtColor() {
String hash = "ID_4481fcdc97864a546f67c76536e0308a3058f75d";
Record result = connector.executeRead(
"MATCH (:Package {hash: '" + hash + "'})<-[:VISUALIZES]-(d:District)"
+ "RETURN d.color as color")
.single();
String color = result.get("color").asString();
assertEquals("#b1b1b1", color);
}
示例24
@Test
void numberOfInnerTypes() {
Record result = connector.executeRead("MATCH (innerType:Inner) RETURN count(innerType) AS result").
single();
int numberOfInnerType = result.get("result").asInt();
assertEquals(0, numberOfInnerType);
}
示例25
@Test
void layoutAlgorithmPackage() {
String hash = "ID_4481fcdc97864a546f67c76536e0308a3058f75d";
Record result = connector.executeRead(
"MATCH (:Package {hash: '" + hash + "'})<-[:VISUALIZES]-(:MainDisk)-[:HAS]->(position:Position) "
+ "RETURN position.x as x, position.y as y, position.z as z")
.single();
double x = result.get("x").asDouble();
double y = result.get("y").asDouble();
double z = result.get("z").asDouble();
assertEquals(-9.214771, x);
assertEquals(-11.790536, y);
assertEquals(0, z);
}
示例26
@Test
void layoutAlgorithmClass() {
String hash = "ID_26f25e4da4c82dc2370f3bde0201e612dd88c04c";
Record result = connector
.executeRead("MATCH (:Type {hash: '" + hash + "'})<-[:VISUALIZES]-(:SubDisk)-[:HAS]->(position:Position) "
+ "RETURN position.x as x, position.y as y, position.z as z")
.single();
double x = result.get("x").asDouble();
double y = result.get("y").asDouble();
double z = result.get("z").asDouble();
assertEquals(-9.214771, x);
assertEquals(-1.498676, y);
assertEquals(0, z);
}
示例27
@Test
void methodSorting() {
String hash = "ID_d2955e64b6776b754f9d69f8f480a62f849584ca";
Record result = connector.executeRead("MATCH (segment:DiskSegment)-[:VISUALIZES]->(:Method {hash: '" + hash
+ "'}) RETURN segment.anglePosition as anglePosition").single();
double anglePosition = result.get("anglePosition").asDouble();
assertEquals(360.03296703296706, anglePosition);
}
示例28
@Test
void layoutAlgorithmPackage() {
Record result = connector.executeRead(
"MATCH (:Package {hash: 'ID_52a6b74381719655ffacf4d2c8c38a22d5581078'})<-[:VISUALIZES]-(:MainDisk)-[:HAS]->(position:Position) " +
"RETURN position.x as x, position.y as y, position.z as z").single();
double x = result.get("x").asDouble();
double y = result.get("y").asDouble();
double z = result.get("z").asDouble();
assertEquals(-2.576052, x);
assertEquals(-12.33451, y);
assertEquals(0, z);
}
示例29
@Test
void layoutAlgorithmClass() {
Record result = connector.executeRead(
"MATCH (:Type {hash: 'ID_0d6283470c75fbfeae1b0199ae41f732db8dc820'})<-[:VISUALIZES]-(:SubDisk)-[:HAS]->(position:Position) " +
"RETURN position.x as x, position.y as y, position.z as z"
).single();
double x = result.get("x").asDouble();
double y = result.get("y").asDouble();
double z = result.get("z").asDouble();
assertEquals(93.130997, x);
assertEquals(-56.163684, y);
assertEquals(0, z);
}
示例30
@Test
void methodSorting() {
Record result = connector.executeRead(
"MATCH (segment:DiskSegment)-[:VISUALIZES]->(:Method {hash: 'ID_bfdf7b3da476584de47af84e759495ce14f31e46'}) " +
"RETURN segment.anglePosition as anglePosition"
).single();
double anglePosition = result.get("anglePosition").asDouble();
assertEquals(354.44000000000005, anglePosition);
}