summaryrefslogtreecommitdiff
path: root/scripts/rhizicore.js
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 /scripts/rhizicore.js
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)
Diffstat (limited to 'scripts/rhizicore.js')
-rw-r--r--scripts/rhizicore.js56
1 files changed, 56 insertions, 0 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();