Skip to content

Commit 7e77a4a

Browse files
committed
README: added second example (sparql client)
1 parent cd19b6d commit 7e77a4a

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ Sending SPARQL queries can be done via `query` method.
1919
Your data is stored inside an in-memory SQLite database file.
2020
**After the script ends all your data inside the store will be gone**.
2121

22-
### Example
22+
## Examples
23+
24+
### Example 1 (First steps)
2325

2426
Create a store instance, load a few triples into it and run a query.
2527

@@ -42,6 +44,47 @@ echo \count($triples); // outputs: 2
4244
// $triples contains result set, which consists of arrays and scalar values
4345
```
4446

47+
### Example 2 (Consume SPARQL client result)
48+
49+
Use `sweetrdf/sparql-client` and a PSR-7-compatible library (like `guzzlehttp/guzzle`) to query a SPARQL endpoint.
50+
For now you have to "transform" SPARQL result set to a quad-list manually.
51+
Afterwards add quads to store and query it.
52+
Check test `testSparqlClientCompatibility` in [InMemoryStoreSqliteTest.php](tests/Integration/Store/InMemoryStoreSqliteTest.php) for a working example.
53+
54+
```php
55+
use GuzzleHttp\Client;
56+
use GuzzleHttp\Psr7\Request;
57+
use simpleRdf\Connection;
58+
use simpleRdf\DataFactory;
59+
60+
/*
61+
* get data from a SPARQL endpoint
62+
*/
63+
$httpClient = new Client();
64+
$dataFactory = new DataFactory();
65+
$connection = new Connection($httpClient, $dataFactory);
66+
$query = 'SELECT * WHERE {?s ?p ?o} limit 5';
67+
// set SPARQL endpoint URL
68+
$url = 'https://arche-sparql.acdh-dev.oeaw.ac.at/sparql?query=';
69+
$query = new Request('GET', $url.rawurlencode($query));
70+
$statement = $connection->query($query);
71+
72+
/*
73+
* add result to the store
74+
*/
75+
$quads = [];
76+
foreach ($statement as $entry) {
77+
// $entry is an object; s, p and o are variables from your query
78+
$quads[] = $dataFactory->quad($entry->s, $entry->p, $entry->o);
79+
}
80+
$store = InMemoryStoreSqlite::createInstance();
81+
$store->addQuads($quads);
82+
83+
// send query and check result
84+
$result = $store->query('SELECT * WHERE {?s ?p ?o.}');
85+
echo count($result['result']['rows']); // outputs 5
86+
```
87+
4588
## SPARQL support
4689

4790
Store supports a lot of SPARQL 1.0/1.1 features.

tests/Integration/Store/InMemoryStoreSqliteTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ public function testSparqlClientCompatibility()
184184
/*
185185
* add result to the store
186186
*/
187-
$dataFactory = new DataFactory();
188187
$quads = [];
189188
foreach ($statement as $entry) {
190189
$quads[] = $dataFactory->quad($entry->s, $entry->p, $entry->o);

0 commit comments

Comments
 (0)