From 67c21494ed5371c3a7e6cd9811b83401d4329362 Mon Sep 17 00:00:00 2001 From: Joshua Shinavier Date: Sat, 16 Apr 2011 11:09:10 +0800 Subject: [PATCH] Here's the revision of Jig which was used in the screencast. --- .../com/tinkerpop/jig/JigScriptEngine.java | 3 + src/main/javascript/jig-play.js | 8 +- src/main/resources/com/tinkerpop/jig/jig.js | 266 +++++++++++++++++- 3 files changed, 265 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/tinkerpop/jig/JigScriptEngine.java b/src/main/java/com/tinkerpop/jig/JigScriptEngine.java index c849b00..4d61a41 100644 --- a/src/main/java/com/tinkerpop/jig/JigScriptEngine.java +++ b/src/main/java/com/tinkerpop/jig/JigScriptEngine.java @@ -35,6 +35,7 @@ public class JigScriptEngine implements ScriptEngine { private static final String[] steps = new String[]{ "bothE", + "common", "distinct", "e", "ends", @@ -42,11 +43,13 @@ public class JigScriptEngine implements ScriptEngine { "inE", "label", "limit", + "nearby", "outE", "tail", "triples", "v"}; private static final String[] methods = new String[]{ + "aggr", "count", "eval", "mean", diff --git a/src/main/javascript/jig-play.js b/src/main/javascript/jig-play.js index 74bc741..62a75ad 100644 --- a/src/main/javascript/jig-play.js +++ b/src/main/javascript/jig-play.js @@ -45,9 +45,15 @@ t.breadthFirstPaths(gene1, target1, 2) +Jig.undirectedUnlabeledGenerator.neighbors(d[0], 1) -t = new Generator(store, neighbors); +function intersect(v1, v2) { + +} + + +t = new Generator(store, neighborNew); t.neighbors(r, 2); t.breadthFirstPath(drug1, drug2, 2); t.breadthFirstPaths(drug1, drug2, 2); diff --git a/src/main/resources/com/tinkerpop/jig/jig.js b/src/main/resources/com/tinkerpop/jig/jig.js index b6d1f4b..81e6e3c 100644 --- a/src/main/resources/com/tinkerpop/jig/jig.js +++ b/src/main/resources/com/tinkerpop/jig/jig.js @@ -17,6 +17,94 @@ function pushCursor(cursor, solutions) { return true; } +function neighbors(r) { + var hash = new UPIHash(); + var val = {}; + + var c = store.getTriples(r, null, null, null); + var t = c.next(); + while (null != t) { + hash.set(t.object, val); + t = c.next(); + } + c.close(); + + c = store.getTriples(null, null, r, null); + t = c.next(); + while (null != t) { + hash.set(t.subject, val); + t = c.next(); + } + c.close(); + + return hash.keys(); +} + +function compareWeighted(v1, v2) { + return v1[0] - v2[0]; +} + +// Please forgive me for using bubble sort. It's just so easy. +function sortVector(array) { + var x, y, tmp; + // The Bubble Sort method. + for(x = 0; x < array.length; x++) { + for(y = 0; y < (array.length-1); y++) { + if(compareWeighted(array[y], array[y+1]) > 0) { + tmp = array[y+1]; + array[y+1] = array[y]; + array[y] = tmp; + } + } + } +} + +function toHash(weightArray) { + var hash = new UPIHash(); + for (var i = 0; i < weightArray.length; i++) { + var b = weightArray[i]; + hash.set(b[1], b[0]); + } + return hash; +} + +function toArray(hash) { + +} + +function union(v1, v2) { + +} + +function intersect(v1, v2) { + var hash1 = toHash(v1); + var result = new Array(); + var k = 0; + + for (var i = 0; i < v2.length; i++) { + var b = v2[i]; + var w2 = b[0]; + var v = b[1]; + if (0 < w2) { + var w1 = hash1.get(v); + if (null != w1 || 0 < w1) { + var w = w1 * w2; + // Dunno why this extra check is necessary, but it seems to be. + if (w > 0) { + var c = new Array(); + c[0] = w1 * w2; + c[1] = v; + result[k] = c; + k++; + } + } + } + } + + sortVector(result); + return result; +} + Jig.toNumber = function(r) { // TODO return 0; @@ -31,6 +119,11 @@ Jig.compose = function(up, down) { } } +/* global objects *************************************************************/ + +Jig.undirectedUnlabeledGenerator = new Generator(store, neighbors); + + /* filters ********************************************************************/ Jig.LimitFilter = function(limit) { @@ -54,6 +147,76 @@ Jig.LimitFilter = function(limit) { } } +Jig.NearbyFilter = function(steps) { + return { + id: "nearby", + apply: function(solutions) { + return { + put: function(arg) { + var a = Jig.undirectedUnlabeledGenerator.neighbors(arg, steps); + for (var i = 0; i < a.length; i++) { + if (!solutions.put(a[i])) { + return false; + } + } + return true; + } + } + } + } +} + +Jig.NeighborsFilter = function() { + return { + id: "neighbors", + apply: function(solutions) { + return { + put: function(arg) { + var c = store.getTriples(arg, null, null, null); + var t = c.next(); + while (null != t) { + if (!solutions.put(t.object)) { + c.close(); + return false; + } + t = c.next(); + } + c.close(); + +/* + c = store.getTriples(null, null, arg, null); + t = c.next(); + while (null != t) { + if (!solutions.put(t.subject)) { + c.close(); + return false; + } + t = c.next(); + } + c.close(); +*/ + + return true; + } + } + } + } +} + +Jig.OptionFilter = function(other) { + return { + id: "option", + apply: function(solutions) { + var p = other.apply(solutions); + return { + put: function(arg) { + return solutions.put(arg) && p.put(arg); + } + } + } + } +} + Jig.HeadFilter = function() { return { id: "head", @@ -110,6 +273,34 @@ Jig.TriplesFilter = function(subject, predicate, object, context) { } } +Jig.CommonFilter = function(v1, v2, depth) { + if (null == depth) { + depth = 2; + } + return { + id: "common", + apply: function(solutions) { + return { + put: function(arg) { + var a = Jig.undirectedUnlabeledGenerator.breadthFirstPaths(v1, v2, depth); + for (var i = 0; i < a.length; i++) { + var p = a[i]; + var r = new Array(); + for (var j = 0; j < p.length - 2; j++) { + r[j] = p[j + 1]; + } + if (!solutions.put(r)) { + return false; + } + } + + return true; + } + } + } + } +} + Jig.OutEdgesFilter = function(predicate) { return { id: "outEdges", @@ -214,11 +405,53 @@ Jig.CountPipe = function() { } } +Jig.AggregatePipe = function(min) { + if (null == min) { + min = -1; + } + var hash = new UPIHash(); + return { + put: function(arg) { + var val = hash.get(arg); + + if (null == val) { + val = 0; + } + val += 1; + + hash.set(arg, val); + return true; + }, + + getVector: function() { + var a = new Array(); + var keys = hash.keys(); + var k = 0; + for (var i = 0; i < keys.length; i++) { + var key = keys[i]; + var val = hash.get(key); + if (min < 0 || val >= min) { + var b = new Array(); + b[0] = val; + b[1] = key; + a[k] = b; + k++; + } + } + + //return a.sort(compareWeighted); + sortVector(a); + return a; + } + } +} + Jig.SumPipe = function() { var sum = 0; return { put: function(arg) { sum += Jig.toNumber(arg); + return true; }, getSum: function() { @@ -287,6 +520,10 @@ Jig.Generator = function(filter) { return extend(new Jig.HeadFilter()); }, + common: function(v1, v2, depth) { + return extend(new Jig.CommonFilter(v1, v2, depth)); + }, + label: function() { return extend(new Jig.LabelFilter()); }, @@ -295,6 +532,17 @@ Jig.Generator = function(filter) { return extend(new Jig.LimitFilter(lim)); }, + nearby: function(steps) { + var f = filter; + var o = new Jig.OptionFilter(new Jig.NeighborsFilter()); + for (var i = 0; i < steps; i++) { + f = Jig.compose(f, o); + } + + return new Jig.Generator(f); + //return extend(new Jig.NearbyFilter(steps)); + }, + bothE: function(predicate) { return extend(new Jig.TeeFilter( new Jig.InEdgesFilter(predicate), @@ -319,6 +567,12 @@ Jig.Generator = function(filter) { //////////////////////////////// + aggr: function(limit) { + var c = new Jig.AggregatePipe(limit); + filter.apply(c).put(null); + return c.getVector(); + }, + count: function() { var c = new Jig.CountPipe(); filter.apply(c).put(null); @@ -363,14 +617,4 @@ Jig.Graph = function() { return new Jig.Generator(new Jig.TrivialFilter()); } -function neighbors(r) { - var p = new Jig.CollectorPipe(); - var d = new Jig.DistinctFilter().apply(p); - - new Jig.Graph().triples(r, null, null, null).head()._getFilter().apply(d).put(null); - new Jig.Graph().triples(null, null, r, null).tail()._getFilter().apply(d).put(null); -// new Jig.TriplesFilter(r, null, null, null).apply(d).put(null); -// new Jig.TriplesFilter(null, null, r, null).apply(d).put(null); - - return p.getArray(); -} \ No newline at end of file +g = new Jig.Graph();