All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neo4j.gds.GraphProjectConfigBuilders Maven / Gradle / Ivy

There is a newer version: 2.11.0
Show newest version
/*
 * 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;

import org.immutables.builder.Builder;
import org.immutables.value.Value;
import org.neo4j.gds.config.ConcurrencyConfig;
import org.neo4j.gds.core.Aggregation;
import org.neo4j.gds.core.concurrency.Concurrency;
import org.neo4j.gds.core.utils.progress.JobId;
import org.neo4j.gds.legacycypherprojection.GraphProjectFromCypherConfig;
import org.neo4j.gds.legacycypherprojection.GraphProjectFromCypherConfigImpl;
import org.neo4j.gds.projection.GraphProjectFromStoreConfig;
import org.neo4j.gds.projection.GraphProjectFromStoreConfigImpl;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.neo4j.gds.NodeLabel.ALL_NODES;
import static org.neo4j.gds.RelationshipType.ALL_RELATIONSHIPS;
import static org.neo4j.gds.legacycypherprojection.GraphProjectFromCypherConfig.ALL_NODES_QUERY;
import static org.neo4j.gds.legacycypherprojection.GraphProjectFromCypherConfig.ALL_RELATIONSHIPS_QUERY;

@Value.Style(builderVisibility = Value.Style.BuilderVisibility.PUBLIC, depluralize = true, deepImmutablesDetection = true)
public final class GraphProjectConfigBuilders {

    private GraphProjectConfigBuilders() {}

    /**
     * Factory method that defines the generation of {@link StoreConfigBuilder}.
     */
    @Builder.Factory
    public static GraphProjectFromStoreConfig storeConfig(
        Optional userName,
        Optional graphName,
        List nodeLabels,
        List relationshipTypes,
        List nodeProjections,
        List relationshipProjections,
        Map nodeProjectionsWithIdentifier,
        Map relationshipProjectionsWithIdentifier,
        List nodeProperties,
        List relationshipProperties,
        Optional concurrency,
        Optional jobId,
        Optional globalProjection,
        Optional globalAggregation,
        Optional globalIndexInverse,
        Optional validateRelationships
    ) {
        // Node projections
        Map tempNP = new LinkedHashMap<>();
        nodeLabels.forEach(label -> tempNP.put(label, NodeProjection.of(label)));
        nodeProjections.forEach(np -> tempNP.put(np.label(), np));
        nodeProjectionsWithIdentifier.forEach(tempNP::put);

        if (tempNP.isEmpty()) {
            tempNP.put(ALL_NODES.name, NodeProjection.all());
        }

        // Relationship projections
        Map tempRP = new LinkedHashMap<>();
        Orientation orientation = globalProjection.orElse(Orientation.NATURAL);
        Aggregation aggregation = globalAggregation.orElse(Aggregation.DEFAULT);
        boolean indexInverse = globalIndexInverse.orElse(false);

        relationshipTypes.forEach(relType -> tempRP.put(
            relType,
            RelationshipProjection
                .builder()
                .type(relType)
                .orientation(orientation)
                .aggregation(aggregation)
                .indexInverse(indexInverse)
                .build()
        ));
        relationshipProjections.forEach(rp -> tempRP.put(rp.type(), rp));
        relationshipProjectionsWithIdentifier.forEach(tempRP::put);

        if (tempRP.isEmpty()) {
            tempRP.put(ALL_RELATIONSHIPS.name, RelationshipProjection.builder()
                .type("*")
                .orientation(orientation)
                .aggregation(aggregation)
                .indexInverse(indexInverse)
                .build());
        }

        PropertyMappings relationshipPropertyMappings = PropertyMappings.builder()
            .addAllMappings(relationshipProperties)
            .withDefaultAggregation(aggregation)
            .build();

        NodeProjections np = ImmutableNodeProjections.of(tempNP.entrySet().stream().collect(Collectors.toMap(
            e -> NodeLabel.of(e.getKey()),
            Map.Entry::getValue
        )));

        RelationshipProjections rp = ImmutableRelationshipProjections.of(tempRP.entrySet().stream().collect(Collectors.toMap(
            e -> RelationshipType.of(e.getKey()),
            Map.Entry::getValue
        )));

        return GraphProjectFromStoreConfigImpl.builder()
            .username(userName.orElse(""))
            .graphName(graphName.orElse(""))
            .nodeProjections(np)
            .relationshipProjections(rp)
            .nodeProperties(ImmutablePropertyMappings.of(nodeProperties))
            .relationshipProperties(relationshipPropertyMappings)
            .readConcurrency(concurrency.orElse(ConcurrencyConfig.TYPED_DEFAULT_CONCURRENCY))
            // TODO: should be able to just not set readConcurrency
            .jobId(jobId.orElse(new JobId()))
            .validateRelationships(validateRelationships.orElse(false))
            .build()
            .withNormalizedPropertyMappings();
    }

    /**
     * Factory method that defines the generation of {@link CypherConfigBuilder}.
     */
    @Builder.Factory
    public static GraphProjectFromCypherConfig cypherConfig(
        Optional userName,
        Optional graphName,
        Optional nodeQuery,
        Optional relationshipQuery,
        Optional concurrency,
        Optional jobId,
        Optional validateRelationships,
        Optional> parameters
    ) {

        return GraphProjectFromCypherConfigImpl.builder()
            .username(userName.orElse(""))
            .graphName(graphName.orElse(""))
            .nodeQuery(nodeQuery.orElse(ALL_NODES_QUERY))
            .relationshipQuery(relationshipQuery.orElse(ALL_RELATIONSHIPS_QUERY))
            .readConcurrency(concurrency.orElse(ConcurrencyConfig.TYPED_DEFAULT_CONCURRENCY))
            .validateRelationships(validateRelationships.orElse(true))
            .parameters(parameters.orElse(Collections.emptyMap()))
            .jobId(jobId.orElse(new JobId()))
            .build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy