From f043ef0eaa89230c696c116e1de7f8b8c84be53c Mon Sep 17 00:00:00 2001 From: Markus Lanthaler Date: Sat, 2 Feb 2013 21:54:11 +0100 Subject: [PATCH] Introduce Graph as a first step towards supporting named graphs in parsed documents Furthermore, NodeInterface was created so that more specialized nodes can be created in custom implementations. This addresses #15. --- Document.php | 140 ++++++--------- DocumentInterface.php | 76 ++++---- Graph.php | 202 ++++++++++++++++++++++ GraphInterface.php | 99 +++++++++++ Node.php | 206 ++++++---------------- NodeInterface.php | 210 +++++++++++++++++++++++ Processor.php | 7 +- README.md | 9 +- Test/{DocumentTest.php => GraphTest.php} | 202 +++++++++++----------- 9 files changed, 769 insertions(+), 382 deletions(-) create mode 100644 Graph.php create mode 100644 GraphInterface.php create mode 100644 NodeInterface.php rename Test/{DocumentTest.php => GraphTest.php} (73%) diff --git a/Document.php b/Document.php index 7be58a2..8832f8d 100644 --- a/Document.php +++ b/Document.php @@ -21,33 +21,19 @@ class Document implements DocumentInterface { /** - * The base IRI - * - * @var IRI + * @var IRI The document's IRI */ - private $baseIri = null; + private $iri = null; /** - * An associative array holding all nodes in the graph - * - * @var array + * @var GraphInterface The default graph */ - protected $nodes = array(); + private $defaultGraph = null; /** - * A term map containing terms/prefixes mapped to IRIs. This is similar - * to a JSON-LD context but ignores all definitions except the IRI. - * - * @var array + * @var array An associative array holding all named graphs in the document */ - protected $termMap = array(); - - /** - * Blank node counter - * - * @var int - */ - private $blankNodeCounter = 0; + protected $namedGraphs = array(); /** * Parses a JSON-LD document and returns it as a Document @@ -60,8 +46,8 @@ class Document implements DocumentInterface * $document = Document::load('document.jsonld'); * * - * Please note that currently all data is merged into one graph, - * named graphs are not supported yet! + * Please note that currently all data is merged into the + * default graph, named graphs are not supported yet! * * It is possible to configure the processing by setting the options * parameter accordingly. Available options are: @@ -87,116 +73,102 @@ public static function load($document, $options = null) */ public function __construct($iri = null) { - $this->baseIri = new IRI($iri); + $this->iri = new IRI($iri); + $this->defaultGraph = new Graph($this); } /** * {@inheritdoc} */ - public function createNode($id = null) + public function setIri($iri) { - if (!is_string($id) || ('_:' === substr($id, 0, 2))) { - $id = $this->createBlankNodeId(); - $abs_id = $id; - } else { - $id = (string) $this->baseIri->resolve($id); - if (isset($this->nodes[$id])) { - return $this->nodes[$id]; - } - } - - return $this->nodes[$id] = new Node($this, $id); + $this->iri = new IRI($iri); } /** * {@inheritdoc} */ - public function removeNode(Node $node) + public function getIri($asObject = false) { - if ($node->getDocument() === $this) { - $node->removeFromDocument(); - } + return ($asObject) ? $this->iri : (string) $this->iri; + } - $id = $node->getId(); + /** + * {@inheritdoc} + */ + public function createGraph($name) + { + $name = (string) $this->iri->resolve($name); - if (!$node->isBlankNode()) { - $id = (string) $this->baseIri->resolve($id); + if (isset($this->namedGraphs[$name])) { + return $this->namedGraphs[$name]; } - unset($this->nodes[$id]); + return $this->namedGraphs[$name] = new Graph($this, $name); } /** * {@inheritdoc} */ - public function getNodes() + public function getGraph($name = null) { - return array_values($this->nodes); + if (null === $name) { + return $this->defaultGraph; + } + + $name = (string) $this->iri->resolve($name); + + return isset($this->namedGraphs[$name]) + ? $this->namedGraphs[$name] + : null; } /** * {@inheritdoc} */ - public function getNode($id) + public function getGraphNames() { - if (!((strlen($id) >= 2) && ('_:' === substr($id, 0, 2)))) { - $id = (string) $this->baseIri->resolve($id); - } - - return isset($this->nodes[$id]) - ? $this->nodes[$id] - : null; + return array_keys($this->namedGraphs); } /** * {@inheritdoc} */ - public function getNodesByType($type) + public function containsGraph($name) { - if (is_string($type)) { - if (null === ($type = $this->getNode($type))) { - return array(); - } - } + $name = (string) $this->iri->resolve($name); - return $type->getNodesWithThisType(); + return isset($this->namedGraphs[$name]); } /** * {@inheritdoc} */ - public function contains($id) + public function removeGraph($graph = null) { - $node = $id; + if (null === $graph) { + $this->defaultGraph = new Graph($this); - if ($node instanceof Node) { - $id = $node->getId(); + return; } - if ((null === $id) || !is_string($id)) { - return false; - } + $name = $graph; - if ((strlen($id) >= 2) && ('_:' === substr($id, 0, 2))) { - if (isset($this->nodes[$id]) && ($node === $this->nodes[$id])) { - return true; + if ($graph instanceof GraphInterface) { + foreach ($this->namedGraphs as $n => $g) { + if ($g === $graph) { + $name = $n; + break; + } } - - return false; } - $id = (string) $this->baseIri->resolve($id); - - return isset($this->nodes[$id]); - } + if (isset($this->namedGraphs[$name])) { + if ($this->namedGraphs[$name]->getDocument() === $this) { + $this->namedGraphs[$name]->removeFromDocument(); + } - /** - * Create a new blank node identifier unique to the document. - * - * @return string The new blank node identifier. - */ - protected function createBlankNodeId() - { - return '_:b' . $this->blankNodeCounter++; + unset($this->namedGraphs[$name]); + } } } diff --git a/DocumentInterface.php b/DocumentInterface.php index 03e1018..00c8c4b 100644 --- a/DocumentInterface.php +++ b/DocumentInterface.php @@ -19,69 +19,67 @@ interface DocumentInterface { /** - * Creates a new node which is linked to this document + * Set the document's IRI * - * If a blank node identifier or an invalid ID is passed, the ID will be - * ignored and a new blank node identifier unique to the document is - * created for the node. - * - * If there exists already a node with the passed ID in the document, - * that node will be returned instead of creating a new one. + * @param string|IRI The IRI. + */ + public function setIri($iri); + + /** + * Get the document's IRI * - * @param null|string $id The ID of the node. + * @param boolean $asObject If set to true, the return value will be an + * {@link IRI} object; otherwise a string. * - * @return Node The newly created node. + * @return string|IRI The document's IRI (might be empty). */ - public function createNode($id = null); + public function getIri($asObject = false); /** - * Removes a node from the document + * Creates a new graph which is linked to this document * - * This will also eliminate all references to the node within the - * document. + * If there exists already a graph with the passed name in the document, + * that graph will be returned instead of creating a new one. * - * @param Node $node The node to remove from the document. + * @param string|IRI $name The graph's name. + * + * @return GraphInterface The newly created graph. */ - public function removeNode(Node $node); + public function createGraph($name); /** - * Get all nodes + * Get a graph by name + * + * @param null|string $name The name of the graph to retrieve. If null + * is passed, the default will be returned. * - * @return array[Node] Returns an array containing all nodes defined in - * the document. + * @return GraphInterface|null Returns the graph if found; null otherwise. */ - public function getNodes(); + public function getGraph($name = null); /** - * Get a node by ID + * Get graph names * - * @param string $id The ID of the node to retrieve. - * - * @return Node|null Returns the node if found; null otherwise. + * @return array[string] Returns the names of all graphs in the document. */ - public function getNode($id); + public function getGraphNames(); /** - * Get nodes by type + * Check whether the document contains a graph with the specified name * - * @param string|Node $type The type + * @param string $name The graph name. * - * @return array[Node] Returns an array containing all nodes of the - * specified type in the document. + * @return bool Returns true if the document contains a graph with the + * specified name; false otherwise. */ - public function getNodesByType($type); + public function containsGraph($name); /** - * Check whether the document already contains a node with the - * specified ID - * - * @param string|Node $id The node ID to check. Blank node identifiers - * will always return false except a node instance - * which is part of the document will be passed - * instead of a string. + * Removes a graph from the document * - * @return bool Returns true if the document contains a node with the - * specified ID; false otherwise. + * @param null|string|GraphInterface $graph The graph (or its name) to + * remove. If null is passed, + * the default will be reset. */ - public function contains($id); + public function removeGraph($graph = null); } diff --git a/Graph.php b/Graph.php new file mode 100644 index 0000000..cfd67a7 --- /dev/null +++ b/Graph.php @@ -0,0 +1,202 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ML\JsonLD; + +use ML\IRI\IRI; + +/** + * A Graph represents a JSON-LD graph. + * + * @author Markus Lanthaler + */ +class Graph implements GraphInterface +{ + /** + * @var DocumentInterface The document this graph belongs to. + */ + private $document; + + /** + * @var array An associative array holding all nodes in the graph + */ + protected $nodes = array(); + + /** + * A term map containing terms/prefixes mapped to IRIs. This is similar + * to a JSON-LD context but ignores all definitions except the IRI. + * + * @var array + */ + protected $termMap = array(); + + /** + * @var int Blank node counter + */ + private $blankNodeCounter = 0; + + /** + * Constructor + * + * @param DocumentInterface $document The document the graph belongs to. + */ + public function __construct(DocumentInterface $document = null) + { + $this->document = $document; + } + + /** + * {@inheritdoc} + */ + public function createNode($id = null) + { + if (!is_string($id) || ('_:' === substr($id, 0, 2))) { + $id = $this->createBlankNodeId(); + } else { + $id = (string) $this->resolveIri($id); + if (isset($this->nodes[$id])) { + return $this->nodes[$id]; + } + } + + return $this->nodes[$id] = new Node($this, $id); + } + + /** + * {@inheritdoc} + */ + public function removeNode(NodeInterface $node) + { + if ($node->getGraph() === $this) { + $node->removeFromGraph(); + } + + $id = $node->getId(); + + if (!$node->isBlankNode()) { + $id = (string) $this->resolveIri($id); + } + + unset($this->nodes[$id]); + } + + /** + * {@inheritdoc} + */ + public function getNodes() + { + return array_values($this->nodes); + } + + /** + * {@inheritdoc} + */ + public function getNode($id) + { + if (!((strlen($id) >= 2) && ('_:' === substr($id, 0, 2)))) { + $id = (string) $this->resolveIri($id); + } + + return isset($this->nodes[$id]) + ? $this->nodes[$id] + : null; + } + + /** + * {@inheritdoc} + */ + public function getNodesByType($type) + { + if (is_string($type)) { + if (null === ($type = $this->getNode($type))) { + return array(); + } + } + + return $type->getNodesWithThisType(); + } + + /** + * {@inheritdoc} + */ + public function containsNode($id) + { + $node = $id; + + if ($node instanceof Node) { + $id = $node->getId(); + } + + if ((null === $id) || !is_string($id)) { + return false; + } + + if ((strlen($id) >= 2) && ('_:' === substr($id, 0, 2))) { + if (isset($this->nodes[$id]) && ($node === $this->nodes[$id])) { + return true; + } + + return false; + } + + $id = (string) $this->resolveIri($id); + + return isset($this->nodes[$id]); + } + + /** + * {@inheritdoc} + */ + public function getDocument() + { + return $this->document; + } + + /** + * {@inheritdoc} + */ + public function removeFromDocument() + { + $doc = $this->document; + $this->document = null; + + $doc->removeGraph($this); + } + + /** + * Create a new blank node identifier unique to the document. + * + * @return string The new blank node identifier. + */ + protected function createBlankNodeId() + { + return '_:b' . $this->blankNodeCounter++; + } + + /** + * Resolves an IRI against the document's IRI + * + * If the graph isn't attached to a document or the document's IRI is + * not set, the IRI is returned as-is. + * + * @param string|IRI $iri The (relative) IRI to resolve + * + * @return IRI The resolved IRI. + */ + protected function resolveIri($iri) + { + if (null === $this->document) { + $base = new IRI(); + } else { + $base = $this->document->getIri(true); + } + + return $base->resolve($iri); + } +} diff --git a/GraphInterface.php b/GraphInterface.php new file mode 100644 index 0000000..df5c9d8 --- /dev/null +++ b/GraphInterface.php @@ -0,0 +1,99 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ML\JsonLD; + +/** + * JSON-LD graph interface + * + * @author Markus Lanthaler + */ +interface GraphInterface +{ + /** + * Creates a new node which is linked to this document + * + * If a blank node identifier or an invalid ID is passed, the ID will be + * ignored and a new blank node identifier unique to the document is + * created for the node. + * + * If there exists already a node with the passed ID in the document, + * that node will be returned instead of creating a new one. + * + * @param null|string $id The ID of the node. + * + * @return Node The newly created node. + */ + public function createNode($id = null); + + /** + * Removes a node from the document + * + * This will also eliminate all references to the node within the + * document. + * + * @param NodeInterface $node The node to remove from the document. + */ + public function removeNode(NodeInterface $node); + + /** + * Get all nodes + * + * @return array[Node] Returns an array containing all nodes defined in + * the document. + */ + public function getNodes(); + + /** + * Get a node by ID + * + * @param string $id The ID of the node to retrieve. + * + * @return Node|null Returns the node if found; null otherwise. + */ + public function getNode($id); + + /** + * Get nodes by type + * + * @param string|Node $type The type + * + * @return array[Node] Returns an array containing all nodes of the + * specified type in the document. + */ + public function getNodesByType($type); + + /** + * Check whether the document already contains a node with the + * specified ID + * + * @param string|Node $id The node ID to check. Blank node identifiers + * will always return false except a node instance + * which is part of the document will be passed + * instead of a string. + * + * @return bool Returns true if the document contains a node with the + * specified ID; false otherwise. + */ + public function containsNode($id); + + /** + * Get the document the node belongs to + * + * @return null|DocumentInterface Returns the document the node belongs + * to or null if the node doesn't belong + * to any document. + */ + public function getDocument(); + + /** + * Removes the graph from the document + */ + public function removeFromDocument(); +} diff --git a/Node.php b/Node.php index 7c7c49b..32509f5 100644 --- a/Node.php +++ b/Node.php @@ -10,39 +10,33 @@ namespace ML\JsonLD; /** - * A Node represents a node in a JSON-LD document. + * A Node represents a node in a JSON-LD graph. * * @author Markus Lanthaler */ -class Node +class Node implements NodeInterface { /** The @type constant. */ const TYPE = '@type'; /** - * The document the node belongs to. - * - * @var Document + * @var GraphInterface The graph the node belongs to. */ - private $document; + private $graph; /** - * The ID of the node - * - * @var string + * @var string The ID of the node */ private $id; /** - * An associative array holding all node's properties except it's ID - * - * @var array + * @var array An associative array holding all properties of the node except it's ID */ private $properties = array(); /** * An associative array holding all reverse properties of this node, i.e., - * a pointers to all nodes that link to this Node. + * a pointers to all nodes that link to this node. * * @var array */ @@ -51,19 +45,17 @@ class Node /** * Constructor * - * @param Document $document The document the node belong to. - * @param null|string $id The ID of the node. + * @param GraphInterface $graph The graph the node belongs to. + * @param null|string $id The ID of the node. */ - public function __construct(Document $document, $id = null) + public function __construct(GraphInterface $graph, $id = null) { - $this->document = $document; + $this->graph = $graph; $this->id = $id; } /** - * Get ID - * - * @return string|null The ID of the node or null. + * {@inheritdoc} */ public function getId() { @@ -71,19 +63,14 @@ public function getId() } /** - * Set the node type - * - * @param null|Node|array[Node] The type(s) of this node. - * - * @throws \InvalidArgumentException If type is not null, a Node or an - * array of Nodes. + * {@inheritdoc} */ public function setType($type) { - if ((null !== $type) && !($type instanceof Node)) { + if ((null !== $type) && !($type instanceof NodeInterface)) { if (is_array($type)) { foreach ($type as $val) { - if ((null !== $val) && !($val instanceof Node)) { + if ((null !== $val) && !($val instanceof NodeInterface)) { throw new \InvalidArgumentException('type must be null, a Node, or an array of Nodes'); } } @@ -96,29 +83,23 @@ public function setType($type) } /** - * Add a type to this node - * - * @param Node The type to add. + * {@inheritdoc} */ - public function addType(Node $type) + public function addType(NodeInterface $type) { return $this->addPropertyValue(self::TYPE, $type); } /** - * Remove a type from this node - * - * @param Node The type to remove. + * {@inheritdoc} */ - public function removeType(Node $type) + public function removeType(NodeInterface $type) { return $this->removePropertyValue(self::TYPE, $type); } /** - * Get node type - * - * @return null|Node|array[Node] Returns the type(s) of this node. + * {@inheritdoc} */ public function getType() { @@ -126,13 +107,7 @@ public function getType() } /** - * Get the nodes which have this node as their type - * - * This will return all nodes that link to this Node instance via the - * @type (rdf:type) property. - * - * @return array[Node] Returns the node(s) having this node as their - * type. + * {@inheritdoc} */ public function getNodesWithThisType() { @@ -142,23 +117,17 @@ public function getNodesWithThisType() } /** - * Get the document the node belongs to - * - * @return null|Document Returns the document the node belongs to or - * null if the node doesn't belong to any document. + * {@inheritdoc} */ - public function getDocument() + public function getGraph() { - return $this->document; + return $this->graph; } /** - * Removes the node from the document - * - * This will also remove all references to and from other nodes in this - * node's document. + * {@inheritdoc} */ - public function removeFromDocument() + public function removeFromGraph() { // Remove other node's properties and reverse properties pointing to // this node @@ -174,26 +143,20 @@ public function removeFromDocument() } foreach ($values as $value) { - if ($value instanceof Node) { + if ($value instanceof NodeInterface) { $this->removePropertyValue($property, $value); } } } - $doc = $this->document; - $this->document = null; + $g = $this->graph; + $this->graph = null; - $doc->removeNode($this); + $g->removeNode($this); } /** - * Is this node a blank node - * - * A blank node is a node whose identifier has just local meaning. It has - * therefore a node identifier with the prefix _: or no - * identifier at all. - * - * @return bool Returns true if the node is a blank node, otherwise false. + * {@inheritdoc} */ public function isBlankNode() { @@ -201,20 +164,7 @@ public function isBlankNode() } /** - * Set a property of the node - * - * If the value is or contains a reference to a node which is not part - * of the document, the referenced node will added to the document as - * well. If the referenced node is already part of another document a - * copy of the node will be created and added to the document. - * - * @param string $property The name of the property. - * @param mixed $value The value of the property. This MUST NOT be - * an array. Use null to remove the property. - * - * @throws \InvalidArgumentException If value is an array or an object - * which is neither a language-tagged - * string nor a typed value or a node. + * {@inheritdoc} */ public function setProperty($property, $value) { @@ -228,23 +178,7 @@ public function setProperty($property, $value) } /** - * Adds a value to a property of the node - * - * If the value already exists, it won't be added again, i.e., there - * won't be any duplicate property values. - * - * If the value is or contains a reference to a node which is not part - * of the document, the referenced node will added to the document as - * well. If the referenced node is already part of another document a - * copy of the node will be created and added to the document. - * - * @param string $property The name of the property. - * @param mixed $value The value of the property. This MUST NOT be - * an array. - * - * @throws \InvalidArgumentException If value is an array or an object - * which is neither a language-tagged - * string nor a typed value or a node. + * {@inheritdoc} */ public function addPropertyValue($property, $value) { @@ -299,15 +233,13 @@ private function doMergeIntoProperty($property, $existingValues, $value) $this->properties[$property] = $existingValues; - if ($value instanceof Node) { + if ($value instanceof NodeInterface) { $value->addReverseProperty($property, $this); } } /** - * Removes a property and all it's values - * - * @param string $property The name of the property to remove. + * {@inheritdoc} */ public function removeProperty($property) { @@ -320,7 +252,7 @@ public function removeProperty($property) : array($this->properties[(string) $property]); foreach ($values as $value) { - if ($value instanceof Node) { + if ($value instanceof NodeInterface) { $value->removeReverseProperty((string) $property, $this); } } @@ -329,11 +261,7 @@ public function removeProperty($property) } /** - * Removes a property value - * - * @param string $property The name of the property. - * @param mixed $value The value of the property. This MUST NOT be - * an array. + * {@inheritdoc} */ public function removePropertyValue($property, $value) { @@ -349,7 +277,7 @@ public function removePropertyValue($property, $value) for ($i = 0, $length = count($values); $i < $length; $i++) { if ($this->equalValues($values[$i], $value)) { - if ($value instanceof Node) { + if ($value instanceof NodeInterface) { $value->removeReverseProperty((string) $property, $this); } @@ -372,11 +300,7 @@ public function removePropertyValue($property, $value) } /** - * Get the properties of this node - * - * @return array Returns an associative array containing all properties - * of this node. The key is the property name whereas the - * value is the property's value. + * {@inheritdoc} */ public function getProperties() { @@ -384,12 +308,7 @@ public function getProperties() } /** - * Get the value of a property - * - * @param string $property The name of the property. - * - * @return mixed Returns the value of the property or null if the - * property doesn't exist. + * {@inheritdoc} */ public function getProperty($property) { @@ -399,12 +318,7 @@ public function getProperty($property) } /** - * Get the reverse properties of this node - * - * @return array Returns an associative array containing all reverse - * properties of this node. The key is the property name - * whereas the value is an array of nodes linking to this - * instance via that property. + * {@inheritdoc} */ public function getReverseProperties() { @@ -417,16 +331,7 @@ public function getReverseProperties() } /** - * Get the nodes of a reverse property - * - * This will return all nodes that link to this Node instance via the - * specified property. - * - * @param string $property The name of the reverse property. - * - * @return null|Node|array[Node] Returns the node(s) pointing to this - * instance via the specified property or - * null if no such node exists. + * {@inheritdoc} */ public function getReverseProperty($property) { @@ -442,14 +347,9 @@ public function getReverseProperty($property) } /** - * Compares this Node object to the specified value. - * - * @param mixed $other The value this instance should be compared to. - * - * @return bool Returns true if the passed value is the same as this - * instance; false otherwise. + * {@inheritdoc} */ - public function equals($other) + public function equals(NodeInterface $other) { return $this === $other; } @@ -457,11 +357,11 @@ public function equals($other) /** * Add a reverse property. * - * @param string $property The name of the property. - * @param Node $node The node which has a property pointing to this - * Node instance. + * @param string $property The name of the property. + * @param NodeInterface $node The node which has a property pointing + * to this node instance. */ - protected function addReverseProperty($property, Node $node) + protected function addReverseProperty($property, NodeInterface $node) { $this->revProperties[$property][$node->getId()] = $node; } @@ -469,11 +369,11 @@ protected function addReverseProperty($property, Node $node) /** * Remove a reverse property. * - * @param string $property The name of the property. - * @param Node $node The node which has a property pointing to this - * Node instance. + * @param string $property The name of the property. + * @param NodeInterface $node The node which has a property pointing + * to this node instance. */ - protected function removeReverseProperty($property, Node $node) + protected function removeReverseProperty($property, NodeInterface $node) { unset($this->revProperties[$property][$node->getId()]); @@ -493,7 +393,7 @@ protected function removeReverseProperty($property, Node $node) protected function isValidPropertyValue($value) { if (is_scalar($value) || (is_object($value) && - ((($value instanceof Node) && ($value->document === $this->document)) || + ((($value instanceof NodeInterface) && ($value->graph === $this->graph)) || ($value instanceof Value)))) { return true; } diff --git a/NodeInterface.php b/NodeInterface.php new file mode 100644 index 0000000..ce2f591 --- /dev/null +++ b/NodeInterface.php @@ -0,0 +1,210 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ML\JsonLD; + +/** + * A generic interface for nodes in a JSON-LD graph. + * + * @author Markus Lanthaler + */ +interface NodeInterface +{ + /** + * Constructor + * + * @param GraphInterface $graph The graph the node belongs to. + * @param null|string $id The ID of the node. + */ + public function __construct(GraphInterface $graph, $id = null); + + /** + * Get ID + * + * @return string|null The ID of the node or null. + */ + public function getId(); + + /** + * Set the node type + * + * @param null|NodeInterface|array[NodeInterface] The type(s) of this node. + * + * @throws \InvalidArgumentException If type is not null, a Node or an + * array of Nodes. + */ + public function setType($type); + + /** + * Add a type to this node + * + * @param NodeInterface The type to add. + */ + public function addType(NodeInterface $type); + + /** + * Remove a type from this node + * + * @param NodeInterface The type to remove. + */ + public function removeType(NodeInterface $type); + + /** + * Get node type + * + * @return null|NodeInterface|array[NodeInterface] Returns the type(s) of this node. + */ + public function getType(); + + /** + * Get the nodes which have this node as their type + * + * This will return all nodes that link to this Node instance via the + * @type (rdf:type) property. + * + * @return array[NodeInterface] Returns the node(s) having this node as their + * type. + */ + public function getNodesWithThisType(); + + /** + * Get the graoh the node belongs to + * + * @return null|GraphInterface Returns the graph the node belongs to or + * null if the node doesn't belong to any graph. + */ + public function getGraph(); + + /** + * Removes the node from the graph + * + * This will also remove all references to and from other nodes in this + * node's graph. + */ + public function removeFromGraph(); + + /** + * Is this node a blank node + * + * A blank node is a node whose identifier has just local meaning. It has + * therefore a node identifier with the prefix _: or no + * identifier at all. + * + * @return bool Returns true if the node is a blank node, otherwise false. + */ + public function isBlankNode(); + + /** + * Set a property of the node + * + * If the value is or contains a reference to a node which is not part + * of the graph, the referenced node will added to the graph as well. + * If the referenced node is already part of another graph a copy of the + * node will be created and added to the graph. + * + * @param string $property The name of the property. + * @param mixed $value The value of the property. This MUST NOT be + * an array. Use null to remove the property. + * + * @throws \InvalidArgumentException If value is an array or an object + * which is neither a language-tagged + * string nor a typed value or a node. + */ + public function setProperty($property, $value); + + /** + * Adds a value to a property of the node + * + * If the value already exists, it won't be added again, i.e., there + * won't be any duplicate property values. + * + * If the value is or contains a reference to a node which is not part + * of the graph, the referenced node will added to the graph as well. + * If the referenced node is already part of another graph a copy of the + * node will be created and added to the graph. + * + * @param string $property The name of the property. + * @param mixed $value The value of the property. This MUST NOT be + * an array. + * + * @throws \InvalidArgumentException If value is an array or an object + * which is neither a language-tagged + * string nor a typed value or a node. + */ + public function addPropertyValue($property, $value); + + /** + * Removes a property and all it's values + * + * @param string $property The name of the property to remove. + */ + public function removeProperty($property); + + /** + * Removes a property value + * + * @param string $property The name of the property. + * @param mixed $value The value of the property. This MUST NOT be + * an array. + */ + public function removePropertyValue($property, $value); + + /** + * Get the properties of this node + * + * @return array Returns an associative array containing all properties + * of this node. The key is the property name whereas the + * value is the property's value. + */ + public function getProperties(); + + /** + * Get the value of a property + * + * @param string $property The name of the property. + * + * @return mixed Returns the value of the property or null if the + * property doesn't exist. + */ + public function getProperty($property); + + /** + * Get the reverse properties of this node + * + * @return array Returns an associative array containing all reverse + * properties of this node. The key is the property name + * whereas the value is an array of nodes linking to this + * instance via that property. + */ + public function getReverseProperties(); + + /** + * Get the nodes of a reverse property + * + * This will return all nodes that link to this Node instance via the + * specified property. + * + * @param string $property The name of the reverse property. + * + * @return null|NodeInterface|array[NodeInterface] Returns the node(s) pointing + * to this instance via the specified + * property or null if no such node exists. + */ + public function getReverseProperty($property); + + /** + * Compares this node object to the specified value. + * + * @param mixed $other The value this instance should be compared to. + * + * @return bool Returns true if the passed value is the same as this + * instance; false otherwise. + */ + public function equals(NodeInterface $other); +} diff --git a/Processor.php b/Processor.php index f989e7f..474edeb 100644 --- a/Processor.php +++ b/Processor.php @@ -223,11 +223,12 @@ public function getDocument($input) } $document = $this->documentFactory->createDocument($this->baseIri); + $graph = $document->getGraph(); $nodes = array(); foreach ($nodeMap as $id => &$item) { if (!isset($nodes[$id])) { - $nodes[$id] = $document->createNode($item->{'@id'}); + $nodes[$id] = $graph->createNode($item->{'@id'}); } $node = $nodes[$id]; @@ -238,7 +239,7 @@ public function getDocument($input) if (property_exists($item, '@type')) { foreach ($item->{'@type'} as $type) { if (!isset($nodes[$type])) { - $nodes[$type] = $document->createNode($type); + $nodes[$type] = $graph->createNode($type); } $node->addType($nodes[$type]); } @@ -260,7 +261,7 @@ public function getDocument($input) } } elseif (property_exists($val, '@id')) { if (!isset($nodes[$val->{'@id'}])) { - $nodes[$val->{'@id'}] = $document->createNode($val->{'@id'}); + $nodes[$val->{'@id'}] = $graph->createNode($val->{'@id'}); } $node->addPropertyValue($property, $nodes[$val->{'@id'}]); } else { diff --git a/README.md b/README.md index 6656059..7d4dd97 100644 --- a/README.md +++ b/README.md @@ -87,17 +87,20 @@ print JsonLD::toString($document, true); // Node-centric API $doc = JsonLD::getDocument('document.jsonld'); +// get the default graph +$graph = $doc->getGraph(); + // get all nodes in the document -$nodes = $doc->getNodes(); +$nodes = $graph->getNodes(); // retrieve a node by ID -$node = $doc->getNode('http://example.com/node1'); +$node = $graph->getNode('http://example.com/node1'); // get a property $node->getProperty('http://example.com/vocab/name'); // add a new blank node to the document -$newNode = $doc->createNode(); +$newNode = $graph->createNode(); // link the new blank node to the existing node $node->addPropertyValue('http://example.com/vocab/link', $newNode); diff --git a/Test/DocumentTest.php b/Test/GraphTest.php similarity index 73% rename from Test/DocumentTest.php rename to Test/GraphTest.php index 891569b..cf4ab2c 100644 --- a/Test/DocumentTest.php +++ b/Test/GraphTest.php @@ -12,6 +12,7 @@ use ML\JsonLD\JsonLD; use ML\JsonLD\Processor; use ML\JsonLD\Document; +use ML\JsonLD\Graph; use ML\JsonLD\Node; use ML\JsonLD\LanguageTaggedString; use ML\JsonLD\TypedValue; @@ -24,14 +25,14 @@ class DocumentTest extends \PHPUnit_Framework_TestCase { /** - * The document instance being used throughout the tests. + * The graph instance being used throughout the tests. * - * @var Document + * @var Graph */ - protected $document; + protected $graph; /** - * Create the Document to test. + * Create the graph to test. */ protected function setUp() { @@ -88,7 +89,8 @@ protected function setUp() } JSON_LD_DOCUMENT; - $this->document = Document::load($json, array('base' => 'http://example.com/node/index.jsonld')); + $doc = Document::load($json, array('base' => 'http://example.com/node/index.jsonld')); + $this->graph = $doc->getGraph(); } @@ -110,7 +112,7 @@ public function testGetNodes() 'http://vocab.com/type/nodeWithAliases' ); - $nodes = $this->document->getNodes(); + $nodes = $this->graph->getNodes(); $this->assertCount(count($nodeIds), $nodes); foreach ($nodes as $node) { @@ -121,10 +123,10 @@ public function testGetNodes() $this->assertInstanceOf('ML\JsonLD\Node', $node); // Does the document return the same instance? - $n = $this->document->getNode($node->getId()); + $n = $this->graph->getNode($node->getId()); $this->assertSame($node, $n, 'same instance'); $this->assertTrue($node->equals($n), 'equals'); - $this->assertSame($this->document, $n->getDocument(), 'linked to document'); + $this->assertSame($this->graph, $n->getGraph(), 'linked to document'); } } @@ -133,17 +135,17 @@ public function testGetNodes() */ public function testNodeRelationships() { - $node1 = $this->document->getNode('http://example.com/node/1'); - $node2 = $this->document->getNode('http://example.com/node/2'); - $node3 = $this->document->getNode('http://example.com/node/3'); + $node1 = $this->graph->getNode('http://example.com/node/1'); + $node2 = $this->graph->getNode('http://example.com/node/2'); + $node3 = $this->graph->getNode('http://example.com/node/3'); - $node1_1 = $this->document->getNode('_:b0'); - $node2_1 = $this->document->getNode('_:b1'); - $node2_2 = $this->document->getNode('_:b2'); - $node3_1 = $this->document->getNode('_:b3'); + $node1_1 = $this->graph->getNode('_:b0'); + $node2_1 = $this->graph->getNode('_:b1'); + $node2_2 = $this->graph->getNode('_:b2'); + $node3_1 = $this->graph->getNode('_:b3'); - $nodeType = $this->document->getNode('http://vocab.com/type/node'); - $nodeWithAliasesType = $this->document->getNode('http://vocab.com/type/nodeWithAliases'); + $nodeType = $this->graph->getNode('http://vocab.com/type/node'); + $nodeWithAliasesType = $this->graph->getNode('http://vocab.com/type/nodeWithAliases'); $this->assertSame($node2, $node1->getProperty('http://vocab.com/link'), 'n1 -link-> n2'); $this->assertSame($node1_1, $node1->getProperty('http://vocab.com/contains'), 'n1 -contains-> n1.1'); @@ -166,17 +168,17 @@ public function testNodeRelationships() */ public function testNodeReverseRelationships() { - $node1 = $this->document->getNode('http://example.com/node/1'); - $node2 = $this->document->getNode('http://example.com/node/2'); - $node3 = $this->document->getNode('http://example.com/node/3'); + $node1 = $this->graph->getNode('http://example.com/node/1'); + $node2 = $this->graph->getNode('http://example.com/node/2'); + $node3 = $this->graph->getNode('http://example.com/node/3'); - $node1_1 = $this->document->getNode('_:b0'); - $node2_1 = $this->document->getNode('_:b1'); - $node2_2 = $this->document->getNode('_:b2'); - $node3_1 = $this->document->getNode('_:b3'); + $node1_1 = $this->graph->getNode('_:b0'); + $node2_1 = $this->graph->getNode('_:b1'); + $node2_2 = $this->graph->getNode('_:b2'); + $node3_1 = $this->graph->getNode('_:b3'); - $nodeType = $this->document->getNode('http://vocab.com/type/node'); - $nodeWithAliasesType = $this->document->getNode('http://vocab.com/type/nodeWithAliases'); + $nodeType = $this->graph->getNode('http://vocab.com/type/node'); + $nodeWithAliasesType = $this->graph->getNode('http://vocab.com/type/nodeWithAliases'); $this->assertSame($node1, $node2->getReverseProperty('http://vocab.com/link'), 'n2 <-link- n1'); $this->assertSame($node1, $node1_1->getReverseProperty('http://vocab.com/contains'), 'n1.1 <-contains- n1'); @@ -197,22 +199,22 @@ public function testNodeReverseRelationships() */ public function testNodeIsBlankNode() { - $this->assertFalse($this->document->getNode('http://example.com/node/1')->isBlankNode(), 'n1'); - $this->assertFalse($this->document->getNode('http://example.com/node/2')->isBlankNode(), 'n2'); - $this->assertFalse($this->document->getNode('http://example.com/node/3')->isBlankNode(), 'n3'); + $this->assertFalse($this->graph->getNode('http://example.com/node/1')->isBlankNode(), 'n1'); + $this->assertFalse($this->graph->getNode('http://example.com/node/2')->isBlankNode(), 'n2'); + $this->assertFalse($this->graph->getNode('http://example.com/node/3')->isBlankNode(), 'n3'); - $this->assertTrue($this->document->getNode('_:b0')->isBlankNode(), '_:b0'); - $this->assertTrue($this->document->getNode('_:b1')->isBlankNode(), '_:b1'); - $this->assertTrue($this->document->getNode('_:b2')->isBlankNode(), '_:b2'); - $this->assertTrue($this->document->getNode('_:b3')->isBlankNode(), '_:b3'); + $this->assertTrue($this->graph->getNode('_:b0')->isBlankNode(), '_:b0'); + $this->assertTrue($this->graph->getNode('_:b1')->isBlankNode(), '_:b1'); + $this->assertTrue($this->graph->getNode('_:b2')->isBlankNode(), '_:b2'); + $this->assertTrue($this->graph->getNode('_:b3')->isBlankNode(), '_:b3'); - $node = $this->document->createNode(); + $node = $this->graph->createNode(); $this->assertTrue($node->isBlankNode(), 'new node without ID'); - $node = $this->document->createNode('_:fljdf'); + $node = $this->graph->createNode('_:fljdf'); $this->assertTrue($node->isBlankNode(), 'new node blank node ID'); - $node = $this->document->createNode('http://www.example.com/node/new'); + $node = $this->graph->createNode('http://www.example.com/node/new'); $this->assertFalse($node->isBlankNode(), 'new node with ID'); } @@ -221,13 +223,13 @@ public function testNodeIsBlankNode() */ public function testNodeReverseRelationshipsUpdated() { - $node1 = $this->document->getNode('http://example.com/node/1'); - $node1_1 = $this->document->getNode('_:b0'); - $node2 = $this->document->getNode('http://example.com/node/2'); - $node3 = $this->document->getNode('http://example.com/node/3'); + $node1 = $this->graph->getNode('http://example.com/node/1'); + $node1_1 = $this->graph->getNode('_:b0'); + $node2 = $this->graph->getNode('http://example.com/node/2'); + $node3 = $this->graph->getNode('http://example.com/node/3'); - $nodeType = $this->document->getNode('http://vocab.com/type/node'); - $nodeWithAliasesType = $this->document->getNode('http://vocab.com/type/nodeWithAliases'); + $nodeType = $this->graph->getNode('http://vocab.com/type/node'); + $nodeWithAliasesType = $this->graph->getNode('http://vocab.com/type/nodeWithAliases'); $revProperties = $node2->getReverseProperties(); $this->assertCount(1, $revProperties, 'Check number of node2\'s reverse properties'); @@ -247,7 +249,7 @@ public function testNodeReverseRelationshipsUpdated() $this->assertNull($node1_1->getReverseProperty('http://vocab.com/contains'), 'n1.1 <-contains- n1 removed'); $expectedProperties = array( - Node::TYPE => $this->document->getNode('http://vocab.com/type/node'), + Node::TYPE => $this->graph->getNode('http://vocab.com/type/node'), 'http://vocab.com/name' => '1' ); $properties = $node1->getProperties(); @@ -270,11 +272,11 @@ public function testNodeReverseRelationshipsUpdated() public function testNodeRemoval() { // Remove node 1 - $node1 = $this->document->getNode('/node/1'); - $node1_1 = $this->document->getNode('_:b0'); - $node2 = $this->document->getNode('http://example.com/node/2'); + $node1 = $this->graph->getNode('/node/1'); + $node1_1 = $this->graph->getNode('_:b0'); + $node2 = $this->graph->getNode('http://example.com/node/2'); - $this->assertTrue($this->document->contains('http://example.com/node/1'), 'node 1 in document?'); + $this->assertTrue($this->graph->containsNode('http://example.com/node/1'), 'node 1 in document?'); $this->assertSame( array('http://vocab.com/link' => array($node1)), @@ -288,7 +290,7 @@ public function testNodeRemoval() 'Check node1.1\'s reverse properties' ); - $node1->removeFromDocument(); + $node1->removeFromGraph(); $this->assertSame(array(), $node2->getReverseProperties(), 'n2 reverse properties'); $this->assertNull($node2->getReverseProperty('http://vocab.com/link'), 'n2 <-link- n1 removed'); @@ -296,16 +298,16 @@ public function testNodeRemoval() $this->assertSame(array(), $node1_1->getReverseProperties(), 'n1.1 reverse properties'); $this->assertNull($node1_1->getReverseProperty('http://vocab.com/contains'), 'n1.1 <-contains- n1 removed'); - $this->assertFalse($this->document->contains('/node/1'), 'node 1 still in document?'); - $this->assertNull($node1->getDocument(), 'node 1\'s document reset?'); + $this->assertFalse($this->graph->containsNode('/node/1'), 'node 1 still in document?'); + $this->assertNull($node1->getGraph(), 'node 1\'s document reset?'); // Remove node 2 - $node2 = $this->document->getNode('http://example.com/node/2'); - $node2_1 = $this->document->getNode('_:b1'); - $node2_2 = $this->document->getNode('_:b2'); - $node3 = $this->document->getNode('/node/3'); + $node2 = $this->graph->getNode('http://example.com/node/2'); + $node2_1 = $this->graph->getNode('_:b1'); + $node2_2 = $this->graph->getNode('_:b2'); + $node3 = $this->graph->getNode('/node/3'); - $this->assertTrue($this->document->contains('/node/2'), 'node 2 in document?'); + $this->assertTrue($this->graph->containsNode('/node/2'), 'node 2 in document?'); $this->assertSame( array('http://vocab.com/link' => array($node2)), @@ -325,7 +327,7 @@ public function testNodeRemoval() 'Check node2.2\'s reverse properties' ); - $this->document->removeNode($node2); + $this->graph->removeNode($node2); $this->assertSame(array(), $node3->getReverseProperties(), 'n3 reverse properties'); $this->assertNull($node3->getReverseProperty('http://vocab.com/link'), 'n3 <-link- n2 removed'); @@ -336,7 +338,7 @@ public function testNodeRemoval() $this->assertSame(array(), $node2_2->getReverseProperties(), 'n2.2 reverse properties'); $this->assertNull($node2_2->getReverseProperty('http://vocab.com/contains'), 'n2.2 <-contains- n2 removed'); - $this->assertFalse($this->document->contains('./2'), 'node 2 still in document?'); + $this->assertFalse($this->graph->containsNode('./2'), 'node 2 still in document?'); } /** @@ -345,11 +347,11 @@ public function testNodeRemoval() public function testNodeTypeRemoval() { // Remove nodeType - $node1 = $this->document->getNode('http://example.com/node/1'); - $node3 = $this->document->getNode('/node/3'); - $nodeType = $this->document->getNode('http://vocab.com/type/node'); + $node1 = $this->graph->getNode('http://example.com/node/1'); + $node3 = $this->graph->getNode('/node/3'); + $nodeType = $this->graph->getNode('http://vocab.com/type/node'); - $this->assertTrue($this->document->contains('http://vocab.com/type/node'), 'node type in document?'); + $this->assertTrue($this->graph->containsNode('http://vocab.com/type/node'), 'node type in document?'); $this->assertSame($nodeType, $node1->getType(), 'n1 type'); $this->assertSame($nodeType, $node3->getType(), 'n3 type'); @@ -360,7 +362,7 @@ public function testNodeTypeRemoval() 'Check node type\'s reverse properties' ); - $this->document->removeNode($nodeType); + $this->graph->removeNode($nodeType); $this->assertSame(array(), $nodeType->getReverseProperties(), 'node type\'s reverse properties'); $this->assertSame(array(), $nodeType->getNodesWithThisType(), 'n1+n3 <-type- node type removed'); @@ -368,7 +370,7 @@ public function testNodeTypeRemoval() $this->assertNull($node1->getType(), 'n1 type removed'); $this->assertNull($node3->getType(), 'n3 type removed'); - $this->assertFalse($this->document->contains('http://vocab.com/type/node'), 'node type still in document?'); + $this->assertFalse($this->graph->containsNode('http://vocab.com/type/node'), 'node type still in document?'); } /** @@ -377,7 +379,7 @@ public function testNodeTypeRemoval() public function testNodePropertyUniqueness() { // Null - $node = $this->document->getNode('http://example.com/node/1'); + $node = $this->graph->getNode('http://example.com/node/1'); $this->assertNull($node->getProperty('http://example.com/node/1'), 'inexistent'); $node->addPropertyValue('http://vocab.com/inexistent', null); @@ -388,7 +390,7 @@ public function testNodePropertyUniqueness() $this->assertNull($node->getProperty('http://example.com/node/1'), 'inexistent removed'); // Scalars - $node = $this->document->getNode('http://example.com/node/1'); + $node = $this->graph->getNode('http://example.com/node/1'); $this->assertSame('1', $node->getProperty('http://vocab.com/name', 'name: initial value')); @@ -403,7 +405,7 @@ public function testNodePropertyUniqueness() $this->assertSame('1', $node->getProperty('http://vocab.com/name', 'name: removed new value')); // Language-tagged strings - $node = $this->document->getNode('http://example.com/node/2'); + $node = $this->graph->getNode('http://example.com/node/2'); $value = $node->getProperty('http://vocab.com/lang'); $this->assertInstanceOf('ML\JsonLD\LanguageTaggedString', $value, 'lang: initial value type'); @@ -440,7 +442,7 @@ public function testNodePropertyUniqueness() $this->assertTrue($newLangValue2->equals($value[1]), 'lang: check values 2 (2)'); // Typed values - $node = $this->document->getNode('http://example.com/node/2'); + $node = $this->graph->getNode('http://example.com/node/2'); $value = $node->getProperty('http://vocab.com/typed'); $this->assertInstanceOf('ML\JsonLD\TypedValue', $value, 'typed: initial value class'); @@ -477,19 +479,19 @@ public function testNodePropertyUniqueness() $this->assertTrue($newTypedValue2->equals($value[1]), 'typed: check values 2 (2)'); // Nodes - $node = $this->document->getNode('http://example.com/node/3'); - $node1 = $this->document->getNode('http://example.com/node/1'); + $node = $this->graph->getNode('http://example.com/node/3'); + $node1 = $this->graph->getNode('http://example.com/node/1'); $value = $node->getProperty('http://vocab.com/link'); $this->assertInstanceOf('ML\JsonLD\Node', $value, 'node: initial value class'); $this->assertSame($node1, $value, 'node: initial node'); - $newNode1 = $this->document->createNode(); - $this->assertTrue($this->document->contains($newNode1), 'node: new1 in document'); + $newNode1 = $this->graph->createNode(); + $this->assertTrue($this->graph->containsNode($newNode1), 'node: new1 in document'); - $newNode2 = $this->document->createNode('http://example.com/node/new/2'); - $this->assertTrue($this->document->contains($newNode2), 'node: new2 in document'); + $newNode2 = $this->graph->createNode('http://example.com/node/new/2'); + $this->assertTrue($this->graph->containsNode($newNode2), 'node: new2 in document'); $node->addPropertyValue('http://vocab.com/link', $node1); $this->assertSame($node1, $node->getProperty('http://vocab.com/link'), 'node: still same'); @@ -512,14 +514,14 @@ public function testNodePropertyUniqueness() $this->assertTrue($newNode2->equals($value[1]), 'node: check values 2 (2)'); // Node types - $node1 = $this->document->getNode('http://example.com/node/1'); - $nodeType = $this->document->getNode('http://vocab.com/type/node'); - $nodeWithAliasesType = $this->document->getNode('http://vocab.com/type/nodeWithAliases'); + $node1 = $this->graph->getNode('http://example.com/node/1'); + $nodeType = $this->graph->getNode('http://vocab.com/type/node'); + $nodeWithAliasesType = $this->graph->getNode('http://vocab.com/type/nodeWithAliases'); $this->assertSame($nodeType, $node1->getType(), 'type: n1 initial type'); - $newType1 = $this->document->createNode(); - $this->assertTrue($this->document->contains($newNode1), 'type: new1 in document'); + $newType1 = $this->graph->createNode(); + $this->assertTrue($this->graph->containsNode($newNode1), 'type: new1 in document'); $node1->addType($nodeType); $this->assertSame($nodeType, $node1->getType(), 'type: n1 type still same'); @@ -550,10 +552,10 @@ public function testNodePropertyUniqueness() */ public function testAddInvalidPropertyValue() { - $document = new Document(); - $newNode = $document->createNode(); + $graph = new Graph(); + $newNode = $graph->createNode(); - $node1 = $this->document->getNode('http://example.com/node/1'); + $node1 = $this->graph->getNode('http://example.com/node/1'); $node1->addPropertyValue('http://vocab.com/link', $newNode); } @@ -565,7 +567,7 @@ public function testAddInvalidPropertyValue() */ public function testSetInvalidTypeValue() { - $node1 = $this->document->getNode('http://example.com/node/1'); + $node1 = $this->graph->getNode('http://example.com/node/1'); $node1->setType('http://vocab.com/type/aTypeAsString'); } @@ -578,11 +580,11 @@ public function testSetInvalidTypeValue() public function testSetInvalidTypeArray() { $types = array( - $this->document->getNode('http://vocab.com/type/nodeWithAliases'), + $this->graph->getNode('http://vocab.com/type/nodeWithAliases'), 'http://vocab.com/type/aTypeAsString' ); - $node1 = $this->document->getNode('http://example.com/node/1'); + $node1 = $this->graph->getNode('http://example.com/node/1'); $node1->setType($types); } @@ -595,10 +597,10 @@ public function testSetInvalidTypeArray() */ public function testAddTypeNotInDocument() { - $document = new Document(); - $newType = $document->createNode(); + $graph = new Graph(); + $newType = $graph->createNode(); - $node1 = $this->document->getNode('http://example.com/node/1'); + $node1 = $this->graph->getNode('http://example.com/node/1'); $node1->addType($newType); } @@ -607,15 +609,15 @@ public function testAddTypeNotInDocument() */ public function testContains() { - $node1 = $this->document->getNode('http://example.com/node/1'); - $nodeb_0 = $this->document->getNode('_:b0'); - - $this->assertTrue($this->document->contains($node1), 'node1 obj'); - $this->assertTrue($this->document->contains('http://example.com/node/1'), 'node1 IRI'); - $this->assertFalse($this->document->contains('http://example.com/node/X'), 'inexistent IRI'); - $this->assertTrue($this->document->contains($nodeb_0), '_:b0'); - $this->assertFalse($this->document->contains('_:b0'), '_:b0 IRI'); - $this->assertFalse($this->document->contains(new TypedValue('val', 'http://example.com/type')), 'typed value'); + $node1 = $this->graph->getNode('http://example.com/node/1'); + $nodeb_0 = $this->graph->getNode('_:b0'); + + $this->assertTrue($this->graph->containsNode($node1), 'node1 obj'); + $this->assertTrue($this->graph->containsNode('http://example.com/node/1'), 'node1 IRI'); + $this->assertFalse($this->graph->containsNode('http://example.com/node/X'), 'inexistent IRI'); + $this->assertTrue($this->graph->containsNode($nodeb_0), '_:b0'); + $this->assertFalse($this->graph->containsNode('_:b0'), '_:b0 IRI'); + $this->assertFalse($this->graph->containsNode(new TypedValue('val', 'http://example.com/type')), 'typed value'); } /** @@ -623,10 +625,10 @@ public function testContains() */ public function testCreateExistingNode() { - $node1 = $this->document->getNode('http://example.com/node/1'); - $nodeType = $this->document->getNode('http://vocab.com/type/node'); + $node1 = $this->graph->getNode('http://example.com/node/1'); + $nodeType = $this->graph->getNode('http://vocab.com/type/node'); - $this->assertSame($node1, $this->document->createNode('http://example.com/node/1')); - $this->assertSame($nodeType, $this->document->createNode('http://vocab.com/type/node')); + $this->assertSame($node1, $this->graph->createNode('http://example.com/node/1')); + $this->assertSame($nodeType, $this->graph->createNode('http://vocab.com/type/node')); } }