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

com.graphaware.module.algo.path.JsonPath Maven / Gradle / Ivy

Go to download

GraphAware Framework Module exposing custom graph algorithms as Java and REST APIs

There is a newer version: 3.0.4.43.5
Show newest version
/*
 * Copyright (c) 2013-2016 GraphAware
 *
 * This file is part of the GraphAware Framework.
 *
 * GraphAware Framework 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 com.graphaware.module.algo.path;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.graphaware.api.json.JsonNode;
import com.graphaware.api.json.JsonRelationship;
import com.graphaware.api.json.LongIdJsonNode;
import com.graphaware.api.json.LongIdJsonRelationship;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;

import java.util.LinkedList;
import java.util.List;

/**
 * JSON-serializable representation of a Neo4j path.
 */
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class JsonPath {

    private JsonNode[] nodes;
    private JsonRelationship[] relationships;
    private Long cost;

    public JsonPath(Path path, JsonPathFinderInput jsonInput) {
        List jsonNodes = new LinkedList<>();
        for (Node node : path.nodes()) {
            jsonNodes.add(new LongIdJsonNode(node, jsonInput.getNodeProperties()));
        }

        List jsonRelationships = new LinkedList<>();
        for (Relationship relationship : path.relationships()) {
            jsonRelationships.add(new LongIdJsonRelationship(relationship, jsonInput.getRelationshipProperties()));
        }

        setNodes(jsonNodes.toArray(new JsonNode[jsonNodes.size()]));
        setRelationships(jsonRelationships.toArray(new JsonRelationship[jsonRelationships.size()]));

        if (path instanceof WeightedPath) {
            setCost(((WeightedPath) path).getCost());
        }
    }

    public JsonNode[] getNodes() {
        return nodes;
    }

    public void setNodes(JsonNode[] nodes) {
        this.nodes = nodes;
    }

    public JsonRelationship[] getRelationships() {
        return relationships;
    }

    public void setRelationships(JsonRelationship[] relationships) {
        this.relationships = relationships;
    }

    public Long getCost() {
        return cost;
    }

    public void setCost(Long cost) {
        this.cost = cost;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy