Exemple #1
0
/**
 * @param \Tripod\ExtendedGraph $graph
 * @param string $format
 * @return string
 */
function getFormattedGraph(\Tripod\ExtendedGraph $graph, $format)
{
    switch ($format) {
        case FORMAT_RDF_XML:
            $output = $graph->to_rdfxml();
            break;
        case FORMAT_NTRIPLES:
            $output = $graph->to_ntriples();
            break;
        case FORMAT_TURTLE:
            $output = $graph->to_turtle();
            break;
        default:
            $output = $graph->to_json();
    }
    return $output;
}
 /**
  * @param \Tripod\ExtendedGraph $graph
  * @param string $s
  * @param string $p
  * @param string $o
  */
 protected function assertDoesNotHaveResourceTriple(\Tripod\ExtendedGraph $graph, $s, $p, $o)
 {
     $this->assertFalse($graph->has_resource_triple($s, $p, $o), "Graph should not contain the resource triple: <{$s}> <{$p}> <{$o}>");
 }
 public function testReplaceLiteralTriples()
 {
     $graph = new ExtendedGraph();
     $graph->add_literal_triple("http://test/1", 'http://www.w3.org/2000/01/rdf-schema#label', "value1");
     $graph->add_literal_triple("http://test/1", 'http://www.w3.org/2000/01/rdf-schema#label', "value2");
     $graph->replace_literal_triples("http://test/1", 'http://www.w3.org/2000/01/rdf-schema#label', "value3");
     $this->assertTrue($graph->has_literal_triple("http://test/1", 'http://www.w3.org/2000/01/rdf-schema#label', "value3"));
     $this->assertFalse($graph->has_literal_triple("http://test/1", 'http://www.w3.org/2000/01/rdf-schema#label', "value1"));
     $this->assertFalse($graph->has_literal_triple("http://test/1", 'http://www.w3.org/2000/01/rdf-schema#label', "value2"));
 }
Exemple #4
0
 /**
  * Ensure that the graph we want to persist has data with valid cardinality.
  *
  * @param \Tripod\ExtendedGraph $graph
  * @throws \Tripod\Exceptions\CardinalityException
  */
 protected function validateGraphCardinality(\Tripod\ExtendedGraph $graph)
 {
     $config = Config::getInstance();
     $cardinality = $config->getCardinality($this->getStoreName(), $this->getPodName());
     $namespaces = $config->getNamespaces();
     $graphSubjects = $graph->get_subjects();
     if (empty($cardinality) || $graph->is_empty()) {
         return;
     }
     foreach ($cardinality as $qname => $cardinalityValue) {
         list($namespace, $predicateName) = explode(':', $qname);
         if (!array_key_exists($namespace, $namespaces)) {
             //TODO This may be changed to a namespace exception at some point...
             throw new \Tripod\Exceptions\CardinalityException("Namespace '{$namespace}' not defined for qname: {$qname}");
         }
         // NB: The only constraint we currently support is a value of 1 to enforce one triple per subject/predicate.
         if ($cardinalityValue == 1) {
             foreach ($graphSubjects as $subjectUri) {
                 $predicateUri = $namespaces[$namespace] . $predicateName;
                 $predicateValues = $graph->get_subject_property_values($subjectUri, $predicateUri);
                 if (count($predicateValues) > 1) {
                     $v = array();
                     foreach ($predicateValues as $predicateValue) {
                         $v[] = $predicateValue['value'];
                     }
                     throw new \Tripod\Exceptions\CardinalityException("Cardinality failed on {$subjectUri} for '{$qname}' - should only have 1 value and has: " . implode(', ', $v));
                 }
             }
         }
     }
 }