-
Notifications
You must be signed in to change notification settings - Fork 12
/
cities-example.js
76 lines (61 loc) · 2.66 KB
/
cities-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
const Fiber = require('fibers');
const Neo4jDB = require('neo4j-fiber').Neo4jDB;
Fiber(function () {
const db = new Neo4jDB('http://localhost:7474', {
username: 'neo4j',
password: '1234'
});
// Create some data:
const cities = {};
cities['Zürich'] = db.nodes({
title: 'Zürich',
lat: 47.27,
long: 8.31
}).label(['City']);
cities['Tokyo'] = db.nodes({
title: 'Tokyo',
lat: 35.40,
long: 139.45
}).label(['City']);
cities['Athens'] = db.nodes({
title: 'Athens',
lat: 37.58,
long: 23.43
}).label(['City']);
cities['Cape Town'] = db.nodes({
title: 'Cape Town',
lat: 33.55,
long: 18.22
}).label(['City']);
// Add relationship between cities
// At this example we set distance
cities['Zürich'].to(cities['Tokyo'], "DISTANCE", {m: 9576670, km: 9576.67, mi: 5950.67});
cities['Tokyo'].to(cities['Zürich'], "DISTANCE", {m: 9576670, km: 9576.67, mi: 5950.67});
// Create route 1 (Zürich -> Athens -> Cape Town -> Tokyo)
cities['Zürich'].to(cities['Athens'], "ROUTE", {m: 1617270, km: 1617.27, mi: 1004.93, price: 50});
cities['Athens'].to(cities['Cape Town'], "ROUTE", {m: 8015080, km: 8015.08, mi: 4980.34, price: 500});
cities['Cape Town'].to(cities['Tokyo'], "ROUTE", {m: 9505550, km: 9505.55, mi: 5906.48, price: 850});
// Create route 2 (Zürich -> Cape Town -> Tokyo)
cities['Zürich'].to(cities['Cape Town'], "ROUTE", {m: 1617270, km: 1617.27, mi: 1004.93, price: 550});
cities['Cape Town'].to(cities['Tokyo'], "ROUTE", {m: 9576670, km: 9576.67, mi: 5950.67, price: 850});
// Create route 3 (Zürich -> Athens -> Tokyo)
cities['Zürich'].to(cities['Athens'], "ROUTE", {m: 1617270, km: 1617.27, mi: 1004.93, price: 50});
cities['Athens'].to(cities['Tokyo'], "ROUTE", {m: 9576670, km: 9576.67, mi: 5950.67, price: 850});
// Get Shortest Route (in km) between two Cities:
const shortest = cities['Zürich'].path(cities['Tokyo'], "ROUTE", {cost_property: 'km', algorithm: 'dijkstra'})[0];
let shortestStr = 'Shortest from Zürich to Tokyo, via: ';
shortest.nodes.forEach((id) => {
shortestStr += db.nodes(id).property('title') + ', ';
});
shortestStr += '| Distance: ' + shortest.weight + ' km';
console.info(shortestStr);
// Get Cheapest Route (in notional currency) between two Cities:
const cheapest = cities['Zürich'].path(cities['Tokyo'], "ROUTE", {cost_property: 'price', algorithm: 'dijkstra'})[0];
let cheapestStr = 'Cheapest from Zürich to Tokyo, via: ';
cheapest.nodes.forEach((id) => {
cheapestStr += db.nodes(id).property('title') + ', ';
});
cheapestStr += '| Price: ' + cheapest.weight + ' nc';
console.info(cheapestStr);
}).run();