Skip to content

Commit 0fa9ab8

Browse files
committed
Edge validation now produces a set of invalid facts
1 parent 5703700 commit 0fa9ab8

4 files changed

Lines changed: 33 additions & 36 deletions

File tree

solves/base-template/src/runSolver/index.ts.ejs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function mergeIndexLists(a: number[], b: number[]): number[] {
2020
interface GeneratedClingoFacts {
2121
clingoFacts: string[];
2222
invalidConstants: number[];
23+
invalidFacts: Set<string>;
2324
}
2425

2526
function generateClingoFacts(
@@ -31,23 +32,27 @@ function generateClingoFacts(
3132
console.assert(name.match(reConstant));
3233
const clingoFacts: string[] = [];
3334
const invalidPairs: number[] = [];
35+
const invalidFacts: Set<string> = new Set();
3436
for (const [i, part] of constantTuples.entries()) {
3537
const substrs: string[] = part.split(",").map(s => s.trim());
3638

3739
const isCorrectTupleSize = (substrs.length === expectedTupleSize);
3840
const allConstantsAreCorrectSyntax = substrs.every(x => x.match(reConstant));
3941

42+
const clingFact = lineGenFn(name, substrs.join(","));
4043
if (isCorrectTupleSize && allConstantsAreCorrectSyntax) {
41-
clingoFacts.push(lineGenFn(name, substrs.join(",")));
44+
clingoFacts.push(clingFact);
4245
} else {
4346
// Placeholder string added to preserve index correspondence
4447
clingoFacts.push("");
4548
invalidPairs.push(i);
49+
invalidFacts.add(clingFact);
4650
}
4751
}
4852
return {
4953
clingoFacts: clingoFacts,
5054
invalidConstants: invalidPairs,
55+
invalidFacts: invalidFacts,
5156
};
5257
}
5358

@@ -78,13 +83,12 @@ function generateInstanceDef(
7883
}
7984

8085
async function getInvalidEdgeFacts(
81-
requiredFacts: string,
86+
problemInstance: string,
8287
edgeFacts: string[],
8388
): Promise<number[]> {
8489
// Use Clingo to get all invalid facts
8590
const fullQuery = [
86-
requiredFacts,
87-
edgeFacts.join("\n"),
91+
problemInstance,
8892
validationSpecStr,
8993
].join("\n\n");
9094
const result = await runClingo(fullQuery);
@@ -140,53 +144,47 @@ export interface SolverResult {
140144

141145
export async function runSolver(params: SolverParameters): Promise<SolverResult> {
142146
console.log("Running 'runSolver()'.");
143-
<%-
144-
Object.entries(inputBase).map(([k, v]) => {
145-
const x = {};
146-
x.id = k;
147-
x.tupleSize = v.parameters;
148-
return ht.runSolverStep1a(x);
149-
}).join("")
150-
-%>
151-
<%-
152-
Object.entries(inputConstrained).map(([k, v]) => {
153-
const x = {};
154-
x.id = k;
155-
x.tupleSize = v.parameters;
156-
return ht.runSolverStep1b(x);
157-
}).join("")
158-
-%>
159147

160-
const baseFacts = [
148+
const facts: {[K in string]: GeneratedClingoFacts} = {
161149
<%-
162150
Object.entries(inputBase).map(([k, v]) => {
163151
const x = {};
164152
x.id = k;
165-
return ht.runSolverStep2(x);
153+
x.tupleSize = v.parameters;
154+
return ht.runSolverStep1a(x);
166155
}).join("")
167156
-%>
168-
].join("\n\n");
157+
<%-
158+
Object.entries(inputConstrained).map(([k, v]) => {
159+
const x = {};
160+
x.id = k;
161+
x.tupleSize = v.parameters;
162+
return ht.runSolverStep1b(x);
163+
}).join("")
164+
-%>
165+
};
166+
167+
const problemInstance: string = Object.values(facts)
168+
.map(x => x.clingoFacts.join("\n"))
169+
.join("\n");
169170

170171
const invalidEdgeFactIndices: number[] = await getInvalidEdgeFacts(
171-
baseFacts,
172-
edgeFacts.clingoFacts,
172+
problemInstance,
173+
(facts["edge"] as GeneratedClingoFacts).clingoFacts, // TODO: Remove assert
173174
);
174175

175176
const totalInvalidEdgeFacts: number[] = mergeIndexLists(
176-
edgeFacts.invalidConstants,
177+
(facts["edge"] as GeneratedClingoFacts).invalidConstants, // TODO: Remove assert
177178
invalidEdgeFactIndices,
178179
);
179180

180181
const resultObj: null | ClingoResult = await (async()=>{
181-
const inputsValid = (colourFacts.invalidConstants.length == 0)
182-
&& (vertexFacts.invalidConstants.length == 0)
183-
&& (edgeFacts.invalidConstants.length == 0)
182+
const inputsValid = Object.values(facts).every(x => (x.invalidFacts.size === 0))
184183
&& (invalidEdgeFactIndices.length === 0);
185184

186185
if (inputsValid) {
187186
const fullQuery = [
188-
baseFacts,
189-
edgeFacts.clingoFacts.join("\n"),
187+
problemInstance,
190188
logicSpecStr,
191189
].join("\n\n");
192190
return runClingo(fullQuery);
@@ -198,8 +196,8 @@ export async function runSolver(params: SolverParameters): Promise<SolverResult>
198196
return {
199197
resultObj: resultObj,
200198
invalidInputs: {
201-
colour: colourFacts.invalidConstants,
202-
vertex: vertexFacts.invalidConstants,
199+
colour: (facts["colour"] as GeneratedClingoFacts).invalidConstants, // TODO: Remove assert
200+
vertex: (facts["vertex"] as GeneratedClingoFacts).invalidConstants, // TODO: Remove assert
203201
edge: totalInvalidEdgeFacts,
204202
},
205203
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
const <%- id %>Facts = generateBaseDef(params.<%- id %>, "<%- id %>", <%- tupleSize %>);
1+
<%- id %>: generateBaseDef(params.<%- id %>, "<%- id %>", <%- tupleSize %>),
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
const <%- id %>Facts = generateInstanceDef(params.<%- id %>, "<%- id %>", <%- tupleSize %>);
1+
<%- id %>: generateInstanceDef(params.<%- id %>, "<%- id %>", <%- tupleSize %>),

solves/base-template/src/runSolver/index.ts.ejshelpers/runSolverStep2.ts.ejs

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)