Skip to content

Commit

Permalink
Merge pull request #138 from Phuire-Research/UI
Browse files Browse the repository at this point in the history
UI
  • Loading branch information
REllEK-IO committed Nov 1, 2023
2 parents 7640d39 + 15fd674 commit 4ce3e8b
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "stratimux",
"license": "GPL-3.0",
"version": "0.0.60",
"version": "0.0.63",
"description": "Unified Turing Machine",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
3 changes: 2 additions & 1 deletion src/concepts/axium/qualities/addConceptsFromQue.quality.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Observable, Subject, Subscriber, catchError } from 'rxjs';
import { defaultMethodCreator } from '../../../model/concept';
import { defaultMethodCreator, qualityToString } from '../../../model/concept';
import { AxiumState } from '../axium.concept';
import { Action, ActionType, prepareActionCreator } from '../../../model/action';
import { createQuality } from '../../../model/concept';
Expand All @@ -22,6 +22,7 @@ function addConceptsFromQueReducer(state: AxiumState, action: Action) {
}
return caught;
}));
quality.toString = qualityToString(quality);
const methodSub = quality.method.subscribe((act: Action) => {
const action$ = state.action$ as Subject<Action>;
blockingMethodSubscription(action$, act);
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export {
isConceptLoaded,
areConceptsLoaded,
unifyConcepts,
forEachConcept
forEachConcept,
conceptToString,
conceptsToString
} from './model/concept';
export type {
Concept,
Expand Down
8 changes: 3 additions & 5 deletions src/model/axium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import {
} from 'rxjs';
import { Action, createCacheSemaphores } from './action';
import { strategyBegin } from './actionStrategy';
import { Concept, Concepts, Mode, forEachConcept } from './concept';
import { Concept, Concepts, Method, Mode, forEachConcept, qualityToString } from './concept';
import {
createAxiumConcept,
AxiumState,
initializationStrategy,
axiumName,
} from '../concepts/axium/axium.concept';
import { axiumClose } from '../concepts/axium/qualities/close.quality';
import {
axiumAppendActionListToDialog,
} from '../concepts/axium/qualities/appendActionListToDialog.quality';
Expand Down Expand Up @@ -82,16 +81,15 @@ export function createAxium(name: string, initialConcepts: Concept[], logging?:
forEachConcept(concepts, ((concept, semaphore) => {
concept.qualities.forEach(quality => {
if (quality.methodCreator) {
const [method, subject] = quality.methodCreator(axiumState.concepts$, semaphore);
quality.method = method;
quality.subject = subject;
[quality.method, quality.subject] = quality.methodCreator(axiumState.concepts$, semaphore);
quality.method.pipe(
catchError((err: unknown, caught: Observable<Action>) => {
if (axiumState.logging) {
console.error('METHOD ERROR', err);
}
return caught;
}));
quality.toString = qualityToString(quality);
const methodSub = quality.method.subscribe((action: Action) => {
blockingMethodSubscription(axiumState.action$, action);
}) as Subscriber<Action>;
Expand Down
58 changes: 58 additions & 0 deletions src/model/concept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type MethodCreator = (concept$?: UnifiedSubject, semaphore?: number) => [
export type Quality = {
actionType: ActionType;
reducer: Reducer;
toString: () => string;
methodCreator?: MethodCreator;
method?: Method;
subject?: Subject<Action>;
Expand Down Expand Up @@ -241,6 +242,7 @@ export function createQuality(
export function defaultReducer(state: unknown, _: Action) {
return state;
}
defaultReducer.toString = () => ('Default Reducer');

export const defaultMethodCreator: MethodCreator = () : [Method, Subject<Action>] => {
const defaultSubject = new Subject<Action>();
Expand All @@ -255,6 +257,8 @@ export const defaultMethodCreator: MethodCreator = () : [Method, Subject<Action>
};
}),
);

defaultMethod.toString = () => ('Default Method');
return [defaultMethod, defaultSubject];
};

Expand Down Expand Up @@ -296,3 +300,57 @@ export const forEachConcept = (concepts: Concepts, each: (concept: Concept, sema
each(concepts[index], index);
}
};

const stateToString = (state: Record<string, unknown>): string => {
let final = '{\n';
const keys = Object.keys(state);
for (const key of keys) {
let input = '';
try {
input += `${key}: ${JSON.stringify(state[key])},\n`;
} catch (err) {
input = `${key}: CIRCULAR,\n`;
}
final += input;
}
final += '}';
return final;
};

export const conceptToString = (concept: Concept): string => {
let output = '';
output += `{\nname: ${concept.name},`;
if (concept.unified.length > 0) {
output += `\nunified: ${concept.unified},`;
}
output += `\nqualities: [ ${concept.qualities.toString()}\n],`;
output += `\nstate: ${stateToString(concept.state)}, `;
if (concept.principles) {
output += `\nprinciples: [ ${concept.principles.map(p => p.toString()).join(',')} ]`;
}
if (concept.mode) {
output += `\nmode: [ ${concept.mode.map(m => m.toString()).join(',')} ]`;
}
if (concept.meta) {
output += `\nmeta: ${JSON.stringify(concept.meta)}`;
}
output += '\n}';
return output;
};

export const conceptsToString = (concepts: Concepts): string => {
const conceptStringArray: string[] = [];
forEachConcept(concepts, (concept) => {
conceptStringArray.push(conceptToString(concept));
});
conceptStringArray.push(']');
return '[\n' + conceptStringArray.join(',\n');
};

export const qualityToString = (quality: Quality) => () => {
const actionType = quality.actionType;
const r = quality.reducer.toString();
const reducer = r === 'Default Reducer' ? r : 'Reducer';
const method = quality.method?.toString();
return (`\n{\nactionType: ${actionType},\nreducer: ${reducer},\nmethod: ${method}\n}`);
};
14 changes: 13 additions & 1 deletion src/model/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Action = {
expiration: number;
axium?: string;
};
type Method = Observable<Action>;
type Method = Observable<Action> & {toString: () => string};

export const createMethod =
(method: (action: Action) => Action): [Method, Subject<Action>] => {
Expand All @@ -37,6 +37,7 @@ export const createMethod =
};
}),
);
defaultMethod.toString = () => ('Method');
return [defaultMethod, defaultSubject];
};
export const createMethodWithState =
Expand All @@ -61,6 +62,7 @@ export const createMethodWithState =
};
}),
);
defaultMethod.toString = () => ('Method with State');
return [defaultMethod, defaultSubject];
};
export const createAsyncMethod =
Expand All @@ -71,6 +73,7 @@ export const createAsyncMethod =
asyncMethod(controller, action);
})),
);
defaultMethod.toString = () => ('Async Method');
return [defaultMethod, defaultSubject];
};
export const createAsyncMethodWithState =
Expand All @@ -88,6 +91,7 @@ export const createAsyncMethodWithState =
asyncMethodWithState(controller, action, state);
})),
);
defaultMethod.toString = () => ('Async Method with State');
return [defaultMethod, defaultSubject];
};
export const createMethodDebounce =
Expand All @@ -112,6 +116,7 @@ export const createMethodDebounce =
}
}),
);
defaultMethod.toString = () => ('Debounce Method');
return [defaultMethod, defaultSubject];
};
export const createMethodDebounceWithState =
Expand Down Expand Up @@ -139,6 +144,7 @@ export const createMethodDebounceWithState =
}
}),
);
defaultMethod.toString = () => ('Debounce Method with State');
return [defaultMethod, defaultSubject];
};
export const createAsyncMethodDebounce =
Expand All @@ -152,6 +158,7 @@ export const createAsyncMethodDebounce =
});
}),
);
defaultMethod.toString = () => ('Async Debounce Method');
return [defaultMethod, defaultSubject];
};
export const createAsyncMethodDebounceWithState =
Expand All @@ -168,6 +175,7 @@ export const createAsyncMethodDebounceWithState =
});
})
);
defaultMethod.toString = () => ('Async Debounce Method with State');
return [defaultMethod, defaultSubject];
};
export const createMethodThrottle =
Expand All @@ -192,6 +200,7 @@ export const createMethodThrottle =
}
}),
);
defaultMethod.toString = () => ('Throttle Method');
return [defaultMethod, defaultSubject];
};
export const createMethodThrottleWithState =
Expand Down Expand Up @@ -219,6 +228,7 @@ export const createMethodThrottleWithState =
}
}),
);
defaultMethod.toString = () => ('Throttle Method with State');
return [defaultMethod, defaultSubject];
};
export const createAsyncMethodThrottle =
Expand All @@ -232,6 +242,7 @@ export const createAsyncMethodThrottle =
});
}),
);
defaultMethod.toString = () => ('Async Throttle Method');
return [defaultMethod, defaultSubject];
};
export const createAsyncMethodThrottleWithState =
Expand All @@ -248,5 +259,6 @@ export const createAsyncMethodThrottleWithState =
});
})
);
defaultMethod.toString = () => ('Async Throttle Method with State');
return [defaultMethod, defaultSubject];
};
19 changes: 19 additions & 0 deletions src/test/conceptToString.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createAxium } from '../model/axium';
import { strategyBegin } from '../model/actionStrategy';
import { selectState } from '../model/selector';
import { Counter, createCounterConcept, countingStrategy, counterName } from '../concepts/counter/counter.concept';
import { AxiumState } from '../concepts/axium/axium.concept';
import { countingTopic } from '../concepts/counter/strategies/counting.strategy';
import { conceptToString, conceptsToString } from '../model/concept';

test('Axium Counting Strategy Test', (done) => {
const axium = createAxium('axiumStrategyTest', [createCounterConcept()], true, true);
const sub = axium.subscribe(concepts => {
console.log('CONCEPTS:', conceptsToString(concepts));
expect(true).toBe(true);
sub.unsubscribe();
setTimeout(() => {
done();
}, 500);
});
});

0 comments on commit 4ce3e8b

Please sign in to comment.