Ejemplo n.º 1
0
 public function __construct(Erfurt_Sparql_Query2_IriRef $type, $member_predicate = EF_RDF_TYPE, Erfurt_Sparql_Query2 $query, $varName = null, $withChilds = true)
 {
     $this->query = $query;
     if ($member_predicate == EF_RDF_TYPE) {
         $type = new Erfurt_Sparql_Query2_Abstraction_RDFSClass($type, $withChilds);
         $member_predicate = new Erfurt_Sparql_Query2_A();
     } else {
         $type = new Erfurt_Sparql_Query2_Abstraction_NoClass($type, $member_predicate);
     }
     if (is_string($member_predicate)) {
         $member_predicate = new Erfurt_Sparql_Query2_IriRef($member_predicate);
     }
     $this->type = $type;
     if ($varName == null) {
         $this->classVar = new Erfurt_Sparql_Query2_Var($type->getIri());
     } else {
         $this->classVar = new Erfurt_Sparql_Query2_Var($varName);
     }
     if (!$member_predicate instanceof Erfurt_Sparql_Query2_Verb) {
         throw new RuntimeException('Argument 2 passed to Erfurt_Sparql_Query2_Abstraction_ClassNode::__construct must be an instance of Erfurt_Sparql_Query2_IriRef or string instance of ' . typeHelper($member_predicate) . ' given');
     }
     $subclasses = $type->getSubclasses();
     if (count($subclasses) > 1) {
         //the class itself is somehow included in the subclasses...
         $typeVar = new Erfurt_Sparql_Query2_Var($type->getIri());
         $typePart = new Erfurt_Sparql_Query2_Triple($this->classVar, $member_predicate, $typeVar);
         $this->query->getWhere()->addElement($typePart);
         $or = new Erfurt_Sparql_Query2_ConditionalOrExpression();
         foreach ($subclasses as $subclass) {
             $or->addElement(new Erfurt_Sparql_Query2_sameTerm($typeVar, $subclass));
         }
         $filter = new Erfurt_Sparql_Query2_Filter($or);
         $this->query->getWhere()->addElement($filter);
     } else {
         $typePart = new Erfurt_Sparql_Query2_Triple($this->classVar, $member_predicate, $type->getIri());
         $this->query->getWhere()->addElement($typePart);
     }
 }
Ejemplo n.º 2
0
 /**
  *
  * @param string $type the uri of the class
  * @param string $id
  * @param array  $options
  *
  * @return int id
  */
 public function addTypeFilter($type, $id = null, $option = array())
 {
     if ($id == null) {
         $id = 'type' . count($this->_filter);
     } else {
         if (isset($this->_filter[$id])) {
             $this->removeFilter($id);
         }
     }
     //shortcut navigation - only a rdfs class given
     $options['mode'] = 'instances';
     $options['type'] = $type;
     $options['memberPredicate'] = EF_RDF_TYPE;
     $options['withChilds'] = isset($option['withChilds']) ? $option['withChilds'] : true;
     $options['hierarchyUp'] = EF_RDFS_SUBCLASSOF;
     $options['hierarchyIsInverse'] = true;
     //$options['hierarchyDown'] = null;
     $options['direction'] = 1;
     // down the tree
     $memberPredicate = $options['memberPredicate'];
     if (is_string($memberPredicate)) {
         $memberPredicate = new Erfurt_Sparql_Query2_IriRef($memberPredicate);
     }
     if (!$memberPredicate instanceof Erfurt_Sparql_Query2_Verb) {
         throw new RuntimeException('Option "member_predicate" passed to Ontowiki_Model_Instances ' . 'must be an instance of Erfurt_Sparql_Query2_IriRef ' . 'or string instance of ' . typeHelper($memberPredicate) . ' given');
     }
     $type = new Erfurt_Sparql_Query2_IriRef($options['type']);
     $subClasses = array();
     if ($options['withChilds']) {
         $subClasses = array_keys($this->_store->getTransitiveClosure($this->_graph, $options['hierarchyUp'], array($type->getIri()), $options['hierarchyIsInverse']));
     } else {
         if (isset($options['subtypes'])) {
             //dont query, take the given. maybe the new navigation can use this
             $subClasses = $options['subtypes'];
         } else {
             $subClasses = array();
         }
     }
     if (count($subClasses) > 1) {
         // there are subclasses. "1" because the class itself is somehow included in the subclasses...
         $typeVar = new Erfurt_Sparql_Query2_Var($type);
         $triple = $this->_resourceQuery->addTriple($this->_resourceVar, $memberPredicate, $typeVar);
         $or = new Erfurt_Sparql_Query2_ConditionalOrExpression();
         foreach ($subClasses as $subclass) {
             $or->addElement(new Erfurt_Sparql_Query2_Equals($typeVar, new Erfurt_Sparql_Query2_IriRef($subclass)));
         }
         $filterObj = $this->_resourceQuery->addFilter($or);
     } else {
         // no subclasses
         $triple = $this->_resourceQuery->addTriple($this->_resourceVar, $memberPredicate, new Erfurt_Sparql_Query2_IriRef($type->getIri()));
     }
     //save
     $this->_filter[$id] = array('id' => $id, 'mode' => 'rdfsclass', 'rdfsclass' => $options['type'], 'withChilds' => $options['withChilds'], 'objects' => array($triple, isset($filterObj) ? $filterObj : null));
     //these filters bring there own triple
     $this->removeAllTriple();
     $this->invalidate();
     return $id;
 }
 /**
  * Method that counts already existing distinct datasets for given uri
  *
  * @param $uri uri string
  *
  * @return int distinct existing datasets
  */
 private function countUriPattern($uri)
 {
     $query = new Erfurt_Sparql_Query2();
     $query->setDistinct(true);
     $unions = new Erfurt_Sparql_Query2_GroupOrUnionGraphPattern();
     $subjectVar = new Erfurt_Sparql_Query2_Var('s');
     $query->addProjectionVar($subjectVar);
     // create six temporary vars (not selected in query)
     $tempVars = array();
     for ($i = 0; $i < 6; $i++) {
         $tempVars[] = new Erfurt_Sparql_Query2_Var('var' . $i);
     }
     $singlePattern = new Erfurt_Sparql_Query2_GroupGraphPattern();
     $singlePattern->addTriple($subjectVar, $tempVars[0], $tempVars[1]);
     $unions->addElement($singlePattern);
     $singlePattern = new Erfurt_Sparql_Query2_GroupGraphPattern();
     $singlePattern->addTriple($tempVars[2], $subjectVar, $tempVars[3]);
     $unions->addElement($singlePattern);
     $singlePattern = new Erfurt_Sparql_Query2_GroupGraphPattern();
     $singlePattern->addTriple($tempVars[4], $tempVars[5], $subjectVar);
     $unions->addElement($singlePattern);
     $query->getWhere()->addElement($unions);
     $filter = new Erfurt_Sparql_Query2_ConditionalOrExpression();
     $filter->addElement(new Erfurt_Sparql_Query2_Regex($subjectVar, new Erfurt_Sparql_Query2_RDFLiteral('^' . $uri), new Erfurt_Sparql_Query2_RDFLiteral('i')));
     $query->addFilter($filter);
     $result = $this->_owApp->erfurt->getStore()->countWhereMatches($this->_model->getModelIri(), $query->getWhere(), 's', true);
     return $result;
 }