Skip to content

Commit b1ddb85

Browse files
committed
Add DDAR example in the javascript interface and fix some code style.
1 parent 4bd8802 commit b1ddb85

File tree

5 files changed

+125
-3
lines changed

5 files changed

+125
-3
lines changed

examples/ddar.ejs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<%
2+
3+
const ddar_data = `
4+
perp A B C D, perp C D E F, ncoll A B E => para A B E F
5+
cong O A O B, cong O B O C, cong O C O D => cyclic A B C D
6+
eqangle A B P Q C D P Q => para A B C D
7+
cyclic A B P Q => eqangle P A P B Q A Q B
8+
eqangle6 P A P B Q A Q B, ncoll P Q A B => cyclic A B P Q
9+
cyclic A B C P Q R, eqangle C A C B R P R Q => cong A B P Q
10+
midp E A B, midp F A C => para E F B C
11+
para A B C D, coll O A C, coll O B D => eqratio3 A B C D O O
12+
perp A B C D, perp E F G H, npara A B E F => eqangle A B E F C D G H
13+
eqangle a b c d m n p q, eqangle c d e f p q r u => eqangle a b e f m n r u
14+
eqratio a b c d m n p q, eqratio c d e f p q r u => eqratio a b e f m n r u
15+
eqratio6 d b d c a b a c, coll d b c, ncoll a b c => eqangle6 a b a d a d a c
16+
eqangle6 a b a d a d a c, coll d b c, ncoll a b c => eqratio6 d b d c a b a c
17+
cong O A O B, ncoll O A B => eqangle O A A B A B O B
18+
eqangle6 A O A B B A B O, ncoll O A B => cong O A O B
19+
circle O A B C, perp O A A X => eqangle A X A B C A C B
20+
circle O A B C, eqangle A X A B C A C B => perp O A A X
21+
circle O A B C, midp M B C => eqangle A B A C O B O M
22+
circle O A B C, coll M B C, eqangle A B A C O B O M => midp M B C
23+
perp A B B C, midp M A C => cong A M B M
24+
circle O A B C, coll O A C => perp A B B C
25+
cyclic A B C D, para A B C D => eqangle A D C D C D C B
26+
midp M A B, perp O M A B => cong O A O B
27+
cong A P B P, cong A Q B Q => perp A B P Q
28+
cong A P B P, cong A Q B Q, cyclic A B P Q => perp P A A Q
29+
midp M A B, midp M C D => para A C B D
30+
midp M A B, para A C B D, para A D B C => midp M C D
31+
eqratio O A A C O B B D, coll O A C, coll O B D, ncoll A B C, sameside A O C B O D => para A B C D
32+
para A B A C => coll A B C
33+
midp M A B, midp N C D => eqratio M A A B N C C D
34+
eqangle A B P Q C D U V, perp P Q U V => perp A B C D
35+
eqratio A B P Q C D U V, cong P Q U V => cong A B C D
36+
cong A B P Q, cong B C Q R, cong C A R P, ncoll A B C => contri* A B C P Q R
37+
cong A B P Q, cong B C Q R, eqangle6 B A B C Q P Q R, ncoll A B C => contri* A B C P Q R
38+
eqangle6 B A B C Q P Q R, eqangle6 C A C B R P R Q, ncoll A B C => simtri A B C P Q R
39+
eqangle6 B A B C Q R Q P, eqangle6 C A C B R Q R P, ncoll A B C => simtri2 A B C P Q R
40+
eqangle6 B A B C Q P Q R, eqangle6 C A C B R P R Q, ncoll A B C, cong A B P Q => contri A B C P Q R
41+
eqangle6 B A B C Q R Q P, eqangle6 C A C B R Q R P, ncoll A B C, cong A B P Q => contri2 A B C P Q R
42+
eqratio6 B A B C Q P Q R, eqratio6 C A C B R P R Q, ncoll A B C => simtri* A B C P Q R
43+
eqratio6 B A B C Q P Q R, eqangle6 B A B C Q P Q R, ncoll A B C => simtri* A B C P Q R
44+
eqratio6 B A B C Q P Q R, eqratio6 C A C B R P R Q, ncoll A B C, cong A B P Q => contri* A B C P Q R
45+
para a b c d, coll m a d, coll n b c, eqratio6 m a m d n b n c, sameside m a d n b c => para m n a b
46+
para a b c d, coll m a d, coll n b c, para m n a b => eqratio6 m a m d n b n c
47+
`;
48+
49+
function statementToTerm(statement) {
50+
const [predicate, ...objects] = statement.split(" ").map(s => s.trim());
51+
return `(${predicate} ${objects.map(s => `\`${s}`).join(" ")})`;
52+
}
53+
54+
for (let line of ddar_data.trim().split("\n")) {
55+
const trimmedLine = line.trim();
56+
if (trimmedLine !== "") {
57+
const [premisesStr, conclusionStr] = trimmedLine.split(" => ");
58+
const premises = premisesStr.split(",").map(p => p.trim());
59+
const conclusion = conclusionStr.trim();
60+
for (let premise of premises) {
61+
-%>
62+
<%- statementToTerm(premise) %>
63+
<%
64+
}
65+
-%>
66+
-----------------------------------
67+
<%-
68+
statementToTerm(conclusion)
69+
%>
70+
71+
<%
72+
}
73+
}
74+
%>

examples/ddar.mjs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import express from "express";
2+
import ejs from "ejs";
3+
import { search } from "./search.mjs";
4+
5+
const rules = await ejs.renderFile("examples/ddar.ejs");
6+
7+
const app = express();
8+
9+
app.use(express.text({ type: "*/*" }));
10+
11+
app.post("/", (req, res) => {
12+
const terms = req.body
13+
.split(/[,;\n]/)
14+
.map((x) => x.trim())
15+
.filter((x) => x.length > 0)
16+
.map((x) => `(${x})`)
17+
.join("\n\n");
18+
const data = [rules, terms]
19+
.join("\n\n")
20+
.split("\n\n")
21+
.map((x) => x.trim())
22+
.filter((x) => x.length > 0);
23+
const result = [];
24+
const generator = search(data, req.query.size || 1024, (candidate) => {
25+
if (candidate.length() === 0) {
26+
const str = candidate.conclusion().toString();
27+
const toAppend = str.slice(1, -1);
28+
result.push(toAppend);
29+
}
30+
});
31+
while (true) {
32+
if (generator.next().done) {
33+
break;
34+
}
35+
}
36+
res.type("text/plain").send(`${result.join(req.query.sep || ",")}\n`);
37+
});
38+
39+
app.listen(3000);

examples/engine.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import { search } from "./search.mjs";
77

88
async function read_file_to_string_array(file_path) {
99
const content = await readFile(file_path, "utf-8");
10-
const sections = content.split(/\n\n/);
11-
const results = sections.filter((section) => section.trim().length > 0).map((section) => section.trim());
12-
return results;
10+
return content
11+
.split("\n\n")
12+
.map((x) => x.trim())
13+
.filter((x) => x.length > 0);
1314
}
1415

1516
function default_callback(candidate) {

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
"all": "run-s build test"
2020
},
2121
"devDependencies": {
22+
"@rollup/plugin-commonjs": "^28.0.6",
23+
"@rollup/plugin-json": "^6.1.0",
2224
"@rollup/plugin-node-resolve": "^16.0.1",
2325
"@rollup/plugin-terser": "^0.4.4",
2426
"@rollup/plugin-typescript": "^12.1.4",
2527
"@types/jest": "^30.0.0",
2628
"@web/rollup-plugin-copy": "^0.5.1",
2729
"cross-env": "^7.0.3",
2830
"ejs": "^3.1.10",
31+
"express": "^5.1.0",
2932
"jest": "^30.0.5",
3033
"npm-run-all": "^4.1.5",
3134
"rollup": "^4.45.1",

rollup.config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import json from "@rollup/plugin-json";
12
import terser from "@rollup/plugin-terser";
3+
import commonjs from "@rollup/plugin-commonjs";
24
import typescript from "@rollup/plugin-typescript";
35
import nodeResolve from "@rollup/plugin-node-resolve";
46
import { copy } from "@web/rollup-plugin-copy";
@@ -10,6 +12,7 @@ export default {
1012
example: "examples/main.mjs",
1113
search: "examples/search.mjs",
1214
engine: "examples/engine.mjs",
15+
ddar: "examples/ddar.mjs",
1316
},
1417
output: {
1518
dir: "dist",
@@ -18,7 +21,9 @@ export default {
1821
entryFileNames: "[name].mjs",
1922
},
2023
plugins: [
24+
json(),
2125
terser(),
26+
commonjs(),
2227
typescript(),
2328
nodeResolve(),
2429
copy({

0 commit comments

Comments
 (0)