<commit_before>// ==========================================================================
// Project:   MySystem.Node
// Copyright: ©2010 My Company, Inc.
// ==========================================================================
/*globals MySystem LinkIt */

/** @class

  Simple model of a MySystem node.

  @extends SC.Record
  @extends LinkIt.Node
  @version 0.1
*/
MySystem.Node = SC.Record.extend(LinkIt.Node, 
/** @scope MySystem.Node.prototype */ {

  image: SC.Record.attr(String),
  title: SC.Record.attr(String),
  outputs: SC.Record.toMany('MySystem.Node'),
  inputs: SC.Record.toMany('MySystem.Node'),
  
  terminals: ['input', 'output'],
  
  links: [],
  
  init: function () {
    sc_super();
    // setup the links property initially
    this.invokeLater(this._calculateLinks);
  },
  
  _nodeArraysDidChange: function () {
    console.log('_nodeArraysDidChange!');
    this.invokeOnce(this._calculateLinks);
  }.observes('.inputs.[]', '.outputs.[]'),
  
  _calculateLinks: function () {
    var links = [], 
        link;
    var inputs = this.get('inputs'),
        outputs = this.get('outputs');
    
    // process inputs
    for (var i = 0, ii = inputs.get('length'); i < ii; i++) {
      link = SC.Object.create(LinkIt.Link, {
        startNode: inputs.objectAt(i),
        startTerminal: 'output',
        endNode: this,
        endTerminal: 'input'
      });
      links.pushObject(link);
    }
    
    // process outputs
    for (i = 0, ii = outputs.get('length'); i < ii; i++) {
      link = SC.Object.create(LinkIt.Link, {
        startNode: this,
        startTerminal: 'output',
        endNode: outputs.objectAt(i),
        endTerminal: 'input'
      });
      links.pushObject(link);
    }
    
    this.set('links', links);
  },
  
  // tell LinkIt whether the proposed link is valid
  canLink: function (link) {
    if (!link) return NO;
    
    var sn = link.get('startNode'), 
        st = link.get('startTerminal');
    var en = link.get('endNode'), 
        et = link.get('endTerminal');
    
    // Make sure we don't connect to ourselves.
    if (sn === en) return NO;

    // Make sure we don't already have this link.
    if (this._hasLink(link)) return NO;
    
    if ( (st === 'input' && et === 'output') || (st === 'output' && et === 'input')) {
      return YES;
    }
    
    // TODO under what other circumstances would we refuse a link?
    return NO;
  },
    
  // do we already have the proposed new link 'link'?  
  _hasLink: function (link) {
    var links = this.get('links') || [];
    var len = links.get('length');
    var n;
    var linkID = LinkIt.genLinkID(link);
    for (var i = 0; i < len; i++) {
      n = links.objectAt(i);
      if (LinkIt.genLinkID(n) === linkID) {
        return YES;
      }
    }
  },
  
  didCreateLink: function (link) {
    var sn = link.get('startNode'), 
        st = link.get('startTerminal');
    var en = link.get('endNode'), 
        et = link.get('endTerminal');
    
    console.log(
      'didCreateLink: this.id = %s, startNode.id = %s, startTerminal = %s, endNode.id = %s, endTerminal = %s', 
      this.get('id'), sn.get('id'), st, en.get('id'), et);
    
    if (sn === this && st === 'output' && et === 'input') {
      var outputs = this.get('outputs');
      outputs.pushObject(en);
    }
    else if (sn === this && st === 'input' && et === 'output') {
      var inputs = this.get('inputs');
      inputs.pushObject(en);
    }
  },
  
  willDeleteLink: function (link) {
    var sn = link.get('startNode'), 
        st = link.get('startTerminal');
    var en = link.get('endNode'), 
        et = link.get('endTerminal');
      
    if (et === 'input') {
      var outputs = this.get('outputs');
      outputs.removeObject(en);
    }
    else if (et === 'output') {
      var inputs = this.get('inputs');
      inputs.removeObject(en);
    }  
  }

}) ;<commit_msg>Node models have inLinks and outLinks ...<commit_after>// ==========================================================================
// Project:   MySystem.Node
// Copyright: ©2010 My Company, Inc.
// ==========================================================================
/*globals MySystem LinkIt */

/** @class

  Simple model of a MySystem node.

  @extends SC.Record
  @extends LinkIt.Node
  @version 0.1
*/
MySystem.Node = SC.Record.extend(LinkIt.Node, 
/** @scope MySystem.Node.prototype */ {

  image: SC.Record.attr(String),
  title: SC.Record.attr(String),
  
  outLinks: SC.Record.toMany('MySystem.Link',{
    inverse: 'startNode'
  }),
  
  inLinks: SC.Record.toMany('MySystem.Link', {
    inverse: 'endNode'
  }),
  
  terminals: ['a', 'b'],
  
  // We have to maintain this list. Its observed from our mixin: LinkIt.Node 
  links: [],
  
  init: function () {
    sc_super();
    // setup the links property initially
    this.invokeLater(this._calculateLinks);
  },
  

  _linkArraysDidChange: function () {
     console.log('_linkArraysDidChange!');
     this.invokeOnce(this._calculateLinks);
  }.observes('.outLinks.[]', '.inLinks.[]'),

  // method to maintain our links property
  _calculateLinks: function () {
     var links = [], 
         link;
     var inputs = this.get('inLinks'),
         outputs = this.get('outLinks');
     
     // process inputs
     for (var i = 0, ii = inputs.get('length'); i < ii; i++) {
       link = inputs.objectAt(i);
       links.pushObject(link);
     }
     
     // process outputs
     for (i = 0, ii = outputs.get('length'); i < ii; i++) {
       link = outputs.objectAt(i);
       links.pushObject(link);
     }
     
    this.set('links', links);
   },
  
  // tell LinkIt whether the proposed link is valid
  canLink: function (link) {
    if (!link) return NO;
    
    var sn = link.get('startNode'), 
        st = link.get('startTerminal');
    var en = link.get('endNode'), 
        et = link.get('endTerminal');
    
    // Make sure we don't connect to ourselves.
    if (sn === en) return NO;

    // Make sure we don't already have this link.
    if (this._hasLink(link)) return NO;
    
    if ( (st === 'input' && et === 'output') || (st === 'output' && et === 'input')) {
      return YES;
    }
    return YES;
    // TODO under what other circumstances would we refuse a link?
    // return NO;
  },
    
  
  // do we already have the proposed new link 'link'?  
  _hasLink: function (link) {
    var links = this.get("links");
    var len = links.get('length');
    var n;
    var linkID = LinkIt.genLinkID(link);
    for (var i = 0; i < len; i++) {
      n = links.objectAt(i);
      if (LinkIt.genLinkID(n) === linkID) {
        return YES;
      }
    }
  },
  
  didCreateLink: function (link) {
    var sn = link.get('startNode'), 
        st = link.get('startTerminal');
    var en = link.get('endNode'), 
        et = link.get('endTerminal');
    link.set("guid", 'something fake for now');
    link.set("text", 'label me');
    link.set("color", 'default color');
    console.log(
      'didCreateLink: this.id = %s, startNode.id = %s, startTerminal = %s, endNode.id = %s, endTerminal = %s', 
      this.get('id'), sn.get('id'), st, en.get('id'), et);
    if (sn === this) {
      this.get('outLinks').pushObject(link);
      SC.Logger.log("this: output %@",this);
    }
    else if (en === this) {
      this.get('inLinks').pushObject(link); // is that going to work?
      SC.Logger.log("this: input %@",this);
    }
  },
  
  willDeleteLink: function (link) {
    var sn = link.get('startNode'), 
        st = link.get('startTerminal');
    var en = link.get('endNode'), 
        et = link.get('endTerminal');
      
    this.get("outlinks").removeObject(link);
    this.get("inlinks").removeObject(link);
  }

}) ;
{'fraction_non_alphanumeric': 0.20423816952678106, 'fraction_numerical': 0.00234009360374415, 'mean_word_length': 2.5208237986270023, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2108209', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2459, 'n_words': 609}