Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Fletcher91 committed Nov 27, 2019
1 parent 39c2ef2 commit 9c37982
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 371 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules/*
node_modules/
.idea
.rpt2*
dist/
pkg/
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
"devDependencies": {
"@types/jest": "^24.0.11",
"jest": "^24.7.1",
"rdflib": "npm:link-rdflib@0.19.1-20190124T144209",
"rollup": "^1.9.0",
"rollup-plugin-commonjs": "^9.3.4",
"rollup-plugin-typescript2": "^0.20.1",
"rollup": "^1.27.2",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-typescript2": "^0.25.2",
"ts-jest": "^24.0.2",
"typescript": "^3.4.1"
"@ontologies/core": "^1.7.2",
"typescript": "^3.7.2"
},
"jest": {
"preset": "ts-jest",
Expand Down
60 changes: 46 additions & 14 deletions src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import { IndexedFormula, Literal } from "rdflib";
import "./useFactory";

import { NQuadsParser, OIndex } from '../index';
import rdf, {
LowLevelStore,
NamedNode,
Node,
Quad,
QuadPosition,
SomeTerm,
} from "@ontologies/core";

import { NQuadsParser } from '../index';

const getParser = () => {
const store = new IndexedFormula();
const parser = new NQuadsParser(store);
const store = {
rdfFactory: rdf,

quads: [] as Quad[],

add(s: Node, p: NamedNode, o: SomeTerm, g: Node) {
this.quads.push(rdf.quad(s, p, o, g));
}
};
const parser = new NQuadsParser(store as unknown as LowLevelStore);

return { parser, store };
};
Expand All @@ -16,6 +33,21 @@ describe("index", () => {

describe("triples", () => {
describe("literals", () => {
it ("parses canonical booleans", () => {
const { parser } = getParser();
const input = '<http://example.com/> <http://example.com/p> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> .';

const result = parser.parseString(input);

expect(result).toHaveLength(1);
const stmt = result[0];
expect(stmt).toBeDefined();
if (!stmt) {
return;
}
expect(stmt[QuadPosition.object]).toEqual(rdf.literal(true));
});

it ("parses booleans", () => {
const { parser } = getParser();
const input = '<http://example.com/> <http://example.com/p> "1"^^<http://www.w3.org/2001/XMLSchema#boolean> .';
Expand All @@ -28,7 +60,7 @@ describe("index", () => {
if (!stmt) {
return;
}
expect(stmt[OIndex]).toEqual(Literal.fromBoolean(true));
expect(stmt[QuadPosition.object]).toEqual(rdf.literal(true));
});

it ("parses numbers", () => {
Expand All @@ -43,7 +75,7 @@ describe("index", () => {
if (!stmt) {
return;
}
expect(stmt[OIndex]).toEqual(Literal.fromNumber(4));
expect(stmt[QuadPosition.object]).toEqual(rdf.literal(4));
});

it ("parses strings", () => {
Expand All @@ -58,7 +90,7 @@ describe("index", () => {
if (!stmt) {
return;
}
expect(stmt[OIndex]).toEqual(new Literal("test"));
expect(stmt[QuadPosition.object]).toEqual(rdf.literal("test"));
});

it ("parses language tags", () => {
Expand All @@ -73,7 +105,7 @@ describe("index", () => {
if (!stmt) {
return;
}
expect(stmt[OIndex]).toEqual(new Literal("test", 'nl'));
expect(stmt[QuadPosition.object]).toEqual(rdf.literal("test", 'nl'));
});
});
});
Expand All @@ -93,7 +125,7 @@ describe("index", () => {
if (!stmt) {
return;
}
expect(stmt[OIndex]).toEqual(Literal.fromBoolean(true));
expect(stmt[QuadPosition.object]).toEqual(rdf.literal(true));
});

it ("parses numbers", () => {
Expand All @@ -108,7 +140,7 @@ describe("index", () => {
if (!stmt) {
return;
}
expect(stmt[OIndex]).toEqual(Literal.fromNumber(4));
expect(stmt[QuadPosition.object]).toEqual(rdf.literal(4));
});

it ("parses strings", () => {
Expand All @@ -123,7 +155,7 @@ describe("index", () => {
if (!stmt) {
return;
}
expect(stmt[OIndex]).toEqual(new Literal("test"));
expect(stmt[QuadPosition.object]).toEqual(rdf.literal("test"));
});

it ("parses language tags", () => {
Expand All @@ -138,7 +170,7 @@ describe("index", () => {
if (!stmt) {
return;
}
expect(stmt[OIndex]).toEqual(new Literal("test", 'nl'));
expect(stmt[QuadPosition.object]).toEqual(rdf.literal("test", 'nl'));
});

it("handles windows newlines", () => {
Expand All @@ -153,7 +185,7 @@ describe("index", () => {
if (!stmt) {
return;
}
expect(stmt[OIndex]).toEqual(new Literal("test\ntest2"));
expect(stmt[QuadPosition.object]).toEqual(rdf.literal("test\ntest2"));
});

it("handles unix newlines", () => {
Expand All @@ -168,7 +200,7 @@ describe("index", () => {
if (!stmt) {
return;
}
expect(stmt[OIndex]).toEqual(new Literal("test\ntest2"));
expect(stmt[QuadPosition.object]).toEqual(rdf.literal("test\ntest2"));
});
});
});
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/useFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { setup, PlainFactory } from "@ontologies/core";
setup(new PlainFactory());
64 changes: 37 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { IndexedFormula, NamedNode } from 'rdflib';

import { Quadruple } from "./types";

export { Quadruple } from './types';

export const SIndex = 0;
export const PIndex = 1;
export const OIndex = 2;
export const GIndex = 3;
import {
DataFactory,
LowLevelStore,
Quadruple,
NamedNode,
QuadPosition,
} from "@ontologies/core";

export class NQuadsParser {
public store: IndexedFormula;
public store: LowLevelStore;
public rdfFactory: DataFactory;

public nnClosingTagError: Error;
public unexpectedCharError: (identifier: string) => Error;

public xsdLangString: NamedNode;
public xsdString: NamedNode;
public xsdBool: NamedNode;

public nnOpeningToken: string;
public nnOpeningTokenOffset: number;
Expand All @@ -40,14 +39,16 @@ export class NQuadsParser {
public dtSplitPrefix: string;
public dtSplitPrefixOffset: number;

constructor(store: IndexedFormula) {
constructor(store: LowLevelStore) {
this.store = store;
this.rdfFactory = store.rdfFactory;

this.nnClosingTagError = new Error(`named node without closing angle bracket`);
this.unexpectedCharError = (identifier) => new Error(`Unexpected character '${identifier}'`);

this.xsdLangString = store.sym('http://www.w3.org/2001/XMLSchema#langString');
this.xsdString = store.sym('http://www.w3.org/2001/XMLSchema#string');
this.xsdLangString = this.rdfFactory.namedNode('http://www.w3.org/2001/XMLSchema#langString');
this.xsdString = this.rdfFactory.namedNode('http://www.w3.org/2001/XMLSchema#string');
this.xsdBool = this.rdfFactory.namedNode('http://www.w3.org/2001/XMLSchema#boolean');

this.nnOpeningToken = '<';
this.nnOpeningTokenOffset = this.nnOpeningToken.length;
Expand Down Expand Up @@ -113,13 +114,13 @@ export class NQuadsParser {
throw this.nnClosingTagError;
}

subject = this.store.sym(cleaned.substring(this.nnOpeningTokenOffset, rightBoundary));
subject = this.rdfFactory.namedNode(cleaned.substring(this.nnOpeningTokenOffset, rightBoundary));
leftBoundary = rightBoundary + this.nnClosingPostfixOffset;
break;

case this.bnOpeningToken:
rightBoundary = cleaned.indexOf(this.bnClosingToken);
subject = this.store.bnode(cleaned.substring(this.bnOpeningPrefixOffset, rightBoundary));
subject = this.rdfFactory.blankNode(cleaned.substring(this.bnOpeningPrefixOffset, rightBoundary));
leftBoundary = rightBoundary + this.bnClosingTokenOffset;
break;

Expand All @@ -138,7 +139,7 @@ export class NQuadsParser {
}

leftBoundary = cleaned.indexOf(this.nnOpeningToken, leftBoundary) + this.nnOpeningTokenOffset;
predicate = this.store.sym(cleaned.substring(leftBoundary, rightBoundary));
predicate = this.rdfFactory.namedNode(cleaned.substring(leftBoundary, rightBoundary));
leftBoundary = rightBoundary + this.nnClosingPostfixOffset;

/*
Expand All @@ -156,7 +157,7 @@ export class NQuadsParser {
throw this.nnClosingTagError;
}

object = this.store.sym(cleaned.substring(leftBoundary, rightBoundary));
object = this.rdfFactory.namedNode(cleaned.substring(leftBoundary, rightBoundary));
break;
case this.bnOpeningToken:
leftBoundary = cleaned.indexOf(this.bnOpeningPrefix, leftBoundary) + this.bnOpeningPrefixOffset;
Expand All @@ -165,7 +166,7 @@ export class NQuadsParser {
if (rightBoundary === -1) {
rightBoundary = Infinity
}
object = this.store.bnode(cleaned.substring(leftBoundary, rightBoundary));
object = this.rdfFactory.blankNode(cleaned.substring(leftBoundary, rightBoundary));
break;
case '"':
leftBoundary = leftBoundary + this.ltOpeningTokenOffset;
Expand All @@ -174,10 +175,15 @@ export class NQuadsParser {
.replace(this.ltWhitespaceReplace, this.ltNewline);
leftBoundary += object.length;
dtOrLgBoundary = cleaned.indexOf(this.dtSplitPrefix, leftBoundary);
object = object.replace(this.ltQuoteUnescape, this.ltQuoteReplaceValue);

if (dtOrLgBoundary >= 0) {
// Typed literal
rightBoundary = cleaned.indexOf(this.nnClosingToken, dtOrLgBoundary);
datatype = this.store.sym(cleaned.substring(dtOrLgBoundary + this.dtSplitPrefixOffset, rightBoundary));
datatype = this.rdfFactory.namedNode(cleaned.substring(dtOrLgBoundary + this.dtSplitPrefixOffset, rightBoundary));
if (datatype.value === this.xsdBool.value && (object === "1" || object === "0")) {
object = object === "1" ? "true" : "false";
}
leftBoundary = rightBoundary
} else {
dtOrLgBoundary = cleaned.indexOf(this.lgOpeningToken, leftBoundary);
Expand All @@ -192,10 +198,9 @@ export class NQuadsParser {
datatype = this.xsdString;
}
}
object = this.store.literal(
object.replace(this.ltQuoteUnescape, this.ltQuoteReplaceValue),
lang,
datatype
object = this.rdfFactory.literal(
object,
lang || datatype
);
break;
default:
Expand All @@ -207,8 +212,8 @@ export class NQuadsParser {
*/
leftBoundary = cleaned.indexOf(this.nnOpeningToken, leftBoundary) + this.nnOpeningTokenOffset;
graph = leftBoundary - this.nnOpeningTokenOffset >= 0
? this.store.sym(cleaned.substring(leftBoundary, cleaned.indexOf(this.nnClosingPostfix, leftBoundary)))
: this.store.defaultGraphIRI;
? this.rdfFactory.namedNode(cleaned.substring(leftBoundary, cleaned.indexOf(this.nnClosingPostfix, leftBoundary)))
: this.rdfFactory.defaultGraph();

quads[i] = [subject, predicate, object, graph];
} catch(e) {
Expand All @@ -224,7 +229,12 @@ export class NQuadsParser {
for (let i = 0, len = quads.length; i < len; i++) {
q = quads[i];
if (q) {
this.store.add(q[SIndex], q[PIndex], q[OIndex], q[GIndex]);
this.store.add(
q[QuadPosition.subject],
q[QuadPosition.predicate],
q[QuadPosition.object],
q[QuadPosition.graph],
);
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/types.ts

This file was deleted.

Loading

0 comments on commit 9c37982

Please sign in to comment.