/**
  * Create a new, or update an existing entity based on the input RDF document.
  * 
  * @param mixed $document
  * @param mixed $advancedIndexation
  *  
  * @author Frederick Giasson, Structured Dynamics LLC.
  */
 public function createOrUpdateEntity($document, $advancedIndexation)
 {
     $this->initiateOwlBridgeSession();
     $this->getOntologyReference();
     if ($this->isValid()) {
         // Now read the RDF file that we got as input to update the ontology with it.
         // Basically, we list all the entities (classes, properties and instance)
         // and we update each of them, one by one, in both the OWLAPI instance
         // and structWSF if the advancedIndexation is enabled.
         include_once "../../framework/arc2/ARC2.php";
         $parser = ARC2::getRDFParser();
         $parser->parse($this->ws->ontology->getBaseUri(), $document);
         $rdfxmlSerializer = ARC2::getRDFXMLSerializer();
         $resourceIndex = $parser->getSimpleIndex(0);
         if (count($parser->getErrors()) > 0) {
             $errorsOutput = "";
             $errors = $parser->getErrors();
             foreach ($errors as $key => $error) {
                 $errorsOutput .= "[Error #{$key}] {$error}\n";
             }
             $this->ws->conneg->setStatus(400);
             $this->ws->conneg->setStatusMsg("Bad Request");
             $this->ws->conneg->setError($this->ws->errorMessenger->_301->id, $this->ws->errorMessenger->ws, $this->ws->errorMessenger->_301->name, $this->ws->errorMessenger->_301->description, $errorsOutput, $this->ws->errorMessenger->_301->level);
             return;
         }
         // Get all entities
         foreach ($resourceIndex as $uri => $description) {
             $types = array();
             $literalValues = array();
             $objectValues = array();
             foreach ($description as $predicate => $values) {
                 switch ($predicate) {
                     case Namespaces::$rdf . "type":
                         foreach ($values as $value) {
                             array_push($types, $value["value"]);
                         }
                         break;
                     default:
                         foreach ($values as $value) {
                             if ($value["type"] == "literal") {
                                 if (!is_array($literalValues[$predicate])) {
                                     $literalValues[$predicate] = array();
                                 }
                                 array_push($literalValues[$predicate], $value["value"]);
                             } else {
                                 if (!is_array($objectValues[$predicate])) {
                                     $objectValues[$predicate] = array();
                                 }
                                 array_push($objectValues[$predicate], $value["value"]);
                             }
                         }
                         break;
                 }
             }
             // Call different API calls depending what we are manipulating
             if ($this->in_array_r(Namespaces::$owl . "Ontology", $description[Namespaces::$rdf . "type"])) {
                 $this->ws->ontology->updateOntology($literalValues, $objectValues);
                 // Make sure advanced indexation is off when updating an ontology's description
                 $advancedIndexation = FALSE;
             } elseif ($this->in_array_r(Namespaces::$owl . "Class", $description[Namespaces::$rdf . "type"])) {
                 $this->ws->ontology->updateClass($uri, $literalValues, $objectValues);
             } elseif ($this->in_array_r(Namespaces::$owl . "DatatypeProperty", $description[Namespaces::$rdf . "type"]) || $this->in_array_r(Namespaces::$owl . "ObjectProperty", $description[Namespaces::$rdf . "type"]) || $this->in_array_r(Namespaces::$owl . "AnnotationProperty", $description[Namespaces::$rdf . "type"])) {
                 foreach ($types as $type) {
                     if (!is_array($objectValues[Namespaces::$rdf . "type"])) {
                         $objectValues[Namespaces::$rdf . "type"] = array();
                     }
                     array_push($objectValues[Namespaces::$rdf . "type"], $type);
                 }
                 $this->ws->ontology->updateProperty($uri, $literalValues, $objectValues);
             } else {
                 $this->ws->ontology->updateNamedIndividual($uri, $types, $literalValues, $objectValues);
             }
             // Call different API calls depending what we are manipulating
             if ($advancedIndexation == TRUE) {
                 include_once "../../framework/arc2/ARC2.php";
                 $rdfxmlParser = ARC2::getRDFParser();
                 $rdfxmlSerializer = ARC2::getRDFXMLSerializer();
                 $resourcesIndex = $rdfxmlParser->getSimpleIndex(0);
                 // Index the entity to update
                 $rdfxmlParser->parse($uri, $rdfxmlSerializer->getSerializedIndex(array($uri => $resourceIndex[$uri])));
                 $rIndex = $rdfxmlParser->getSimpleIndex(0);
                 $resourcesIndex = ARC2::getMergedIndex($resourcesIndex, $rIndex);
                 // Check if the entity got punned
                 $entities = $this->ws->ontology->_getEntities($uri);
                 if (count($entities) > 1) {
                     // The entity got punned.
                     $isClass = FALSE;
                     $isProperty = FALSE;
                     $isNamedEntity = FALSE;
                     foreach ($entities as $entity) {
                         if ((bool) java_values($entity->isOWLClass())) {
                             $isClass = TRUE;
                         }
                         if ((bool) java_values($entity->isOWLDataProperty()) || (bool) java_values($entity->isOWLObjectProperty()) || (bool) java_values($entity->isOWLAnnotationProperty())) {
                             $isProperty = TRUE;
                         }
                         if ((bool) java_values($entity->isOWLNamedIndividual())) {
                             $isNamedEntity = TRUE;
                         }
                     }
                     $queries = array();
                     if ($description[Namespaces::$rdf . "type"][0]["value"] != Namespaces::$owl . "Class" && $isClass) {
                         array_push($queries, array("function" => "getClass", "params" => "uri=" . $uri));
                     }
                     if ($description[Namespaces::$rdf . "type"][0]["value"] != Namespaces::$owl . "DatatypeProperty" && $description[Namespaces::$rdf . "type"][0]["value"] != Namespaces::$owl . "ObjectProperty" && $description[Namespaces::$rdf . "type"][0]["value"] != Namespaces::$owl . "AnnotationProperty" && $isProperty) {
                         array_push($queries, array("function" => "getProperty", "params" => "uri=" . $uri));
                     }
                     if ($description[Namespaces::$rdf . "type"][0]["value"] != Namespaces::$owl . "NamedIndividual" && $isNamedEntity) {
                         array_push($queries, array("function" => "getNamedIndividual", "params" => "uri=" . $uri));
                     }
                     foreach ($queries as $query) {
                         // Get the class description of the current punned entity
                         $ontologyRead = new OntologyRead($this->ws->ontologyUri, $query["function"], $query["params"], $this->ws->registered_ip, $this->ws->requester_ip);
                         // Since we are in pipeline mode, we have to set the owlapisession using the current one.
                         // otherwise the java bridge will return an error
                         $ontologyRead->setOwlApiSession($this->OwlApiSession);
                         $ontologyRead->ws_conneg("application/rdf+xml", $_SERVER['HTTP_ACCEPT_CHARSET'], $_SERVER['HTTP_ACCEPT_ENCODING'], $_SERVER['HTTP_ACCEPT_LANGUAGE']);
                         if ($this->ws->reasoner) {
                             $ontologyRead->useReasoner();
                         } else {
                             $ontologyRead->stopUsingReasoner();
                         }
                         $ontologyRead->process();
                         if ($ontologyRead->pipeline_getResponseHeaderStatus() != 200) {
                             $this->ws->conneg->setStatus($ontologyRead->pipeline_getResponseHeaderStatus());
                             $this->ws->conneg->setStatusMsg($ontologyRead->pipeline_getResponseHeaderStatusMsg());
                             $this->ws->conneg->setStatusMsgExt($ontologyRead->pipeline_getResponseHeaderStatusMsgExt());
                             $this->ws->conneg->setError($ontologyRead->pipeline_getError()->id, $ontologyRead->pipeline_getError()->webservice, $ontologyRead->pipeline_getError()->name, $ontologyRead->pipeline_getError()->description, $ontologyRead->pipeline_getError()->debugInfo, $ontologyRead->pipeline_getError()->level);
                             return;
                         }
                         $entitySerialized = $ontologyRead->pipeline_serialize();
                         $rdfxmlParser->parse($uri, $entitySerialized);
                         $rIndex = $rdfxmlParser->getSimpleIndex(0);
                         $resourcesIndex = ARC2::getMergedIndex($resourcesIndex, $rIndex);
                         unset($ontologyRead);
                     }
                 }
                 switch ($description[Namespaces::$rdf . "type"][0]["value"]) {
                     case Namespaces::$owl . "Class":
                     case Namespaces::$owl . "DatatypeProperty":
                     case Namespaces::$owl . "ObjectProperty":
                     case Namespaces::$owl . "AnnotationProperty":
                     case Namespaces::$owl . "NamedIndividual":
                     default:
                         // We have to check if this entity to update is punned. If yes, we have to merge all the
                         // punned descriptison together before updating them in structWSF (Virtuoso and Solr).
                         // otherwise we will loose information in these other systems.
                         // Once we start the ontology creation process, we have to make sure that even if the server
                         // loose the connection with the user the process will still finish.
                         ignore_user_abort(true);
                         // However, maybe there is an issue with the server handling that file tht lead to some kind of infinite or near
                         // infinite loop; so we have to limit the execution time of this procedure to 45 mins.
                         set_time_limit(2700);
                         $serializedResource = $rdfxmlSerializer->getSerializedIndex($resourcesIndex);
                         // Update the classes and properties into the Solr index
                         $crudUpdate = new CrudUpdate($serializedResource, "application/rdf+xml", $this->ws->ontologyUri, $this->ws->registered_ip, $this->ws->requester_ip);
                         $crudUpdate->ws_conneg($_SERVER['HTTP_ACCEPT'], $_SERVER['HTTP_ACCEPT_CHARSET'], $_SERVER['HTTP_ACCEPT_ENCODING'], $_SERVER['HTTP_ACCEPT_LANGUAGE']);
                         $crudUpdate->process();
                         if ($crudUpdate->pipeline_getResponseHeaderStatus() != 200) {
                             $this->ws->conneg->setStatus($crudUpdate->pipeline_getResponseHeaderStatus());
                             $this->ws->conneg->setStatusMsg($crudUpdate->pipeline_getResponseHeaderStatusMsg());
                             $this->ws->conneg->setStatusMsgExt($crudUpdate->pipeline_getResponseHeaderStatusMsgExt());
                             $this->ws->conneg->setError($crudUpdate->pipeline_getError()->id, $crudUpdate->pipeline_getError()->webservice, $crudUpdate->pipeline_getError()->name, $crudUpdate->pipeline_getError()->description, $crudUpdate->pipeline_getError()->debugInfo, $crudUpdate->pipeline_getError()->level);
                             return;
                         }
                         unset($crudUpdate);
                         /*            
                                       // Once we start the ontology creation process, we have to make sure that even if the server
                                       // loose the connection with the user the process will still finish.
                                       ignore_user_abort(true);
                         
                                       // However, maybe there is an issue with the server handling that file tht lead to some kind of infinite or near
                                       // infinite loop; so we have to limit the execution time of this procedure to 45 mins.
                                       set_time_limit(2700);  
                                       
                                       $ser = ARC2::getTurtleSerializer();
                                       $serializedResource = $ser->getSerializedIndex(array($uri => $resourceIndex[$uri]));
                                       
                                       // Update the classes and properties into the Solr index
                                       $crudUpdate = new CrudUpdate($serializedResource, "application/rdf+n3", $this->ws->ontologyUri, 
                                                                    $this->ws->registered_ip, $this->ws->requester_ip);
                         
                                       $crudUpdate->ws_conneg($_SERVER['HTTP_ACCEPT'], $_SERVER['HTTP_ACCEPT_CHARSET'], $_SERVER['HTTP_ACCEPT_ENCODING'],
                                         $_SERVER['HTTP_ACCEPT_LANGUAGE']);
                         
                                       $crudUpdate->process();
                                       
                                       if($crudUpdate->pipeline_getResponseHeaderStatus() != 200)
                                       {
                                         $this->ws->conneg->setStatus($crudUpdate->pipeline_getResponseHeaderStatus());
                                         $this->ws->conneg->setStatusMsg($crudUpdate->pipeline_getResponseHeaderStatusMsg());
                                         $this->ws->conneg->setStatusMsgExt($crudUpdate->pipeline_getResponseHeaderStatusMsgExt());
                                         $this->ws->conneg->setError($crudUpdate->pipeline_getError()->id, $crudUpdate->pipeline_getError()->webservice,
                                           $crudUpdate->pipeline_getError()->name, $crudUpdate->pipeline_getError()->description,
                                           $crudUpdate->pipeline_getError()->debugInfo, $crudUpdate->pipeline_getError()->level);
                         
                                         return;
                                       } 
                                       
                                       unset($crudUpdate);  
                         */
                         break;
                 }
             }
         }
         // Update the name of the file of the ontology to mark it as "changed"
         $this->ws->ontology->addOntologyAnnotation("http://purl.org/ontology/wsf#ontologyModified", "true");
     }
 }
Example #2
0
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
$start_datetime = date("Y-m-d h:i:s");
$requester_ip = "0.0.0.0";
if (isset($_SERVER['REMOTE_ADDR'])) {
    $requester_ip = $_SERVER['REMOTE_ADDR'];
}
$parameters = "";
if (isset($_SERVER['REQUEST_URI'])) {
    $parameters = $_SERVER['REQUEST_URI'];
    $pos = strpos($parameters, "?");
    if ($pos !== FALSE) {
        $parameters = substr($parameters, $pos, strlen($parameters) - $pos);
    }
} elseif (isset($_SERVER['PHP_SELF'])) {
    $parameters = $_SERVER['PHP_SELF'];
}
$ws_crudupdate = new CrudUpdate($document, $mime, $dataset, $registered_ip, $requester_ip, $interface, $version);
$ws_crudupdate->ws_conneg(isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : "", isset($_SERVER['HTTP_ACCEPT_CHARSET']) ? $_SERVER['HTTP_ACCEPT_CHARSET'] : "", isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : "", isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : "");
$ws_crudupdate->process();
$ws_crudupdate->ws_respond($ws_crudupdate->ws_serialize());
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = $endtime - $starttime;
//echo $totaltime."ms\n";
if ($ws_crudupdate->isLoggingEnabled()) {
    $logger = new Logger("crud_update", $requester_ip, "?document=" . substr($document, 0, 64) . "&mime=" . $mime . "&dataset=" . $dataset . "&registered_ip=" . $registered_ip . "&requester_ip={$requester_ip}", isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : "", $start_datetime, $totaltime, $ws_crudupdate->pipeline_getResponseHeaderStatus(), isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "");
}
//@}