From bc0441b6b52c47fcc1d6a031918d788665a2a66e Mon Sep 17 00:00:00 2001 From: Xie Yuheng Date: Sun, 7 Jul 2024 04:04:05 +0800 Subject: [PATCH] simple `assertSameContext` --- TODO.md | 4 +++- src/concept/assertSameContext.ts | 22 ++++++++++++++-------- src/concept/conceptJoin.ts | 5 +---- src/concept/conceptMeet.ts | 5 +---- src/lattice/conceptBelow.ts | 2 ++ 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/TODO.md b/TODO.md index 2e3ce63..e00e606 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,8 @@ # lattice -`lattice/conceptBelow` & `lattice/conceptAbove` +`lattice/conceptBelow` +`lattice/conceptAbove` + `LatticeLayout` `layoutLattice(context)` diff --git a/src/concept/assertSameContext.ts b/src/concept/assertSameContext.ts index d711224..8ec974c 100644 --- a/src/concept/assertSameContext.ts +++ b/src/concept/assertSameContext.ts @@ -3,20 +3,26 @@ import type { Concept } from "./Concept.js" export function assertSameContext( concepts: Array, - options: { - who: string - message: string - } & Record, + options: { who: string }, ): void { if (concepts.length < 2) return - const { who, message } = options + const { who } = options - const [first, ...rest] = concepts - const firstContext = first.context + const [firstConcept, ...rest] = concepts + const firstContext = firstConcept.context for (const concept of rest) { if (firstContext !== concept.context) { - log(options) + const message = "I expect the contexts of x and y to be the same." + + log({ + who, + message, + concepts, + first: firstConcept, + offendingConcept: concept, + }) + throw new Error(`[assertSameContext by ${who}] ${message}`) } } diff --git a/src/concept/conceptJoin.ts b/src/concept/conceptJoin.ts index e7df609..a55ca36 100644 --- a/src/concept/conceptJoin.ts +++ b/src/concept/conceptJoin.ts @@ -4,10 +4,7 @@ import type { Concept } from "./Concept.js" import { conceptFromAttributes } from "./conceptFromAttributes.js" export function conceptJoin(x: Concept, y: Concept): Concept { - assertSameContext([x, y], { - who: "conceptJoin", - message: "I expect the contexts of x and y to be the same.", - }) + assertSameContext([x, y], { who: "conceptJoin" }) return conceptFromAttributes(x.context, setIntersection(x.intent, y.intent)) } diff --git a/src/concept/conceptMeet.ts b/src/concept/conceptMeet.ts index 85a7601..d60a383 100644 --- a/src/concept/conceptMeet.ts +++ b/src/concept/conceptMeet.ts @@ -4,10 +4,7 @@ import type { Concept } from "./Concept.js" import { conceptFromEntities } from "./conceptFromEntities.js" export function conceptMeet(x: Concept, y: Concept): Concept { - assertSameContext([x, y], { - who: "conceptMeet", - message: "I expect the contexts of x and y to be the same.", - }) + assertSameContext([x, y], { who: "conceptMeet" }) return conceptFromEntities(x.context, setIntersection(x.extent, y.extent)) } diff --git a/src/lattice/conceptBelow.ts b/src/lattice/conceptBelow.ts index a74b7a5..b6001f6 100644 --- a/src/lattice/conceptBelow.ts +++ b/src/lattice/conceptBelow.ts @@ -1,5 +1,7 @@ +import { assertSameContext } from "../concept/assertSameContext.js" import type { Concept } from "../concept/Concept.js" export function conceptBelow(x: Concept, y: Concept): boolean { + assertSameContext([x, y], { who: "conceptBelow" }) return false }