# all files / clique/model/ Graph.js

16.25% Statements 13/80
10% Branches 4/40
4.55% Functions 1/22
9.46% Lines 7/74
1 statement Ignored

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    1751×
    1×
    1×
    1×

    1×

    1×

     import _ from 'underscore';
    import Backbone from 'backbone';
    import { concat } from '../../candela/util';
    import MultiTable from '../util/MultiTable';

    const Graph = Backbone.Model.extend({
      constructor: function (options) {
        Backbone.Model.call(this, {}, options || {});
      },

      initialize: function (attributes, options) {
        this.adapter = options.adapter;

        this.nodes = {};
        this.links = new Set();

        this.forward = new MultiTable();
        this.back = new MultiTable();

        this.set({
          nodes: [],
          links: []
        });
      },

      addNeighborhood: function (node) {
        return this.adapter.neighborNodes(node).then(_.bind(function (neighbors) {
          _.map(neighbors.nodes, _.partial(this.addNode, _), this);
        }, this));
      },

      addNode: function (node) {
        // Bail if node is already in graph.
        if (_.has(this.nodes, node.key())) {
          return;
        }

        // Get all neighboring links.
        this.adapter.neighborLinks(node).then(_.bind(function (links) {
          var newLinks;

          // Add the node to the graph model.
          this.nodes[node.key()] = node.getRaw();

          // Filter away links not incident on nodes currently in the
          // graph.
          links = _.filter(links, function (link) {
            return _.has(this.nodes, link.source()) && _.has(this.nodes, link.target());
          }, this);

          // Add the links to the graph.
          newLinks = _.compact(_.map(links, function (link) {
            var key = link.key();

            if (!this.links.has(key)) {
              this.links.add(key);

              this.forward.add(link.source(), link.target());

              if (link.getAttribute('undirected')) {
                this.back.add(link.target(), link.source());
              }

              link.getRaw().source = this.nodes[link.source()];
              link.getRaw().target = this.nodes[link.target()];

              return link.getRaw();
            }
          }, this));

          this.set({
            nodes: concat(this.get('nodes'), node.getRaw()),
            links: concat(this.get('links'), newLinks)
          });
        }, this));
      },

      removeNode: function (node) {
        this.removeNodes([node]);
      },

      removeNodes: function (nodes) {
        let marked = new Set();
        let newNodes;
        let newLinks;

        // Mark the nodes for removal.
        marked = new Set();
        _.each(nodes, _.bind(function (node) {
          marked.add(node);
          delete this.nodes[node];

          _.each(this.forward[node], _.bind(function (to) {
            this.back.remove(to, node);
          }, this));

          _.each(this.back[node], _.bind(function (from) {
            this.forward.remove(from, node);
          }, this));

          this.forward.strike(node);
          this.back.strike(node);
        }, this));

        // Copy over the nodes into a new array that omits the marked ones.
        newNodes = _.filter(this.get('nodes'), function (node) {
          return !marked.has(node.key);
        });

        // Copy over the links into a new array that omits ones involved
        // with deleted nodes.
        newLinks = [];
        _.each(this.get('links'), _.bind(function (link) {
          if (!marked.has(link.source.key) && !marked.has(link.target.key)) {
            newLinks.push(link);
          } else {
            this.links.delete(link.key);
          }
        }, this));

        // Set the new node and link data on the model.
        this.set({
          nodes: newNodes,
          links: newLinks
        });
      },

      inNeighbors: function (key) {
        return _.clone(this.back.items(key));
      },

      outNeighbors: function (key) {
        return _.clone(this.forward.items(key));
      },

      neighbors: function (key) {
        var inn = this.inNeighbors(key);
        let outn = this.outNeighbors(key);
        let nbs;

        if (_.isUndefined(inn) && _.isUndefined(outn)) {
          return undefined;
        }

        nbs = new Set();
        _.each(concat(inn || [], outn || []), nbs.add, nbs);

        return nbs.items();
      },

      inDegree: function (key) {
        var neighbors = this.back.items(key);
        return neighbors && _.size(neighbors) || -1;
      },

      outDegree: function (key) {
        var neighbors = this.forward.items(key);
        return neighbors && _.size(neighbors) || -1;
      },

      degree: function (key) {
        let ind = this.inDegree(key);
        let outd = this.outDegree(key);

        if (ind < 0 && outd < 0) {
          return -1;
        }

        return (ind < 0 ? 0 : ind) + (outd < 0 ? 0 : outd);
      }
    });

    export default Graph;