public function testRemoveSubjectsOfType() { $graph = new ExtendedGraph(); $graph->add_resource_triple("http://test/1", ExtendedGraph::rdf . 'type', self::ONT_resource . 'Item'); $graph->add_resource_triple("http://test/2", ExtendedGraph::rdf . 'type', self::ONT_resource . 'Item'); $graph->add_resource_triple("http://test/3", ExtendedGraph::rdf . 'type', self::ONT_resource . 'List'); $graph->remove_subjects_of_type(self::ONT_resource . 'Item'); $subjects = $graph->get_subjects(); $this->assertEquals(1, count($subjects)); $this->assertEquals("http://test/3", $subjects[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)); } } } } }