Skip to content

Commit d4115b4

Browse files
committed
assertSameContext -- take options with who and message
1 parent 0de4291 commit d4115b4

File tree

9 files changed

+53
-21
lines changed

9 files changed

+53
-21
lines changed

TODO.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
# lattice
22

3-
实现 concept 之间的序关系
4-
5-
- lessGeneral
6-
- moreGeneral
7-
8-
`buildLattice(context)`
3+
`lattice/conceptBelow` & `lattice/conceptAbove`
4+
`LatticeLayout`
5+
`layoutLattice(context)`
96

107
# optimize
118

src/concept/assertSameContext.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { log } from "../utils/log.js"
2+
import type { Concept } from "./Concept.js"
3+
4+
export function assertSameContext(
5+
concepts: Array<Concept>,
6+
options: {
7+
who: string
8+
message: string
9+
} & Record<string, any>,
10+
): void {
11+
if (concepts.length < 2) return
12+
13+
const { who, message } = options
14+
15+
const [first, ...rest] = concepts
16+
const firstContext = first.context
17+
for (const concept of rest) {
18+
if (firstContext !== concept.context) {
19+
log(options)
20+
throw new Error(`[assertSameContext by ${who}] ${message}`)
21+
}
22+
}
23+
}

src/concept/conceptJoin.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { log } from "../utils/log.js"
21
import { setIntersection } from "../utils/Set.js"
2+
import { assertSameContext } from "./assertSameContext.js"
33
import type { Concept } from "./Concept.js"
44
import { conceptFromAttributes } from "./conceptFromAttributes.js"
55

66
export function conceptJoin(x: Concept, y: Concept): Concept {
7-
if (x.context !== y.context) {
8-
const who = "conceptJoin"
9-
const message = "I expect the contexts of x and y to be the same."
10-
log({ who, x, y })
11-
throw new Error(`[${who}] ${message}`)
12-
}
7+
assertSameContext([x, y], {
8+
who: "conceptJoin",
9+
message: "I expect the contexts of x and y to be the same.",
10+
})
1311

1412
return conceptFromAttributes(x.context, setIntersection(x.intent, y.intent))
1513
}

src/concept/conceptMeet.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { log } from "../utils/log.js"
21
import { setIntersection } from "../utils/Set.js"
2+
import { assertSameContext } from "./assertSameContext.js"
33
import type { Concept } from "./Concept.js"
44
import { conceptFromEntities } from "./conceptFromEntities.js"
55

66
export function conceptMeet(x: Concept, y: Concept): Concept {
7-
if (x.context !== y.context) {
8-
const who = "conceptMeet"
9-
const message = "I expect the contexts of x and y to be the same."
10-
log({ who, x, y })
11-
throw new Error(`[${who}] ${message}`)
12-
}
7+
assertSameContext([x, y], {
8+
who: "conceptMeet",
9+
message: "I expect the contexts of x and y to be the same.",
10+
})
1311

1412
return conceptFromEntities(x.context, setIntersection(x.extent, y.extent))
1513
}

src/concept/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from "./assertSameContext.js"
12
export * from "./closureAttributes.js"
23
export * from "./closureEntities.js"
34
export * from "./Concept.js"

src/lattice/conceptAbove.ts

Whitespace-only changes.

src/lattice/conceptBelow.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { Concept } from "../concept/Concept.js"
2+
3+
export function conceptBelow(x: Concept, y: Concept): boolean {
4+
return false
5+
}

src/lattice/generateConcepts.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@ import assert from "node:assert"
22
import test from "node:test"
33
import { createContextFromCrossTable } from "../context/createContextFromCrossTable.js"
44
import { planets } from "../examples/planets.js"
5+
import { waterbodies } from "../examples/waterbodies.js"
56
import { generateConcepts } from "./generateConcepts.js"
67

7-
test("generateConcepts", () => {
8+
test("generateConcepts -- planets", () => {
89
const context = createContextFromCrossTable(planets)
910
const concepts = generateConcepts(context)
1011

1112
assert.strictEqual(concepts.length, 12)
1213
})
14+
15+
test("generateConcepts -- waterbodies", () => {
16+
const context = createContextFromCrossTable(waterbodies)
17+
const concepts = generateConcepts(context)
18+
19+
assert.strictEqual(concepts.length, 13)
20+
})

src/lattice/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
export * from "./conceptAbove.js"
2+
export * from "./conceptBelow.js"
13
export * from "./generateConcepts.js"

0 commit comments

Comments
 (0)