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

feat: bnode relabelling param #1107

Merged
merged 8 commits into from
May 27, 2024
Merged
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
32 changes: 31 additions & 1 deletion __test_utils__/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import { n3reasoner, linguareasoner } from '../dist';
import { data as blogicData, result as blogicResult } from '../data/blogic';
import { data as regexData, result as regexResult } from '../data/regex';
import { askCallback, askQuery, askResult } from '../data/ask';
import { query as surfaceQuery, relabelingQuery as surfaceRelabelingQuery, relabelingResult as surfaceRelabelingResult } from '../data/surface';
import { mapTerms } from 'rdf-terms';

const parser = new Parser({ format: 'text/n3' });
const trigparser = new Parser({ format: 'trig' });
// Workaround for https://github.com/rdfjs/N3.js/issues/324
// @ts-expect-error
parser._supportsRDFStar = true;
Expand All @@ -21,6 +22,11 @@ export const dataStarQuads = parser.parse(dataStar);
export const resultQuads = parser.parse(result);
export const resultBlogicQuads = parser.parse(blogicResult);
export const askResultQuads = parser.parse(askResult);
export const surfaceQueryQuads = parser.parse(surfaceQuery);
export const surfaceRelabelingQueryQuads = parser.parse(surfaceRelabelingQuery)
// see https://github.com/rdfjs/N3.js/issues/332
.map(quad => mapTerms(quad, term => term.termType === 'BlankNode' ? DataFactory.blankNode(term.value.replace(/\./g, '__dot__')) : term));
export const surfaceRelabelingResultQuads = parser.parse(surfaceRelabelingResult);

export function mockFetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch> {
switch (args[0]) {
Expand Down Expand Up @@ -276,5 +282,29 @@ export function universalTests() {
it('should throw error when eye cannot process the query', async () => {
await expect(n3reasoner('invalid', 'invalid')).rejects.toThrowError('Error while executing query');
});

it('should execute the n3reasoner on surface query', async () => {
await expect(n3reasoner(surfaceQuery)).rejects.toThrow(/inference_fuse/);
await expect(n3reasoner(surfaceQueryQuads)).rejects.toThrow(/inference_fuse/);

await expect(n3reasoner(surfaceQuery, undefined, { bnodeRelabeling: true })).rejects.toThrow(/inference_fuse/);
// FIXME: This should reject but it resolves
// await expect(n3reasoner(surfaceQueryQuads, undefined, { bnodeRelabeling: true })).rejects.toThrow(/inference_fuse/);
});

it('should execute the n3reasoner on surface query [bnodeRelabeling: false]', async () => {
await expect(n3reasoner(surfaceQuery, undefined, { bnodeRelabeling: false })).rejects.toThrow(/inference_fuse/);
await expect(n3reasoner(surfaceQueryQuads, undefined, { bnodeRelabeling: false })).rejects.toThrow(/inference_fuse/);

await expect(n3reasoner(surfaceRelabelingQuery, undefined, { bnodeRelabeling: false })).rejects.toThrow(/inference_fuse/);
// FIXME: This should reject but it resolves
// await expect(n3reasoner(surfaceRelabelingQueryQuads, undefined, { bnodeRelabeling: false })).rejects.toThrow(/inference_fuse/);
});

it('should execute the n3reasoner on surface query that doesnt fuse [bnodeRelabeling: true]', async () => {
await expect(n3reasoner(surfaceRelabelingQuery, undefined, { outputType: 'quads' })).resolves.toBeRdfIsomorphic(surfaceRelabelingResultQuads);
await expect(n3reasoner(surfaceRelabelingQuery, undefined, { bnodeRelabeling: true, outputType: 'quads' })).resolves.toBeRdfIsomorphic(surfaceRelabelingResultQuads);
await expect(n3reasoner(surfaceRelabelingQueryQuads, undefined, { bnodeRelabeling: true })).resolves.toBeRdfIsomorphic(surfaceRelabelingResultQuads);
});
});
}
52 changes: 52 additions & 0 deletions data/surface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @see https://github.com/eyereasoner/eye-js/pull/1107
*/
export const query = `
@prefix : <https://example.org/ns#> .
@prefix log: <http://www.w3.org/2000/10/swap/log#> .

:ABC a :DepartmentPreference .
:ABC a :ResearcherPreference .

() log:onNegativeSurface {
:ABC a :DepartmentPreference .
:ABC a :ResearcherPreference .
} .
`

/**
* @see https://github.com/eyereasoner/eye-js/pull/1107#issuecomment-2133460498
*/
export const relabelingQuery = `
@prefix : <https://example.org/ns#> .
@prefix log: <http://www.w3.org/2000/10/swap/log#> .

:ABC a :DepartmentPreference .
:ABC a :ResearcherPreference .

(_:WHAT) log:onNegativeSurface {
_:WHAT a :DepartmentPreference .
_:WHAT a :ResearcherPreference .
} .
`

export const relabelingResult = `
@prefix : <https://example.org/ns#>.
@prefix log: <http://www.w3.org/2000/10/swap/log#>.

{
_:WHAT a :DepartmentPreference.
} => {
([]) log:onNegativeSurface {
[] a :ResearcherPreference.
}.
}.

{
_:WHAT a :ResearcherPreference.
} => {
([]) log:onNegativeSurface {
[] a :DepartmentPreference.
}.
}.
`
13 changes: 11 additions & 2 deletions lib/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import SEE_PVM from './lingua';
import { qaQuery, queryOnce } from './query';

export type ICoreQueryOptions = {
/**
* Whether or not to perform bnodeRelabeling
* @default true
*/
bnodeRelabeling?: boolean;
output?: 'derivations' | 'deductive_closure' | 'deductive_closure_plus_rules' | 'grounded_deductive_closure_plus_rules';
}

Expand Down Expand Up @@ -56,7 +61,7 @@ export function runQuery(
Module: SWIPLModule,
data: string[],
queryString: string | undefined,
{ output, cb }: Options & { cb?: undefined },
options: Options & { cb?: undefined },
noOptions?: boolean,
): SWIPLModule
export function runQuery(
Expand All @@ -70,7 +75,7 @@ export function runQuery(
Module: SWIPLModule,
data: string[],
queryString?: string,
{ output, cb }: Options = {}, // eslint-disable-line default-param-last
{ output, cb, bnodeRelabeling }: Options = {}, // eslint-disable-line default-param-last
noOptions?: boolean,
): SWIPLModule | Promise<SWIPLModule> {
const args = noOptions ? [] : ['--nope', '--quiet'];
Expand Down Expand Up @@ -106,6 +111,10 @@ export function runQuery(
}
}

if (bnodeRelabeling === false) {
args.push('--no-bnode-relabeling');
}

if (cb) {
return qaQuery(Module, 'main', args, cb).then(() => Module);
}
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"parse-url": "^9.0.1",
"playwright": "^1.30.0",
"pre-commit": "^1.2.2",
"rdf-terms": "^1.11.0",
"semantic-release": "^23.0.0",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
Expand Down
Loading