|
1 | 1 | import React from "react"; |
2 | 2 |
|
3 | | -import {type ClingoResult} from "../runSolver"; |
| 3 | +import {type ParsedSolutionSpecialCode} from "../runSolver"; |
4 | 4 |
|
5 | 5 | import "./ResultDisplay.css"; |
6 | 6 |
|
7 | | -const solutionRE = /solution\(color\(([^,]+),([^,]+)*\)\)/g; |
8 | | - |
9 | | -function parseSolutionStr(s: string) { |
10 | | - const matches = [...s.matchAll(solutionRE)]; |
11 | | - if (matches.length !== 1) return null; |
12 | | - const match = matches[0]; |
13 | | - if (match?.length !== 3) return null; |
14 | | - return { |
15 | | - vertex: match?.[1] || "<INVALID>", |
16 | | - colour: match?.[2] || "<INVALID>", |
17 | | - }; |
18 | | -} |
19 | | - |
20 | | -function renderRow(s: string, i: number) { |
21 | | - const parsed = parseSolutionStr(s); |
22 | | - if (parsed === null) return null; |
| 7 | +function renderRow(strs: string[], i: number) { |
23 | 8 | return <tr key={i}> |
24 | | - <td>{parsed.vertex}</td> |
25 | | - <td>{parsed.colour}</td> |
| 9 | + {strs.map((x: string, j: number) => |
| 10 | + <td key={j}>{x}</td> |
| 11 | + )} |
26 | 12 | </tr>; |
27 | 13 | } |
28 | 14 |
|
29 | | -function renderSatisfiable(solution: string[]) { |
30 | | - return <table className="result-satisfiable"> |
31 | | - <thead> |
32 | | - <tr> |
33 | | - <th>Vertex</th> |
34 | | - <th>Colour</th> |
35 | | - </tr> |
36 | | - </thead> |
37 | | - <tbody> |
38 | | - {solution.map(renderRow)} |
39 | | - </tbody> |
40 | | - </table>; |
41 | | -} |
42 | | - |
43 | 15 | /*** ***/ |
44 | 16 |
|
45 | 17 | interface Props { |
46 | | - initialized: boolean; |
47 | | - clingoResult: null | ClingoResult; |
| 18 | + fieldLabels: string[]; |
| 19 | + solutionData: string[][] | ParsedSolutionSpecialCode | "not-initialized"; |
48 | 20 | } |
49 | 21 |
|
50 | 22 | export function ResultDisplay(props: Props) { |
51 | | - if (!props.initialized) { |
52 | | - console.assert(props.clingoResult === null); |
53 | | - return <>Loading...</>; |
54 | | - } else if (props.clingoResult === null) { |
55 | | - return <>Invalid Input.</>; |
56 | | - } |
57 | | - |
58 | | - switch (props.clingoResult.result) { |
59 | | - case "SATISFIABLE": |
60 | | - const solution = props.clingoResult.solution; |
61 | | - if (solution !== null && solution.length !== 0) { |
62 | | - return renderSatisfiable(solution); |
63 | | - } else { |
64 | | - return <>There is a solution, but it has no values.</>; |
65 | | - } |
66 | | - case "UNSATISFIABLE": |
| 23 | + switch (props.solutionData) { |
| 24 | + case "not-initialized": |
| 25 | + return <>Loading...</>; |
| 26 | + case "no-solution": |
67 | 27 | return <>No solution.</>; |
68 | | - default: console.error("Invalid value."); // Fallthrough |
69 | | - case "UNKNOWN": |
70 | | - return <>{props.clingoResult.stderr || "UNKNOWN" }</>; |
| 28 | + case "invalid-input": |
| 29 | + return <>Invalid input.</>; |
| 30 | + case "error": |
| 31 | + return <>An error has occurred.</>; |
| 32 | + default: // Fallthrough |
| 33 | + } |
| 34 | + if (props.solutionData.length === 0) { |
| 35 | + return <>There is a solution, but it has no values.</>; |
71 | 36 | } |
| 37 | + |
| 38 | + console.log(props.solutionData); |
| 39 | + return <table className="result-satisfiable"> |
| 40 | + <thead> |
| 41 | + <tr key={0}> |
| 42 | + {props.fieldLabels.map((x: string, i: number) => |
| 43 | + <th key={i}>{x}</th> |
| 44 | + )} |
| 45 | + </tr> |
| 46 | + </thead> |
| 47 | + <tbody> |
| 48 | + {props.solutionData.map(renderRow)} |
| 49 | + </tbody> |
| 50 | + </table>; |
72 | 51 | } |
73 | 52 |
|
0 commit comments