blake-regalia / graphy.js

A collection of RDF libraries for JavaScript
https://graphy.link/
ISC License
161 stars 3 forks source link

Reader and Writer for Concise Triples Missing #26

Open houghtonap opened 4 years ago

houghtonap commented 4 years ago

Concise Triples (C3) are useful, but I noticed that there are no reader or writer for them:

const reader = require( '@graphy/content.c3.read' ) ;
const writer = require( '@graphy/content.c3.write' ) ;

This makes it more difficult to stream data, for example, one would really like to do:

fs.createReadStream( './c3.js' )
  .pipe ( reader(  ) )
;
blake-regalia commented 4 years ago

@houghtonap the readers and writers are for serialization formats, whereas c3 is more about programmatically creating RDF.

Based on your example, I think what you might be after are the factory methods for generating triples/quads. For example, factory.c3 accepts a c3 object and yields triples.

const factory = require('@graphy/core.data.factory');
let prefixes = {
  rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
  rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
  dbo: 'http://dbpedia.org/ontology/',
  dbr: 'http://dbpedia.org/resource/',
};

let triples = factory.c3({
  'dbr:Banana': {
    'a': 'dbo:Fruit',
    'rdfs:label': [
      'Banana@en',
      'Bananae@fr',
    ],
  },
}, prefixes);

for(let triple of triples) {
  console.log(triple+'');
}

If you had something else in mind, could you expand on your use case a bit more?

houghtonap commented 4 years ago

I have a large database (millions of rows) and need to pull select data subsets out and combine with other RDF data. I can pull this data subset by creating JSON-LD or Concise Triples. IMHO Concise Triples are simpler to deal with than JSON-LD and I would also like to use graphy, quadstore and quadstore-sparql to handle loading and analysis of the data. Graphy has support for Concise Triples, but not JSON-LD. I pulled the data subset and created Concise Triples and would prefer to stream that data, but it looks like graphy's implementation of Concise Triples wasn't meant for this use case :(

I had a question on the above snippet using core.data.factory. Does it produce RDFJS compatible triples/quads?

blake-regalia commented 4 years ago

I had a question on the above snippet using core.data.factory. Does it produce RDFJS compatible triples/quads?

Yes, triple from the example is an instance of Quad which implements RDFJS Quad. In fact, every available RDFJS interface is implemented in graphy where applicable.

Concise triples are about constructing RDF programmatically either for passing Quads around in memory or for serializing to some RDF format such as Turtle, RDF/XML, etc. Concise triples are not a serialization format but more of an API.

I am still a little unclear about your use case. Do you need to serialize the data to disk between selects and reads or can you pass objects in memory? If you can pass them in memory, using the factory.c3 iterator will give you the fastest results and keep a very low memory profile. If you need to serialize RDF and then deserialize it later, you will need to choose a serialization format (and can still use c3 to create the quads in memory before passing them to the serializer).