Skip to content

Commit 50ec1d6

Browse files
Add triangle count to business facade
1 parent 67e7904 commit 50ec1d6

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

algorithms-compute-business-facade/src/main/java/org/neo4j/gds/community/CommunityComputeBusinessFacade.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.neo4j.gds.cliquecounting.CliqueCountingParameters;
3131
import org.neo4j.gds.collections.ha.HugeLongArray;
3232
import org.neo4j.gds.community.validation.ApproxMaxKCutValidation;
33+
import org.neo4j.gds.community.validation.TriangleCountGraphStoreValidation;
3334
import org.neo4j.gds.community.validation.UndirectedAndSeedableGraphStoreValidation;
3435
import org.neo4j.gds.conductance.ConductanceParameters;
3536
import org.neo4j.gds.conductance.ConductanceResult;
@@ -63,6 +64,8 @@
6364
import org.neo4j.gds.scc.SccParameters;
6465
import org.neo4j.gds.triangle.LocalClusteringCoefficientParameters;
6566
import org.neo4j.gds.triangle.LocalClusteringCoefficientResult;
67+
import org.neo4j.gds.triangle.TriangleCountParameters;
68+
import org.neo4j.gds.triangle.TriangleCountResult;
6669

6770
import java.util.List;
6871
import java.util.Optional;
@@ -508,4 +511,35 @@ public <TR> CompletableFuture<TR> scc(
508511
).thenApply(resultTransformerBuilder.build(graphResources));
509512
}
510513

514+
public <TR> CompletableFuture<TR> triangleCount(
515+
GraphName graphName,
516+
GraphParameters graphParameters,
517+
Optional<String> relationshipProperty,
518+
TriangleCountParameters parameters,
519+
JobId jobId,
520+
boolean logProgress,
521+
ResultTransformerBuilder<TimedAlgorithmResult<TriangleCountResult>, TR> resultTransformerBuilder
522+
) {
523+
// Fetch the Graph the algorithm will operate on
524+
var graphResources = graphStoreCatalogService.fetchGraphResources(
525+
graphName,
526+
graphParameters,
527+
relationshipProperty,
528+
TriangleCountGraphStoreValidation.create(parameters.labelFilter()),
529+
Optional.empty(),
530+
user,
531+
databaseId
532+
);
533+
var graph = graphResources.graph();
534+
535+
return computeFacade.triangleCount(
536+
graph,
537+
parameters,
538+
jobId,
539+
logProgress
540+
541+
).thenApply(resultTransformerBuilder.build(graphResources));
542+
}
543+
544+
511545
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.community.validation;
21+
22+
import org.neo4j.gds.NodeLabel;
23+
import org.neo4j.gds.RelationshipType;
24+
import org.neo4j.gds.api.GraphStore;
25+
import org.neo4j.gds.core.loading.validation.GraphStoreValidation;
26+
import org.neo4j.gds.core.loading.validation.UndirectedOnlyGraphStoreValidation;
27+
28+
import java.util.Collection;
29+
import java.util.List;
30+
31+
import static org.neo4j.gds.utils.StringFormatting.formatWithLocale;
32+
33+
public final class TriangleCountGraphStoreValidation extends GraphStoreValidation {
34+
35+
private final UndirectedOnlyGraphStoreValidation undirectedOnlyGraphStoreValidation;
36+
private final List<String> labelFilter;
37+
38+
public static TriangleCountGraphStoreValidation create(List<String> labelFilter){
39+
return new TriangleCountGraphStoreValidation(new UndirectedOnlyGraphStoreValidation("Triangle Counting"),labelFilter);
40+
}
41+
private TriangleCountGraphStoreValidation(
42+
UndirectedOnlyGraphStoreValidation undirectedOnlyGraphStoreValidation,
43+
List<String> labelFilter
44+
) {
45+
this.undirectedOnlyGraphStoreValidation = undirectedOnlyGraphStoreValidation;
46+
this.labelFilter = labelFilter;
47+
}
48+
49+
50+
@Override
51+
protected void validateAlgorithmRequirements(
52+
GraphStore graphStore,
53+
Collection<NodeLabel> selectedLabels,
54+
Collection<RelationshipType> selectedRelationshipTypes
55+
) {
56+
undirectedOnlyGraphStoreValidation.validateAlgorithmRequirements(graphStore,selectedLabels,selectedRelationshipTypes);
57+
validateLabelsExist(selectedLabels);
58+
}
59+
60+
61+
void validateLabelsExist(
62+
Collection<NodeLabel> nodeLabels
63+
) {
64+
for (String givenLabelString : labelFilter) {
65+
var givenLabel = NodeLabel.of(givenLabelString);
66+
if (!nodeLabels.contains(givenLabel)) {
67+
throw new IllegalArgumentException(formatWithLocale(
68+
"TriangleCount requires the provided 'labelFilter' node label '%s' to be present in the graph.",
69+
givenLabel.name()
70+
));
71+
}
72+
}
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.community.validation;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.neo4j.gds.NodeLabel;
24+
25+
import java.util.List;
26+
27+
import static org.assertj.core.api.Assertions.assertThatNoException;
28+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
29+
30+
class TriangleCountGraphStoreValidationTest {
31+
32+
@Test
33+
void shouldNotComplainForExistingLabels(){
34+
var validation = TriangleCountGraphStoreValidation.create(List.of("A"));
35+
assertThatNoException().isThrownBy(() -> validation.validateLabelsExist(List.of(NodeLabel.of("A"),NodeLabel.of("B"))));
36+
}
37+
38+
@Test
39+
void shouldComplainForMissingLabels(){
40+
var validation = TriangleCountGraphStoreValidation.create(List.of("C"));
41+
assertThatThrownBy(()-> validation.validateLabelsExist(List.of(NodeLabel.of("A"),NodeLabel.of("B"))))
42+
.hasMessageContaining("TriangleCount requires the provided 'labelFilter' node label 'C' to be present in the graph");
43+
}
44+
45+
}

0 commit comments

Comments
 (0)