Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linking nodes in the graph doesn't create links in the nodes #42

Open
arsaccol opened this issue Mar 21, 2022 · 0 comments
Open

Linking nodes in the graph doesn't create links in the nodes #42

arsaccol opened this issue Mar 21, 2022 · 0 comments

Comments

@arsaccol
Copy link

Hello! I am using ngraph.graph version ^20.0.0 as specified in my package.json.

I am creating a graph from a file planet-route-data.json like so:

import createGraph from 'ngraph.graph'

function makeTravelGraph () {
    const graph = createGraph()
    const travelGraphData = JSON.parse(fs.readFileSync('./src/planet-route-data.json'))

    // first, we create nodes
    travelGraphData.forEach(sourcePlanet => {
        graph.addNode(sourcePlanet.name)
    })

    // then, we link them
    travelGraphData.forEach(sourcePlanet => {
        sourcePlanet.destinations.forEach(destination => {
            graph.addLink(sourcePlanet.name, destination.name, { fuelCost: destination.cost })
        })
    })

   return graph
}

My planet-route-data.json file looks like this (please ignore the values themselves, haha):

[
    {
        "name": "Earth",
        "destinations": [
            {
                "name": "Mars",
                "cost": 13
            },
            {
                "name": "Venus",
                "cost": 23 
            }
        ]
    },
    {
        "name": "Mars",
        "destinations": [
            {
                "name": "Jupiter",
                "cost": 22
            },
            {
                "name": "Saturn",
                "cost": 25 
            }
        ]
    },
    {
        "name": "Saturn",
        "destinations": [
            {
                "name": "Jupiter",
                "cost": 30 
            },
            {
                "name": "Earth",
                "cost": 12 
            }
        ]
    },

    {
        "name": "Venus",
        "destinations": [
            {
                "name": "Earth",
                "cost": 20 
            },
            {
                "name": "Mars",
                "cost": 25
            },
            {
                "name": "Mercury",
                "cost": 15
            }
        ]
    }
]

The nodes in the graph are being created, and links seem to be created successfully -- that is, I can list them as expected with something like:

returnedGraph.forEachLink(link => {
    console.log(link)
}

However, the nodes themselves, which contain a property named links, do not seem to be updated. If I try to print them, their links property looks like an empty object: {}. If I do:

returnedGraph.forEachLinkedNode(node => {
    console.log(node)
}

nothing is iterated upon, presumably as there are no nodes with links to be found.

Is this, by any chance, by design, or is it not intended behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant