<commit_before>var React = require('react');
var Reflux = require('reflux');
var FermActions = require('../actions');
var _ = require('../../lodash.min');

var NodeCollection = require('../../nodecollection');
var EdgeCollection = require('../../edgecollection');

class Egraph {
  constructor(args){
    this.nodes = new NodeCollection(args.nodes, this);
    this.edges = new EdgeCollection(args.edges, this);
  }

  outsideMetrics(node){
    var nodes = this.outsideNodes(node)
    return _.select(nodes, function(n) {return (n.ttype() !== 'function')})
  }
  // Used to find possible outputs for a function node
  outsideNodes(node){
    var insideNodes = _.union([node], node.allOutputs())
    return _.difference(this.nodes.models, insideNodes)
  }
}

var todoCounter = 1,
    localStorageKey = "fermi";

var fermGraphStore = Reflux.createStore({
    listenables: [FermActions],
    getNodes: function() {
        return this.list;
    },
    addEstimate: function() {
      var newNodeInfo = {
          pid: todoCounter++,
          nodeType: 'estimate'
      };
      var newNode = this.graph.nodes.create(newNodeInfo)
      FermActions.updateEditingNode(newNode.id)
    },
    addFunction: function() {
      var newDependentInfo = {
        pid: todoCounter++,
        nodeType: 'dependent',
        name: '',
        value: ''
      };
      var newFunctionInfo = {
        pid: todoCounter++,
        nodeType: 'function',
        outputIds: newDependentInfo.pid
      };
      //this.updateNodes([newResult, newFun].concat(this.list));
      //var newDependent = this.graph.nodes.create(newDependentInfo)
      var newDependent = this.graph.nodes.create(newDependentInfo)
      var newFunction = this.graph.nodes.create(newFunctionInfo)
      var newEdge = this.graph.edges.create({0:newFunction.id, 1: newDependent.id})
      //if (this.get('outputIds')){
      //var inp = this.id
      //var out = this.get('outputIds')
      //var foo = this.collection.graph.edges.add({0:inp, 1:out})
      FermActions.updateEditingNode(newFunction)
    },
    onAddNode: function(type) {
      if (type=="estimate"){
          this.addEstimate();
      } else {
          this.addFunction();
      }
    },
    onUpdateNodes: function(list){
      _.map(list, function(n){this._onUpdateNode(n.id, n)}, this)
      this.updateGraph();
    },
    onUpdateNode: function(nodeId, newValues) {
      this._onUpdateNode(nodeId, newValues)
      this.updateGraph();
    },
    updateGraph: function(graph) {
      //localStorage.setNode(localStorageKey, JSON.stringify(list));
      this.trigger(this.graph);
    },
    _onUpdateNode: function(nodeId, newValues){
      var node = this.getNode(parseInt(nodeId));
      if (!node) {
          return;
      };
      node.set(newValues)
      node.propogate()
    },
    onRemoveNode: function(nodeId) {
      var node = this.graph.nodes.get(nodeId)
      var edges = node.edges();
      edges.map(e => e.destroy())
      node.destroy()
      this.updateGraph()
      FermActions.resetEditingNode()
    },
    getNode: function(nodeId){
      return this.graph.nodes.get(nodeId)
    },
    getInitialState: function() {

      var data = {
        nodes: [
          {pid: 1, nodeType: 'estimate', name: 'people in NYC', value: 10000000},
          {pid: 2, nodeType: 'estimate', name: 'families per person', value: 0.3},
          {pid: 3, nodeType: 'estimate', name: 'pianos per family', value: 0.1},
          {pid: 4, nodeType: 'estimate', name: 'piano tuners per family', value: 0.001},
          {pid: 5, nodeType: 'dependent', name: 'families in NYC'},
          {pid: 6, nodeType: 'dependent', name: 'pianos in NYC'},
          {pid: 7, nodeType: 'dependent', name: '<NAME> NYC'},
          {pid: 8, nodeType: 'function', functionType: 'multiplication'},
          {pid: 9, nodeType: 'function', functionType: 'multiplication'},
          {pid: 10, nodeType: 'function', functionType: 'multiplication'},
        ],
        edges: [
          [1,8],
          [2,8],
          [8,5],
          [5,9],
          [3,9],
          [9,6],
          [6,10],
          [4,10],
          [10,7],
        ]
      };
      this.graph = new Egraph(data);
      var dependents = this.graph.nodes.allOfTtype('dependent')
      dependents.map(n => n.propogate())
      var todoCounter = parseInt(_.max(this.graph.nodes.models, 'id').id) + 1

      // var loadedNodes = localStorage.getNode(localStorageKey);
      // if (!loadedNodes) {
      //     // If no list is in localstorage, start out with a default one
      //     this.list = [
      //         {
      //             id: todoCounter++,
      //             created: new Date(),
      //             name: '<NAME>',
      //             mean: 0,
      //             type: 'estimate'
      //         },
      //         {
      //             id: todoCounter++,
      //             created: new Date(),
      //             name: '<NAME>',
      //             mean: 0,
      //             type: 'estimate'<commit_msg>adding works with new nodes.  not sure how it broke<commit_after>var React = require('react');
var Reflux = require('reflux');
var FermActions = require('../actions');
var _ = require('../../lodash.min');

var NodeCollection = require('../../nodecollection');
var EdgeCollection = require('../../edgecollection');

class Egraph {
  constructor(args){
    this.nodes = new NodeCollection(args.nodes, this);
    this.edges = new EdgeCollection(args.edges, this);
  }

  outsideMetrics(node){
    var nodes = this.outsideNodes(node)
    return _.select(nodes, function(n) {return (n.ttype() !== 'function')})
  }
  // Used to find possible outputs for a function node
  outsideNodes(node){
    var insideNodes = _.union([node], node.allOutputs())
    return _.difference(this.nodes.models, insideNodes)
  }
}

var todoCounter = 1,
    localStorageKey = "fermi";

var fermGraphStore = Reflux.createStore({
    listenables: [FermActions],
    getNodes: function() {
        return this.list;
    },
    addEstimate: function() {
      var newNodeInfo = {
          pid: todoCounter++,
          nodeType: 'estimate'
      };
      var newNode = this.graph.nodes.create(newNodeInfo)
      FermActions.updateEditingNode(newNode.id)
    },
    addFunction: function() {
      var newDependentInfo = {
        pid: todoCounter++,
        nodeType: 'dependent',
        name: '',
        value: ''
      };
      var newFunctionInfo = {
        pid: todoCounter++,
        nodeType: 'function',
        outputIds: newDependentInfo.pid
      };
      //this.updateNodes([newResult, newFun].concat(this.list));
      //var newDependent = this.graph.nodes.create(newDependentInfo)
      var newDependent = this.graph.nodes.create(newDependentInfo)
      var newFunction = this.graph.nodes.create(newFunctionInfo)
      var newEdge = this.graph.edges.create({0:newFunction.id, 1: newDependent.id})
      //if (this.get('outputIds')){
      //var inp = this.id
      //var out = this.get('outputIds')
      //var foo = this.collection.graph.edges.add({0:inp, 1:out})
      FermActions.updateEditingNode(newFunction)
    },
    onAddNode: function(type) {
      if (type=="estimate"){
          this.addEstimate();
      } else {
          this.addFunction();
      }
    },
    onUpdateNodes: function(list){
      _.map(list, function(n){this._onUpdateNode(n.id, n)}, this)
      this.updateGraph();
    },
    onUpdateNode: function(nodeId, newValues) {
      this._onUpdateNode(nodeId, newValues)
      this.updateGraph();
    },
    updateGraph: function(graph) {
      //localStorage.setNode(localStorageKey, JSON.stringify(list));
      this.trigger(this.graph);
    },
    _onUpdateNode: function(nodeId, newValues){
      var node = this.getNode(parseInt(nodeId));
      if (!node) {
          return;
      };
      node.set(newValues)
      node.propogate()
    },
    onRemoveNode: function(nodeId) {
      var node = this.graph.nodes.get(nodeId)
      var edges = node.edges();
      edges.map(e => e.destroy())
      node.destroy()
      this.updateGraph()
      FermActions.resetEditingNode()
    },
    getNode: function(nodeId){
      return this.graph.nodes.get(nodeId)
    },
    getInitialState: function() {

      var data = {
        nodes: [
          {pid: 11, nodeType: 'estimate', name: 'people in NYC', value: 10000000},
          {pid: 2, nodeType: 'estimate', name: 'families per person', value: 0.3},
          {pid: 3, nodeType: 'estimate', name: 'pianos per family', value: 0.1},
          {pid: 4, nodeType: 'estimate', name: 'piano tuners per family', value: 0.001},
          {pid: 5, nodeType: 'dependent', name: 'families in NYC'},
          {pid: 6, nodeType: 'dependent', name: 'pianos in NYC'},
          {pid: 7, nodeType: 'dependent', name: 'piano tuners in NYC'},
          {pid: 8, nodeType: 'function', functionType: 'multiplication'},
          {pid: 9, nodeType: 'function', functionType: 'multiplication'},
          {pid: 10, nodeType: 'function', functionType: 'multiplication'},
        ],
        edges: [
          [11,8],
          [2,8],
          [8,5],
          [5,9],
          [3,9],
          [9,6],
          [6,10],
          [4,10],
          [10,7],
        ]
      };
      this.graph = new Egraph(data);
      var dependents = this.graph.nodes.allOfTtype('dependent')
      dependents.map(n => n.propogate())
      todoCounter = parseInt(_.max(this.graph.nodes.models, 'id').id) + 1

      // var loadedNodes = localStorage.getNode(localStorageKey);
      // if (!loadedNodes) {
      //     // If no list is in localstorage, start out with a default one
      //     this.list = [
      //         {
      //             id: todoCounter++,
      //             created: new Date(),
      //             name: '<NAME>',
      //             mean: 0,
      //             type: 'estimate'
      //         },
      //         {
      //             id: todoCounter++,
      //             created: new Date(),
      //             name: '<NAME>',
      //             mean: 0,
      //             type: 'estimate'
{'fraction_non_alphanumeric': 0.15220624690133863, 'fraction_numerical': 0.011303916707982151, 'mean_word_length': 2.1528602688340106, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '6703649', 'n_tokens_mistral': 3168, 'n_tokens_neox': 3068, 'n_words': 671}