import { AwareTypes, } from './AwareTypes'; import { EpistemicTypes, } from '../epistemology/EpistemicTypes'; import { findAllGenerate, } from '../querying/findAllGenerate'; import { FindAwarenessArgs, IFindBaseArgs, } from '../querying/FindModelArgs'; import { IAwarenessRelation, } from './IAwarenessRelation'; import { IModel, } from '../models/IModel'; import { ISerializedAwarenessRelation, } from './ISerializedAwarenessRelation'; import { ModelType, } from '../models/ModelType'; import { OnticTypes, } from '../ontology/OnticTypes'; import { RelationBase, } from './RelationBase'; export class AwarenessRelation< Type extends EpistemicTypes & OnticTypes, Knowledge extends ModelType, > extends RelationBase implements IAwarenessRelation { protected readonly __modelType: Type; public get modelType() { return this.__modelType; } private __perceptions: readonly IModel< Type, OnticTypes, Knowledge >[] = Object.freeze([]); public get perceptions() { return this.__perceptions; } public readonly addPerception = ( perception: IModel, ) => void (this.__perceptions = this.__perceptions.concat([ perception ])); public readonly clone = ( self: IAwarenessRelation, ): IAwarenessRelation => { const copy = Object.assign( Object.create(Object.getPrototypeOf(self)), self, ); self.perceptions.forEach(copy.addPerception); return copy; }; public readonly destroy = ( self: IAwarenessRelation, ) => { this.tags.forEach(self.removeTag); this.perceptions.forEach(self.removePerception); ((self: any) => { delete self.__perceives; delete self.perceives; delete self.__tags; delete self.tags; })(this); }; public readonly find: ( args: string | IFindBaseArgs & FindAwarenessArgs, ) => IModel | null; public readonly findAll: ( args: '*' | IFindBaseArgs & FindAwarenessArgs, ) => readonly IModel[]; public readonly findAllGenerator = (( self: IAwarenessRelation, ) => function* ( args: '*' | IFindBaseArgs & FindAwarenessArgs, ): IterableIterator> { yield* findAllGenerate( self.perceptions, args, ); })(this); public readonly removePerception = ( { name }: IModel, ) => { const index = this.perceptions.findIndex((item) => item.name === name); if (index >= 0) { this.__perceptions = this.perceptions .slice(0, index) .concat(this.perceptions.slice(index + 1)); } }; public readonly serializeToObject = ( self: IAwarenessRelation, ): ISerializedAwarenessRelation => ({ modelType: this.modelType, perceptions: self.perceptions.map(({ name }) => name), tags: [ ...self.tags ], }); }