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

Commit

Permalink
ARC2: added CommonNamespaces as constructor parameter (BC break!) ...
Browse files Browse the repository at this point in the history
* support prefixed URI usage in addStatements and deleteMatchingStatements,
if you registered your namespace in CommonNamespaces instance before usage
* usage of nodeFactory only in StoreAbstractTest
* added two tests to prove support for prefixed URIs
* fixed two assert functions in TestCase
  • Loading branch information
k00ni committed Jun 8, 2017
1 parent 2f2833d commit 428886f
Show file tree
Hide file tree
Showing 4 changed files with 317 additions and 168 deletions.
58 changes: 50 additions & 8 deletions src/Saft/Addition/ARC2/Store/ARC2.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Saft\Addition\ARC2\Store;

use Saft\Rdf\CommonNamespaces;
use Saft\Rdf\NamedNode;
use Saft\Rdf\NamedNodeImpl;
use Saft\Rdf\Node;
Expand Down Expand Up @@ -35,6 +36,11 @@ class ARC2 extends AbstractSparqlStore
*/
protected $nodeFactory = null;

/**
* @var CommonNamespaces
*/
protected $commonNamespaces = null;

/**
* @var RdfHelpers
*/
Expand Down Expand Up @@ -80,9 +86,11 @@ public function __construct(
ResultFactory $resultFactory,
StatementIteratorFactory $statementIteratorFactory,
RdfHelpers $rdfHelpers,
CommonNamespaces $commonNamespaces,
array $configuration
) {
$this->configuration = $configuration;
$this->commonNamespaces = $commonNamespaces;

// Open connection
$this->openConnection();
Expand Down Expand Up @@ -145,11 +153,29 @@ public function addStatements($statements, Node $graph = null, array $options =
}
// else: non-concrete Statement instances not allowed

// use full URIs, if available
$s = $statement->getSubject();
if ($s->isNamed()) {
$s = $this->nodeFactory->createNamedNode(
$this->commonNamespaces->extendUri($statement->getSubject()->getUri())
);
}
$p = $this->nodeFactory->createNamedNode(
$this->commonNamespaces->extendUri($statement->getPredicate()->getUri())
);
$o = $statement->getObject();
if ($o->isNamed()) {
$o = $this->nodeFactory->createNamedNode(
$this->commonNamespaces->extendUri($statement->getObject()->getUri())
);
}

// execute query
$this->query(
'INSERT INTO <'. $graphUriToUse .'> {'
. $this->rdfHelpers->getNodeInSparqlFormat($statement->getSubject()) . ' '
. $this->rdfHelpers->getNodeInSparqlFormat($statement->getPredicate()) . ' '
. $this->rdfHelpers->getNodeInSparqlFormat($statement->getObject()) . ' . '
. $this->rdfHelpers->getNodeInSparqlFormat($s) . ' '
. $this->rdfHelpers->getNodeInSparqlFormat($p) . ' '
. $this->rdfHelpers->getNodeInSparqlFormat($o) . ' . '
. '}',
$options
);
Expand Down Expand Up @@ -217,12 +243,28 @@ public function deleteMatchingStatements(
$graph = $statement->getGraph();
}

// use full URIs, if available
$s = $statement->getSubject();
if ($s->isNamed()) {
$s = $this->nodeFactory->createNamedNode(
$this->commonNamespaces->extendUri($statement->getSubject()->getUri())
);
}
$p = $statement->getPredicate();
if ($p->isNamed()) {
$p = $this->nodeFactory->createNamedNode(
$this->commonNamespaces->extendUri($statement->getPredicate()->getUri())
);
}
$o = $statement->getObject();
if ($o->isNamed()) {
$o = $this->nodeFactory->createNamedNode(
$this->commonNamespaces->extendUri($statement->getObject()->getUri())
);
}

// create triple statement, because we have to handle the graph extra
$tripleStatement = $this->statementFactory->createStatement(
$statement->getSubject(),
$statement->getPredicate(),
$statement->getObject()
);
$tripleStatement = $this->statementFactory->createStatement($s, $p, $o);

$statementIterator = $this->statementIteratorFactory->createStatementIteratorFromArray(
array($tripleStatement)
Expand Down
11 changes: 8 additions & 3 deletions src/Saft/Addition/ARC2/Test/ARC2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Saft\Addition\ARC2\Store\ARC2;
use Saft\Rdf\AnyPatternImpl;
use Saft\Rdf\ArrayStatementIteratorImpl;
use Saft\Rdf\CommonNamespaces;
use Saft\Rdf\LiteralImpl;
use Saft\Rdf\NamedNode;
use Saft\Rdf\NamedNodeImpl;
Expand Down Expand Up @@ -41,6 +42,7 @@ public function setUp()
new ResultFactoryImpl(),
new StatementIteratorFactoryImpl(),
new RdfHelpers(),
new CommonNamespaces(),
$this->configuration['arc2Config']
);
} catch (\Exception $e) {
Expand Down Expand Up @@ -90,7 +92,7 @@ protected function countTriples(NamedNode $graph)
// gets deleted.
public function testDropGraphEffects()
{
$secondGraph = new NamedNodeImpl(new RdfHelpers(), $this->testGraph->getUri() . '2');
$secondGraph = $this->nodeFactory->createNamedNode($this->testGraph->getUri() . '2');

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

Expand Down Expand Up @@ -150,6 +152,7 @@ public function testOpenConnectionCheckDatabase()
new ResultFactoryImpl(),
new StatementIteratorFactoryImpl(),
new RdfHelpers(),
new CommonNamespaces(),
array()
);
}
Expand All @@ -166,6 +169,7 @@ public function testOpenConnectionCheckHost()
new ResultFactoryImpl(),
new StatementIteratorFactoryImpl(),
new RdfHelpers(),
new CommonNamespaces(),
array('database' => 'saft')
);
}
Expand All @@ -182,6 +186,7 @@ public function testOpenConnectionCheckUsername()
new ResultFactoryImpl(),
new StatementIteratorFactoryImpl(),
new RdfHelpers(),
new CommonNamespaces(),
array('database' => 'saft', 'host' => 'localhost')
);
}
Expand Down Expand Up @@ -242,8 +247,8 @@ public function testRemovalOfTriplesAfterDropGraph()
),
new StatementImpl(
$this->testGraph,
new NamedNodeImpl(new RdfHelpers(), 'http://foobar/1'),
new NamedNodeImpl(new RdfHelpers(), 'http://foobar/2'),
$this->nodeFactory->createNamedNode('http://foobar/1'),
$this->nodeFactory->createNamedNode('http://foobar/2'),
$this->testGraph
)
)
Expand Down
Loading

0 comments on commit 428886f

Please sign in to comment.