/**
 * @file
 * @ingroup SMWHaloTriplestore
 * 
 * Simple contributor only updates the 'has type' annotations.
 *
 * Called when property annotations get updated in a triple store.
 *
 * @param $semData All semantic data (for context)
 * @param $property Currently processed property
 * @param $propertyValueArray Values of current property
 * @param $triplesFromHook Triples which are returned.
 *
 * @return Array of triples or false. If return value is a non-empty array processing stops for this property. Same if it is explicitly false.
 * Otherwise normal processing goes on.
 */
function smwfTripleStorePropertyUpdate(&$data, &$property, &$propertyValueArray, &$triplesFromHook)
{
    global $smwgTripleStoreGraph;
    if (!$property instanceof SMWPropertyValue) {
        // error. should not happen
        trigger_error("Triple store update: property is not SMWPropertyValue");
        return true;
    }
    // check if it is a property with special semantics
    // check for 'has domain, range' and 'is inverse of' and 'has type'
    // 'has min cardinality' and 'has max cardinality are read implictly when processing 'has domain and range'
    // and therefore ignored.
    $allProperties = $data->getProperties();
    if (smwfGetSemanticStore()->inverseOf->getDBkey() == array_shift($property->getDBkeys())) {
        foreach ($propertyValueArray as $inverseProps) {
            if (count($propertyValueArray) == 1) {
                $triplesFromHook[] = array("<{$smwgTripleStoreGraph}/property#" . $data->getSubject()->getDBkey() . ">", "owl:inverseOf", "<{$smwgTripleStoreGraph}/property#" . $inverseProps->getDBkey() . ">");
            }
        }
    } elseif ($property->getPropertyID() == "_TYPE") {
        // insert RDFS range/domain
        foreach ($propertyValueArray as $value) {
            $typeID = array_shift($value->getDBkeys());
            if ($typeID != '_wpg') {
                $triplesFromHook[] = array("<{$smwgTripleStoreGraph}/property#" . $data->getSubject()->getDBkey() . ">", "<{$smwgTripleStoreGraph}/property#Has_type>", WikiTypeToXSD::getXSDType($typeID));
            }
        }
    }
    return true;
}
 private function getLiteral($literal, $predicate)
 {
     list($literalValue, $literalType) = $literal;
     if (!empty($literalValue)) {
         // create SMWDataValue either by property or if that is not possible by the given XSD type
         if ($predicate instanceof SMWPropertyValue) {
             $value = SMWDataValueFactory::newPropertyObjectValue($predicate, $literalValue);
         } else {
             $value = SMWDataValueFactory::newTypeIDValue(WikiTypeToXSD::getWikiType($literalType));
         }
         if ($value->getTypeID() == '_dat') {
             // exception for dateTime
             if ($literalValue != '') {
                 $value->setDBkeys(array(str_replace("-", "/", $literalValue)));
             }
         } else {
             if ($value->getTypeID() == '_ema') {
                 // exception for email
                 $value->setDBkeys(array($literalValue));
             } else {
                 $value->setUserValue($literalValue);
             }
         }
     } else {
         if ($predicate instanceof SMWPropertyValue) {
             $value = SMWDataValueFactory::newPropertyObjectValue($predicate);
         } else {
             $value = SMWDataValueFactory::newTypeIDValue('_wpg');
         }
     }
     return $value;
 }
Exemplo n.º 3
0
 /**
  * Gets an array of literal tuples (value, type, metadata-uri) and creates according
  * SMWDataValue objects.
  *
  * @param array Tuple (string value, string xsd-type, hash array metadata) $literals
  * @param PrintRequest $pr QueryPrinter contains property and thus denotes type (optional)
  * @param array SMWDataValue (out) & $allValues
  */
 protected function addLiteralToResult($literals, $pr, &$allValues)
 {
     foreach ($literals as $literal) {
         list($literalValue, $literalType, $metadata) = $literal;
         $property = !is_null($pr) ? $pr->getData() : NULL;
         if (!empty($literalValue)) {
             // create SMWDataValue either by property or if that is not possible by the given XSD type
             if ($property instanceof SMWPropertyValue) {
                 $propertyTitle = Title::newFromText($pr->getData()->getText(), SMW_NS_PROPERTY);
                 if (!$propertyTitle->exists()) {
                     // fallback if property does not exist, then use tyoe
                     $value = SMWDataValueFactory::newTypeIDValue(WikiTypeToXSD::getWikiType($literalType));
                 } else {
                     $value = SMWDataValueFactory::newPropertyObjectValue($pr->getData(), $literalValue);
                 }
             } else {
                 $value = SMWDataValueFactory::newTypeIDValue(WikiTypeToXSD::getWikiType($literalType));
             }
             if ($value->getTypeID() == '_dat') {
                 // exception for dateTime
                 if ($literalValue != '') {
                     // do not display time if it is 00:00:00
                     if (substr($literalValue, -9) == 'T00:00:00') {
                         $literalValue = substr($literalValue, 0, strpos($literalValue, "T"));
                     }
                     $value->setDBkeys(array(str_replace("-", "/", $literalValue)));
                 }
             } else {
                 if ($value->getTypeID() == '_ema' || $value->getTypeID() == '_tel') {
                     // exception for email
                     $value->setDBkeys(array($literalValue));
                 } else {
                     $value->setUserValue($literalValue);
                 }
             }
         } else {
             if ($property instanceof SMWPropertyValue) {
                 $value = SMWDataValueFactory::newPropertyObjectValue($property);
             } else {
                 $value = SMWDataValueFactory::newTypeIDValue('_wpg');
             }
         }
         foreach ($metadata as $mdProperty => $mdValue) {
             if (strpos($mdProperty, "_meta_") === 0) {
                 $value->setMetadata(substr($mdProperty, 6), explode("|||", $mdValue));
             }
         }
         $allValues[] = $value;
     }
 }
/**
 * @file
 * @ingroup SMWHaloTriplestore
 * 
 * Schema contributor tries to add all schema information from the wiki
 * Warning: may created complex models.
 * 
 * Called when property annotations get updated in a triple store.
 *
 * @param $semData All semantic data (for context)
 * @param $property Currently processed property
 * @param $propertyValueArray Values of current property
 * @param $triplesFromHook Triples which are returned.
 *
 * @return Array of triples or false. If return value is a non-empty array processing stops for this property. Same if it is explicitly false.
 * Otherwise normal processing goes on.
 */
function smwfTripleStorePropertyUpdate(&$data, &$property, &$propertyValueArray, &$triplesFromHook)
{
    global $smwgTripleStoreGraph;
    if (!$property instanceof SMWPropertyValue) {
        // error. should not happen
        trigger_error("Triple store update: property is not SMWPropertyValue");
        return true;
    }
    // check if it is a property with special semantics
    // check for 'has domain, range' and 'is inverse of' and 'has type'
    // 'has min cardinality' and 'has max cardinality are read implictly when processing 'has domain and range'
    // and therefore ignored.
    $allProperties = $data->getProperties();
    if (smwfGetSemanticStore()->domainRangeHintRelation->getDBkey() == array_shift($property->getDBkeys())) {
        foreach ($propertyValueArray as $domRange) {
            if (!$domRange instanceof SMWRecordValue) {
                continue;
            }
            // occurs if 'has domain and range' is not n-ary
            if (count($domRange->getDVs()) == 2) {
                $dvs = $domRange->getDVs();
                if ($dvs[0] != NULL && $dvs[1] != NULL && $dvs[0]->isValid() && $dvs[1]->isValid()) {
                    // domain and range
                    $minCard = $data->getPropertyValues(smwfGetSemanticStore()->minCardProp);
                    $maxCard = $data->getPropertyValues(smwfGetSemanticStore()->maxCardProp);
                    // insert RDFS
                    $triplesFromHook[] = array("<{$smwgTripleStoreGraph}/property#" . $data->getSubject()->getDBkey() . ">", "rdfs:domain", "cat:" . $dvs[0]->getDBkey());
                    $triplesFromHook[] = array("<{$smwgTripleStoreGraph}/property#" . $data->getSubject()->getDBkey() . ">", "rdfs:range", "cat:" . $dvs[1]->getDBkey());
                    // insert OWL
                    $triplesFromHook[] = array("<{$smwgTripleStoreGraph}/category#" . $dvs[0]->getDBkey() . ">", "rdfs:subClassOf", "_:1");
                    $triplesFromHook[] = array("_:1", "owl:Restriction", "_:2");
                    $triplesFromHook[] = array("_:2", "owl:onProperty", "<{$smwgTripleStoreGraph}/property#" . $data->getSubject()->getDBkey() . ">");
                    $triplesFromHook[] = array("_:2", "owl:allValuesFrom", "<{$smwgTripleStoreGraph}/category#" . $dvs[1]->getDBkey() . ">");
                    foreach ($minCard as $value) {
                        if (array_shift($value->getDBkeys()) !== false) {
                            $triplesFromHook[] = array("_:2", "owl:minCardinality", "\"" . array_shift($value->getDBkeys()) . "\"");
                        }
                    }
                    foreach ($maxCard as $value) {
                        if (array_shift($value->getDBkeys()) !== false) {
                            $triplesFromHook[] = array("_:2", "owl:minCardinality", "\"" . array_shift($value->getDBkeys()) . "\"");
                        }
                    }
                } elseif ($dvs[0] != NULL && $dvs[0]->isValid()) {
                    // only domain
                    $typeValues = $data->getPropertyValues(SMWPropertyValue::makeProperty("_TYPE"));
                    $minCard = $data->getPropertyValues(smwfGetSemanticStore()->minCardProp);
                    $maxCard = $data->getPropertyValues(smwfGetSemanticStore()->maxCardProp);
                    // insert RDFS
                    $triplesFromHook[] = array("prop:" . $data->getSubject()->getDBkey(), "rdfs:domain", "<{$smwgTripleStoreGraph}/category#" . $dvs[0]->getDBkey() . ">");
                    foreach ($typeValues as $value) {
                        if (array_shift($value->getDBkeys()) !== false) {
                            $typeID = array_shift($value->getDBkeys());
                            if ($typeID != '_wpg') {
                                $triplesFromHook[] = array("<{$smwgTripleStoreGraph}/property#" . $data->getSubject()->getDBkey() . ">", "rdfs:range", WikiTypeToXSD::getXSDType($typeID));
                            }
                        }
                    }
                    // insert OWL
                    $triplesFromHook[] = array("<{$smwgTripleStoreGraph}/category#" . $dvs[0]->getDBkey() . ">", "rdfs:subClassOf", "_:1");
                    $triplesFromHook[] = array("_:1", "owl:Restriction", "_:2");
                    $triplesFromHook[] = array("_:2", "owl:onProperty", "<{$smwgTripleStoreGraph}/property#" . $data->getSubject()->getDBkey() . ">");
                    foreach ($typeValues as $value) {
                        if (array_shift($value->getDBkeys()) !== false) {
                            $triplesFromHook[] = array("_:2", "owl:allValuesFrom", WikiTypeToXSD::getXSDType(array_shift($value->getDBkeys())));
                        }
                    }
                    foreach ($minCard as $value) {
                        if (array_shift($value->getDBkeys()) !== false) {
                            $triplesFromHook[] = array("_:2", "owl:minCardinality", "\"" . array_shift($value->getDBkeys()) . "\"");
                        }
                    }
                    foreach ($maxCard as $value) {
                        if (array_shift($value->getDBkeys()) !== false) {
                            $triplesFromHook[] = array("_:2", "owl:maxCardinality", "\"" . array_shift($value->getDBkeys()) . "\"");
                        }
                    }
                }
            }
        }
    } elseif (smwfGetSemanticStore()->inverseOf->getDBkey() == array_shift($property->getDBkeys())) {
        foreach ($propertyValueArray as $inverseProps) {
            if (count($propertyValueArray) == 1) {
                $triplesFromHook[] = array("<{$smwgTripleStoreGraph}/property#" . $data->getSubject()->getDBkey() . ">", "owl:inverseOf", "<{$smwgTripleStoreGraph}/property#" . $inverseProps->getDBkey() . ">");
            }
        }
    } elseif (smwfGetSemanticStore()->minCard->getDBkey() == array_shift($property->getDBkeys())) {
        // do nothing
        $triplesFromHook = false;
    } elseif (smwfGetSemanticStore()->maxCard->getDBkey() == array_shift($property->getDBkeys())) {
        // do nothing
        $triplesFromHook = false;
    } elseif ($property->getPropertyID() == "_TYPE") {
        // serialize type only if there is no domain and range annotation
        $domRanges = $data->getPropertyValues(smwfGetSemanticStore()->domainRangeHintProp);
        if (count($domRanges) == 0) {
            // insert only if domain and range annotation does not exist
            // insert OWL restrictions
            $minCard = $data->getPropertyValues(smwfGetSemanticStore()->minCardProp);
            $maxCard = $data->getPropertyValues(smwfGetSemanticStore()->maxCardProp);
            $triplesFromHook[] = array("<{$smwgTripleStoreGraph}/property#DefaultRootCategory>", "rdfs:subClassOf", "_:1");
            $triplesFromHook[] = array("_:1", "owl:Restriction", "_:2");
            $triplesFromHook[] = array("_:2", "owl:onProperty", "<{$smwgTripleStoreGraph}/property#" . $data->getSubject()->getDBkey() . ">");
            foreach ($propertyValueArray as $value) {
                if (array_shift($value->getDBkeys()) !== false) {
                    $typeID = array_shift($value->getDBkeys());
                    $triplesFromHook[] = array("_:2", "owl:allValuesFrom", WikiTypeToXSD::getXSDType($typeID));
                }
            }
            foreach ($minCard as $value) {
                if (array_shift($value->getDBkeys()) !== false) {
                    $triplesFromHook[] = array("_:2", "owl:minCardinality", "\"" . array_shift($value->getDBkeys()) . "\"");
                }
            }
            foreach ($maxCard as $value) {
                if (array_shift($value->getDBkeys()) !== false) {
                    $triplesFromHook[] = array("_:2", "owl:maxCardinality", "\"" . array_shift($value->getDBkeys()) . "\"");
                }
            }
            // insert RDFS range/domain
            foreach ($propertyValueArray as $value) {
                $typeID = array_shift($value->getDBkeys());
                //$triplesFromHook[] = array("prop:".$data->getSubject()->getDBkey(), "rdfs:domain", "cat:DefaultRootCategory");
                if ($typeID != '_wpg') {
                    $triplesFromHook[] = array("<{$smwgTripleStoreGraph}/property#" . $data->getSubject()->getDBkey() . ">", "rdfs:range", WikiTypeToXSD::getXSDType($typeID));
                }
            }
        }
        // insert Has type
        foreach ($propertyValueArray as $value) {
            $typeID = array_shift($value->getDBkeys());
            if ($typeID != '_wpg') {
                $triplesFromHook[] = array("<{$smwgTripleStoreGraph}/property#" . $data->getSubject()->getDBkey() . ">", "Has_type", WikiTypeToXSD::getXSDType($typeID));
            }
        }
    }
    return true;
}