Getting started
Install npm install graph-calc --save
Test npm test
Generate Docs npm run doc
Constructing the graph
Setup
A more complete setup can be found here.
var Edge = Edge;var Factory = Factory;var Graph = Graph;var Node = Node;var util = ; /* create node types */ var { thisid = thistype + '-' + optionsrawid; Node;};util;TeacherNodeprototypetype = 'teacher'; var { thisid = thistype + '-' + optionsrawid; Node;};util;ClassNodeprototypetype = 'class'; /* create an edge type */ var { thisid = thistype + '-' + optionsrawteacherid + '-' + optionsrawclassid; thisfromNode = optionsfactory; thistoNode = optionsfactory; Edge;};util;TeachesEdgeprototypefromNodeType = 'teacher';TeachesEdgeprototypetype = 'teaches';TeachesEdgeprototypetoNodeType = 'class';TeachesEdge { return TeachesEdgeprototypetype + '-' + rawteacherid + '-' + rawclassid }; /* create the graph, registering the nodes/edges via a Factory */ var schoolGraph = factory: nodeClasses: TeacherNode ClassNode edgeClasses: TeachesEdge ;;
Node / Edge creation
Create an edge, automatically creating the node if it doesn't exist (more examples):
schoolGraphfactory; schoolGraphfactory;
Traversal
Traverse the full graph (more examples):
var Traverser = Traverser; var startNode = graphfactory;var traverser = { var path = metapath; var depth = metapath; var viaEdge = metaviaEdge; var previouslyVisited = metapreviouslyVisited; ... } { ... } // Other options: // onlyNodeTypes: ['teacher', 'student', ...], // onlyEdgeTypes: ['teaches', ...], // excludeNodeTypes: ['teacher', ...], // excludeEdgeTypes: ['teaches', ...], // maxDepth: 3, // allowLoops: true, // shouldVisitNode: function (node) { return true; }, // shouldTraverseEdge: function (edge) { return true; },;traverser;
Traverse along a path of edge types (more examples):
var PathTraverser = PathTraverser; var traverser = path: 'teaches' /* -> CLASS */ 'attendedBy' /* -> STUDENT */ { ... };traverser;
Calculation
Perform relationship-based calculations, and queries of the graph aided by lodash (more examples):
var Calculator = Calculator; var classByDepartmentCalculator = acceptsNodeType: 'student' start ; var teacherStudentCalculator = acceptsNodeType: 'teacher' start ; var result = teacherStudentCalculator; // e.g result:// {// 'student-Bobby': {// 'department-Science': 2,// 'department-Art': 1// },// 'student-Jo': {// 'department-Science': 4// }// }
Events
Subscribe to events within a node's network (more examples):
var NetworkSubscriber = NetworkSubscriber; // Subscribe to all the 'grade-added' and 'report-due' (fictional) events for // all student's taught by a teacher. node: graphfactory // Define the network maxDepth: 3 onlyEdgeTypes: 'teaches' 'attendedBy' // Attach the listeners nodeEvents: { ... } { ... } ;