This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let joe = {type: 'node', properties: {name: 'joe'}, input: [], output: []}; | |
let likes = {type: 'edge', properties: {name: 'likes'}, input: null, output: null}; | |
let minecraft = {type: 'node', properties: {name: 'minecraft'}, input: [], output: []}; | |
joe.output.push(likes); | |
likes.input = joe; | |
likes.output = minecraft; | |
minecraft.input.push(likes); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Unit { | |
// Entity is used as node or edge type, for different classifications | |
// i.e. 'person', 'game', 'road', etc. | |
constructor(entity, properties) { | |
this.entity = entity + ''; | |
this.load(properties || {}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node extends Unit { | |
constructor(entity, properties) { | |
super(entity, properties); | |
this.edges = []; | |
this.inputEdges = []; | |
this.outputEdges = []; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Edge extends Unit { | |
constructor(entity, properties) { | |
super(entity, properties); | |
this.inputNode = null; | |
this.outputNode = null; | |
this.duplex = false; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create nodes... | |
let joe = new Node('person'); | |
joe.set('name', 'Joe'); | |
let minecraft = new Node('game'); | |
minecraft.set('name', 'Minecraft'); | |
// Create edge... | |
let likes = new Edge('likes'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add even more nodes | |
let mojang = new Node('company', {name: 'Mojang'}); | |
let microsoft = new Node('company', {name: 'Microsoft'}); | |
let jennifer = new Node('person', {name: 'Jennifer'}); | |
new Edge('founded').link(notch, mojang); | |
new Edge('acquired').link(microsoft, mojang); | |
new Edge('purchased').link(jennifer, minecraft); | |
new Edge('prints_money_for').link(minecraft, microsoft); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let users = getUsers(); // abstract function to get user data (i.e. SQL) | |
let listings = getListings(); // ... listings | |
let views = getViews(); // ... etc. | |
let favorites = getFavorites(); | |
let requests = getRequests(); | |
// quick and dirty O(n) function to get a node by id | |
function getNodeById(nodes, id) { | |
return nodes.filter(function(node) { | |
return node.get('id') === id; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
views.forEach(function(view) { | |
view.setDistance(4); | |
}); | |
favorites.forEach(function(favorite) { | |
favorite.setDistance(2); | |
}); | |
requests.forEach(function(request) { | |
request.setDistance(1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let ug = require('ug'); | |
let graph = new ug.Graph(); | |
// load graph from flatfile into RAM | |
graph.load('/path_to_saved_graph.ugd', function() { | |
// get the closest 100 'listings' nodes, at a minimum depth (distance) of 3 | |
let results = graph.closest(node, { | |
compare: function(node) { return node.entity === 'listing'; }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let ug = require('ug'); | |
let graph = new ug.Graph(); | |
// fetch data | |
let users = getUsers(); // abstract function to get user data (i.e. SQL) | |
let listings = getListings(); // ... listings | |
let views = getViews(); // ... etc. | |
let favorites = getFavorites(); |
OlderNewer