555
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. var utils = require("../bbhverse-pb-wip/utils")
  2. var js = utils.js;
  3. function asyncDynamicTaskStream(opts){
  4. const design = {
  5. create(){
  6. var conceptspace = 0; // 32 bit to start with.
  7. var next = 1;
  8. var all = 0;
  9. var segments = [];
  10. var __concepts = {};
  11. var __entagledRefs = new WeakMap();
  12. function assert(concept, multi){ return __concepts[concept].spin ? !(concept.id & multi) : (concept.id & multi) }
  13. function define_exclusions(exclusions, tangle){
  14. // Note : Do not store references to concept.entanglement as this is switched on update.
  15. if(exclusions.length < 2) throw 'Why would we call define_exclusions on a single or no item ? At the least 2 items are needed for mutual exclusion.'
  16. var results = [];
  17. // if any 2 concepts were setup as independent concpts they can later be entangled as mutual exclusion here.
  18. var entanglement = { unsync : 0 }; // Set of entagled bits including self.
  19. var entaglements = [];
  20. var maskForSet = 0;
  21. __entagledRefs.set(entanglement, entaglements);
  22. var __entangledFn = (exclusion)=>{ // entaglement object has been fixed for all => collapse all previous.
  23. entanglement.unsync |= exclusion.entanglement.unsync | exclusion.id ;
  24. var collapsibles = __entagledRefs.get(exclusion.entanglement);
  25. if(collapsibles) { collapsibles.forEach(collapsible => { collapsible.entanglement = entanglement; }) }
  26. __entagledRefs.delete(exclusion.entanglement);
  27. exclusion.entanglement = entanglement;
  28. entaglements.push(exclusion);
  29. }
  30. var entangledFn = (exclusion)=>{ // entaglement object has not yet been fixed => replace.
  31. exclusion.entanglement.unsync |= entanglement.unsync | exclusion.id ;
  32. var collapsibles = __entagledRefs.get(entanglement);
  33. if(collapsibles){ collapsibles.forEach(collapsible => { collapsible.entanglement = exclusion.entanglement; }) }
  34. __entagledRefs.delete(entanglement);
  35. entanglement = exclusion.entanglement;
  36. entaglements = __entagledRefs.get(entanglement);
  37. entangledFn = __entangledFn;
  38. }
  39. var __tangle = Object.assign({}, tangle);
  40. // __tangle.unsync = __tangle.unsync & ~__tangle.insync;
  41. // __tangle.insync |= __concept.results[0].id;
  42. for(var i = 0; i < exclusions.length; i++) {
  43. var innerexclusions = __concepts[exclusions[i]] ? __concepts[exclusions[i]] : define(exclusions[i], __tangle);
  44. __tangle.unsync |= innerexclusions.results[0].id //| innerexclusions.results[0].entanglement.insync;
  45. innerexclusions.results.forEach(exclusion=>{
  46. (exclusion.entanglement) ? entangledFn(exclusion) // previously entangled.
  47. : (()=>{
  48. entanglement.unsync |= exclusion.id;
  49. exclusion.entanglement = entanglement;
  50. entaglements.push(exclusion)
  51. })()
  52. results.push(exclusion)
  53. maskForSet |= exclusion.id;
  54. });
  55. }
  56. return { results : results, maskForSet : maskForSet} ;
  57. }
  58. function define_string(concept, tangle) {
  59. all |= next;
  60. __concepts[concept] = { id : next, entanglement : Object.assign({}, tangle) }; // self is included in entanglement.
  61. __entagledRefs.set(__concepts[concept].entanglement, [__concepts[concept]] )
  62. next = next << 1;
  63. return { results : [__concepts[concept]], maskForSet : __concepts[concept].id };
  64. }
  65. function define_array(concepts, tangle) {
  66. if(concepts.length > 1) return define_exclusions(concepts, tangle);
  67. else return define(concepts[0], tangle)
  68. }
  69. function define_object(concepts, tangle){
  70. var results = [];
  71. var maskForSet = 0;
  72. tangle = tangle || {};
  73. Object.keys(concepts).forEach(concept => {
  74. var __concept = define(concept, tangle);
  75. results.push.apply(results, __concept.results)
  76. var __tangle = Object.assign({}, __concept.results[0].entanglement);
  77. __tangle.insync |= __concept.results[0].id | tangle.insync;
  78. __tangle.unsync |= tangle.unsync;
  79. var isconcept = define( concepts[concept][0], __tangle )
  80. __tangle = Object.assign({}, __concept.results[0].entanglement);
  81. __tangle.unsync |= tangle.unsync | __concept.results[0].id;
  82. var notconcept = define( concepts[concept][1], __tangle )
  83. if(notconcept && notconcept.results) {
  84. __concept.results[0].entanglement.unsync |= notconcept.maskForSet;
  85. maskForSet |= __concept.maskForSet | notconcept.maskForSet ;
  86. }
  87. if(isconcept && isconcept.results) {
  88. __concept.results[0].entanglement.insync |= isconcept.maskForSet;
  89. maskForSet |= __concept.maskForSet | isconcept.maskForSet ;
  90. }
  91. });
  92. results[0].entanglement.insync = maskForSet;
  93. return { results : results, maskForSet : maskForSet };
  94. }
  95. const conceptvariants = Object.assign(utils.__TPL__typeHandlers, {
  96. '[object String]' : define_string
  97. , '[object Array]' : define_array
  98. , '[object Object]' : define_object
  99. , '[object Undefined]' : ()=>{} // Ignore
  100. })
  101. function define(concept, tangle){
  102. return conceptvariants[Object.prototype.toString.call(concept)](concept, tangle);
  103. }
  104. function reveal(){ console.dir(__concepts) }
  105. return { define, reveal }
  106. }
  107. }
  108. const coexstates = {
  109. create(){
  110. var conceptspace = 0; // 32 bit to start with.
  111. var next = 1;
  112. var all = 0;
  113. var segments = [];
  114. var __concepts = {};
  115. var __entagledRefs = new WeakMap();
  116. var __tango = { // JS : Need a hash with integer keys for performance
  117. // Eg:
  118. // number matches key as a mask
  119. // all bits in assertions bits get turned on and negations are turned off.
  120. // number doesn't match key as a mask
  121. // independents ? or
  122. // all bits in assertions bits get turned off and negations are turned on.
  123. // 0b1011110 : [
  124. // 0b1010000 // assertions : bits that are in sync.
  125. // , 0b0001110 // negations : bits that are off sync.
  126. // ]
  127. }
  128. // Tango Entaglement internal model Specs - These are permissable constraints for 2^n bit variations.
  129. // Key = any concept bit
  130. var t = { 0b00100000 /* a concept bit */ : [
  131. /* concept bit is on - entangled coexistence with the state of concept bit.. */
  132. [
  133. // all these bits must be turned off i.e these bits cannot co exist when the primary bit is turned on.
  134. /* cannot coexit */ 0b00100110 // index 0 => 0 bits in this mask can coexit except for one bit which is the main concept bit which is included in this mask.
  135. // Group constraints are not relevant here so not an array.
  136. , /* any one bit within a group can coexit */ [ 0b00101001 /*, ...othergroups Eg: 0b11000000*/ ]
  137. // each item in the array represents a constrained group of bits.
  138. // index 1 => 1 bit in this mask can coexit with the main concept bit which is included in this mask.
  139. // /*any n bits can coexit */ // index n => n bits in this mask can coexit with the main concept bit which is included in this mask.
  140. // index i=1 to n each have 2^i variations each. At this level we have abstract nondeterministic view until all the bits computation results fix and reduce the result domain.
  141. // Until such time they remain potential possibilities and which specific bits are on is ambiguious.
  142. // all these bits must be turned on.
  143. , /*all these bits have to exist with this state (must coexit)*/ 0b00110000
  144. // can be collapsed to 1 single group.
  145. ]
  146. // independent bits where there is no entanglement are not stored. There can be an infinite or a large number of independent bits.
  147. , /* concept bit is off */
  148. [
  149. 0 // all these bits must be turned off.
  150. // one or more permissible
  151. , [0] // Index 1 - any one is permissible from the none can coexit category since main bit is off.
  152. // /*index (1 -> n) =>any n bits may be turned on in opposition */
  153. //all these bits must be turned on.
  154. , /*all these bits have to exist with this state in opposition*/ 0b11000000
  155. ]
  156. ]
  157. }
  158. (()=>{
  159. var cannotCoexist = 0;
  160. for(var i = 1; i < t[0b00100000][0].length; i++) { cannotCoexist |= t[0b00100000][0][i] }
  161. t[0b00100000][1][0] |= cannotCoexist;
  162. })()
  163. (()=>{
  164. var cannotCoexist = 0;
  165. for(var i = 1; i < t[0b00100000][1].length; i++) { cannotCoexist |= t[0b00100000][1][i] }
  166. t[0b00100000][0][0] |= cannotCoexist;
  167. })()
  168. t[0b00100000][1][1] |= t[0b00100000][0][0]
  169. function define_exclusions(exclusions, tangle){
  170. // Note : Do not store references to concept.entanglement as this is switched on update.
  171. if(exclusions.length < 2) throw 'Why would we call define_exclusions on a single or no item ? At the least 2 items are needed for mutual exclusion.'
  172. var results = [];
  173. // if any 2 concepts were setup as independent concpts they can later be entangled as mutual exclusion here.
  174. var entanglement = { unsync : 0 }; // Set of entagled bits including self.
  175. var entaglements = [];
  176. var maskForSet = 0;
  177. __entagledRefs.set(entanglement, entaglements);
  178. var __entangledFn = (exclusion)=>{ // entaglement object has been fixed for all => collapse all previous.
  179. entanglement.unsync |= exclusion.entanglement.unsync | exclusion.id ;
  180. var collapsibles = __entagledRefs.get(exclusion.entanglement);
  181. if(collapsibles) { collapsibles.forEach(collapsible => { collapsible.entanglement = entanglement; }) }
  182. __entagledRefs.delete(exclusion.entanglement);
  183. exclusion.entanglement = entanglement;
  184. entaglements.push(exclusion);
  185. }
  186. var entangledFn = (exclusion)=>{ // entaglement object has not yet been fixed => replace.
  187. exclusion.entanglement.unsync |= entanglement.unsync | exclusion.id ;
  188. var collapsibles = __entagledRefs.get(entanglement);
  189. if(collapsibles){ collapsibles.forEach(collapsible => { collapsible.entanglement = exclusion.entanglement; }) }
  190. __entagledRefs.delete(entanglement);
  191. entanglement = exclusion.entanglement;
  192. entaglements = __entagledRefs.get(entanglement);
  193. entangledFn = __entangledFn;
  194. }
  195. var __tangle = Object.assign({}, tangle);
  196. // __tangle.unsync = __tangle.unsync & ~__tangle.insync;
  197. // __tangle.insync |= __concept.results[0].id;
  198. for(var i = 0; i < exclusions.length; i++) {
  199. var innerexclusions = __concepts[exclusions[i]] ? __concepts[exclusions[i]] : define(exclusions[i], __tangle);
  200. __tangle.unsync |= innerexclusions.results[0].id //| innerexclusions.results[0].entanglement.insync;
  201. innerexclusions.results.forEach(exclusion=>{
  202. (exclusion.entanglement) ? entangledFn(exclusion) // previously entangled.
  203. : (()=>{
  204. entanglement.unsync |= exclusion.id;
  205. exclusion.entanglement = entanglement;
  206. entaglements.push(exclusion)
  207. })()
  208. results.push(exclusion)
  209. maskForSet |= exclusion.id;
  210. });
  211. }
  212. return { results : results, maskForSet : maskForSet} ;
  213. }
  214. function define_string(concept, containing) {
  215. all |= next;
  216. __concepts[concept] = { id : next, ex : 0, in : 0, entanglement : Object.assign({}, tangle) }; // self is included in entanglement.
  217. next = next << 1;
  218. return { results : [__concepts[concept]], maskForSet : __concepts[concept].id };
  219. }
  220. function define_array(concepts, tangle) {
  221. if(concepts.length > 1) return define_exclusions(concepts, tangle);
  222. else return define(concepts[0], tangle)
  223. }
  224. function define_object(concepts, tangle){
  225. var results = [];
  226. var maskForSet = 0;
  227. tangle = tangle || {};
  228. Object.keys(concepts).forEach(concept => {
  229. var __concept = define(concept, tangle);
  230. results.push.apply(results, __concept.results)
  231. var __tangle = Object.assign({}, __concept.results[0].entanglement);
  232. __tangle.insync |= __concept.results[0].id | tangle.insync;
  233. __tangle.unsync |= tangle.unsync;
  234. var isconcept = define( concepts[concept][0], __tangle )
  235. __tangle = Object.assign({}, __concept.results[0].entanglement);
  236. __tangle.unsync |= tangle.unsync | __concept.results[0].id;
  237. var notconcept = define( concepts[concept][1], __tangle )
  238. if(notconcept && notconcept.results) {
  239. __concept.results[0].entanglement.unsync |= notconcept.maskForSet;
  240. maskForSet |= __concept.maskForSet | notconcept.maskForSet ;
  241. }
  242. if(isconcept && isconcept.results) {
  243. __concept.results[0].entanglement.insync |= isconcept.maskForSet;
  244. maskForSet |= __concept.maskForSet | isconcept.maskForSet ;
  245. }
  246. });
  247. results[0].entanglement.insync = maskForSet;
  248. return { results : results, maskForSet : maskForSet };
  249. }
  250. const conceptvariants = Object.assign(utils.__TPL__typeHandlers, {
  251. '[object String]' : define_string
  252. , '[object Array]' : define_array
  253. , '[object Object]' : define_object
  254. , '[object Undefined]' : ()=>{} // Ignore
  255. })
  256. function define(concept, tangle){
  257. return conceptvariants[Object.prototype.toString.call(concept)](concept, tangle);
  258. }
  259. function reveal(){ console.dir(__concepts) }
  260. return { define, reveal }
  261. }
  262. }
  263. // 'created' is implied by existence.
  264. var states = {
  265. 'running' : [
  266. /* assertions */ [ ]
  267. , /* negations = not running */ [ {
  268. 'unstarted' : [[ /* unstarted */]
  269. , /* not unstarted = started at some point */ [
  270. 'paused' // can be resumed from interrupted (=incomplete or in progress)
  271. // , sleeping -> ran a little and is now paused same as parent state.
  272. // , waiting on other things
  273. // ... sleeping, interrupted ...
  274. , { 'stopped' : [[ // unlike paused cannot be resumed can be restarted i.e rerun from the beginning.
  275. 'succeded' // - completed -> process completed
  276. , 'failed' // - Either processing failed or prematurely exited with exception -> with an error. Non exception cases may have run to completion and failed.
  277. , 'aborted' // - terminated -> was not allowd to completed. Cannot say succeded or failed.
  278. // incomplete result ( neither success nor failure ) was stopped and abandoned befeore it could run to completion.
  279. ], [] ]}
  280. ]
  281. ]} ]
  282. ]
  283. // unassociated independent co-existence to 'running'. Sibling level in an object.
  284. }
  285. var stateFactory = design.create();
  286. var x = stateFactory.define(states);
  287. stateFactory.reveal();
  288. // console.dir(JSON.stringify(x));
  289. }
  290. module.exports = asyncDynamicTaskStream;
  291. asyncDynamicTaskStream();