From ffee492fecab8034b2c8ef7a5518c6254fbac28e Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Thu, 9 Dec 2021 18:50:46 +0100 Subject: [PATCH] Debug-dumping of skeleton structure --- packages/core/src/runtime/dataspace.ts | 7 ++- packages/core/src/runtime/skeleton.ts | 62 +++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/packages/core/src/runtime/dataspace.ts b/packages/core/src/runtime/dataspace.ts index 3ad551a..cb6ca7e 100644 --- a/packages/core/src/runtime/dataspace.ts +++ b/packages/core/src/runtime/dataspace.ts @@ -1,7 +1,7 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones -import { IdentityMap } from '@preserves/core'; +import { IdentityMap, stringify } from '@preserves/core'; import { Index } from './skeleton.js'; import { Assertion, Entity, Facet, Handle, LocalAction, Ref, Turn } from './actor.js'; import { fromObserve, Observe, toObserve } from '../gen/dataspace.js'; @@ -12,11 +12,13 @@ export class Dataspace implements Partial { readonly handleMap = new IdentityMap(); assert(v: Assertion, handle: Handle): void { + // console.log('+', stringify(v)); this.index.addAssertion(v); const o = toObserve(v); if (o !== void 0) { this.index.addObserver(o.pattern, o.observer); } + // this.index.dump(); this.handleMap.set(handle, [v, o]); } @@ -24,13 +26,16 @@ export class Dataspace implements Partial { const entry = this.handleMap.get(handle); if (entry === void 0) return; const [v, o] = entry; + // console.log('-', stringify(v)); if (o !== void 0) { this.index.removeObserver(o.pattern, o.observer); } this.index.removeAssertion(v); + // this.index.dump(); } message(v: Assertion): void { + // console.log('!', stringify(v)); this.index.deliverMessage(v); } } diff --git a/packages/core/src/runtime/skeleton.ts b/packages/core/src/runtime/skeleton.ts index 786827a..0ea2c53 100644 --- a/packages/core/src/runtime/skeleton.ts +++ b/packages/core/src/runtime/skeleton.ts @@ -1,7 +1,7 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones -import { Set, Dictionary, KeyedDictionary, IdentityMap } from '@preserves/core'; +import { Set, Dictionary, KeyedDictionary, IdentityMap, stringify, Value, embed } from '@preserves/core'; import { AnyValue, Assertion, Handle, Ref, Turn } from './actor.js'; import { Bag, ChangeDescription } from './bag.js'; import * as Stack from './stack.js'; @@ -16,6 +16,8 @@ enum EventType { const _nop = function() {}; +const INDENT = ' '; + export class Index { readonly allAssertions: Bag = new Bag(); readonly root: Node = new Node(new Continuation(new Set())); @@ -127,6 +129,25 @@ export class Index { this.root.modify(EventType.MESSAGE, v, _nop, leafCallback, (h, vs) => h.observers.forEach((_captureMap, observer) => Turn.active.message(observer, vs))); } + + dump() { + console.log('INDEX'); + // console.log('allAssertions:'); + // dumpBag(this.allAssertions, 4); + console.log('tree:'); + this.root.dump(INDENT); + console.log(); + } +} + +function dumpBag>(b: Bag, indent: string) { + for (const [v, count] of b.entries()) { + console.log(indent + stringify(v) + ' = ' + count); + } +} + +function dumpSet(s: Set, indent: string) { + s.forEach(v => console.log(indent + stringify(v))); } type Selector = [number, AnyValue]; @@ -224,6 +245,18 @@ class Node { walkNode(this, Stack.push([outerValue], Stack.empty())); } + + dump(indent: string) { + this.continuation.dump(indent); + this.edges.forEach((shapeMap, selector) => { + const [popCount, stepIndex] = selector; + console.log(indent + `popCount ${popCount} stepIndex ${stringify(stepIndex)} -->`); + for (const shape in shapeMap) { + console.log(indent + INDENT + stringify(shape)); + shapeMap[shape].dump(indent + INDENT + INDENT); + } + }); + } } class Continuation { @@ -233,6 +266,16 @@ class Continuation { constructor(cachedAssertions: Set) { this.cachedAssertions = cachedAssertions; } + + dump(indent: string) { + dumpSet(this.cachedAssertions, indent); + this.leafMap.forEach((valueMap, paths) => { + valueMap.forEach((leaf, values) => { + console.log(indent + `when ${stringify(paths)} == ${stringify(values)} -->`); + leaf.dump(indent + INDENT); + }); + }); + } } class Leaf { @@ -242,6 +285,13 @@ class Leaf { isEmpty(): boolean { return this.cachedAssertions.size === 0 && this.observerGroups.size === 0; } + + dump(indent: string) { + dumpSet(this.cachedAssertions, indent); + this.observerGroups.forEach((observerGroup, paths) => { + observerGroup.dump(paths, indent); + }); + } } class ObserverGroup { @@ -251,6 +301,16 @@ class ObserverGroup { constructor(cachedCaptures: Bag>) { this.cachedCaptures = cachedCaptures; } + + dump(paths: Path[], indent: string) { + dumpBag(this.cachedCaptures, indent); + this.observers.forEach((valueMap, observer) => { + console.log(indent + `${stringify(embed(observer))} projecting ${stringify(paths)}`); + valueMap.forEach((handle, values) => { + console.log(indent + INDENT + `captured ${stringify(values)} handle ${handle}`); + }); + }); + } } // Total by assumption that path is valid for v