Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/run blogic test only #168

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage
dist
bundle
lib/eye.ts
eye/
199 changes: 199 additions & 0 deletions __tests__/all-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/* eslint-disable */
// TODO: Remove the above before merging
import fs from 'fs';
import path from 'path';
import { Parser, Store, DataFactory as DF } from 'n3';
import { mapTerms } from 'rdf-terms';
import { n3reasoner } from '../dist';
import type * as RDF from "@rdfjs/types";
import 'jest-rdf';

const examplesPath = path.join(__dirname, '..', 'eye', 'reasoning');
const expectedStart = 'eye "$@"';
const cacheComponent = ' --wcache http://eyereasoner.github.io/eye/reasoning .. ';
const longStart = expectedStart + cacheComponent;

const ignoreFolders = [
// n3reasoner does not support extra images
'dt', 'image',
// n3reasoner does not support multiquery
'mq',
];

// These exceptions should eventually be removed
const ignoreOutputs = [
// This complains because `$` is used as a term
'bi/biA.n3',
// This complains because of https://github.com/rdfjs/N3.js/issues/328
'crypto/crypto-proof.n3',
// This just states `true .` but it's not a valid N3 file
'entail/socrates-check.n3',
// This complains because of https://github.com/rdfjs/N3.js/issues/328
'preduction/palindrome-proof.n3',
// This complains because of https://github.com/rdfjs/N3.js/issues/328
'preduction/palindrome2-proof.n3',
// This causes isomorphism timepouts
];

let i = 0;

function readFile(subPath: string) {
return fs.readFileSync(path.join(examplesPath, subPath)).toString();
}

function dereference(subPath: string) {
const parser = new Parser({ format: 'text/n3', baseIRI: `http://eyereasoner.github.io/eye/reasoning${subPath}` });
// @ts-expect-error
parser._supportsRDFStar = true;
return parser.parse(readFile(subPath));
}

describe('Testing examples from eye repository', () => {
for (const folder of fs.readdirSync(examplesPath)) {
if (fs.statSync(path.join(examplesPath, folder)).isDirectory()) {
describe(`Testing examples for ${folder}`, () => {
for (const testCase of fs.readFileSync(path.join(examplesPath, folder, 'test')).toString().split('\n').slice(1)
.filter((x) => x !== '')) {
// eslint-disable-next-line no-loop-func
let fn = describe;

if (ignoreFolders.includes(folder)) {
fn = describe.skip;
} else if (!testCase.startsWith(expectedStart)) {
throw new Error(`Found more test case without the expected start of ${expectedStart}`);
}

let args = testCase.slice(testCase.startsWith(longStart) ? longStart.length : expectedStart.length);

if (testCase.startsWith(longStart)) {
args = args.replace(/http\:\/\/eyereasoner\.github\.io\/eye\/reasoning\//g, '');
args = args.replace(/ --turtle/g, '');
}

let argsArray = args.trim().split(' ');

if (argsArray[argsArray.length - 2] !== '--output') {
throw new Error(`Expected --output to be the last argument on ${args}`);
}

const subPath = path.join(folder, argsArray[argsArray.length - 1]);

argsArray = argsArray.slice(0, -2);

// Skip if the output is not a valid N3 file
if (ignoreOutputs.includes(subPath) || !subPath.endsWith('n3')) {
// eslint-disable-next-line no-loop-func
fn = describe.skip;
}

fn(`should run for [${argsArray.join(' ')}]`, () => {
// it('using string i/o', () => {

// });

// (argsArray[0] === '--blogic' && !argsArray.includes('blogic/socrates-star.n3') ? it : it.skip)('using string i/o', () => {
// return expect(n3reasoner(argsArray.slice(1).map(file => readFile(file)).join('\n'), undefined, { blogic: true, outputType: 'quads' }))
// .resolves
// .toBeRdfIsomorphic(dereference(subPath));
// });

const args = argsArray.filter((arg) => arg.startsWith('--'));

// Workaround for https://github.com/rdfjs/N3.js/issues/332
function normalize(quad: RDF.Quad[], descope = false): RDF.Quad[] {
return quad
.map((quad) => mapTerms(quad, (term) => (term.termType === 'BlankNode' ? DF.blankNode(term.value.replace(descope ? /^(n3-\d+)?\./ : /^\./, '')) : term)));
}

function loadFiles(files: string[], descope = false) {
return normalize([...new Store(files.map((file) => dereference(file)).flat())], descope)
// Workaround for https://github.com/rdfjs/N3.js/issues/332
// .map((quad) => mapTerms(quad, (term) => (term.termType === 'BlankNode' ? DF.blankNode(term.value.replace(descope ? /^(n3-\d+)?\./ : /^\./, '')) : term)));
}

if (args.length === 1 && args[0] === '--blogic'
// Skip socrates-star because it contains '{|' and the PR to support that has not been merged in N3.js
&& !argsArray.includes('blogic/socrates-star.n3')
) {
it('using quad i/o', () => expect(
Promise.race([
n3reasoner(loadFiles(argsArray.slice(1), true), undefined, { blogic: true })
.then(quads => normalize(quads, true)),
new Promise((resolve, rej) => setTimeout(() => rej(new Error('timeout')), 10)),
]))
.resolves
.toBeRdfIsomorphic(normalize(dereference(subPath), true)));
} else if (args.length === 1 && args[0] === '--query') {
it.skip('using quad i/o', () => expect(
n3reasoner(
loadFiles(argsArray.filter((_, i) => i !== argsArray.indexOf('--query') && i !== argsArray.indexOf('--query') + 1)),
loadFiles([argsArray[argsArray.indexOf('--query') + 1]]),
),
)
.resolves
.toBeRdfIsomorphic(dereference(subPath)));
} else if (args.length === 2 && args.includes('--query') && args.includes('--nope')) {
it.skip('using quad i/o', () => expect(
n3reasoner(
loadFiles(argsArray.filter((_, i) => i !== argsArray.indexOf('--query')
&& i !== argsArray.indexOf('--nope')
&& i !== argsArray.indexOf('--query') + 1)),
loadFiles([argsArray[argsArray.indexOf('--query') + 1]]),
),
)
.resolves
.toBeRdfIsomorphic(dereference(subPath)));
} else if (args.length === 2 && args.includes('--pass') && args.includes('--nope')) {
it.skip('using quad i/o', () => expect(
n3reasoner(
loadFiles(argsArray.filter((_, i) => i !== argsArray.indexOf('--pass') && i !== argsArray.indexOf('--nope'))),
undefined,
{ output: 'deductive_closure' },
),
)
.resolves
.toBeRdfIsomorphic(dereference(subPath)));
}else if (args.length === 2 && args.includes('--pass-only-new') && args.includes('--nope')) {
it.skip('using quad i/o', () => expect(
n3reasoner(
loadFiles(argsArray.filter((_, i) => i !== argsArray.indexOf('--pass-only-new') && i !== argsArray.indexOf('--nope'))),
undefined,
{ output: 'derivations' },
),
)
.resolves
.toBeRdfIsomorphic(dereference(subPath)));
} else {
test.todo('using quad i/o');
}

// let ifn = it;

// // Skip socrates-star because it contains '{|' and the PR to support that has not been merged in N3.js
// if (argsArray.includes('blogic/socrates-star.n3') || argsArray[0] !== '--blogic') {
// ifn = it.skip;
// }

// ifn('using quad i/o', async () => {
// expect(true).toBeTruthy();
// // const quads = [...new Store(argsArray.slice(1).map(file => dereference(file)).flat())]
// // // Workaround for https://github.com/rdfjs/N3.js/issues/332
// // .map(quad => mapTerms(quad, term => term.termType === 'BlankNode' ? DF.blankNode(term.value.replace(/^\./, '')) : term));

// // return expect(n3reasoner(quads, undefined, { blogic: true }))
// // .resolves
// // .toBeRdfIsomorphic(dereference(subPath));
// });
});

// if (argsArray.includes('--pass-only-new')) {
// console.log(argsArray, output.length)
// }

// console.log(testCase)
// process.exit()
}
});
}
}
});
169 changes: 169 additions & 0 deletions __tests__/no-jest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/* eslint-disable */
// TODO: Remove the above before merging
import fs from 'fs';
import path from 'path';
import { Parser, Store, DataFactory as DF } from 'n3';
import { mapTerms } from 'rdf-terms';
import { n3reasoner } from '../dist';
import type * as RDF from "@rdfjs/types";
import { isomorphic } from 'rdf-isomorphic';
import { write } from '../dist/n3Writer.temp';
import { write as writePretty } from '@jeswr/pretty-turtle';
// import 'jest-rdf';

const examplesPath = path.join(__dirname, '..', 'eye', 'reasoning');
const expectedStart = 'eye "$@"';
const cacheComponent = ' --wcache http://eyereasoner.github.io/eye/reasoning .. ';
const longStart = expectedStart + cacheComponent;

const ignoreFolders = [
// n3reasoner does not support extra images
'dt', 'image',
// n3reasoner does not support multiquery
'mq',
];

// These exceptions should eventually be removed
const ignoreOutputs = [
// This complains because `$` is used as a term
'bi/biA.n3',
// This complains because of https://github.com/rdfjs/N3.js/issues/328
'crypto/crypto-proof.n3',
// This just states `true .` but it's not a valid N3 file
'entail/socrates-check.n3',
// This complains because of https://github.com/rdfjs/N3.js/issues/328
'preduction/palindrome-proof.n3',
// This complains because of https://github.com/rdfjs/N3.js/issues/328
'preduction/palindrome2-proof.n3',
];

let i = 0, j = 0;

function readFile(subPath: string) {
return fs.readFileSync(path.join(examplesPath, subPath)).toString();
}

function dereference(subPath: string) {
const parser = new Parser({ format: 'text/n3', baseIRI: `http://eyereasoner.github.io/eye/reasoning${subPath}` });
// @ts-expect-error
parser._supportsRDFStar = true;
return parser.parse(readFile(subPath));
}

async function main() {
for (const folder of fs.readdirSync(examplesPath)) {
if (fs.statSync(path.join(examplesPath, folder)).isDirectory()) {
for (const testCase of fs.readFileSync(path.join(examplesPath, folder, 'test')).toString().split('\n').slice(1)
.filter((x) => x !== '')) {

// console.log('a')
// eslint-disable-next-line no-loop-func

if (ignoreFolders.includes(folder)) {
continue
} else if (!testCase.startsWith(expectedStart)) {
throw new Error(`Found more test case without the expected start of ${expectedStart}`);
}

let args_ = testCase.slice(testCase.startsWith(longStart) ? longStart.length : expectedStart.length);

if (testCase.startsWith(longStart)) {
args_ = args_.replace(/http\:\/\/eyereasoner\.github\.io\/eye\/reasoning\//g, '');
args_ = args_.replace(/ --turtle/g, '');
}

let argsArray = args_.trim().split(' ');

if (argsArray[argsArray.length - 2] !== '--output') {
throw new Error(`Expected --output to be the last argument on ${args_}`);
}

const subPath = path.join(folder, argsArray[argsArray.length - 1]);

argsArray = argsArray.slice(0, -2);

// Skip if the output is not a valid N3 file
if (ignoreOutputs.includes(subPath) || !subPath.endsWith('n3')) {
// eslint-disable-next-line no-loop-func
continue
}

// it('using string i/o', () => {

// });

// (argsArray[0] === '--blogic' && !argsArray.includes('blogic/socrates-star.n3') ? it : it.skip)('using string i/o', () => {
// return expect(n3reasoner(argsArray.slice(1).map(file => readFile(file)).join('\n'), undefined, { blogic: true, outputType: 'quads' }))
// .resolves
// .toBeRdfIsomorphic(dereference(subPath));
// });

const args = argsArray.filter((arg) => arg.startsWith('--'));

function descopeQuad(quad: RDF.Quad, descope = false): RDF.Quad {
return mapTerms(quad, (term) => {
if (term.termType === 'BlankNode') {
return DF.blankNode(term.value.replace(descope ? /^(n3-\d+)?\./ : /^\./, ''));
}
if (term.termType === 'Quad') {
return descopeQuad(term as RDF.Quad, descope);
}
return term;
});
}

// Workaround for https://github.com/rdfjs/N3.js/issues/332
function normalize(quad: RDF.Quad[], descope = false): RDF.Quad[] {
return quad
.map((quad) => descopeQuad(quad, descope));
}

function loadFiles(files: string[], descope = false) {
return normalize([...new Store(files.map((file) => dereference(file)).flat())], descope)
// Workaround for https://github.com/rdfjs/N3.js/issues/332
// .map((quad) => mapTerms(quad, (term) => (term.termType === 'BlankNode' ? DF.blankNode(term.value.replace(descope ? /^(n3-\d+)?\./ : /^\./, '')) : term)));
}

// console.log('b', args.)



if (args.length === 1 && args[0] === '--blogic'
// Skip socrates-star because it contains '{|' and the PR to support that has not been merged in N3.js
&& !argsArray.includes('blogic/socrates-star.n3')
&& !argsArray.includes('blogic/bmt.n3')
&& !argsArray.includes('lubm/facts.ttl')
// && subPath === 'blogic/category-answer.n3'
) {
i += 1;
let input: RDF.Quad[] | undefined
try {
input = loadFiles(argsArray.slice(1), true);
const output = await n3reasoner(loadFiles(argsArray.slice(1), true), undefined, { blogic: true }).then(quads => normalize(quads, true));
const expected = normalize(dereference(subPath), true);
const res = isomorphic(
output,
expected
)

if (res) {
j += 1;
} else {
console.log(`Expected ${expected.length} quads recieved ${output.length} for ${subPath}`)
// console.log('expected', await writePretty(expected!, { format: 'text/n3' }))
// console.log('recieved', await writePretty(output!, { format: 'text/n3' }))
}
} catch (e) {
console.log('error on input', write(input!))
console.error(e)
}

}
}
}
}

console.log(`${j}/${i}`)
}

main();
1 change: 1 addition & 0 deletions abc.n3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<http://example.org/ns#i> a <http://example.org/ns#A> . <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> <http://www.w3.org/2000/10/swap/log#onNegativeSurface> {<http://example.org/ns#i> a <http://example.org/ns#C> . } . _:n3-1 <http://www.w3.org/2000/10/swap/log#onNegativeSurface> {<http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> <http://www.w3.org/2000/10/swap/log#onNegativeSurface> {_:n3-3.S a <http://example.org/ns#B> . } . <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> <http://www.w3.org/2000/10/swap/log#onNegativeSurface> {_:n3-4.S a <http://example.org/ns#C> . } . _:n3-2.S a <http://example.org/ns#A> . } . _:n3-1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:S . _:n3-1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> . _:n3-5 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:S . _:n3-5 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> _:n3-6 . _:n3-5 <http://www.w3.org/2000/10/swap/log#onQuerySurface> {_:n3-7.S a _:n3-7.C . } . _:n3-6 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:C . _:n3-6 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
Loading