Skip to content

Commit

Permalink
Here's the revision of Jig which was used in the screencast.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshsh committed Apr 16, 2011
1 parent 20a5756 commit 67c2149
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/tinkerpop/jig/JigScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,21 @@
public class JigScriptEngine implements ScriptEngine {
private static final String[] steps = new String[]{
"bothE",
"common",
"distinct",
"e",
"ends",
"head",
"inE",
"label",
"limit",
"nearby",
"outE",
"tail",
"triples",
"v"};
private static final String[] methods = new String[]{
"aggr",
"count",
"eval",
"mean",
Expand Down
8 changes: 7 additions & 1 deletion src/main/javascript/jig-play.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
266 changes: 255 additions & 11 deletions src/main/resources/com/tinkerpop/jig/jig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +119,11 @@ Jig.compose = function(up, down) {
}
}

/* global objects *************************************************************/

Jig.undirectedUnlabeledGenerator = new Generator(store, neighbors);


/* filters ********************************************************************/

Jig.LimitFilter = function(limit) {
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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());
},
Expand All @@ -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),
Expand All @@ -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);
Expand Down Expand Up @@ -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();
}
g = new Jig.Graph();

0 comments on commit 67c2149

Please sign in to comment.