From 4a5a572f88beaffccf6dbb3edac74e786bbc00b1 Mon Sep 17 00:00:00 2001 From: Fred Eisele Date: Fri, 14 Oct 2016 14:04:41 -0500 Subject: [PATCH 1/4] introduction of typings for N3, the RDF tool --- n3/n3-tests.ts | 181 +++++++++++++++++++++++++++++++++++++++++++++++++ n3/n3.d.ts | 116 +++++++++++++++++++++++++++++++ 2 files changed, 297 insertions(+) create mode 100644 n3/n3-tests.ts create mode 100644 n3/n3.d.ts diff --git a/n3/n3-tests.ts b/n3/n3-tests.ts new file mode 100644 index 0000000000..d506bb6f0a --- /dev/null +++ b/n3/n3-tests.ts @@ -0,0 +1,181 @@ +/// +/// + +import * as N3 from "n3"; +import * as fs from "fs"; +import * as stream from "stream"; + +function test_serialize() { + + var writer: N3.N3Writer = N3.Writer( + { + format: "ttl", + prefixes: { + foaf: "http://xmlns.com/foaf/0.1", + freebase: "http://rdf.freebase.com/ns/", + g: "http://base.google.com/ns/1.0" + } + }); + writer.addTriple({ + subject: "subject-name", + predicate: "predicate-name", + object: N3.Util.createLiteral(12) + }); + + writer.end((error, result) => { + console.log(`result ${result}`); + }); +} + +function test_doc_rdf_to_triples_1() { + var parser = N3.Parser(); + parser.parse('@prefix c: .\n' + + 'c:Tom a c:Cat.\n' + + 'c:Jerry a c:Mouse;\n' + + ' c:smarterThan c:Tom.', + function (error: Error, triple: N3.Triple, prefixes: N3.Prefixes) { + if (triple) + console.log(triple.subject, triple.predicate, triple.object, '.'); + else + console.log("# That's all, folks!", prefixes) + }); +} + +function test_doc_rdf_to_triples_2() { + var parser1 = N3.Parser({ format: 'N-Triples' }); + var parser2 = N3.Parser({ format: 'application/trig' }); + // Notation3 (N3) is supported only through the format argument: + + var parser3 = N3.Parser({ format: 'N3' }); + var parser4 = N3.Parser({ format: 'Notation3' }); + var parser5 = N3.Parser({ format: 'text/n3' }); +} + +function test_doc_rdf_stream_to_triples_1() { + var parser = N3.Parser(); + var rdfStream = fs.createReadStream('cartoons.ttl'); + parser.parse(rdfStream, console.log); + + var streamParser = N3.StreamParser(); + var rdfStream = fs.createReadStream('cartoons.ttl'); + // rdfStream.pipe(streamParser); + streamParser.pipe(new SlowConsumer()); + + class SlowConsumer extends stream.Writable { + constructor() { + super({ objectMode: true }); + this._write = function (triple, encoding, done) { + console.log(triple); + setTimeout(done, 1000); + }; + } + } +} + +function test_doc_from_triples_to_string() { + var writer = N3.Writer({ prefixes: { c: 'http://example.org/cartoons#' } }); + writer.addTriple('http://example.org/cartoons#Tom', + 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', + 'http://example.org/cartoons#Cat'); + writer.addTriple({ + subject: 'http://example.org/cartoons#Tom', + predicate: 'http://example.org/cartoons#name', + object: '"Tom"' + }); + writer.end(function (error, result) { console.log(result); }); + + var writer1 = N3.Writer({ format: 'N-Triples' }); + var writer2 = N3.Writer({ format: 'application/trig' }); +} + +function test_doc_from_triples_to_rdf_stream() { + var writer = N3.Writer(process.stdout, { prefixes: { c: 'http://example.org/cartoons#' } }); + writer.addTriple('http://example.org/cartoons#Tom', + 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', + 'http://example.org/cartoons#Cat'); + writer.addTriple({ + subject: 'http://example.org/cartoons#Tom', + predicate: 'http://example.org/cartoons#name', + object: '"Tom"' + }); + writer.end(); +} + +function test_doc_from_triple_stream_to_rdf_stream() { + var streamParser = N3.StreamParser(), + // inputStream = fs.createReadStream('cartoons.ttl'), + streamWriter = N3.StreamWriter({ prefixes: { c: 'http://example.org/cartoons#' } }); + // inputStream.pipe(streamParser); + streamParser.pipe(streamWriter); + // streamWriter.pipe(process.stdout); +} + +function test_doc_blank_nodes_and_lists() { + var writer = N3.Writer({ + prefixes: { + c: 'http://example.org/cartoons#', + foaf: 'http://xmlns.com/foaf/0.1/' + } + }); + writer.addTriple(writer.blank('http://xmlns.com/foaf/0.1/givenName', '"Tom"@en'), + 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', + 'http://example.org/cartoons#Cat'); + writer.addTriple('http://example.org/cartoons#Jerry', + 'http://xmlns.com/foaf/0.1/knows', + writer.blank([{ + predicate: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', + object: 'http://example.org/cartoons#Cat' + }, { + predicate: 'http://xmlns.com/foaf/0.1/givenName', + object: '"Tom"@en', + }])); + writer.addTriple('http://example.org/cartoons#Mammy', + 'http://example.org/cartoons#hasPets', + writer.list([ + 'http://example.org/cartoons#Tom', + 'http://example.org/cartoons#Jerry' + ])); + writer.end(function (error, result) { console.log(result); }); +} + +function test_doc_storing() { + var store = N3.Store(); + store.addTriple('http://ex.org/Pluto', 'http://ex.org/type', 'http://ex.org/Dog'); + store.addTriple('http://ex.org/Mickey', 'http://ex.org/type', 'http://ex.org/Mouse'); + + var mickey = store.find('http://ex.org/Mickey', null, null)[0]; + console.log(mickey.subject, mickey.predicate, mickey.object, '.'); +} + +function test_doc_utility() { + var N3Util = N3.Util; + N3Util.isIRI('http://example.org/cartoons#Mickey'); // true + + N3Util.isLiteral('"Mickey Mouse"'); // true + N3Util.getLiteralValue('"Mickey Mouse"'); // 'Mickey Mouse' + N3Util.isLiteral('"Mickey Mouse"@en'); // true + N3Util.getLiteralLanguage('"Mickey Mouse"@en'); // 'en' + N3Util.isLiteral('"3"^^http://www.w3.org/2001/XMLSchema#integer'); // true + N3Util.getLiteralType('"3"^^http://www.w3.org/2001/XMLSchema#integer'); // 'http://www.w3.org/2001/XMLSchema#integer' + N3Util.isLiteral('"http://example.org/"'); // true + N3Util.getLiteralValue('"http://example.org/"'); // 'http://example.org/' + + N3Util.isLiteral('"This word is "quoted"!"'); // true + N3Util.isLiteral('"3"^^http://www.w3.org/2001/XMLSchema#integer'); // true + + N3.Parser().parse(' "This word is \\"quoted\\"!".', console.log); + // { subject: 'a', predicate: 'b', object: '"This word is "quoted"!"' } + + N3Util.createLiteral('My text', 'en-gb'); + N3Util.createLiteral('123', 'http://www.w3.org/2001/XMLSchema#integer'); + N3Util.createLiteral(123); + N3Util.createLiteral(false); + + N3Util.isBlank('_:b1'); // true + N3Util.isIRI('_:b1'); // false + N3Util.isLiteral('_:b1'); // false + + var prefixes = { rdfs: 'http://www.w3.org/2000/01/rdf-schema#' }; + N3Util.isPrefixedName('rdfs:label'); // true; + N3Util.expandPrefixedName('rdfs:label', prefixes); // http://www.w3.org/2000/01/rdf-schema#label +} diff --git a/n3/n3.d.ts b/n3/n3.d.ts new file mode 100644 index 0000000000..28debcb94b --- /dev/null +++ b/n3/n3.d.ts @@ -0,0 +1,116 @@ +// Type definitions for N3 v0.8.1 +// Project: https://github.com/RubenVerborgh/N3.js +// Definitions by: Fred Eisele +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare module "n3" { + import * as fs from "fs"; + import * as stream from "stream"; + + namespace N3 { + + type ErrorCallback = (err: Error, result: any) => void; + + interface Prefixes { + [key: string]: string; + } + + interface LiteralValue { + value: string | number; + } + + interface Triple { + subject: string, + predicate: string, + object: string + } + + interface BlankTriple { + predicate: string, + object: string + } + + // class Parser { + // constructor(options?: ParserOptions); + // } + function Parser(options?: ParserOptions): N3Parser; + // class StreamParser extends Parser { + // constructor(options?: ParserOptions); + //} + function StreamParser(options?: ParserOptions): N3StreamParser; + + interface ParserOptions { + format?: string, + prefixes?: string[] + } + + interface ParseCallback { + (error: Error, triple: Triple, prefixes: Prefixes): void; + } + + interface Logger { + (message?: any, ...optionalParams: any[]): void; + } + interface N3Parser { + parse(input: string, callback: ParseCallback): void; + parse(subject: string, predicate: string, object: string): void; + parse(triple: Triple): void; + parse(stream: fs.ReadStream, log: Logger): void; + } + + interface N3StreamParser extends N3Parser { + pipe(consumer: stream.Writable | N3StreamWriter): void; + } + + // interface fs.ReadStream { + // pipe(parser: StreamParser): void; + // } + + + function Writer(options: WriterOptions): N3Writer; + function Writer(fd: any, options: WriterOptions): N3Writer; + + function StreamWriter(options: WriterOptions): N3StreamWriter; + + interface N3Writer { + addTriple(subject: string, predicate: string, object: string): void; + addTriple(subject: string, predicate: string, object: string[]): void; + addTriple(triple: Triple): void; + end(err?: ErrorCallback, result?: any): void; + blank(ns: string, name: string): string; + blank(triple: BlankTriple[]): string; + list(triple: string[]): string[]; + } + + interface N3StreamWriter extends N3Writer { + pipe(consumer: stream.Writable): void; + } + + interface WriterOptions { + format?: string, + prefixes?: Prefixes + } + + interface N3StoreWriter extends N3Writer { + find(subject: string, predicate: string | null, object: string | null): Triple[]; + } + function Store(): N3StoreWriter; + + namespace Util { + function createLiteral(value: any): string; + function createLiteral(value: any, type: string): string; + + function isIRI(value: string): boolean; + function isLiteral(value: string): boolean; + function getLiteralValue(value: string): string; + function getLiteralLanguage(value: string): string; + function getLiteralType(value: string): string; + function isBlank(value: string): boolean; + function isPrefixedName(name: string): boolean; + function expandPrefixedName(name: string, prefixes: Prefixes): string; + } + } + export = N3; +} From 4b40619a6cd1174a8f5ad371bef3cb7c6518662b Mon Sep 17 00:00:00 2001 From: Fred Eisele Date: Fri, 14 Oct 2016 14:33:02 -0500 Subject: [PATCH 2/4] trying to trigger Travis CI --- n3/n3-tests.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/n3/n3-tests.ts b/n3/n3-tests.ts index d506bb6f0a..a5b97fd891 100644 --- a/n3/n3-tests.ts +++ b/n3/n3-tests.ts @@ -27,6 +27,11 @@ function test_serialize() { }); } +/** +The following tests are taken from ... +https://github.com/RubenVerborgh/N3.js/blob/master/README.md +*/ + function test_doc_rdf_to_triples_1() { var parser = N3.Parser(); parser.parse('@prefix c: .\n' + From 6af6b73bf39c5f6b9b2bdf38e6eebcc5ca0fc85e Mon Sep 17 00:00:00 2001 From: Fred Eisele Date: Fri, 14 Oct 2016 14:36:28 -0500 Subject: [PATCH 3/4] typescript 2.0 features not yet available --- n3/n3.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/n3/n3.d.ts b/n3/n3.d.ts index 28debcb94b..8531a89258 100644 --- a/n3/n3.d.ts +++ b/n3/n3.d.ts @@ -94,7 +94,7 @@ declare module "n3" { } interface N3StoreWriter extends N3Writer { - find(subject: string, predicate: string | null, object: string | null): Triple[]; + find(subject: string, predicate: string, object: string): Triple[]; } function Store(): N3StoreWriter; From aa1764792c127012009b847a0999a716f05427bb Mon Sep 17 00:00:00 2001 From: Fred Eisele Date: Fri, 14 Oct 2016 14:39:27 -0500 Subject: [PATCH 4/4] typescript 2.0 features not yet available --- n3/n3-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/n3/n3-tests.ts b/n3/n3-tests.ts index a5b97fd891..0ca9c5385c 100644 --- a/n3/n3-tests.ts +++ b/n3/n3-tests.ts @@ -180,7 +180,7 @@ function test_doc_utility() { N3Util.isIRI('_:b1'); // false N3Util.isLiteral('_:b1'); // false - var prefixes = { rdfs: 'http://www.w3.org/2000/01/rdf-schema#' }; + var prefixes: N3.Prefixes = { rdfs: 'http://www.w3.org/2000/01/rdf-schema#' }; N3Util.isPrefixedName('rdfs:label'); // true; N3Util.expandPrefixedName('rdfs:label', prefixes); // http://www.w3.org/2000/01/rdf-schema#label }