Skip to content

Commit 7aea345

Browse files
committed
remove code duplication
1 parent f796f58 commit 7aea345

2 files changed

Lines changed: 34 additions & 79 deletions

File tree

scripts/deduce-category-properties.ts

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -131,27 +131,30 @@ async function delete_deduced_category_properties(tx: Transaction, category_id:
131131
})
132132
}
133133

134-
async function deduce_satisfied_category_properties(
134+
async function get_decided_properties(
135135
tx: Transaction,
136136
category_id: string,
137-
implications: NormalizedCategoryImplication[],
138-
options: { check_conflicts: boolean } = { check_conflicts: true },
137+
value: boolean,
139138
) {
140-
const satisfied_res = await tx.execute({
139+
const res = await tx.execute({
141140
sql: `
142141
SELECT property_id
143142
FROM category_property_assignments
144-
WHERE
145-
category_id = ?
146-
AND is_satisfied = TRUE
147-
AND is_deduced = FALSE
143+
WHERE category_id = ? AND is_satisfied = ?
148144
`,
149-
args: [category_id],
145+
args: [category_id, value],
150146
})
151147

152-
const satisfied_props = new Set(
153-
satisfied_res.rows.map((row) => row.property_id) as string[],
154-
) as Set<string>
148+
return new Set(res.rows.map((row) => row.property_id) as string[])
149+
}
150+
151+
async function deduce_satisfied_category_properties(
152+
tx: Transaction,
153+
category_id: string,
154+
implications: NormalizedCategoryImplication[],
155+
options: { check_conflicts: boolean } = { check_conflicts: true },
156+
) {
157+
const satisfied_props = await get_decided_properties(tx, category_id, true)
155158

156159
const deduced_satisfied_props: string[] = []
157160
const reasons: Record<string, string> = {}
@@ -211,33 +214,8 @@ async function deduce_unsatisfied_category_properties(
211214
implications: NormalizedCategoryImplication[],
212215
options: { check_conflicts: boolean } = { check_conflicts: true },
213216
) {
214-
const satisfied_res = await tx.execute({
215-
sql: `
216-
SELECT property_id
217-
FROM category_property_assignments
218-
WHERE category_id = ? AND is_satisfied = TRUE
219-
`,
220-
args: [category_id],
221-
})
222-
223-
const satisfied_props = new Set(
224-
satisfied_res.rows.map((row) => row.property_id) as string[],
225-
)
226-
227-
const unsatisfied_res = await tx.execute({
228-
sql: `
229-
SELECT property_id
230-
FROM category_property_assignments
231-
WHERE
232-
category_id = ?
233-
AND is_satisfied = FALSE
234-
`,
235-
args: [category_id],
236-
})
237-
238-
const unsatisfied_props = new Set(
239-
unsatisfied_res.rows.map((row) => row.property_id) as string[],
240-
)
217+
const satisfied_props = await get_decided_properties(tx, category_id, true)
218+
const unsatisfied_props = await get_decided_properties(tx, category_id, false)
241219

242220
const deduced_unsatisfied_props: string[] = []
243221
const reasons: Record<string, string> = {}

scripts/deduce-functor-properties.ts

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -158,26 +158,29 @@ async function delete_deduced_functor_properties(tx: Transaction, functor: Funct
158158
})
159159
}
160160

161-
async function deduce_satisfied_functor_properties(
161+
async function get_decided_functor_properties(
162162
tx: Transaction,
163-
functor: FunctorMeta,
164-
implications: NormalizedFunctorImplication[],
163+
functor_id: string,
164+
value: boolean,
165165
) {
166-
const satisfied_res = await tx.execute({
166+
const res = await tx.execute({
167167
sql: `
168168
SELECT property_id
169169
FROM functor_property_assignments
170-
WHERE
171-
functor_id = ?
172-
AND is_satisfied = TRUE
173-
AND is_deduced = FALSE
170+
WHERE functor_id = ? AND is_satisfied = ?
174171
`,
175-
args: [functor.id],
172+
args: [functor_id, value],
176173
})
177174

178-
const satisfied_props = new Set(
179-
satisfied_res.rows.map((row) => row.property_id) as string[],
180-
) as Set<string>
175+
return new Set(res.rows.map((row) => row.property_id) as string[])
176+
}
177+
178+
async function deduce_satisfied_functor_properties(
179+
tx: Transaction,
180+
functor: FunctorMeta,
181+
implications: NormalizedFunctorImplication[],
182+
) {
183+
const satisfied_props = await get_decided_functor_properties(tx, functor.id, true)
181184

182185
const deduced_satisfied_props: string[] = []
183186
const reasons: Record<string, string> = {}
@@ -237,34 +240,8 @@ async function deduce_unsatisfied_functor_properties(
237240
functor: FunctorMeta,
238241
implications: NormalizedFunctorImplication[],
239242
) {
240-
const satisfied_res = await tx.execute({
241-
sql: `
242-
SELECT property_id
243-
FROM functor_property_assignments
244-
WHERE functor_id = ? AND is_satisfied = TRUE
245-
`,
246-
args: [functor.id],
247-
})
248-
249-
const satisfied_props = new Set(
250-
satisfied_res.rows.map((row) => row.property_id) as string[],
251-
)
252-
253-
const unsatisfied_res = await tx.execute({
254-
sql: `
255-
SELECT property_id
256-
FROM functor_property_assignments
257-
WHERE
258-
functor_id = ?
259-
AND is_satisfied = FALSE
260-
AND is_deduced = FALSE
261-
`,
262-
args: [functor.id],
263-
})
264-
265-
const unsatisfied_props = new Set(
266-
unsatisfied_res.rows.map((row) => row.property_id) as string[],
267-
)
243+
const satisfied_props = await get_decided_functor_properties(tx, functor.id, true)
244+
const unsatisfied_props = await get_decided_functor_properties(tx, functor.id, false)
268245

269246
const deduced_unsatisfied_props: string[] = []
270247
const reasons: Record<string, string> = {}

0 commit comments

Comments
 (0)