jungle-organic
Version:
The organic programming framework
1,227 lines • 109 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Jungle;
(function (Jungle) {
function G(components, form) {
return new Jungle.ResolutionCell(components, form);
}
Jungle.G = G;
function F(func, components) {
return new Jungle.ResolutionCell(components, { r: func });
}
Jungle.F = F;
function R(reconstructionBundle) {
return new Jungle.Reconstruction(reconstructionBundle);
}
Jungle.R = R;
function T(type) {
return new Jungle.Terminal(type);
}
Jungle.T = T;
function L(crown, form) {
return new Jungle.LinkCell(crown, form);
}
Jungle.L = L;
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var Actions;
(function (Actions) {
var Component = (function () {
function Component(host) {
this.host = host;
}
Component.prototype.add = function (keyOrVal, val) {
this.host.inductComponent(val);
var al = arguments.length;
var ins = null;
if (!(al === 1 || al === 2)) {
throw Error("Requires 1 or 2 arguments");
}
else if (al === 1) {
if (this.host.crown instanceof Array) {
ins = this.host.crown.length;
this.host.crown.push(val);
}
else if (Jungle.Util.isVanillaObject(this.host.crown)) {
throw Error("Requires key and value to add to object crown");
}
else if (this.host.crown instanceof Jungle.Terminal) {
if (this.host.crown.check(val)) {
this.host.crown = val;
}
}
else {
throw Error("Unable to clobber existing value");
}
}
else {
if (Jungle.Util.isVanillaObject(this.host.crown)) {
ins = keyOrVal;
this.host.crown[keyOrVal] = val;
}
else {
throw Error("Requires single arg for non object crown");
}
}
};
return Component;
}());
Actions.Component = Component;
})(Actions = Jungle.Actions || (Jungle.Actions = {}));
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var ASSOCMODE;
(function (ASSOCMODE) {
ASSOCMODE[ASSOCMODE["INHERIT"] = 0] = "INHERIT";
ASSOCMODE[ASSOCMODE["SHARE"] = 1] = "SHARE";
ASSOCMODE[ASSOCMODE["TRACK"] = 2] = "TRACK";
})(ASSOCMODE = Jungle.ASSOCMODE || (Jungle.ASSOCMODE = {}));
var CTXPropertyTypes;
(function (CTXPropertyTypes) {
CTXPropertyTypes[CTXPropertyTypes["NORMAL"] = 0] = "NORMAL";
CTXPropertyTypes[CTXPropertyTypes["BOUND"] = 1] = "BOUND";
CTXPropertyTypes[CTXPropertyTypes["HOOK"] = 2] = "HOOK";
})(CTXPropertyTypes = Jungle.CTXPropertyTypes || (Jungle.CTXPropertyTypes = {}));
var GContext = (function () {
function GContext(host, contextspec) {
this.host = host;
var properties = contextspec.properties, declaration = contextspec.declaration;
this.declaration = declaration;
Object.defineProperties(this, {
internalProperties: {
value: {},
writable: false,
enumerable: false,
configurable: false
},
propertyLayerMap: {
value: {},
writable: false,
enumerable: false,
configurable: false
},
});
this.originals = {};
this.cache = {};
this.closed = false;
this.label = "";
this.nominal = false;
this.path = [];
this.exposed = { path: this.path };
properties.forEach(function (value, index) {
this.addInternalProperty(value);
}, this);
}
GContext.prototype.addInternalProperty = function (spec) {
switch (spec.type) {
case CTXPropertyTypes.NORMAL:
this.addExposedProperty(spec.key, spec.value);
break;
case CTXPropertyTypes.BOUND:
this.addExposedProperty(spec.key, spec.value.bind(this.exposed));
break;
case CTXPropertyTypes.HOOK: this.addHookedProperty(spec);
}
};
GContext.prototype.addHookedProperty = function (spec) {
this.originals[spec.key] = spec;
this.cache[spec.key] = spec.value;
if (spec.reference instanceof Array) {
this.addThroughProperty(spec);
}
else {
var href = spec.reference;
if (href.orientation === Jungle.IO.Orientation.INPUT) {
this.addInputProperty(spec);
}
else {
this.addOutputProperty(spec);
}
}
this.addExposedProperty(spec.key, spec.value);
};
GContext.prototype.addThroughProperty = function (spec) {
var ospec = { type: spec.type, key: spec.key, value: spec.value, reference: spec.reference[1], original: spec.original };
spec.reference = spec.reference[0];
this.addInputProperty(spec);
this.addOutputProperty(ospec);
};
GContext.prototype.addInputProperty = function (spec) {
spec.reference.reactiveValue = true;
spec.reference.tractor = (function (inp) {
this.exposed[spec.key] = inp;
if (spec.reference.eager && this.cache[spec.key] !== inp) {
this.cache[spec.key] = inp;
}
else {
return Jungle.IO.HALT;
}
}).bind(this);
};
GContext.prototype.addOutputProperty = function (spec) {
spec.reference.reactiveValue = true;
spec.reference.tractor = (function (output) {
var current = this.exposed[spec.key];
if (spec.reference.eager || this.cache[spec.key] !== current) {
this.cache[spec.key] = current;
return current;
}
else {
return Jungle.IO.HALT;
}
}).bind(this);
};
GContext.prototype.prepare = function () {
var layers = this.parseMode(this.declaration);
for (var i = 0; i < layers.length; i++) {
var layer = layers[i];
switch (layer.mode) {
case (ASSOCMODE.INHERIT): {
this.addInherentLayer(layer.source);
break;
}
default: {
this.addSourceLayer(layer);
break;
}
}
}
};
GContext.prototype.extract = function () {
var patch = {};
for (var k in this.internalProperties) {
var v = this.internalProperties[k];
if (k in this.originals) {
var orig = this.originals[k];
patch[orig.original || orig.key] = orig.value;
}
else {
patch[k] = Jungle.Util.deepCopy(v);
}
}
return patch;
};
GContext.prototype.parseMode = function (modestr) {
var layers = [];
var usesplit = modestr.split(/use/);
var header, usage;
if (usesplit.length > 2) {
throw new Error("inappropriate appearance of keyword 'use'");
}
else if (usesplit.length == 2) {
if (usesplit[1] == '') {
throw new Error("expected at lease one context label after use");
}
else {
header = usesplit[0], usage = usesplit[1];
}
}
else {
header = usesplit[0];
}
var headerexp = /^\s*(\w*)\s*$/;
var headermatch = header.match(headerexp);
if (headermatch === undefined) {
throw new Error("only one label before the header is allowed");
}
this.label = headermatch[1];
if (this.label !== '') {
this.nominal = true;
}
if (usage) {
var uses = usage.split(/\s/).filter(function (a) { return a !== ''; });
for (var i = 0; i < uses.length; i += 1) {
var layer = { mode: ASSOCMODE.SHARE, source: null };
var sourceKey = uses[i];
layer.source = this.host.getNominal(sourceKey).ctx;
layers.push(layer);
}
}
return layers;
};
GContext.prototype.addExposedProperty = function (name, defaultValue) {
this.internalProperties[name] = defaultValue;
this.propertyLayerMap[name] = { source: this, mode: ASSOCMODE.SHARE };
Object.defineProperty(this.exposed, name, {
set: this.setItem.bind(this, name),
get: this.getItem.bind(this, name),
enumerable: true,
configurable: true
});
};
GContext.prototype.removeExposedProperty = function (name) {
delete this.internalProperties[name];
delete this.propertyLayerMap[name];
delete this.exposed[name];
};
GContext.prototype.setItem = function (key, data) {
var layer = this.propertyLayerMap[key];
if (layer.mode == ASSOCMODE.TRACK) {
throw new Error("Unable to modify key whose source is tracking only");
}
else {
layer.source.internalProperties[key] = data;
}
};
GContext.prototype.getItem = function (key) {
var layer = this.propertyLayerMap[key];
var result = layer.source.internalProperties[key];
return result;
};
GContext.prototype.getItemSource = function (key) {
if (key in this.propertyLayerMap) {
return this.propertyLayerMap[key].source;
}
else {
throw new Error("key %s not found in the context");
}
};
GContext.prototype.addInherentLayer = function (layerctx) {
for (var prop in layerctx.internalProperties) {
var propVal = layerctx.internalProperties[prop];
this.addExposedProperty(prop, propVal);
}
};
GContext.prototype.addSourceLayer = function (layer) {
for (var prop in layer.source.propertyLayerMap) {
var propVal = layer.source.propertyLayerMap[prop];
if (this.propertyLayerMap[prop] != undefined && (this.propertyLayerMap[prop].mode != propVal.mode || this.propertyLayerMap[prop].source != propVal.source)) {
throw new Error("source layer introduces incompatible source/mode of property");
}
else {
this.propertyLayerMap[prop] = { source: propVal.source, mode: layer.mode };
Object.defineProperty(this.exposed, prop, {
set: this.setItem.bind(this, prop),
get: this.getItem.bind(this, prop),
enumerable: true,
configurable: true
});
}
}
};
return GContext;
}());
Jungle.GContext = GContext;
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var BaseCell = (function () {
function BaseCell(components, form) {
if (form === void 0) { form = {}; }
this.depth = 0;
this.isRoot = true;
this.prepared = false;
this.junction = new Jungle.Util.Junction();
this.form = this.constructForm();
var _a = this.form.parse(form), iospec = _a.iospec, contextspec = _a.contextspec;
this.ctx = this.constructContext(contextspec);
this.io = this.constructIO(iospec);
this.act = this.constructActions();
var inductor = this.inductComponent.bind(this);
this.crown = Jungle.Util.typeCaseSplitF(inductor, inductor, null)(components);
}
BaseCell.prototype.constructForm = function () {
return new Jungle.BaseForm(this);
};
BaseCell.prototype.constructIO = function (iospec) {
return new Jungle.IO.BaseIO(this, iospec);
};
BaseCell.prototype.constructContext = function (contextspec) {
return new Jungle.GContext(this, contextspec);
};
BaseCell.prototype.constructActions = function () {
return new Jungle.Actions.Component(this);
};
BaseCell.prototype.constructCore = function (crown, form) {
return new BaseCell(crown, form);
};
BaseCell.prototype.inductComponent = function (component) {
var c;
if (component instanceof BaseCell) {
c = component;
}
else if (component instanceof Object) {
c = new Jungle.ResolutionCell(component);
}
else {
c = component;
}
return c;
};
BaseCell.prototype.prepare = function (prepargs) {
var _this = this;
if (prepargs === void 0) { prepargs = null; }
if (this.isAncestor) {
throw Error("Ancestors cannot be prepared for resolution");
}
this.ancestor = this.ancestor || this.replicate();
this.ancestor.isAncestor = true;
if (!this.prepared) {
this.ctx.prepare();
this.junction = this.junction
.then(function (results, handle) {
_this.ctx.exposed.handle = handle;
_this.form.preparator.call(_this.ctx.exposed, prepargs);
}).then(function (results, handle) {
Jungle.Util.typeCaseSplitF(function (child, k) { return _this.prepareChild(prepargs, handle, child, k); })(_this.crown);
}, false).then(function (results, handle) {
_this.crown = results;
_this.completePrepare();
return _this;
}, false);
return this.junction.realize();
}
else {
this.ancestor = this.replicate();
this.ancestor.isAncestor = true;
return this;
}
};
BaseCell.prototype.completePrepare = function () {
this.prepared = true;
if (this.isRoot) {
this.enshell();
}
};
BaseCell.prototype.prepareChild = function (prepargs, handle, child, k) {
var mergekey = k === undefined ? false : k;
if (child instanceof BaseCell) {
var replica = child.replicate();
replica.setParent(this, k);
var prepared = replica.prepare(prepargs);
handle.merge(prepared, mergekey);
}
else {
handle.merge(child, mergekey);
}
};
BaseCell.prototype.setParent = function (parentCell, dereferent) {
this.ctx.path = parentCell.ctx.path.concat(dereferent);
this.parent = parentCell;
this.isRoot = false;
this.depth = this.parent.depth + 1;
};
BaseCell.prototype.replicate = function () {
if (this.prepared) {
return this.ancestor.replicate();
}
else {
var repl = this.constructCore(this.crown, this.form.consolidate(this.io, this.ctx));
if (this.isAncestor) {
repl.ancestor = this;
}
return repl;
}
};
BaseCell.prototype.getParent = function (toDepth) {
if (toDepth === void 0) { toDepth = 1; }
if (this.parent == undefined) {
throw new Error("parent not set, or exceeding getParent depth");
}
else if (toDepth == 1) {
return this.parent;
}
else {
return this.parent.getParent(toDepth - 1);
}
};
BaseCell.prototype.getRoot = function () {
return this.isRoot ? this : this.getParent().getRoot();
};
BaseCell.prototype.getNominal = function (label) {
if (this.ctx.label == label) {
return this;
}
else {
if (this.parent == undefined) {
throw new Error("Required context label " + label + " is not found");
}
else {
return this.parent.getNominal(label);
}
}
};
BaseCell.prototype.terminalScan = function (recursive, collection, locale) {
if (recursive === void 0) { recursive = false; }
if (collection === void 0) { collection = []; }
if (locale === void 0) { locale = null; }
var locale = locale || this;
Jungle.Util.typeCaseSplitF(function (thing, dereferent) {
if (thing instanceof Jungle.Terminal) {
collection.push({ node: locale, term: thing, deref: dereferent });
}
else if (recursive && thing instanceof BaseCell) {
thing.terminalScan(true, collection, locale = thing);
}
})(this.crown);
return collection;
};
BaseCell.prototype.checkComplete = function (recursive) {
if (recursive === void 0) { recursive = false; }
var result = true;
Jungle.Util.typeCaseSplitF(function (thing) {
if (thing instanceof Jungle.Terminal) {
result = false;
}
else if (recursive && thing instanceof BaseCell) {
thing.checkComplete(true);
}
})(this.crown);
return result;
};
BaseCell.prototype.bundle = function () {
function bundler(node) {
if (node instanceof BaseCell) {
var product = node.bundle();
return product;
}
else {
return node;
}
}
var recurrentCellBundle = Jungle.Util.typeCaseSplitF(bundler, bundler, null)(this.crown);
var product = {
node: recurrentCellBundle,
form: Jungle.deformulate(this),
state: this.ctx.extract()
};
return product;
};
BaseCell.prototype.enshell = function () {
this.io.enshell();
return this;
};
BaseCell.prototype.resolve = function (arg) {
return null;
};
return BaseCell;
}());
Jungle.BaseCell = BaseCell;
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var BaseForm = (function () {
function BaseForm(host) {
this.host = host;
}
BaseForm.prototype.parse = function (formObj) {
var ctxdeclare = formObj.x || "";
this.preparator = formObj.p || function (x) { };
this.preparator = formObj.p || function (x) { };
var contextprops = [];
var linkPropRegex = /^[a-zA-Z](?:\w*[a-zA-Z])?$/;
for (var k in formObj) {
if (Jungle.GForm.RFormProps.indexOf(k) > -1)
continue;
if (k.match(linkPropRegex)) {
contextprops.push({ key: k, type: Jungle.CTXPropertyTypes.NORMAL, value: formObj[k] });
}
else {
throw new Error("Invalid property for link context, use ports");
}
}
return { iospec: null, contextspec: { properties: contextprops, declaration: ctxdeclare } };
};
BaseForm.prototype.consolidate = function (io, ctx) {
return Jungle.Util.melder({
p: this.preparator,
d: this.depreparator,
x: ctx.declaration,
}, ctx.extract());
};
return BaseForm;
}());
Jungle.BaseForm = BaseForm;
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var IO;
(function (IO) {
IO.HALT = {};
Object.freeze(IO.HALT);
function halting(arg) {
return IO.HALT;
}
IO.halting = halting;
function passing(arg) {
return arg;
}
IO.passing = passing;
function defined(arg) {
return arg === undefined ? IO.HALT : arg;
}
IO.defined = defined;
function always(arg) {
return true;
}
IO.always = always;
function nothing(arg) {
return undefined;
}
IO.nothing = nothing;
function host(arg) {
return this.host;
}
IO.host = host;
var Orientation;
(function (Orientation) {
Orientation[Orientation["INPUT"] = 0] = "INPUT";
Orientation[Orientation["OUTPUT"] = 1] = "OUTPUT";
Orientation[Orientation["NEUTRAL"] = 2] = "NEUTRAL";
Orientation[Orientation["MIXED"] = 3] = "MIXED";
})(Orientation = IO.Orientation || (IO.Orientation = {}));
var DesignationTypes;
(function (DesignationTypes) {
DesignationTypes[DesignationTypes["ALL"] = 0] = "ALL";
DesignationTypes[DesignationTypes["MATCH"] = 1] = "MATCH";
DesignationTypes[DesignationTypes["REGEX"] = 2] = "REGEX";
DesignationTypes[DesignationTypes["FUNC"] = 3] = "FUNC";
})(DesignationTypes = IO.DesignationTypes || (IO.DesignationTypes = {}));
var BaseIO = (function () {
function BaseIO(host, iospec) {
this.host = host;
}
BaseIO.prototype.prepare = function (parg) {
};
BaseIO.prototype.dress = function (designation, coat) {
var designator = {
direction: Orientation.OUTPUT,
type: DesignationTypes.MATCH,
data: undefined,
};
if (typeof (designation) === 'string') {
if (designation === '*') {
designator.type = DesignationTypes.ALL;
}
else {
designator.type = DesignationTypes.REGEX;
designator.data = designation;
}
}
else {
throw new Error("Invalid Designator: string required");
}
this.shell.dress(designator, coat);
};
BaseIO.prototype.enshell = function () {
return this.shell;
};
BaseIO.prototype.extract = function () {
return {};
};
return BaseIO;
}());
IO.BaseIO = BaseIO;
})(IO = Jungle.IO || (Jungle.IO = {}));
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var IO;
(function (IO) {
var Port = (function () {
function Port(label) {
this.label = label;
this.shells = [];
}
Port.prototype.addShell = function (shell) {
this.shells.push(shell);
};
Port.prototype.designate = function (designator) {
switch (designator.type) {
case IO.DesignationTypes.ALL: {
return true;
}
case IO.DesignationTypes.REGEX: {
return this.label.match(designator.data);
}
case IO.DesignationTypes.FUNC: {
return designator.data(this);
}
case IO.DesignationTypes.MATCH: {
console.log("match designation label: ", this.label, ' designation', designator.data);
return this.label === designator.data;
}
default:
return false;
}
};
Port.prototype.dress = function (coat) {
this.prepareContext(coat.context);
this.prepareCallback(coat.callback);
};
Port.prototype.prepareCallback = function (callback) {
if (!(typeof (callback) == 'string' || typeof (callback) == 'function')) {
throw new Error('Callback must be method name or');
}
this.callback = callback;
};
Port.prototype.prepareContext = function (outputContext) {
if (typeof (outputContext) == 'function') {
this.callbackContext = new outputContext(this);
}
else if (outputContext instanceof Object) {
this.callbackContext = outputContext;
}
else {
throw Error("Invalid context fabrication, must be object or contructor");
}
};
Port.prototype.handle = function (input) {
if (this.callback) {
if (this.callbackContext) {
if (typeof (this.callback) == 'string') {
var method = this.callbackContext[this.callback];
if (method === undefined) {
throw new Error("method must be accessible in provided context");
}
method.call(this.callbackContext, input);
}
else {
this.callback.call(this.callbackContext, input);
}
}
else {
if (typeof (this.callback) == 'string') {
throw new Error("method name can only be given with context");
}
this.callback.call(null, input);
}
}
};
return Port;
}());
IO.Port = Port;
})(IO = Jungle.IO || (Jungle.IO = {}));
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var IO;
(function (IO) {
var BaseShell = (function () {
function BaseShell(ports) {
this.sinks = {};
this.sources = {};
for (var _i = 0, ports_1 = ports; _i < ports_1.length; _i++) {
var portSpec = ports_1[_i];
switch (portSpec.direction) {
case IO.Orientation.INPUT: {
this.sinks[portSpec.label] = new IO.Port(portSpec.label);
break;
}
case IO.Orientation.OUTPUT: {
this.sources[portSpec.label] = new IO.Port(portSpec.label);
break;
}
}
}
}
BaseShell.prototype.invert = function () {
var inversion = new BaseShell([]);
inversion.sinks = this.sources;
inversion.sources = this.sinks;
return inversion;
};
BaseShell.prototype.designate = function (designator) {
var scanDomain;
var designation = [];
switch (designator.direction) {
case IO.Orientation.NEUTRAL: {
return [];
}
case IO.Orientation.INPUT: {
scanDomain = Jungle.Util.flattenObject(this.sinks, 1);
break;
}
case IO.Orientation.OUTPUT: {
scanDomain = Jungle.Util.flattenObject(this.sources, 1);
break;
}
case IO.Orientation.MIXED: {
scanDomain = Jungle.Util.collapseValues(this.sources).concat(Jungle.Util.collapseValues(this.sinks));
break;
}
}
for (var portlabel in scanDomain) {
var port = scanDomain[portlabel];
if (port.designate(designator)) {
designation.push(port);
}
}
return designation;
};
BaseShell.prototype.dress = function (designator, coat) {
designator.direction = IO.Orientation.OUTPUT;
var designation = this.designate(designator);
for (var k in designation) {
var outport = designation[k];
outport.dress(coat);
}
};
return BaseShell;
}());
IO.BaseShell = BaseShell;
})(IO = Jungle.IO || (Jungle.IO = {}));
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var Inv;
(function (Inv) {
function retract(obj, arg) {
return arg;
}
Inv.retract = retract;
})(Inv = Jungle.Inv || (Jungle.Inv = {}));
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var Inv;
(function (Inv) {
function selectNone() {
return [];
}
Inv.selectNone = selectNone;
})(Inv = Jungle.Inv || (Jungle.Inv = {}));
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var Inv;
(function (Inv) {
function pass(x) {
return x;
}
Inv.pass = pass;
function abstain(x) {
}
Inv.abstain = abstain;
})(Inv = Jungle.Inv || (Jungle.Inv = {}));
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var LinkCell = (function (_super) {
__extends(LinkCell, _super);
function LinkCell(crown, formspec) {
return _super.call(this, crown, formspec) || this;
}
LinkCell.prototype.constructIO = function (iospec) {
return new Jungle.IO.LinkIO(this, iospec);
};
LinkCell.prototype.constructForm = function () {
return new Jungle.LinkForm(this);
};
LinkCell.prototype.prepareChild = function (prepargs, handle, child, k) {
if (child instanceof Jungle.BaseCell) {
var replica = child.replicate();
replica.setParent(this, k);
replica.prepare(prepargs);
var aftershell = new Jungle.Util.Junction().merge(replica, false).then(function (replica) {
replica.enshell();
return replica;
}, false);
handle.merge(aftershell, k);
}
else {
handle.merge(child, k);
}
};
LinkCell.prototype.resolve = function (resarg) {
_super.prototype.resolve.call(this, resarg);
};
return LinkCell;
}(Jungle.BaseCell));
Jungle.LinkCell = LinkCell;
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var LinkForm = (function (_super) {
__extends(LinkForm, _super);
function LinkForm() {
return _super !== null && _super.apply(this, arguments) || this;
}
LinkForm.prototype.parse = function (formObj) {
var ctxdeclare = formObj.x || "";
this.preparator = formObj.p || function (x) { };
var links = formObj.link || [];
var linkf = formObj.lf || function (a, b) { };
var ports = formObj.port || [];
var context = {};
var specialInHook;
var specialOutHook;
var portlabels = [];
var labels = {};
var contextprops = [];
var linkPortRegex = /^(_?)([a-zA-Z](?:\w*[a-zA-Z])?)(_?)$/;
for (var i = 0; i < ports.length; i++) {
var pmatch = ports[i].match(linkPortRegex);
if (pmatch) {
var inp = pmatch[1], label = pmatch[2], out = pmatch[3];
if (inp) {
portlabels.push({ label: label, direction: Jungle.IO.Orientation.INPUT });
}
if (out) {
portlabels.push({ label: label, direction: Jungle.IO.Orientation.OUTPUT });
}
}
}
var linkPropRegex = /^[a-zA-Z](?:\w*[a-zA-Z])?$/;
for (var k in formObj) {
if (Jungle.GForm.RFormProps.indexOf(k) > -1)
continue;
if (k.match(linkPropRegex)) {
contextprops.push({ key: k, type: Jungle.CTXPropertyTypes.NORMAL, value: formObj[k] });
}
else {
throw new Error("Invalid property for link context, use ports");
}
}
return { iospec: { ports: portlabels, links: links, linkFunciton: linkf }, contextspec: { properties: contextprops, declaration: ctxdeclare } };
};
return LinkForm;
}(Jungle.BaseForm));
Jungle.LinkForm = LinkForm;
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var IO;
(function (IO) {
var LINK_FILTERS;
(function (LINK_FILTERS) {
LINK_FILTERS[LINK_FILTERS["PROCEED"] = 0] = "PROCEED";
LINK_FILTERS[LINK_FILTERS["DECEED"] = 1] = "DECEED";
LINK_FILTERS[LINK_FILTERS["ELSEWHERE"] = 2] = "ELSEWHERE";
LINK_FILTERS[LINK_FILTERS["NONE"] = 3] = "NONE";
})(LINK_FILTERS || (LINK_FILTERS = {}));
var LinkIO = (function (_super) {
__extends(LinkIO, _super);
function LinkIO(host, spec) {
var _this = _super.call(this, host, spec) || this;
_this.spec = spec;
_this.linkmap = {};
_this.linker = spec.linkFunciton;
return _this;
}
LinkIO.prototype.enshell = function () {
this.shell = new IO.BaseShell(this.spec.ports);
this.lining = this.shell.invert();
this.innerDress();
this.applyLinks();
return;
};
;
LinkIO.prototype.innerDress = function () {
for (var k in this.host.crown) {
var v = this.host.crown[k];
var cellSources = v.io.shell.sources;
this.linkmap[k] = {};
for (var q in cellSources) {
var source = cellSources[q];
this.linkmap[k][q] = [];
source.callback = this.follow.bind(this, k, source);
}
}
this.linkmap['_'] = {};
for (var q in this.lining.sources) {
var source = this.lining.sources[q];
this.linkmap["_"][q] = [];
source.callback = this.follow.bind(this, '_', source);
}
};
LinkIO.prototype.applyLinks = function () {
for (var _i = 0, _a = this.spec.links; _i < _a.length; _i++) {
var link = _a[_i];
var linkir = this.parseLink(link);
this.interpretLink(linkir);
}
};
LinkIO.prototype.parseLink = function (link) {
var m = link.match(/(?:(\w+).)?(\w+)(\|?)(<?)([\+\-\!]?)([=\-])(>{1,2})(\|?)(?:(\w+).)?(\w+)/);
if (!m) {
throw new Error("Unable to parse link description, expression " + link + " did not match regex");
}
;
var match = m[0], srcCell = m[1], srcPort = m[2], srcClose = m[3], viceVersa = m[4], filter = m[5], matching = m[6], persistent = m[7], snkClose = m[8], snkCell = m[9], snkPort = m[10];
var srcDesig = { direction: IO.Orientation.OUTPUT, type: (srcPort == '*') ? IO.DesignationTypes.ALL : IO.DesignationTypes.MATCH, data: srcPort };
var snkDesig = { direction: IO.Orientation.INPUT, type: (snkPort == '*') ? IO.DesignationTypes.ALL : IO.DesignationTypes.MATCH, data: snkPort };
return {
sourceCell: srcCell,
sourcePort: srcDesig,
sinkCell: snkCell,
sinkPort: snkDesig,
closeSource: false,
closeSink: false,
persistent: false,
matching: false,
propogation: LINK_FILTERS.NONE
};
};
LinkIO.prototype.interpretLink = function (linkspec) {
var sourceShells = {};
var sourceLabels = [];
if (linkspec.sourceCell === "*") {
sourceShells = Jungle.Util.mapObject(this.host.crown, function (k, src) { sourceLabels.push(k); return src.io.shell; });
}
else if (linkspec.sourceCell in this.host.crown) {
sourceShells[linkspec.sourceCell] = this.host.crown[linkspec.sourceCell].io.shell;
sourceLabels = [linkspec.sourceCell];
}
else if (linkspec.sourceCell === '_') {
sourceShells['_'] = this.host.io.lining;
sourceLabels = ['_'];
}
var sinkShells = [];
if (linkspec.sinkCell === "*") {
sinkShells = Jungle.Util.flattenObject(this.host.crown, 1).map(function (x) { return x.io.shell; });
}
else if (linkspec.sinkCell in this.host.crown) {
sinkShells = [this.host.crown[linkspec.sinkCell].io.shell];
}
else if (linkspec.sinkCell === '_') {
sinkShells = [this.host.io.lining];
}
for (var _i = 0, sourceLabels_1 = sourceLabels; _i < sourceLabels_1.length; _i++) {
var sourceLb = sourceLabels_1[_i];
for (var _a = 0, sinkShells_1 = sinkShells; _a < sinkShells_1.length; _a++) {
var sinkSh = sinkShells_1[_a];
var sourcePorts = sourceShells[sourceLb].designate(linkspec.sourcePort);
for (var _b = 0, sourcePorts_1 = sourcePorts; _b < sourcePorts_1.length; _b++) {
var sourceP = sourcePorts_1[_b];
var sinkPorts = sinkSh.designate(linkspec.sinkPort);
for (var _c = 0, sinkPorts_1 = sinkPorts; _c < sinkPorts_1.length; _c++) {
var sinkP = sinkPorts_1[_c];
this.forgeLink(sourceLb, sourceP, sinkP);
}
}
}
}
};
LinkIO.prototype.forgeLink = function (sourceCell, sourcePort, sink, close) {
if (close === void 0) { close = false; }
this.linkmap[sourceCell][sourcePort.label].push(sink);
};
LinkIO.prototype.follow = function (sourceCell, source, throughput) {
var targeted = this.linkmap[sourceCell][source.label];
for (var _i = 0, targeted_1 = targeted; _i < targeted_1.length; _i++) {
var sink = targeted_1[_i];
console.log("Throughput of " + throughput + " from source " + source.label + " to " + sink.label);
sink.handle(throughput);
}
};
LinkIO.prototype.prepare = function (parg) {
};
;
LinkIO.prototype.extract = function () {
};
return LinkIO;
}(IO.BaseIO));
IO.LinkIO = LinkIO;
})(IO = Jungle.IO || (Jungle.IO = {}));
})(Jungle || (Jungle = {}));
(function () {
var root = this;
var define = define || undefined;
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = Jungle;
}
exports.Jungle = Jungle;
}
else if (typeof define !== 'undefined' && define.amd) {
define('Jungle', (function () { return root.Jungle = Jungle; })());
}
else {
root.Jungle = Jungle;
}
}).call(this);
var Jungle;
(function (Jungle) {
function isBundle(object) {
return object instanceof Object && "form" in object && "state" in object && "node" in object;
}
Jungle.isBundle = isBundle;
var ObjectFunctionCache = (function () {
function ObjectFunctionCache() {
this.functions = {};
}
ObjectFunctionCache.prototype.storeFunction = function (func) {
var name = (["", 'anonymous', undefined].indexOf(func.name) == -1) ? func.name : 'anonymous';
this.functions[name] = func;
return name;
};
ObjectFunctionCache.prototype.recoverFunction = function (id) {
return this.functions[id];
};
return ObjectFunctionCache;
}());
var liveCache = new ObjectFunctionCache();
function deformulate(fromCell) {
var rCell = fromCell;
var preform = {
r: rCell.form.resolver,
c: rCell.form.carrier,
x: rCell.ctx.declaration
};
var exForm = {};
for (var k in preform) {
var val = preform[k];
if (val instanceof Function) {
exForm[k] = liveCache.storeFunction(val);
}
else {
exForm[k] = val;
}
}
return exForm;
}
Jungle.deformulate = deformulate;
function reformulate(formRef) {
var recovered = {};
for (var k in formRef) {
recovered[k] = liveCache.recoverFunction(formRef[k]);
}
return recovered;
}
Jungle.reformulate = reformulate;
var Reconstruction = (function (_super) {
__extends(Reconstruction, _super);
function Reconstruction(bundle) {
var _this = this;
function debundle(bundle) {
if (isBundle(bundle)) {
return new Reconstruction(bundle);
}
else {
return bundle;
}
}
var node = Jungle.Util.typeCaseSplitF(debundle)(bundle.node);
var form = Jungle.reformulate(bundle.form);
var state = bundle.state;
_this = _super.call(this, node, Jungle.Util.melder(form, state)) || this;
return _this;
}
return Reconstruction;
}(Jungle.BaseCell));
Jungle.Reconstruction = Reconstruction;
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var ResolutionCell = (function (_super) {
__extends(ResolutionCell, _super);
function ResolutionCell() {
return _super !== null && _super.apply(this, arguments) || this;
}
ResolutionCell.prototype.constructForm = function () {
return new Jungle.GForm(this);
};
ResolutionCell.prototype.constructIO = function (iospec) {
return new Jungle.IO.ResolveIO(this, iospec);
};
ResolutionCell.prototype.constructCore = function (crown, form) {
return new ResolutionCell(crown, form);
};
ResolutionCell.prototype.resolveDenizen = function (handle, args, denizen, reference) {
var mergekey = reference === undefined ? false : reference;
if (denizen instanceof Jungle.BaseCell && denizen !== undefined) {
var denizenArg = args === undefined ? undefined : args[reference];
var resolved = denizen.resolve(denizenArg);
handle.merge(resolved, mergekey);
}
else {
handle.merge(denizen, mergekey);
}
};
ResolutionCell.prototype.resolveCell = function (handle, node, carriedArgs, selection) {
var cut = false;
if (!selection) {
cut = true;
}
else if (selection === true && node instanceof Object) {
selection = Object.keys(node);
}
var projectedCrown = this.crown;
var core = this;
var splitf = core.resolveDenizen.bind(core, handle, carriedArgs);
Jungle.Util.typeCaseSplitF(splitf)(projectedCrown);
};
ResolutionCell.prototype.resolve = function (resolveArgs) {
Object.freeze(resolveArgs);
if (!this.prepared) {
var pr = this.prepare();
}
if (this.io.isShellBase && !this.io.specialGate) {
var sInpHook = this.io.specialInput;
var sInpResult = sInpHook.tractor.call(this.ctx.exposed, resolveArgs);
var sResult;
if (sInpResult != Jungle.IO.HALT && (sInpHook.eager || sInpResult !== undefined)) {
this.io.specialGate = true;
sResult = this.resolve(sInpResult);
this.io.specialGate = false;
return sResult;
}
else {
return this.io.specialOutput.tractor.call(this.ctx.exposed, sInpResult);
}
}
else {
this.resolveCache = {
args: resolveArgs,
carried: null,
crowned: null,
selection: null,
reduced: null
};
this.junction
.then(this.resolveCarryThen.bind(this), false)
.then(this.resolveCrownThen.bind(this), false)
.then(this.resolveReduceThen.bind(this), false)
.then(this.resolveCompleteThen.bind(this), false);
return this.junction.realize();
}
};
ResolutionCell.prototype.resolveCarryThen = function (results, handle) {
this.ctx.exposed.handle = handle;
return this.form.carrier.call(this.ctx.exposed, this.resolveCache.args);
};
ResolutionCell.prototype.resolveCrownThen = function (results, handle) {
this.resolveCache.carried = results;
return this.resolveCell(handle, this.crown, this.resolveCache.carried, true);
};
ResolutionCell.prototype.resolveReduceThen = function (results, handle) {
this.resolveCache.crowned = results;
this.ctx.exposed.handle = handle;
return this.form.resolver.call(this.ctx.exposed, this.resolveCache.crowned, this.resolveCache.args, this.resolveCache.carried);
};
ResolutionCell.prototype.resolveCompleteThen = function (results, handle) {
this.resolveCache.reduced = results;
var dispached = this.io.dispatchResult(this.resolveCache.reduced);
return dispached;
};
return ResolutionCell;
}(Jungle.BaseCell));
Jungle.ResolutionCell = ResolutionCell;
})(Jungle || (Jungle = {}));
var Jungle;
(function (Jungle) {
var LabelTypes;
(function (LabelTypes) {
LabelTypes[LabelTypes["PASSIVE"] = 0] = "PASSIVE";
LabelTypes[LabelTypes["TRIG"] = 1] = "TRIG";
LabelTypes[LabelTypes["ENTRIG"] = 2] = "ENTRIG";
LabelTypes[LabelTypes["GATE"] = 3] = "GATE";
LabelTypes[LabelTypes["GATER"] = 4] = "GATER";
LabelTypes[LabelTypes["TRIGATE"] = 5] = "TRIGATE";
LabelTypes[LabelTypes["TRIGATER"] = 6] = "TRIGATER";
LabelTypes[LabelTypes["ENTRIGATE"] = 7] = "ENTRIGATE";
LabelTypes[LabelTypes["ENTRIGATER"] = 8] = "ENTRIGATER";
})(LabelTypes = Jungle.LabelTypes || (Jungle.LabelTypes = {}));
var TrigateLabelTypesMap = {
'': { '': LabelTypes.PASSIVE,