Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit 313f4a5

Browse files
committed
Same Community function
1 parent 4c7dbb0 commit 313f4a5

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

algo/src/main/java/org/neo4j/graphalgo/linkprediction/LinkPrediction.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ public double totalNeighbors(@Name("node1") Node node1, @Name("node2") Node node
119119
return neighborsFinder.findNeighbors(node1, node2, relationshipType, direction).size();
120120
}
121121

122+
@UserFunction("algo.linkprediction.sameCommunity")
123+
@Description("algo.linkprediction.sameCommunity(node1:Node, node2:Node, communityProperty: String) " +
124+
"given two nodes, indicates if they have the same community")
125+
public double sameCommunity(@Name("node1") Node node1, @Name("node2") Node node2,
126+
@Name(value = "communityProperty", defaultValue = "community") String communityProperty) {
127+
if(!node1.hasProperty(communityProperty) || !node2.hasProperty(communityProperty)) {
128+
return 0.0; }
129+
130+
return node1.getProperty(communityProperty).equals(node2.getProperty(communityProperty)) ? 1.0 : 0.0;
131+
}
132+
122133
private int degree(Node node, RelationshipType relationshipType, Direction direction) {
123134
return relationshipType == null ? node.getDegree(direction) : node.getDegree(relationshipType, direction);
124135
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* Copyright (c) 2017 "Neo4j, Inc." <http://neo4j.com>
3+
*
4+
* This file is part of Neo4j Graph Algorithms <http://github.com/neo4j-contrib/neo4j-graph-algorithms>.
5+
*
6+
* Neo4j Graph Algorithms is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package org.neo4j.graphalgo.algo.linkprediction;
20+
21+
import org.junit.AfterClass;
22+
import org.junit.BeforeClass;
23+
import org.junit.Test;
24+
import org.neo4j.graphalgo.linkprediction.LinkPrediction;
25+
import org.neo4j.graphdb.GraphDatabaseService;
26+
import org.neo4j.graphdb.Result;
27+
import org.neo4j.graphdb.Transaction;
28+
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
29+
import org.neo4j.kernel.impl.proc.Procedures;
30+
import org.neo4j.kernel.internal.GraphDatabaseAPI;
31+
import org.neo4j.test.TestGraphDatabaseFactory;
32+
33+
import java.util.Map;
34+
35+
import static org.junit.Assert.assertEquals;
36+
37+
public class SameCommunityProcIntegrationTest {
38+
private static final String SETUP =
39+
"CREATE (mark:Person {name: 'Mark'})\n" +
40+
"SET mark.community=1, mark.partition = 4\n" +
41+
"CREATE (michael:Person {name: 'Michael'})\n" +
42+
"SET michael.community=2, michael.partition=4\n" +
43+
"CREATE (praveena:Person {name: 'Praveena'})\n" +
44+
"SET praveena.community=1\n" +
45+
"CREATE (jennifer:Person {name: 'Jennifer'})\n";
46+
47+
private static GraphDatabaseService db;
48+
49+
@BeforeClass
50+
public static void setUp() throws Exception {
51+
db = new TestGraphDatabaseFactory()
52+
.newImpermanentDatabaseBuilder()
53+
.setConfig(GraphDatabaseSettings.procedure_unrestricted,"algo.*")
54+
.newGraphDatabase();
55+
56+
((GraphDatabaseAPI) db).getDependencyResolver()
57+
.resolveDependency(Procedures.class)
58+
.registerFunction(LinkPrediction.class);
59+
60+
db.execute(SETUP).close();
61+
}
62+
63+
@AfterClass
64+
public static void tearDown() {
65+
db.shutdown();
66+
}
67+
68+
@Test
69+
public void missingProperty() throws Exception {
70+
String controlQuery =
71+
"MATCH (p1:Person {name: 'Jennifer'})\n" +
72+
"MATCH (p2:Person {name: 'Mark'})\n" +
73+
"RETURN algo.linkprediction.sameCommunity(p1, p2) AS score, " +
74+
" 0.0 AS cypherScore";
75+
76+
try (Transaction tx = db.beginTx()) {
77+
Result result = db.execute(controlQuery);
78+
Map<String, Object> node = result.next();
79+
assertEquals((Double) node.get("cypherScore"), (double) node.get("score"), 0.01);
80+
}
81+
}
82+
83+
@Test
84+
public void sameCommunity() throws Exception {
85+
String controlQuery =
86+
"MATCH (p1:Person {name: 'Praveena'})\n" +
87+
"MATCH (p2:Person {name: 'Mark'})\n" +
88+
"RETURN algo.linkprediction.sameCommunity(p1, p2) AS score, " +
89+
" 1.0 AS cypherScore";
90+
91+
try (Transaction tx = db.beginTx()) {
92+
Result result = db.execute(controlQuery);
93+
Map<String, Object> node = result.next();
94+
assertEquals((Double) node.get("cypherScore"), (double) node.get("score"), 0.01);
95+
}
96+
}
97+
98+
@Test
99+
public void differentCommunity() throws Exception {
100+
String controlQuery =
101+
"MATCH (p1:Person {name: 'Michael'})\n" +
102+
"MATCH (p2:Person {name: 'Mark'})\n" +
103+
"RETURN algo.linkprediction.sameCommunity(p1, p2) AS score, " +
104+
" 0.0 AS cypherScore";
105+
106+
try (Transaction tx = db.beginTx()) {
107+
Result result = db.execute(controlQuery);
108+
Map<String, Object> node = result.next();
109+
assertEquals((Double) node.get("cypherScore"), (double) node.get("score"), 0.01);
110+
}
111+
}
112+
113+
@Test
114+
public void specifyProperty() throws Exception {
115+
String controlQuery =
116+
"MATCH (p1:Person {name: 'Michael'})\n" +
117+
"MATCH (p2:Person {name: 'Mark'})\n" +
118+
"RETURN algo.linkprediction.sameCommunity(p1, p2, 'partition') AS score, " +
119+
" 1.0 AS cypherScore";
120+
121+
try (Transaction tx = db.beginTx()) {
122+
Result result = db.execute(controlQuery);
123+
Map<String, Object> node = result.next();
124+
assertEquals((Double) node.get("cypherScore"), (double) node.get("score"), 0.01);
125+
}
126+
}
127+
128+
129+
130+
}

0 commit comments

Comments
 (0)