Пример #1
0
 /**
  * @param $types
  * @return array|null
  */
 public function getMostAccurateType($types, $knowTypesForExternService = array())
 {
     // filter types to have only types filiation defined with subClassOf annotation
     $definedOntoTypes = array();
     foreach ($types as $type) {
         $type = (string) $type;
         if ($type && !empty($type)) {
             $shortenType = RdfNamespace::shorten($type);
             if (isset($this->rdfFiliation[$shortenType])) {
                 $definedOntoTypes[] = $shortenType;
             }
         }
     }
     $arrayAccurateOnes = array();
     if (count($definedOntoTypes) == 0) {
         return null;
     } else {
         if (count($definedOntoTypes) == 1) {
             $arrayAccurateOnes = $definedOntoTypes;
         } else {
             // try to find the most accurate type
             foreach ($definedOntoTypes as $currentType) {
                 $mostAccurate = true;
                 if (isset($this->rdfFiliation[$currentType]['parentClassOf'])) {
                     $subClassTypes = $this->rdfFiliation[$currentType]['parentClassOf'];
                     // in class children types, look if one of them is defined with subClassOf annotation
                     foreach ($definedOntoTypes as $type) {
                         if (in_array($type, $subClassTypes)) {
                             $mostAccurate = false;
                             break;
                         }
                     }
                 }
                 if ($mostAccurate) {
                     $arrayAccurateOnes[] = $currentType;
                 }
             }
         }
     }
     if (!empty($knowTypesForExternService)) {
         return $this->findMostAccurateTypesInKnowTypesByExternalService($arrayAccurateOnes, $knowTypesForExternService);
     }
     return $arrayAccurateOnes;
 }
Пример #2
0
 /**
  * Protected method to serialise a whole resource and its properties
  * @ignore
  */
 protected function rdfxmlResource($res, $showNodeId, $depth = 1)
 {
     // Keep track of the resources we have already serialised
     if (isset($this->outputtedResources[$res->getUri()])) {
         return '';
     } else {
         $this->outputtedResources[$res->getUri()] = true;
     }
     // If the resource has no properties - don't serialise it
     $properties = $res->propertyUris();
     if (count($properties) == 0) {
         return '';
     }
     $type = $res->type();
     if ($type) {
         $this->addPrefix($type);
     } else {
         $type = 'rdf:Description';
     }
     $indent = str_repeat('  ', $depth);
     $xml = "\n{$indent}<{$type}";
     if ($res->isBNode()) {
         if ($showNodeId) {
             $xml .= ' rdf:nodeID="' . htmlspecialchars($res->getBNodeId()) . '"';
         }
     } else {
         $xml .= ' rdf:about="' . htmlspecialchars($res->getUri()) . '"';
     }
     $xml .= ">\n";
     if ($res instanceof Container) {
         foreach ($res as $item) {
             $xml .= $this->rdfxmlObject('rdf:li', $item, $depth + 1);
         }
     } else {
         foreach ($properties as $property) {
             $short = RdfNamespace::shorten($property, true);
             if ($short) {
                 $this->addPrefix($short);
                 $objects = $res->all("<{$property}>");
                 if ($short == 'rdf:type' && $type != 'rdf:Description') {
                     array_shift($objects);
                 }
                 foreach ($objects as $object) {
                     $xml .= $this->rdfxmlObject($short, $object, $depth + 1);
                 }
             } else {
                 throw new Exception("It is not possible to serialse the property " . "'{$property}' to RDF/XML.");
             }
         }
     }
     $xml .= "{$indent}</{$type}>\n";
     return $xml;
 }
Пример #3
0
 /**
  * Given a an EasyRdf\Resource or URI, convert it into a string, suitable to
  * be written to a Turtle document. URIs will be shortened into CURIES
  * where possible.
  *
  * @param  Resource|string $resource The resource to convert to a Turtle string
  * @param  boolean $createNamespace  If true, a new namespace may be created
  *
  * @return string
  */
 public function serialiseResource($resource, $createNamespace = false)
 {
     if (is_object($resource)) {
         if ($resource->isBNode()) {
             return $resource->getUri();
         }
         $resource = $resource->getUri();
     }
     $short = RdfNamespace::shorten($resource, $createNamespace);
     if ($short) {
         $this->addPrefix($short);
         return $short;
     }
     return self::escapeIri($resource);
 }
Пример #4
0
 /**
  * @param $uri
  * @param bool $createNamespace
  *
  * @return string
  */
 public function shorten($uri, $createNamespace = false)
 {
     return RdfNamespace::shorten($uri, $createNamespace);
 }
Пример #5
0
 /**
  * If a types resource has changed and the new type is mapped to another document type, then the old document is removed
  * @param $uri
  * @param $types
  * @param $oldType
  * @return bool
  */
 protected function deleteOldDocument($uri, $types, $oldType)
 {
     $newTypes = array();
     foreach ($types as $type) {
         $type = (string) $type;
         if ($type && !empty($type)) {
             $newTypes[] = RdfNamespace::shorten($type);
         }
     }
     // Get most accurtype of oldtype
     $mostAccurateTypes = $this->filiationBuilder->getMostAccurateType(array(RdfNamespace::expand($oldType)), $this->serializerHelper->getAllTypes());
     $mostAccurateType = null;
     // not specified in project ontology description
     if (count($mostAccurateTypes) == 1) {
         $mostAccurateType = $mostAccurateTypes[0];
     } else {
         //            echo "Seems to not have to be indexed";
     }
     if (!in_array($mostAccurateType, $newTypes)) {
         $typesConfig = $this->configManager->getTypesConfigurationByClass($mostAccurateType);
         foreach ($typesConfig as $typeConfig) {
             $indexConfig = $typeConfig->getIndex();
             $index = $indexConfig->getName();
             $this->container->get('nemrod.elastica.jsonld.frame.loader')->setEsIndex($index);
             if ($index !== null) {
                 $esType = $this->indexRegistry->getIndex($index)->getType($typeConfig->getType());
                 // Trow an exeption if document does not exist
                 try {
                     $esType->deleteDocument(new Document($uri, array(), $mostAccurateType, $indexConfig->getElasticSearchName()));
                     return true;
                 } catch (\Exception $e) {
                 }
             }
         }
     }
     return false;
 }
Пример #6
0
 /**
  * Internal function to serialise an EasyRdf\Graph into a DOT formatted string
  *
  * @ignore
  */
 protected function serialiseDot(Graph $graph)
 {
     $result = "digraph {\n";
     // Write the graph attributes
     foreach ($this->attributes as $k => $v) {
         $result .= '  ' . $this->escape($k) . '=' . $this->escape($v) . ";\n";
     }
     // Go through each of the properties and write the edges
     $nodes = array();
     $result .= "\n  // Edges\n";
     foreach ($graph->resources() as $resource) {
         $name1 = $this->nodeName($resource);
         foreach ($resource->propertyUris() as $property) {
             $label = null;
             if ($this->useLabels) {
                 $label = $graph->resource($property)->label();
             }
             if ($label === null) {
                 if ($this->onlyLabelled == true) {
                     continue;
                 } else {
                     $label = RdfNamespace::shorten($property);
                 }
             }
             foreach ($resource->all("<{$property}>") as $value) {
                 $name2 = $this->nodeName($value);
                 $nodes[$name1] = $resource;
                 $nodes[$name2] = $value;
                 $result .= $this->serialiseRow($name1, $name2, array('label' => $label));
             }
         }
     }
     ksort($nodes);
     $result .= "\n  // Nodes\n";
     foreach ($nodes as $name => $node) {
         $type = substr($name, 0, 1);
         $label = '';
         if ($type == 'R') {
             if ($this->useLabels) {
                 $label = $node->label();
             }
             if (!$label) {
                 $label = $node->shorten();
             }
             if (!$label) {
                 $label = $node->getURI();
             }
             $result .= $this->serialiseRow($name, null, array('URL' => $node->getURI(), 'label' => $label, 'shape' => 'ellipse', 'color' => 'blue'));
         } elseif ($type == 'B') {
             if ($this->useLabels) {
                 $label = $node->label();
             }
             $result .= $this->serialiseRow($name, null, array('label' => $label, 'shape' => 'circle', 'color' => 'green'));
         } else {
             $result .= $this->serialiseRow($name, null, array('label' => strval($node), 'shape' => 'record'));
         }
     }
     $result .= "}\n";
     return $result;
 }