Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Commit

Permalink
ARC2: fixed createGraph, dropGraph and addStatements
Browse files Browse the repository at this point in the history
- added test to check new "old" behavior
- the problem was, that creating and droping graphs, and add
statements between, ARC2 wasn't creating the data at all
  • Loading branch information
k00ni committed Jun 23, 2017
1 parent bc3ff46 commit 6a13ab8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 42 deletions.
56 changes: 16 additions & 40 deletions src/Saft/Addition/ARC2/Store/ARC2.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function __construct(
}

/**
* Adds multiple Statements to (default-) graph. It overrides parents addStatements because ARC2 only
* Adds multiple Statements to graph. It overrides parents addStatements because ARC2 only
* supports SPARQL+ and not SPARQL Update 1.1, which means an INSERT INTO query has to look like:
* INSERT INTO <http://graph/> { triple ... }.
*
Expand All @@ -131,6 +131,7 @@ public function __construct(
* @param array $options optional Key-value pairs which provide additional
* introductions for the store and/or its
* adapter(s).
* @throws \Exception if one of the given Statements is not concrete.
*/
public function addStatements($statements, Node $graph = null, array $options = array())
{
Expand Down Expand Up @@ -170,15 +171,19 @@ public function addStatements($statements, Node $graph = null, array $options =
);
}

// execute query
$this->query(
'INSERT INTO <'. $graphUriToUse .'> {'
$query = 'INSERT INTO <'. $graphUriToUse .'> {'
. $this->rdfHelpers->getNodeInSparqlFormat($s) . ' '
. $this->rdfHelpers->getNodeInSparqlFormat($p) . ' '
. $this->rdfHelpers->getNodeInSparqlFormat($o) . ' . '
. '}',
$options
);
. '}';

// execute query
$res = $this->store->query($query, $options);

if (0 == $res) {
var_dump($this->store->getErrors());
throw new \Exception('Insert query failed: '. $query);
}
}
}

Expand All @@ -192,17 +197,11 @@ public function addStatements($statements, Node $graph = null, array $options =
*/
public function createGraph(NamedNode $graph, array $options = array())
{
// table names
$g2t = $this->configuration['table-prefix'] . '_g2t';
$id2val = $this->configuration['table-prefix'] . '_id2val';

// check if there is already a graph with the given URI
$result = $this->store->queryDB(
'SELECT id FROM '. $id2val .' WHERE val = "'. $graph->getUri() .'";',
$this->store->getDBCon()
);
if (false === isset($this->getGraphs()[$graph->getUri()])) {
// table names
$g2t = $this->configuration['table-prefix'] . '_g2t';
$id2val = $this->configuration['table-prefix'] . '_id2val';

if (0 == $result->num_rows) {
/*
* for id2val table
*/
Expand Down Expand Up @@ -290,30 +289,7 @@ public function deleteMatchingStatements(
*/
public function dropGraph(NamedNode $graph, array $options = array())
{
// let ARC2 remove all triples first, his way
$this->store->query('DELETE FROM <'. $graph->getUri() .'>');

// table names
$g2t = $this->configuration['table-prefix'] . '_g2t';
$id2val = $this->configuration['table-prefix'] . '_id2val';

/*
* ask for all entries with the given graph URI
*/
$query = 'SELECT id FROM '. $id2val .' WHERE val = "'. $graph->getUri() .'"';
$result = $this->store->queryDB($query, $this->store->getDBCon());

/*
* go through all given entries and remove all according entries in the g2t table
*/
while ($row = $result->fetch_assoc()) {
$query = 'DELETE FROM '. $g2t .' WHERE t="'. $row['id'] .'"';
$this->store->queryDB($query, $this->store->getDBCon());
}

// remove entry/entries in the id2val table too
$query = 'DELETE FROM '. $id2val .' WHERE val = "'. $graph->getUri() .'"';
$this->store->queryDB($query, $this->store->getDBCon());
}

/**
Expand Down
50 changes: 48 additions & 2 deletions src/Saft/Addition/ARC2/Test/ARC2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public function testDropGraphEffects()

$this->fixture->createGraph($secondGraph);

$this->fixture->query('CLEAR GRAPH <'. $this->testGraph->getUri().'>');
$this->fixture->query('CLEAR GRAPH <'. $secondGraph->getUri().'>');
$this->fixture->query('CLEAR GRAPH <'. $this->testGraph->getUri() .'>');
$this->fixture->query('CLEAR GRAPH <'. $secondGraph->getUri() .'>');

// fill graph 1
$this->fixture->addStatements(
Expand Down Expand Up @@ -202,4 +202,50 @@ public function testQueryAddAndQueryStatementsDefaultGraph()
// See: https://github.com/openlink/virtuoso-opensource/issues/417
$this->markTestSkipped('ARC2 does not grant read and write access to the default graph.');
}

// tests how it reacts, when it is called with multiple dropGraph calls and addStatements between
public function testRegressionMultipleDropGraphsWithAddStatements()
{
// recreate graph
$this->fixture->dropGraph($this->testGraph);
$this->assertEquals(0, count($this->fixture->getGraphs()));

$this->fixture->createGraph($this->testGraph);
$this->assertEquals(1, count($this->fixture->getGraphs()));

// add data
$this->fixture->addStatements(array(
$this->statementFactory->createStatement(
$this->nodeFactory->createNamedNode('http://foo/1'),
$this->nodeFactory->createNamedNode('http://foo/2'),
$this->nodeFactory->createNamedNode('http://foo/3')
)
),
$this->testGraph
);

$results = $this->fixture->query('SELECT ?s ?p ?o FROM <'. $this->testGraph .'> WHERE {?s ?p ?o.}');
$this->assertEquals(1, count($results));

// recreate graph
$this->fixture->dropGraph($this->testGraph);
$this->fixture->createGraph($this->testGraph);

// add data
$this->fixture->addStatements(
array(
$this->statementFactory->createStatement(
$this->nodeFactory->createNamedNode($this->testGraph . '1'),
$this->nodeFactory->createNamedNode($this->testGraph . '2'),
$this->nodeFactory->createNamedNode($this->testGraph . '3')
)
),
$this->testGraph
);

$results = $this->fixture->query('SELECT * FROM <'. $this->testGraph .'> WHERE {?s ?p ?o.}');
$this->assertEquals(1, count($results));

$this->fixture->dropGraph($this->testGraph);
}
}

0 comments on commit 6a13ab8

Please sign in to comment.