
org.neo4j.gds.undirected.ToUndirected Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of algo Show documentation
Show all versions of algo Show documentation
Neo4j Graph Data Science :: Algorithms
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.neo4j.gds.undirected;
import org.jetbrains.annotations.NotNull;
import org.neo4j.gds.Algorithm;
import org.neo4j.gds.Orientation;
import org.neo4j.gds.RelationshipType;
import org.neo4j.gds.api.CompositeRelationshipIterator;
import org.neo4j.gds.api.Graph;
import org.neo4j.gds.api.GraphStore;
import org.neo4j.gds.api.properties.relationships.RelationshipIterator;
import org.neo4j.gds.api.schema.PropertySchema;
import org.neo4j.gds.api.schema.RelationshipPropertySchema;
import org.neo4j.gds.core.Aggregation;
import org.neo4j.gds.core.concurrency.Concurrency;
import org.neo4j.gds.core.concurrency.RunWithConcurrency;
import org.neo4j.gds.core.loading.SingleTypeRelationships;
import org.neo4j.gds.core.loading.construction.GraphFactory;
import org.neo4j.gds.core.loading.construction.RelationshipsBuilder;
import org.neo4j.gds.core.loading.construction.RelationshipsBuilderBuilder;
import org.neo4j.gds.core.utils.partition.DegreePartition;
import org.neo4j.gds.core.utils.partition.PartitionUtils;
import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker;
import org.neo4j.gds.termination.TerminationFlag;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ToUndirected extends Algorithm {
private final GraphStore graphStore;
private final ToUndirectedConfig config;
private final ExecutorService executorService;
private final Concurrency concurrency;
public ToUndirected(
GraphStore graphStore,
ToUndirectedConfig config,
ProgressTracker progressTracker,
ExecutorService executorService,
TerminationFlag terminationFlag
) {
super(progressTracker);
this.graphStore = graphStore;
this.config = config;
this.executorService = executorService;
this.concurrency = config.concurrency();
this.terminationFlag = terminationFlag;
}
@Override
public SingleTypeRelationships compute() {
progressTracker.beginSubTask();
RelationshipType fromRelationshipType = config.internalRelationshipType();
var propertySchemas = graphStore
.schema()
.relationshipSchema()
.propertySchemasFor(fromRelationshipType);
var propertyKeys = propertySchemas.stream().map(PropertySchema::key).collect(Collectors.toList());
var relationshipsBuilder = initializeRelationshipsBuilder(
RelationshipType.of(config.mutateRelationshipType()),
propertySchemas
);
var tasks = createTasks(fromRelationshipType, propertyKeys, relationshipsBuilder);
progressTracker.beginSubTask();
RunWithConcurrency.
builder()
.tasks(tasks)
.concurrency(concurrency)
.executor(executorService)
.terminationFlag(terminationFlag)
.build()
.run();
progressTracker.endSubTask();
progressTracker.beginSubTask();
var relationships = relationshipsBuilder.build();
progressTracker.endSubTask();
progressTracker.endSubTask();
return relationships;
}
@NotNull
private RelationshipsBuilder initializeRelationshipsBuilder(
RelationshipType relationshipType,
List propertySchemas
) {
RelationshipsBuilderBuilder relationshipsBuilderBuilder = GraphFactory.initRelationshipsBuilder()
.relationshipType(relationshipType)
.concurrency(concurrency)
.nodes(graphStore.nodes())
.executorService(executorService)
.orientation(Orientation.UNDIRECTED);
config
.aggregation()
.map(ToUndirectedAggregations::globalAggregation)
.ifPresent(relationshipsBuilderBuilder::aggregation);
propertySchemas.forEach(propertySchema ->
{
Aggregation aggregation = config.aggregation()
.map(aggregations -> aggregations.localAggregation(propertySchema.key()))
.orElse(propertySchema.aggregation());
relationshipsBuilderBuilder.addPropertyConfig(
propertySchema.key(),
aggregation,
propertySchema.defaultValue(),
propertySchema.state()
);
}
);
return relationshipsBuilderBuilder.build();
}
@NotNull
private List createTasks(
RelationshipType fromRelationshipType,
List propertyKeys,
RelationshipsBuilder relationshipsBuilder
) {
Function taskCreator;
if (propertyKeys.size() == 1) {
Graph graph = graphStore.getGraph(fromRelationshipType, Optional.of(propertyKeys.get(0)));
taskCreator = partition -> new ToUndirectedTaskWithSingleProperty(
relationshipsBuilder,
graph.concurrentCopy(),
partition,
progressTracker
);
}
else {
CompositeRelationshipIterator relationshipIterator = graphStore.getCompositeRelationshipIterator(
fromRelationshipType,
propertyKeys
);
taskCreator = partition -> new ToUndirectedTaskWithMultipleProperties(
relationshipsBuilder,
relationshipIterator.concurrentCopy(),
partition,
progressTracker
);
}
return PartitionUtils.degreePartition(
graphStore.getGraph(fromRelationshipType),
concurrency,
taskCreator,
Optional.empty()
);
}
private static final class ToUndirectedTaskWithSingleProperty implements Runnable {
private final RelationshipsBuilder relationshipsBuilder;
private final RelationshipIterator relationshipIterator;
private final DegreePartition partition;
private final ProgressTracker progressTracker;
private ToUndirectedTaskWithSingleProperty(
RelationshipsBuilder relationshipsBuilder,
RelationshipIterator relationshipIterator,
DegreePartition partition,
ProgressTracker progressTracker
) {
this.relationshipsBuilder = relationshipsBuilder;
this.relationshipIterator = relationshipIterator;
this.partition = partition;
this.progressTracker = progressTracker;
}
@Override
public void run() {
for (long i = partition.startNode(); i < partition.startNode() + partition.nodeCount(); i++) {
relationshipIterator.forEachRelationship(i, 0.0D, (source, target, property) -> {
relationshipsBuilder.addFromInternal(target, source, property);
return true;
});
progressTracker.logProgress();
}
}
}
private static final class ToUndirectedTaskWithMultipleProperties implements Runnable {
private final RelationshipsBuilder relationshipsBuilder;
private final CompositeRelationshipIterator relationshipIterator;
private final DegreePartition partition;
private final ProgressTracker progressTracker;
private ToUndirectedTaskWithMultipleProperties(
RelationshipsBuilder relationshipsBuilder,
CompositeRelationshipIterator relationshipIterator,
DegreePartition partition,
ProgressTracker progressTracker
) {
this.relationshipsBuilder = relationshipsBuilder;
this.relationshipIterator = relationshipIterator;
this.partition = partition;
this.progressTracker = progressTracker;
}
@Override
public void run() {
for (long i = partition.startNode(); i < partition.startNode() + partition.nodeCount(); i++) {
relationshipIterator.forEachRelationship(i, (source, target, properties) -> {
relationshipsBuilder.addFromInternal(target, source, properties);
return true;
});
progressTracker.logProgress();
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy