Difference between revisions of "Peer-nodal.as"

From Organic Design wiki
(account for 0-length initial queue)
(legacy)
 
(24 intermediate revisions by 2 users not shown)
Line 1: Line 1:
// NODAL ASSOCIATIONS
+
{{legacy}}
 +
<as>
 +
// Liscenced under LGPL: www.gnu.org/copyleft/lesser.html
 +
//
 +
// peer-nodal.as - ecma-script implementation of the nodal core
 +
// Nad - 2006
 +
//
 +
 
 +
// ISSUES
 
// - associations (maintained class:instance relationships)
 
// - associations (maintained class:instance relationships)
//   instance.create/delete(class) calls class.create/delete(instance)
+
// - get should use class content as default - resolve() doesn't
//  create inserts the instance into the classes instance-loop (list-aspect, next/prev)
+
//  so either add get/set which use resolve, or remove object-freedom and have only .data
//  ie. our list of current children (that our E divides amongst) is also our instance-loop
 
//      has or is are one, they're all just relationships that receive a portion of energy
 
//  laster: a chain of delegate classes is created between root and the instance
 
// - the class of any assoc requires a portion of the quanta
 
//  remember, next/prev and complimented by parent/current (parent is also class)
 
// - this allows alternate quanta be divided between parent and current, thus providing
 
//  energy for the class->instance relationship
 
// - get retreives from class value if no instance value at each step of a path
 
 
 
// REDUCTION
 
// - all instance-loops work by going cd this.current then assigning this.current.next to it
 
//  when a function executes, it can update the loop at that location (equiv to array.push)
 
// - a functions parameters (and even further methods and tools) are child associations
 
//  the structures necessary to build the fucntion are child assocs too
 
  
// DATA STRUCTURE
+
// NODAL PROPERTIES & CORE NODAL CONCEPTS
// A question that keeps cropping up is:
+
// - all-capital-identifiers are node-references
//   is it best to use the language's native list type,
+
// - ROOT.idcount is a accumulating-only counter for locally-unique-id number
//   or to use a linked list constructed of ''next'' and ''prev'' refs.
+
// - ROOT.nodes is the current number of instantiated node objects
// The best idea (since swf failed key-as-ref) is to use array-index-only as key,
+
ROOT = { idcount: 0, nodes: 0 };
// but allow any datatype as value, that way we can use a list or not later on
+
VOID = ROOT;
// values could be node-ref (index), list, lambda, or function-ref
+
INIT = new node( ROOT ); // Default method executed if quanta sent to empty/new association
 +
NEXT = new node( ROOT );
 +
PREV = new node( ROOT );
 +
FUNC = new node( ROOT );
 +
FOCUS = new node( ROOT );
  
// ==========================================================================================
+
// NODE OBJECT
 
+
// - note: only a node's parent can adjust a nodes prev/next
// idcount is a accumulating-only counter for locally-unique-id number
+
function node( key ) {
// nodes is the current number of instantiated node objects
+
ROOT.nodes++;
// queue is the root reduction queue
+
this.id = ROOT.idcount++;
nodal = { queue: [], idcount: 0, nodes: 0 };
+
this.set( [FOCUS], key.get( [INIT] ) );
 +
}
  
 +
// NODAL METHODS
 
// - the delete-list is only needed for limited list-space env
 
// - the delete-list is only needed for limited list-space env
 
// - Actionscript failed ref-as-hashkey test,
 
// - Actionscript failed ref-as-hashkey test,
Line 39: Line 39:
 
// - just get/set properties with normal object-accessor way
 
// - just get/set properties with normal object-accessor way
 
// - but keep all hierarchy nodal
 
// - but keep all hierarchy nodal
 +
 +
node.prototype.set = function( path, value ) {
 +
var ptr = this._resolve( path );
 +
ptr.value = value;
 +
};
 +
 +
node.prototype.get = function( path ) {
 +
var ptr = this._resolve( path );
 +
// need to return class.value onNonExistent
 +
return ptr.value;
 +
};
  
 
// Resolve creates if non existent and returns reference to result
 
// Resolve creates if non existent and returns reference to result
nodal.resolve = function( context, path ) {
+
// - this is private, used by get/set
 +
node.prototype._resolve = function( path ) {
 +
var context = this;
 
while( path.length ) {
 
while( path.length ) {
 
var key = path.shift();
 
var key = path.shift();
if ( context[key.id] == null ) {
+
context = context[key.id] == null ? context[key.id] = new node( key ) : context[ key.id ];
this.nodes++;
+
}
var node = { queue: [], id: this.idcount++ };
 
context = context[key.id] = node;
 
}
 
else context = context[ key.id ];
 
}
 
 
return context;
 
return context;
};
+
};
  
 
// Consume one quanta
 
// Consume one quanta
 
// - each quantum is actually two, one for local instance, one for global class
 
// - each quantum is actually two, one for local instance, one for global class
nodal.reduce = function( context ) {
+
// - all instance-loops work by going cd this.current then assigning this.current.next to it
 +
//  when a function executes, it can update the loop at that location (equiv to array.push)
 +
// - a functions parameters (and even further methods and tools) are child associations
 +
//  the structures necessary to build the fucntion are child assocs too
 +
node.prototype.reduce = function() {
 +
 
 +
// Rotate this level's queue
 +
var ptr = this.get( [FOCUS] );
 +
this.set( [FOCUS], this.get( [FOCUS,NEXT] ) );
  
if ( node = context.queue.pop() ) {
+
// If there's a local function declaration, execute
 +
var ref = ptr.get( [FUNC] );
 +
if ( ref != VOID ) {
 +
ref = ref.call( ptr );
 +
if ( ref == VOID ) {
 +
// Remove focus from loop
 +
ptr.set( [NEXT,PREV], ptr.get( [PREV] ) ); // ptr.next.prev = ptr.prev
 +
ptr.set( [PREV,NEXT], ptr.get( [NEXT] ) ); // ptr.prev.next = ptr.next
 +
}
 +
else if ( ref != ptr ) {
 +
// Replace current ptr with result-ref
 +
ref.set( [NEXT], ptr.get( [NEXT] ) ); // ref.next = ptr.next
 +
ref.set( [PREV], ptr.get( [PREV] ) ); // ref.prev = ptr.prev
 +
ptr.set( [NEXT,PREV], ref ); // ptr.next.prev = ref
 +
ptr.set( [PREV,NEXT], ref ); // ptr.prev.next = ref
 +
}
  
// is there a local declaration?
+
// also call class.reduce()
// - if yes, execute it and also reduce( class )
 
// - if no, { reduce( node ) if it has a queue, back to root if not }
 
  
}
+
}
 +
 
 +
// if not, send the quanta downstairs if there's anything in its loop
 +
else if ( ptr = ptr.get( [FOCUS] ) != VOID ) ptr.reduce();
  
// Old ECMA SIC reduction
+
};
for ( var i = 1; i == 1; ) {
+
</as>
var queue = cwd.queue;
+
[[Category:ActionScript]]
if ( queue.length > 0 ) {
 
var todo = queue.shift();
 
if ( next = typeOf( todo ) == 'function' ? ( i = todo.call ) : ( cwd = todo ) ) queue.push( next );
 
}
 
else i = 0;
 
}
 
};
 

Latest revision as of 23:43, 24 September 2013

Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, this is only useful for a historic record of work done. You may find a link to the currently used concept or function in this article, if not you can contact the author to find out what has taken the place of this legacy item.

<as> // Liscenced under LGPL: www.gnu.org/copyleft/lesser.html // // peer-nodal.as - ecma-script implementation of the nodal core // Nad - 2006 //

// ISSUES // - associations (maintained class:instance relationships) // - get should use class content as default - resolve() doesn't // so either add get/set which use resolve, or remove object-freedom and have only .data

// NODAL PROPERTIES & CORE NODAL CONCEPTS // - all-capital-identifiers are node-references // - ROOT.idcount is a accumulating-only counter for locally-unique-id number // - ROOT.nodes is the current number of instantiated node objects ROOT = { idcount: 0, nodes: 0 }; VOID = ROOT; INIT = new node( ROOT ); // Default method executed if quanta sent to empty/new association NEXT = new node( ROOT ); PREV = new node( ROOT ); FUNC = new node( ROOT ); FOCUS = new node( ROOT );

// NODE OBJECT // - note: only a node's parent can adjust a nodes prev/next function node( key ) { ROOT.nodes++; this.id = ROOT.idcount++; this.set( [FOCUS], key.get( [INIT] ) ); }

// NODAL METHODS // - the delete-list is only needed for limited list-space env // - Actionscript failed ref-as-hashkey test, // so we use a id (locally unique identifier) as a lookup-key // - no delete currently // - just get/set properties with normal object-accessor way // - but keep all hierarchy nodal

node.prototype.set = function( path, value ) { var ptr = this._resolve( path ); ptr.value = value; };

node.prototype.get = function( path ) { var ptr = this._resolve( path ); // need to return class.value onNonExistent return ptr.value; };

// Resolve creates if non existent and returns reference to result // - this is private, used by get/set node.prototype._resolve = function( path ) { var context = this; while( path.length ) { var key = path.shift(); context = context[key.id] == null ? context[key.id] = new node( key ) : context[ key.id ]; } return context; };

// Consume one quanta // - each quantum is actually two, one for local instance, one for global class // - all instance-loops work by going cd this.current then assigning this.current.next to it // when a function executes, it can update the loop at that location (equiv to array.push) // - a functions parameters (and even further methods and tools) are child associations // the structures necessary to build the fucntion are child assocs too node.prototype.reduce = function() {

// Rotate this level's queue var ptr = this.get( [FOCUS] ); this.set( [FOCUS], this.get( [FOCUS,NEXT] ) );

// If there's a local function declaration, execute var ref = ptr.get( [FUNC] ); if ( ref != VOID ) { ref = ref.call( ptr ); if ( ref == VOID ) { // Remove focus from loop ptr.set( [NEXT,PREV], ptr.get( [PREV] ) ); // ptr.next.prev = ptr.prev ptr.set( [PREV,NEXT], ptr.get( [NEXT] ) ); // ptr.prev.next = ptr.next } else if ( ref != ptr ) { // Replace current ptr with result-ref ref.set( [NEXT], ptr.get( [NEXT] ) ); // ref.next = ptr.next ref.set( [PREV], ptr.get( [PREV] ) ); // ref.prev = ptr.prev ptr.set( [NEXT,PREV], ref ); // ptr.next.prev = ref ptr.set( [PREV,NEXT], ref ); // ptr.prev.next = ref }

// also call class.reduce()

}

// if not, send the quanta downstairs if there's anything in its loop else if ( ptr = ptr.get( [FOCUS] ) != VOID ) ptr.reduce();

}; </as>