<commit_before>/*
    This file is part of rhizi, a collaborative knowledge graph editor.
    Copyright (C) 2014-2015  Rhizi

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

define(['consts', 'jquery', 'd3', 'underscore'],
function(consts,   $,        d3,   _) {

"use strict";

    function object__append(d, k, v) {
        if (d[k] === undefined) {
            d[k] = [];
        }
        d[k].push(v);
    }

    function calc_node_links(nodes, links) {
        var node_links = {};

        _.each(links, function (l) {
            var src_id = l.__src.id,
                dst_id = l.__dst.id;

            object__append(node_links, src_id, l);
            object__append(node_links, dst_id, l);
        });
        return node_links;
    }

    function stp_degree_max(nodes, links) {
        var ret_links = [],
            node_id_degree_d = {}, // sum of entering and exising degrees
            node_id_degree,
            node_by_id = _.object(_.map(nodes, "id"), nodes),
            node_links = calc_node_links(nodes, links);

        if (nodes.length === 0) {
            return [];
        }

        function inc(d, k) {
            d[k] = d[k] === undefined ? 1 : d[k] + 1;
        }

        _.each(links, function (l) {
            var src_id = l.__src.id,
                dst_id = l.__dst.id;

            inc(node_id_degree_d, src_id);
            inc(node_id_degree_d, dst_id);
        });
        node_id_degree = _.pairs(node_id_degree).map(function (ar) { return [ar[1], ar[0]]; }).sort().reverse();

        /*
        _.each(node_id_degree.slice(0, node_id_degree.length - 1), function (_degree, node_id) {
            var d = node_links[node_id];
            // pick an edge
            if (d.src.length > 0) {
            }
            // remove all incoming edges
        });
        */
        return ret_links;
    }

    function bfs(nodes, links) {
        var ret_links = [],
            queue = [nodes[0].id],
            node_id,
            i,
            link,
            other,
            visited = {},
            node_links = calc_node_links(nodes, links);

        while (queue.length > 0) {
            node_id = queue.pop();
            for (i = 0 ; i < node_links[node_id].length; ++i) {
                link = node_links[node_id][i];
                other = link.__src.id === node_id ? link.__dst.id : link.__src.id;
                if (!visited[other]) {
                    queue.push(other);
                    ret_links.push(link);
                    visited[other] = true;
                }
            }
            if (queue.length === 0 && visited.length < nodes.length) {
                queue.push(_.difference(_.pick(nodes, 'id'), visited)[0]);
            }
        }
        return ret_links;
    }

    function extendOwn(obj, other) {
        for (var prop in other) {
            if (!obj.hasOwnProperty(prop) && other.hasOwnProperty(prop)) {
                obj[prop] = other[prop];
            }
        }
        return obj;
    }

    function layout__d3_force(graph) {
        return layout__empty(graph, d3.layout.force())
                  .distance(240)
                  .gravity(0.12)
                  .charge(-1800);
    }

    function layout__d3_force__link_distance(graph) {
        var ret = layout__d3_force(graph);
        ret.zen_mode_inner = function (zen_mode) {
            if (zen_mode) {
                ret.distance(240);
            } else {
                ret.linkDistance(function (link) {
                    var d_src = graph.degree(link.__src),
                        d_dst = graph.degree(link.__dst),
                        ret = (d_src + d_dst) * 10 + 10;
                    return ret;
                  });
            }
            return ret;
        };
        return ret;
    }

    function layout__cola(graph) {
        return _.extend(layout__empty(graph), cola.d3adaptor())
                  //.linkDistance(120)
                  .avoidOverlaps(true);
        /*
                  .linkDistance(function (link, i) {
                    var d_src = graph.degree(link.__src),
                        d_dst = graph.degree(link.__dst),
                        ret = (d_src + d_dst) * 10 + 10;
                    return ret;
                  })
                  */
    }

    function layout__d3_force_stp(graph) {
        var layout = layout__d3_force(graph);

        layout.nodes_links = function (nodes, links) {
            layout.nodes(nodes);
            return layout.links(bfs(nodes, links));
        };
        return layout;
    }

    function layout__empty(graph, base) {
        function save() {
            layout.saved_nodes_position = _.object(layout.nodes().map(function (node) {
                return [node.id, {
                    x: node.x,
                    y: node.y,
                    px: node.px,
                    py: node.py,<commit_msg>client/view/layouts: layout.save: ignore px/py, use x/y; fixed #619<commit_after>/*
    This file is part of rhizi, a collaborative knowledge graph editor.
    Copyright (C) 2014-2015  Rhizi

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

define(['consts', 'jquery', 'd3', 'underscore'],
function(consts,   $,        d3,   _) {

"use strict";

    function object__append(d, k, v) {
        if (d[k] === undefined) {
            d[k] = [];
        }
        d[k].push(v);
    }

    function calc_node_links(nodes, links) {
        var node_links = {};

        _.each(links, function (l) {
            var src_id = l.__src.id,
                dst_id = l.__dst.id;

            object__append(node_links, src_id, l);
            object__append(node_links, dst_id, l);
        });
        return node_links;
    }

    function stp_degree_max(nodes, links) {
        var ret_links = [],
            node_id_degree_d = {}, // sum of entering and exising degrees
            node_id_degree,
            node_by_id = _.object(_.map(nodes, "id"), nodes),
            node_links = calc_node_links(nodes, links);

        if (nodes.length === 0) {
            return [];
        }

        function inc(d, k) {
            d[k] = d[k] === undefined ? 1 : d[k] + 1;
        }

        _.each(links, function (l) {
            var src_id = l.__src.id,
                dst_id = l.__dst.id;

            inc(node_id_degree_d, src_id);
            inc(node_id_degree_d, dst_id);
        });
        node_id_degree = _.pairs(node_id_degree).map(function (ar) { return [ar[1], ar[0]]; }).sort().reverse();

        /*
        _.each(node_id_degree.slice(0, node_id_degree.length - 1), function (_degree, node_id) {
            var d = node_links[node_id];
            // pick an edge
            if (d.src.length > 0) {
            }
            // remove all incoming edges
        });
        */
        return ret_links;
    }

    function bfs(nodes, links) {
        var ret_links = [],
            queue = [nodes[0].id],
            node_id,
            i,
            link,
            other,
            visited = {},
            node_links = calc_node_links(nodes, links);

        while (queue.length > 0) {
            node_id = queue.pop();
            for (i = 0 ; i < node_links[node_id].length; ++i) {
                link = node_links[node_id][i];
                other = link.__src.id === node_id ? link.__dst.id : link.__src.id;
                if (!visited[other]) {
                    queue.push(other);
                    ret_links.push(link);
                    visited[other] = true;
                }
            }
            if (queue.length === 0 && visited.length < nodes.length) {
                queue.push(_.difference(_.pick(nodes, 'id'), visited)[0]);
            }
        }
        return ret_links;
    }

    function extendOwn(obj, other) {
        for (var prop in other) {
            if (!obj.hasOwnProperty(prop) && other.hasOwnProperty(prop)) {
                obj[prop] = other[prop];
            }
        }
        return obj;
    }

    function layout__d3_force(graph) {
        return layout__empty(graph, d3.layout.force())
                  .distance(240)
                  .gravity(0.12)
                  .charge(-1800);
    }

    function layout__d3_force__link_distance(graph) {
        var ret = layout__d3_force(graph);
        ret.zen_mode_inner = function (zen_mode) {
            if (zen_mode) {
                ret.distance(240);
            } else {
                ret.linkDistance(function (link) {
                    var d_src = graph.degree(link.__src),
                        d_dst = graph.degree(link.__dst),
                        ret = (d_src + d_dst) * 10 + 10;
                    return ret;
                  });
            }
            return ret;
        };
        return ret;
    }

    function layout__cola(graph) {
        return _.extend(layout__empty(graph), cola.d3adaptor())
                  //.linkDistance(120)
                  .avoidOverlaps(true);
        /*
                  .linkDistance(function (link, i) {
                    var d_src = graph.degree(link.__src),
                        d_dst = graph.degree(link.__dst),
                        ret = (d_src + d_dst) * 10 + 10;
                    return ret;
                  })
                  */
    }

    function layout__d3_force_stp(graph) {
        var layout = layout__d3_force(graph);

        layout.nodes_links = function (nodes, links) {
            layout.nodes(nodes);
            return layout.links(bfs(nodes, links));
        };
        return layout;
    }

    function layout__empty(graph, base) {
        function save() {
            layout.saved_nodes_position = _.object(layout.nodes().map(function (node) {
                return [node.id, {
                    x: node.x,
                    y: node.y,
                    px: node.x,
                    py: node.y,
{'fraction_non_alphanumeric': 0.11257441818509832, 'fraction_numerical': 0.010193036261952011, 'mean_word_length': 1.484203450593771, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2103343', 'n_tokens_mistral': 3322, 'n_tokens_neox': 3133, 'n_words': 778}