/**
  * Import a structXML resultset
  * 
  * @param mixed $structXMLResultset structXML resultset to import
  * 
  * @author Frederick Giasson, Structured Dynamics LLC.
  */
 public function importStructXMLResultset($structXMLResultset)
 {
     $xml = new SimpleXMLElement($structXMLResultset);
     /*
      $resultset =   Array("dataset-uri" => 
                        Array("record-uri" =>
                         Array(
                           "type" => Array(URIs...),
                           "prefLabel" => "preferred label",
                           "altLabel" => Array(alternative label literals...),
                           "prefURL" => "http://preferred-url.com",
                           "description" => "some description of the record",
                           
                           "other-data-attribute-uri" => Array(
                             Array(
                               "value" => "some value",
                               "lang" => "language string of the value",
                               "type" => "type of the value"
                               "type" => "optional type of the referenced URI",
                               "reify" => Array(
                                 "reification-attribute-uri" => Array("value of the reification statement"),
                                 "more-reification-attribute-uri" => ...
                               )
                             ),
                             Array(
                               ...
                             )
                           ),
                           "more-data-attribute-uri": ...,
                           
                           "other-object-attribute-uri" => Array(
                             Array(
                               "uri" => "some uri",
                               "type" => "optional type of the referenced URI",
                               "reify" => Array(
                                 "reification-attribute-uri" => Array("value of the reification statement"),
                                 "more-reification-attribute-uri" => ...
                               )
                             ),
                             Array(
                               ...
                             )
                           )
                           "more-object-attribute-uri": ...
                         ),
                         
                         "more-record-uri": ...
                       )
                     )    
     */
     $resultset = array();
     foreach ($xml->prefix as $prefix) {
         if (!isset($this->prefixes[(string) $prefix["uri"]])) {
             $this->prefixes[(string) $prefix["uri"]] = (string) $prefix["entity"];
         }
     }
     foreach ($xml->subject as $s) {
         $type = $this->unprefixize((string) $s["type"]);
         $uri = $this->unprefixize((string) $s["uri"]);
         $subject = array("type" => array($type));
         foreach ($s->predicate as $predicate) {
             $predicateType = $this->unprefixize((string) $predicate["type"]);
             foreach ($predicate->object as $object) {
                 //$objectType = $this->unprefixize((string) $object["type"]);
                 // check if object property
                 if (isset($object["uri"])) {
                     if ($predicateType == Namespaces::$rdf . "type") {
                         array_push($subject["type"], (string) $object["uri"]);
                         continue;
                     }
                     if (!isset($subject[$predicateType])) {
                         $subject[$predicateType] = array();
                     }
                     $value = array();
                     if (isset($object["type"])) {
                         $value["type"] = $this->unprefixize((string) $object["type"]);
                     }
                     if (isset($object["uri"])) {
                         $value["uri"] = $this->unprefixize((string) $object["uri"]);
                     }
                     // Add possible reification statement.
                     if (isset($object->reify)) {
                         $reifyType = $this->unprefixize((string) $object->reify["type"]);
                         $reifyValue = $this->unprefixize((string) $object->reify["value"]);
                         $value["reify"][$reifyType] = array($reifyValue);
                     }
                     array_push($subject[$predicateType], $value);
                 } else {
                     if (!isset($subject[$predicateType])) {
                         $subject[$predicateType] = array();
                     }
                     $value = array();
                     if (isset($object["type"])) {
                         $value["type"] = $this->unprefixize((string) $object["type"]);
                     }
                     if (isset($object["lang"])) {
                         $value["lang"] = (string) $object["lang"];
                     }
                     $value["value"] = (string) $object;
                     // Add possible reification statement.
                     if (isset($predicate->reify)) {
                         $reifyType = $this->unprefixize((string) $predicate->reify["type"]);
                         $reifyValue = $this->unprefixize((string) $predicate->reify["value"]);
                         $value["reify"][$reifyType] = array($reifyValue);
                     }
                     array_push($subject[$predicateType], $value);
                 }
             }
         }
         // Try to find a prefLabel
         $prefLabelProperty = "";
         foreach (Namespaces::getLabelProperties() as $labelProperty) {
             if (isset($subject[$labelProperty])) {
                 $subject["prefLabel"] = $subject[$labelProperty][0]["value"];
                 $prefLabelProperty = $labelProperty;
                 break;
             }
         }
         // Try to find alternative labels
         foreach (Namespaces::getLabelProperties() as $labelProperty) {
             if ($labelProperty != $prefLabelProperty && isset($subject[$labelProperty])) {
                 if (!isset($subject["altLabel"])) {
                     $subject["altLabel"] = array();
                 }
                 array_push($subject["altLabel"], $subject[$labelProperty][0]["value"]);
             }
         }
         // Try to find description
         foreach (Namespaces::getDescriptionProperties() as $descriptionProperty) {
             if (isset($subject[$descriptionProperty])) {
                 $subject["description"] = $subject[$descriptionProperty][0]["value"];
                 break;
             }
         }
         // Try to find preferred URLs
         if (isset($subject[Namespaces::$iron . "prefURL"])) {
             $subject["prefURL"] = $subject[Namespaces::$iron . "prefURL"][0]["value"];
         }
         // Try to get a reference to the dataset where the record could come from
         $dataset = "unspecified";
         if (isset($subject[Namespaces::$dcterms . "isPartOf"])) {
             $dataset = $subject[Namespaces::$dcterms . "isPartOf"][0]["uri"];
         }
         if (!isset($resultset[$dataset])) {
             $resultset[$dataset] = array();
         }
         $resultset[$dataset][$uri] = $subject;
     }
     $this->resultset = $resultset;
 }
Ejemplo n.º 2
0
 /**
  * Convert the OWLAPI named individual description into an array describing the class. This array is a simplification
  * of the OWLAPI that is used by other parts of this API, along with other scripts that uses this
  * API such as the various ontology related structWSF endpoints.
  * 
  * This array is optimized for displaying the named individual references in some list controls. This function
  * only returns the prefLabel, and types of the named individual.
  * 
  * Note: annotations on are converted into non-annotation attribute/value with this API call.
  * 
  * The array is defined as:
  *   
  *   $classDescription = array(
  *                              "predicate-uri" => array(
  *                                                       array(
  *                                                               "value" => "the value of the predicate",
  *                                                               "type" => "the type of the value",
  *                                                               "lang" => "language reference of the value (if literal)"
  *                                                            ),
  *                                                       array(...)
  *                                                     ),
  *                              "..." => array(...)
  *                            )
  * 
  * @param mixed $namedIndividual The OWLAPI named individual instance.
  * 
  * @see http://owlapi.sourceforge.net/javadoc/uk/ac/manchester/cs/owl/owlapi/OWLNamedIndividual.html
  *  
  * @author Frederick Giasson, Structured Dynamics LLC.
  */
 public function _getNamedIndividualListDescription($namedIndividual)
 {
     $niDescription = array();
     // Get the types of the entity
     $niDescription[Namespaces::$rdf . "type"] = array();
     // Get all types of this named individual
     $types = $namedIndividual->getTypes($this->ontology);
     foreach ($types as $type) {
         $typeUri = (string) java_values($type->toStringID());
         array_push($niDescription[Namespaces::$rdf . "type"], array("uri" => $typeUri));
     }
     // Get all the annotations
     $annotations = $namedIndividual->getAnnotations($this->ontology);
     foreach ($annotations as $annotation) {
         $info = $this->getAnnotationInfo($annotation);
         if (in_array($info["property"], Namespaces::getLabelProperties())) {
             if (!isset($niDescription[$info["property"]]) || is_array($niDescription[$info["property"]]) === FALSE) {
                 $niDescription[$info["property"]] = array();
             }
             if (isset($info["uri"])) {
                 array_push($niDescription[$info["property"]], array("uri" => $info["uri"], "reify" => $info["reify"]));
             } else {
                 if ($info["lang"] == "" || $this->lang == "" || $info["lang"] == $this->lang) {
                     array_push($niDescription[$info["property"]], array("value" => $info["value"], "type" => $info["type"], "lang" => $info["lang"]));
                 }
             }
         }
     }
     // Get all dataproperty/values defining this named individual
     $datapropertiesValuesMap = $namedIndividual->getDataPropertyValues($this->ontology);
     $keys = $datapropertiesValuesMap->keySet();
     $size = java_values($datapropertiesValuesMap->size());
     foreach ($keys as $property) {
         $propertyUri = (string) java_values($property->toStringID());
         if (in_array($propertyUri, Namespaces::getLabelProperties())) {
             $valuesOWLLiteral = $datapropertiesValuesMap->get($property);
             if (!isset($niDescription[$propertyUri]) || !is_array($niDescription[$propertyUri])) {
                 $niDescription[$propertyUri] = array();
             }
             foreach ($valuesOWLLiteral as $valueOWLLiteral) {
                 if ((string) $valueOWLLiteral->getLang() == "" || $this->lang == "" || (string) $valueOWLLiteral->getLang() == $this->lang) {
                     array_push($niDescription[$propertyUri], array("value" => (string) $valueOWLLiteral->getLiteral(), "type" => "rdfs:Literal", "lang" => (string) $valueOWLLiteral->getLang()));
                 }
             }
         }
     }
     return $niDescription;
 }