public function toOWL(Ontology $ontology) { $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; $output .= "<rdf:RDF\n"; $output .= " xmlns = \"http://dbpedia.org/ontology/\"\n"; $output .= " xml:base=\"http://dbpedia.org/ontology/\"\n"; $output .= " xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n"; $output .= " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\"\n"; $output .= " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"; $output .= " xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">\n"; $output .= "\t<owl:Ontology rdf:about=\"\">\n"; $output .= "\t\t<owl:versionInfo xml:lang=\"en\">Version " . OWLOntologyWriter::VERSION . "</owl:versionInfo>\n"; $output .= "\t</owl:Ontology>\n\n"; foreach ($ontology->getClassIterator() as $class) { //Only write classes from the default namespace (Don't write owl, rdf and rdfs built-in classes etc.) if (strpos($class->getName(), ':') === false) { $this->writeClass($class, $output); } } foreach ($ontology->getPropertyIterator() as $property) { //Only write properties from the default namespace (Don't write owl, rdf and rdfs built-in properties etc.) if (strpos($property->getName(), ':') === false) { $this->writeProperty($property, $output); } } $output .= "</rdf:RDF>\n"; return $output; }
public static function addProperties(Ontology $ontology) { $ontology->addProperty(new OntologyDataTypeProperty(self::PROPERTY, $ontology->getClass("owl:Thing"), $ontology->getDataType("xsd:string"))); }
/** * Links all classes and properties according to the configuration. * * TODO: It would be cleaner to use a two-stage process: Copy the data (class and property * names etc.) from the page nodes to intermediary data structures (probably simple maps * or simple data objects), then build the final ontology objects from these data * structures. No need to go through the page nodes twice, no need to modify or remove * classes after they were added to the ontology. Would also enable checks for cyclic * dependencies etc. * * @param $ontology The ontology instance * @param $pageNode The page node of the configuration page */ private function linkClasses(Ontology $ontology, PageNode $pageNode) { $name = self::getPageName($pageNode); foreach ($pageNode->getChildren('TemplateNode') as $node) { //Class $templateName = $node->getTitle()->encoded(); if ($templateName == OntologyClass::TEMPLATE_NAME) { $ontologyClass = $ontology->getClass($name); //Sub class $subClassNode = $node->getProperty("rdfs:subClassOf"); if ($subClassNode) { try { $parentClass = $ontology->getClass($subClassNode->getText()); $ontologyClass->setSubClassOf($parentClass); } catch (\InvalidArgumentException $e) { $this->logger->warn("subClassOf " . $name . " (" . $subClassNode->getText() . ") not found"); } } //Equivalent Class $equivalentClassNode = $node->getProperty("owl:equivalentClass"); if ($equivalentClassNode) { try { $equivalentClass = $ontology->getClass($equivalentClassNode->getText()); $ontologyClass . setEquivalentClass($equivalentClass); } catch (\InvalidArgumentException $e) { $this->logger->warn("equivalentClass " . $name . " (" . $equivalentClassNode->getText() . ") not found"); } } } else { if ($templateName == OntologyObjectProperty::TEMPLATE_NAME || $templateName == OntologyDataTypeProperty::TEMPLATE_NAME) { $ontologyProperty = $ontology->getProperty($name); //Domain $domainUriNode = $node->getProperty("rdfs:domain"); if ($domainUriNode) { try { $domainClass = $ontology->getClass($domainUriNode->getText()); } catch (\InvalidArgumentException $e) { $this->logger->warn("Domain of " . $name . " (" . $domainUriNode->getText() . ") not found. Assuming owl:Thing"); $domainClass = $ontology->getClass("owl:Thing"); } $ontologyProperty->setDomain($domainClass); } else { $ontologyProperty->setDomain($ontology->getClass("owl:Thing")); } //Range $rangeUriNode = $node->getProperty("rdfs:range"); if ($rangeUriNode) { if ($ontologyProperty instanceof OntologyObjectProperty) { try { $rangeClass = $ontology->getClass($rangeUriNode->getText()); } catch (\InvalidArgumentException $e) { $this->logger->warn("Range of " . $name . " (" . $rangeUriNode->getText() . ") not found. Assuming owl:Thing"); $rangeClass = $ontology->getClass("owl:Thing"); } $ontologyProperty->setRange($rangeClass); } else { try { $dataType = $ontology->getDataType($rangeUriNode->getText()); $ontologyProperty->setRange($dataType); } catch (\InvalidArgumentException $e) { $this->logger->warn("Range of " . $name . " (" . $rangeUriNode->getText() . ") not found. Removing property."); $ontology->removeProperty($ontologyProperty); } } } else { if ($ontologyProperty instanceof OntologyObjectProperty) { $ontologyProperty->setRange($ontology->getClass("owl:Thing")); } else { $this->logger->warn("Range of " . $name . " not defined. Removing property."); $ontology->removeProperty($ontologyProperty); } } } } } }