Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/node_modules
/.pnp
.pnp.js
/src/util/diproche/diprocheProgram
/diproche_source

# testing
/coverage
Expand Down
4 changes: 4 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 48 additions & 46 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
{
"name": "diproche-webinterface",
"version": "0.1.0",
"private": true,
"dependencies": {
"@types/lodash": "^4.14.136",
"@types/react-router-dom": "^4.3.4",
"@types/shortid": "0.0.29",
"bootstrap": "^4.3.1",
"jquery": "^3.4.1",
"lodash": "^4.17.15",
"node-sass": "^4.12.0",
"react": "^16.8.6",
"react-bootstrap": "^1.0.0-beta.10",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-scripts": "^3.0.1",
"shortid": "^2.2.14",
"tau-prolog": "^0.2.66",
"typescript": "3.4.2"
},
"scripts": {
"start": "react-scripts start",
"lint": "tslint --project .",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"devDependencies": {
"@types/jest": "24.0.11",
"@types/node": "11.13.2",
"@types/node-sass": "^4.11.0",
"@types/react": "16.8.13",
"@types/react-dom": "16.8.3",
"shx": "^0.3.2",
"tslint": "^5.18.0"
},
"proxy": "http://localhost:5000"
"name": "diproche-webinterface",
"version": "0.1.0",
"private": true,
"dependencies": {
"@types/lodash": "^4.14.136",
"@types/react-router-dom": "^4.3.4",
"@types/shortid": "0.0.29",
"bootstrap": "^4.3.1",
"diproche": "git+https://github.com/TimothyGillespie/Diproche-Prolog.git",
"jquery": "^3.4.1",
"lodash": "^4.17.15",
"node-sass": "^4.12.0",
"react": "^16.8.6",
"react-bootstrap": "^1.0.0-beta.10",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-scripts": "^3.0.1",
"shortid": "^2.2.14",
"tau-prolog": "^0.2.66",
"typescript": "3.4.2"
},
"scripts": {
"start": "npm run copy-diproche && react-scripts start",
"lint": "tslint --project .",
"build": "npm run copy-diproche && react-scripts build",
"test": "npm run copy-diproche && react-scripts test",
"eject": "react-scripts eject",
"copy-diproche": "shx cp -r ./node_modules/diproche/*.pl ./diproche_source"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"devDependencies": {
"@types/jest": "24.0.11",
"@types/node": "11.13.2",
"@types/node-sass": "^4.11.0",
"@types/react": "16.8.13",
"@types/react-dom": "16.8.3",
"shx": "^0.3.2",
"tslint": "^5.18.0"
},
"proxy": "http://localhost:5000"
}
91 changes: 0 additions & 91 deletions src/util/prolog/builtInPredicates.ts

This file was deleted.

25 changes: 25 additions & 0 deletions src/util/prolog/fubar.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import "../../../swipl-wasm/swipl-web";
import { loadingFinishedPromise, query, result } from "./fubar";

describe("Fubar", () => {
test("Tests bob is a man", async () => {
await loadingFinishedPromise;
query("man(bob).");
console.log("Inside the test");
console.log(result);
});

test("Tests frank is not a man", async () => {
await loadingFinishedPromise;
query("diproche_fo([[wir,zeigen,[a,and,b],<->,[b,and,a]],[=>],[angenommen,a,and,b],[dann,a],[ferner,gilt,b],[damit,folgt,b,and,a],[qed],[abs],[<=],[nehmen,wir,jetzt,an,dass,b,and,a],[dann,folgt,a],[ausserdem,folgt,b],[also,gilt,nun,a,and,b],[qed],[damit,gilt,[a,and,b],<->,[c,and,a]]], X).");
console.log("Inside the test");
console.log(result);
});

test("Gets X", async () => {
await loadingFinishedPromise;
query("man(X).");
console.log("Inside the test");
console.log(result);
});
});
152 changes: 152 additions & 0 deletions src/util/prolog/fubar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import fs from "fs";
import path from "path";
import { FS } from "../../../swipl-wasm/swipl-web";

// tslint:disable-next-line:prefer-const
let bindings: any = null;
let stdin = "";
let stdinPosition = 0;

const basePathDiproche: string = path.resolve(__dirname, "../../../diproche_source");
const diprocheFiles: string[] = fs.readdirSync(basePathDiproche);
const diprocheMap: Map<string, string> = new Map();
diprocheFiles.forEach((fileName: string) => {
const filePath: string = path.resolve(basePathDiproche, "./" + fileName);
diprocheMap.set(fileName.replace(".pl", ""), fs.readFileSync(filePath, "utf8"));
});

// var to make this variable global
// tslint:disable-next-line:no-var-keyword
export var result: string[] = [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var is not making the variable global. export is.


// We use this to provide data into
// the SWI stdin.
const setStdin = (s: string) => {
stdin = s;
stdinPosition = 0;
};
const readStdin = () => {
if (stdinPosition >= stdin.length) {
return null;
} else {
const code = stdin.charCodeAt(stdinPosition);
stdinPosition++;
return code;
}
};

/**
* Helper function to call a query.
*/
export function query(queryText: string) {
setStdin(queryText);
// This will execute one iteration of toplevel.
call(bindings, "break"); // see call.js
}

// Creates bindings to the SWI foreign API.
function createBindings(bindingModule: any) {
return {
PL_initialise: bindingModule.cwrap("PL_initialise", "number", [
"number",
"number",
]),
PL_new_term_ref: bindingModule.cwrap("PL_new_term_ref", "number", []),
PL_chars_to_term: bindingModule.cwrap("PL_chars_to_term", "number", [
"string",
"number",
]),
PL_call: bindingModule.cwrap("PL_call", "number", ["number", "number"]),
};
}

// Helper function to parse a JavaScript
// string into a Prolog term and call is as a query.
function call(callBindings: any, callQuery: any) {
const ref = callBindings.PL_new_term_ref();
if (!callBindings.PL_chars_to_term(callQuery, ref)) {
throw new Error("Query has a syntax error: " + callQuery);
}
return !!callBindings.PL_call(ref, 0);
}

// This will set up the arguments necessary for the PL_initialise
// function and will call it.
// See http://www.swi-prolog.org/pldoc/doc_for?object=c(%27PL_initialise%27)
function initialise(initBindings: any, initModule: any) {
const argvArray = [
initModule.allocate(
initModule.intArrayFromString("swipl"),
"i8",
initModule.ALLOC_NORMAL,
),
initModule.allocate(
initModule.intArrayFromString("-x"),
"i8",
initModule.ALLOC_NORMAL,
),
initModule.allocate(
initModule.intArrayFromString("wasm-preload/swipl.prc"),
"i8",
initModule.ALLOC_NORMAL,
),
initModule.allocate(
initModule.intArrayFromString("--nosignals"),
"i8",
initModule.ALLOC_NORMAL,
),
];
const argvPtr = initModule._malloc(argvArray.length * 4);
for (let i = 0; i < argvArray.length; i++) {
initModule.setValue(argvPtr + i * 4, argvArray[i], "*");
}
if (!initBindings.PL_initialise(4, argvPtr)) {
throw new Error("SWI-Prolog initialisation failed.");
}
// Set the path of the preloaded (from swipl-web.dat) standard library.
// This makes it possible to call use_module(library(lists)) and so on.
call(
initBindings,
"assert(user:file_search_path(library, 'wasm-preload/library')).",
);
}

function pushToResultArray(value: string): void {
result.push(value);
}

const swiplWasm = fs.readFileSync("swipl-wasm/swipl-web.wasm");
const swiplWasmData = fs.readFileSync("swipl-wasm/swipl-web.data").buffer;

// Stub Module object. Used by swipl-web.js to
// populate the actual Module object.
export const Module = {
noInitialRun: true,
locateFile: (url: string) => `swipl-wasm/${url}`,
print: pushToResultArray,
printErr: console.error,
wasmBinary: swiplWasm,
preRun: [() => FS.init(readStdin)], // sets up stdin
getPreloadedPackage: (fileName: string) =>
fileName === "swipl-wasm/swipl-web.data" ? swiplWasmData : null,
onRuntimeInitialized: () => {
// Bind foreign functions to JavaScript.
bindings = createBindings(Module);
// Initialise SWI-Prolog.
initialise(bindings, Module);

diprocheMap.forEach((programCode: string, fileName: string) => {
FS.writeFile("/" + fileName + ".pl", programCode);
});

query("consult('/diproche.pl').");

loadingFinished();
},
};

let loadingFinished!: () => void;

export const loadingFinishedPromise = new Promise<void>(resolve => {
loadingFinished = () => resolve();
});
Loading