Skip to content

Commit

Permalink
try to use checkConsistent
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Sep 5, 2024
1 parent 6503840 commit 5d7d15c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
`MergeConflict` 移至 `merge-conflict/` -- 正与 `nothing/` 相同

fix `on`MergeConflict`

# 4.4 Dependencies Improve Search

> https://github.com/cicada-lang/propagator/issues/4
Expand Down
3 changes: 2 additions & 1 deletion src/belief-system/checkConsistent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { log } from "../utils/log.js"
export function checkConsistent(value: any): void {
if (detectMergeConflict(value)) {
const who = "checkConsistent"
const message = "inconsistent value"
const message = "Inconsistent value"
log({ who, message, value })
clearScheduledPropagators()
// throw new Error(`[${who}] ${message}`)
}
}
1 change: 0 additions & 1 deletion src/belief/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./Belief.js"
export * from "./beliefCheckConsistent.js"
export * from "./beliefEqual.js"
export * from "./beliefMerge.js"
30 changes: 10 additions & 20 deletions src/cell/put.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
import { detectMergeConflict, merge } from "../merge/index.js"
import { merge } from "../merge/index.js"
import { schedule } from "../scheduler/index.js"
import { log } from "../utils/log.js"
import { type Cell } from "./Cell.js"

export function put<T>(cell: Cell<T>, increment?: any): void {
const newContent = merge(cell.content, increment)
if (newContent === cell.content) {
return
}

if (detectMergeConflict(newContent)) {
const message = "Ack! Inconsistency!"
log({
kind: "Error",
who: "put",
message,
increment,
oldContent: cell.content,
})
try {
const newContent = merge(cell.content, increment)
if (newContent === cell.content) {
return
}

throw new Error(`[put] ${message}`)
cell.content = newContent
schedule(cell.propagators)
} catch (error) {
console.error({ who: "put", error })
}

cell.content = newContent
schedule(cell.propagators)
}
8 changes: 5 additions & 3 deletions src/examples/barometer-belief-system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Cell, put } from "../cell/index.js"
import { Interval, intervalAlmostEqual, isInterval } from "../interval/index.js"
import { isNothing } from "../nothing/index.js"
import { run } from "../scheduler/index.js"
import { log } from "../utils/log.js"
import { fallDuration, similarTriangles } from "./barometer.js"

test("examples / barometer-belief-system", async () => {
Expand Down Expand Up @@ -412,9 +413,10 @@ test("examples / barometer-belief-system", async () => {
),
)

// log(buildingHeight.content)
put(buildingHeight, Belief(Interval(46, 50), ["pressure"]))

// put(buildingHeight, Belief(Interval(46, 50), ["pressure"]))
await run()

// await run()
log(buildingHeight.content)
log(beliefSystemQuery(buildingHeight.content))
})
12 changes: 8 additions & 4 deletions src/scheduler/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import type { Job } from "./Scheduler.js"
import { globalScheduler } from "./initializeScheduler.js"

export async function run(): Promise<void> {
while (globalScheduler.jobs.length > 0) {
// 我们用「先进先出」的方式来处理 job 的 queue。
const job = globalScheduler.jobs.shift() as Job
await job.propagator()
try {
while (globalScheduler.jobs.length > 0) {
// 我们用「先进先出」的方式来处理 job 的 queue。
const job = globalScheduler.jobs.shift() as Job
await job.propagator()
}
} catch (error) {
console.error({ who: "run", error })
}
}

0 comments on commit 5d7d15c

Please sign in to comment.