linkeddata / rdflib.js

Linked Data API for JavaScript
http://linkeddata.github.io/rdflib.js/doc/
Other
566 stars 143 forks source link

Documentation examples how to use the library #656

Open elmeerr opened 1 week ago

elmeerr commented 1 week ago

Hi,

I was suggested to use this library in order to read a TTL and apply a SPARQL on it. This is basically the first time I ever heard of SPARQL / TTL files so, maybe for that reason I can't read the available documentation, I mean...the docs talks a lot about RDF, about turtles and so but I could not find a single example of library usage.

Is there anyone who has an example showing use cases of reading a ttl or using the query function for example? Apparently it would be way more easier to get information out of a TTL file by using this method. Right now my only option is to use N3 library and read the parsed data, one by one and extract the information I need based on IF/ELSE...it can be done but not ideal if there is an alternative where I could just use a SPARQL which would give me what I need fro a TTL file.

Thanks in advance,

angelo-v commented 5 days ago

Hi, I agree the documentation is very bad.

You can read in a turtle (ttl) file and use the parse method to get the data into an rdflib store:

see https://github.com/solidos/solid-tutorial-rdflib.js?tab=readme-ov-file#load-data-into-the-store-from-a-bufferstring

I am not sure how to do a SPARQL query right now. Perhaps @jeff-zucker can help out with that, otherwise I need to look it up myself.

angelo-v commented 5 days ago

From what I read from the API docs you could use SPARQLToQuery to convert textual SPARQL to a query object that can than be passed to querySync but I did not try this

jeff-zucker commented 4 days ago

If you are not familiar with either RDF (turtle) or SPARQL, you may be better off starting with rdflib's built in querying functions like match() and any(). These work by matching a triple pattern. So if you want to find out everyone who is 21 years old

 let statements = store.match( null, SCHEMA('age'), 21);
 for( let statement of statements){
    console.log(statement.subject.value)
}

This boils down to - find all statements, regardless of subject, that have an age (as defined in the SCHEMA vocabulary) equal to the literal value '21' and then cycle through the statements printing the value of the subject.

All of this is documented in https://linkeddata.github.io/rdflib.js/Documentation/webapp-intro.html. But if that's not enough explanation, drop into the matrix chatroom for rdflib and ask as many questions as it takes.

If you want to use SPARQL instead, you can do something like this :

     const preparedQuery = await $rdf.SPARQLToQuery( queryString, false, store );                                                                              
      store.query(preparedQuery, (results)=>{      
        // process results                                         
      }                                                                          

But if SPARQL is your goal, you may be better off with comunica which has a more robust SPARQL engine than rdflib does.

elmeerr commented 4 days ago

@angelo-v @jeff-zucker thanks for the reply. I already contact comunica as well...main goal is to be able to retrieve data from turtle in a more "practical"...for example now after reading "manually" I end up on a situation where I actually need everything based on predicate X...while I was checking only subject to retrieve data and the specific predicate can be part of many subjects...so, kind of tricky doing that the way I am doing...I believe you SCHEMA example is exactly what I need...I will give a try.

Thanks again

elmeerr commented 4 days ago

@angelo-v from your example, it requires a URI but the example shows a URL to a TTL file...while my file is a stream (converted to string) so I don't have a URI to provide. Using N3 I can just parse(string) (which is my turtle) and have all its content available...I am trying to do the same with rdflib but no luck, yet.

elmeerr commented 4 days ago

The last piece of puzzy I need to figure is this error I am having now: Error: TypeError: Cannot read properties of undefined (reading 'decode') while trying to parse <http://www.w3.org/1999/02/22-rdf-syntax-ns> as text/turtle I am sure the error is related to the URI I am using as parameter in the parse function but here I don't really know which URI I should use...I have tried with one URI with a specific turtle and it worked but it does not work with some other files (I am reading many different files) so I am not sure which URI should be given to work with all (all files comes form the same source)

angelo-v commented 4 days ago

You can parse a turtle string with parse. The URL you need to pass into it is just used as a base URL for the relative URLs in the document. I am using it a lot in unit tests, see e.g. here https://github.com/solid-contrib/data-modules/blob/e8596345b131884bfa325a53cee4b45df5bb556a/utils/rdflib/src/queries/TypeIndexQuery.spec.ts#L11

So if the URI does not matter to you, you can basically pass in any URI you like, it does not need to resolve (since it won't be resolved), you just need to make sure you use the correct URIs in your query afterwards, since the relative ones will be based on that URI.

jeff-zucker commented 4 days ago

The last piece of puzzy I need to figure is this error I am having now: Error: TypeError: Cannot read properties of undefined (reading 'decode') while trying to parse <http://www.w3.org/1999/02/22-rdf-syntax-ns> as text/turtle

If that URL is in a prefix declaration, it is missing a # at the end. If it is being used as a predicate you need something like #type (the predicate name) at the end.

elmeerr commented 3 days ago

@jeff-zucker that's was one of the trials I made...I finally got it working! thanks for the support!!!

angelo-v commented 2 days ago

Im am happy you could solve it. Documentation should nevertheless improve on that regard