Skip to content

Commit

Permalink
Merge pull request #19 from jeswr/feat/strip-unused-prefixes
Browse files Browse the repository at this point in the history
feat: strip unused prefixes
  • Loading branch information
jeswr authored Feb 9, 2023
2 parents e00276f + f9f6a85 commit 84562ad
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
28 changes: 26 additions & 2 deletions __tests__/main-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import path from 'path';
import { write } from '../lib';
import 'jest-rdf';

async function getQuads(file: string) {
async function getQuads(file: string, _prefixes: Record<string, string> = {}) {
const parser = new Parser({ rdfStar: true } as any);
const prefixes: Record<string, string> = {};
const prefixes = { ..._prefixes };
const quads = parser.parse(fs.readFileSync(path.join(__dirname, '..', 'data', file)).toString(), undefined, (prefix, iri) => {
prefixes[prefix] = iri.value;
});
Expand Down Expand Up @@ -42,6 +42,30 @@ it('It should correctly write turtle files', async () => {
}
});

it('Should should strip unnecessary prefixes', async () => {
for (const file of fs.readdirSync(path.join(__dirname, '..', 'data'))) {
try {
const { string, quads } = await getQuads(file, {
alt: 'http://example.alt.org/',
v1: 'http://example.v1.org/',
});

if (loose[file]) {
// If loose we only need the quads to match when we re-parse the string
expect((new Parser()).parse(string)).toBeRdfIsomorphic(quads);
} else {
// If not loose we expect an exact string match
expect(string.replace(/b\d+_/g, '')).toEqual(fs.readFileSync(path.join(__dirname, '..', 'data', file)).toString());
}
} catch (e: any) {
// Suppress errors on {| syntax since N3 cannot parse it for now
if (!`${e}`.includes('Unexpected "|"')) {
throw e;
}
}
}
});

it('Should throw error named graphs in quoted and asserted triples', () => {
expect(write([DataFactory.quad(
DataFactory.namedNode('s'),
Expand Down
1 change: 0 additions & 1 deletion data/bnodes4.ttl
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
@prefix ex: <http://example.com/ns#> .

_:s3 a _:s3 .
4 changes: 0 additions & 4 deletions data/star-subject.ttl
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
@prefix ex: <http://example.com/ns#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<<ex:ssn a owl:Thing>> a <<ex:ssn ex:knows owl:Thing>> .
33 changes: 30 additions & 3 deletions lib/ttlwriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,37 @@ export class TTLWriter {
writer: Writer,
prefixes: { [prefix: string]: string } = {},
) {
const terms: Set<string> = new Set();

function addTerm(term: RDF.Term) {
if (term.termType === 'NamedNode') {
terms.add(term.value);
} else if (term.termType === 'Quad') {
addTerm(term.subject);
addTerm(term.predicate);
addTerm(term.object);
addTerm(term.graph);
}
}

for (const list of [
this.store.getSubjects(null, null, null),
this.store.getPredicates(null, null, null),
this.store.getObjects(null, null, null),
]) {
for (const node of list) {
addTerm(node);
}
}

const termList = [...terms];

for (const key of Object.keys(prefixes)) {
const iri = prefixes[key];
this.prefixRev[iri] = key;
this.prefixes[key] = iri;
if (termList.some((term) => term.startsWith(prefixes[key]))) {
const iri = prefixes[key];
this.prefixRev[iri] = key;
this.prefixes[key] = iri;
}
}
this.writer = writer;
}
Expand Down

0 comments on commit 84562ad

Please sign in to comment.