<commit_before>import * as N3 from "n3";
import * as RDF from "rdf-js";
import * as fs from "fs";
import * as stream from "stream";

function test_add_prefix() {
    const writer: N3.N3Writer = new N3.Writer();

    writer.addPrefix('xsd', 'http://www.w3.org/2001/XMLSchema#');
    writer.end((error, result) => {
        console.log(result);
    });
}

function test_add_prefixes() {
    const writer: N3.N3Writer = new N3.Writer();

    writer.addPrefixes({
        freebase: N3.DataFactory.namedNode("http://rdf.freebase.com/ns/"),
        xsd: N3.DataFactory.namedNode("http://www.w3.org/2001/XMLSchema#")
    });

    writer.end((error, result) => {
        console.log(result);
    });
}

function test_serialize() {
    const writer: N3.N3Writer = new 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.addQuad(N3.DataFactory.quad(
      N3.DataFactory.namedNode("subject-name"),
      N3.DataFactory.namedNode("predicate-name"),
      N3.DataFactory.literal(12)
    ));

    writer.end((error, result) => {
        console.log(`result ${result}`);
    });
}

/*
The following tests are taken from ...
https://github.com/RubenVerborgh/N3.js/blob/master/README.md
*/

function test_doc_rdf_to_triples_1() {
    const parser: N3.N3Parser = new N3.Parser();
    parser.parse(`@prefix c: <http://example.org/cartoons#>.
        c:Tom a c:Cat.
        c:Jerry a c:Mouse;
                c:smarterThan c:Tom.`,
        (error: Error, quad: RDF.Quad, prefixes: N3.Prefixes) => {
            if (quad)
                console.log(quad.subject, quad.predicate, quad.object, quad.graph, '.');
            else
                console.log("# That's all, folks!", prefixes);
        });
}

function test_doc_rdf_to_triples_2() {
    const parser1: N3.N3Parser = new N3.Parser({ format: 'N-Triples' });
    const parser2: N3.N3Parser = new N3.Parser({ format: 'application/trig' });
    // Notation3 (N3) is supported only through the format argument:

    const parser3: N3.N3Parser = N3.Parser({ format: 'N3' });
    const parser4: N3.N3Parser = N3.Parser({ format: 'Notation3' });
    const parser5: N3.N3Parser = N3.Parser({ format: 'text/n3' });
}

function test_doc_rdf_stream_to_triples_1() {
    const parser: N3.N3Parser = new N3.Parser();
    parser.parse('abc', console.log);

    const streamParser: N3.N3StreamParser = N3.StreamParser();
    const quad: RDF.Quad = streamParser.read();
    const rdfStream = fs.createReadStream('cartoons.ttl');
    const pipedStreamParser: N3.N3StreamParser = rdfStream.pipe(streamParser);
    streamParser.pipe(new class SlowConsumer extends stream.Writable {
        constructor() {
            super({ objectMode: true });
            this._write = (quad: RDF.Quad, encoding, done) => {
                console.log(quad);
                setTimeout(done, 1000);
            };
        }
    }());
}
<commit_msg>Added check whether N3 datafactory can be passed as option where Rdf DataFactory is expected<commit_after>import * as N3 from "n3";
import * as RDF from "rdf-js";
import * as fs from "fs";
import * as stream from "stream";

function test_add_prefix() {
    const writer: N3.N3Writer = new N3.Writer();

    writer.addPrefix('xsd', 'http://www.w3.org/2001/XMLSchema#');
    writer.end((error, result) => {
        console.log(result);
    });
}

function test_add_prefixes() {
    const writer: N3.N3Writer = new N3.Writer();

    writer.addPrefixes({
        freebase: N3.DataFactory.namedNode("http://rdf.freebase.com/ns/"),
        xsd: N3.DataFactory.namedNode("http://www.w3.org/2001/XMLSchema#")
    });

    writer.end((error, result) => {
        console.log(result);
    });
}

function test_serialize() {
    const writer: N3.N3Writer = new 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.addQuad(N3.DataFactory.quad(
      N3.DataFactory.namedNode("subject-name"),
      N3.DataFactory.namedNode("predicate-name"),
      N3.DataFactory.literal(12)
    ));

    writer.end((error, result) => {
        console.log(`result ${result}`);
    });
}

/*
The following tests are taken from ...
https://github.com/RubenVerborgh/N3.js/blob/master/README.md
*/

function test_doc_rdf_to_triples_1() {
    const parser: N3.N3Parser = new N3.Parser();
    parser.parse(`@prefix c: <http://example.org/cartoons#>.
        c:Tom a c:Cat.
        c:Jerry a c:Mouse;
                c:smarterThan c:Tom.`,
        (error: Error, quad: RDF.Quad, prefixes: N3.Prefixes) => {
            if (quad)
                console.log(quad.subject, quad.predicate, quad.object, quad.graph, '.');
            else
                console.log("# That's all, folks!", prefixes);
        });
}

function test_doc_rdf_to_triples_2() {
    const parser1: N3.N3Parser = new N3.Parser({ format: 'N-Triples' });
    const parser2: N3.N3Parser = new N3.Parser({ format: 'application/trig' });
    // Notation3 (N3) is supported only through the format argument:

    const parser3: N3.N3Parser = N3.Parser({ format: 'N3' });
    const parser4: N3.N3Parser = N3.Parser({ format: 'Notation3' });
    const parser5: N3.N3Parser = N3.Parser({ format: 'text/n3' });
}

function test_doc_rdf_stream_to_triples_1() {
    const parser: N3.N3Parser = new N3.Parser({factory: N3.DataFactory});
    parser.parse('abc', console.log);

    const streamParser: N3.N3StreamParser = N3.StreamParser();
    const quad: RDF.Quad = streamParser.read();
    const rdfStream = fs.createReadStream('cartoons.ttl');
    const pipedStreamParser: N3.N3StreamParser = rdfStream.pipe(streamParser);
    streamParser.pipe(new class SlowConsumer extends stream.Writable {
        constructor() {
            super({ objectMode: true });
            this._write = (quad: RDF.Quad, encoding, done) => {
                console.log(quad);
                setTimeout(done, 1000);
            };
        }
    }());
}

{'fraction_non_alphanumeric': 0.15524967989756722, 'fraction_numerical': 0.02528809218950064, 'mean_word_length': 2.528514963297572, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7208738', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2104, 'n_words': 411}