summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Levy <alon@pobox.com>2014-10-07 08:50:56 +0300
committerAlon Levy <alon@pobox.com>2014-10-07 08:51:43 +0300
commitfc7ecac8c22e79f669b2e4ff28f32641102ebfd3 (patch)
tree2dbb028eea72e5a7e50b56b59f41808f8c02bed4
parent7a0e1027dd7d8c0cbf60f406a64b24c0a9f284b8 (diff)
rhizicore: don't move graph when only changing link or node text
Fixes #49 Note about solution: in general we need to find out whether the graph from the new text (NewVertices, NewLinks) is homologous to the graph from the old text (OldVertices, OldLinks). The implemented solution only checks an easier case, where there is a single renamed vertex. A better solution to do later: - have textanalysis have state - have textanalysis return the difference from the last state: nodes added nodes deleted nodes changed - plus for bonus refactor all the editing functions (edit* in myGraph)
-rw-r--r--scripts/rhizicore.js56
-rw-r--r--scripts/textanalysis.js40
2 files changed, 85 insertions, 11 deletions
diff --git a/scripts/rhizicore.js b/scripts/rhizicore.js
index 0b7253a0..8ba19bc2 100644
--- a/scripts/rhizicore.js
+++ b/scripts/rhizicore.js
@@ -160,6 +160,62 @@ function myGraph(el) {
}
}
+ /* compareSubset:
+ * state: one of the optional states that defines a subgraph
+ * new_nodes: array of objects with id
+ * new_links: array of length two arrays [source_id, target_id]
+ * returns: true if current and new graph are homomorphic up to
+ * a single node id change. false otherwise
+ */
+ this.compareSubset = function(state, new_nodes, new_links) {
+ // Note: the nodes include a state=='temp', type=='bubble' node
+ // but it's ok since it exists both in new_nodes and in state_nodes
+ var state_nodes = findNodes(null, state).sort();
+ var state_links = findLinks(state).map(function(link) {
+ return [link.source.id, link.target.id];
+ }).sort();
+ var k;
+ var changed_old_id = undefined, changed_new_id = undefined;
+ var state_source, state_target, new_source, new_target;
+
+ new_nodes.sort();
+ new_links.sort();
+ if (new_nodes.length != state_nodes.length || new_links.length != state_links.length) {
+ return {graph_same: false};
+ }
+ for (k in state_nodes) {
+ if (new_nodes[k] != state_nodes[k].id) {
+ if (changed_old_id === undefined) {
+ // found the changed node
+ changed_old_id = state_nodes[k].id;
+ changed_new_id = new_nodes[k];
+ } else {
+ return {graph_same: false};
+ }
+ }
+ }
+ for (k in state_links) {
+ state_source = state_links[k][0];
+ state_target = state_links[k][1];
+ new_source = new_links[k][0];
+ new_target = new_links[k][1];
+ if (state_source != new_source ||
+ state_target != new_target) {
+ if ((state_source == changed_old_id &&
+ new_source == changed_new_id &&
+ state_target == new_target) ||
+ (state_target == changed_old_id &&
+ new_target == changed_new_id &&
+ state_source == new_source)) {
+ // this one is ok
+ } else {
+ return {graph_same: false};
+ }
+ }
+ }
+ return {graph_same: true, old_id: changed_old_id, new_id: changed_new_id};
+ }
+
this.addLink = function(sourceId, targetId, name, state, drop_conjugator_links) {
sourceId = sourceId && sourceId.toLowerCase();
targetId = targetId && targetId.toLowerCase();
diff --git a/scripts/textanalysis.js b/scripts/textanalysis.js
index a93839aa..74b029ae 100644
--- a/scripts/textanalysis.js
+++ b/scripts/textanalysis.js
@@ -314,19 +314,37 @@ var textAnalyser2 = function (newtext, finalize) {
lastnode = newnodes[nodeindex];
ret.applyToGraph = function(graph) {
- //REINITIALISE GRAPH (DUMB BUT IT WORKS)
- graph.removeNodes("temp");
- graph.removeLinks("temp");
- for (var k in ret.nodes) {
- var n = ret.nodes[k];
- graph.addNode(n.id, n.type, n.state);
- }
- for (var k in ret.links) {
- var l = ret.links[k];
- graph.addLink(l.sourceId, l.targetId, l.name, l.state, ret.drop_conjugator_links);
+ window.ret = ret;
+ var comp = graph.compareSubset('temp', ret.nodes.filter(
+ function(node) {
+ return !graph.hasNode(node.id, "perm");
+ }).map(function (node) {
+ return node.id;
+ }), ret.links.map(
+ function (link) {
+ return [link.sourceId.toLowerCase(), link.targetId.toLowerCase()];
+ }
+ ));
+ var k, n;
+ if (comp.graph_same && !finalize) {
+ if (comp.old_id && comp.new_id) {
+ graph.editName(comp.old_id, null, comp.new_id);
+ }
+ } else {
+ //REINITIALISE GRAPH (DUMB BUT IT WORKS)
+ graph.removeNodes("temp");
+ graph.removeLinks("temp");
+ for (k in ret.nodes) {
+ n = ret.nodes[k];
+ graph.addNode(n.id, n.type, n.state);
+ }
+ for (k in ret.links) {
+ var l = ret.links[k];
+ graph.addLink(l.sourceId, l.targetId, l.name, l.state, ret.drop_conjugator_links);
+ }
}
//UPDATE GRAPH ONCE
- graph.update();
+ graph.update(!finalize && comp.graph_same);
}
return ret;