/** * This method takes a json encoded rdf-model and a reference to aa (usually empty) MemModel, parses the json * string and adds the statements to the given MemModel. * * @param string $jsonString The string that contains the rdf model, encoded as a json-string. * @param MemModel $model A reference to the model, where to add the statements, usually an empty MemModel. */ public function generateModelFromString($jsonString, $model) { $jsonModel = array(); $jsonModel = json_decode($jsonString, true); // throws an excpetion if json model was corrupt if (!is_array($jsonModel)) { throw new Exception('error in json string'); } foreach ($jsonModel as $subject => $remain) { foreach ($remain as $predicate => $object) { $s = strpos($subject, '_') === 0 ? new BlankNode(substr($subject, 2)) : new Resource($subject); $p = new Resource($predicate); foreach ($object as $obj) { if ($obj['type'] === 'uri') { $o = new Resource($obj['value']); } else { if ($obj['type'] === 'bnode') { $o = new BlankNode(substr($obj['value'], 2)); } else { $dtype = isset($obj['datatype']) ? $obj['datatype'] : ''; $lang = isset($obj['lang']) ? $obj['lang'] : ''; $oVal = $obj['value']; $o = new Literal($oVal, $lang); $o->setDatatype($dtype); } } $model->add(new Statement($s, $p, $o)); } } } }
private function characters($parser, $data) { //print_r("characters : $data<p/> \n"); if ($this->curElement == "URI" || $this->curElement == "BNODE" || $this->curElement == "LITERAL") { switch ($this->curVar) { case "Res": //print_r("Neue Resource : $data <p/> \n"); $this->curArray["?Res"] = new Resource($data); break; case "Attr": //print_r("Neues Attr : $data <p/> \n"); $this->curArray["?Attr"] = new Resource($data); break; case "Val": if ($this->curElement != "LITERAL") { //print_r("Neue Resource als Val: $data <p/> \n"); $this->curArray["?Val"] = new Resource($data); } else { $this->curLiteralData .= $data; $l = new Literal($this->curLiteralData); $l->setDatatype($this->curDataType); $this->curArray["?Val"] = $l; } break; } } }
/** * tests if contains returns false if the Literal in the given statement has wrong datatype * using all kinds of indices. */ function testContainsLiteralFalse() { $_SESSION['test'] = 'MemModel contains literal false test'; for ($i = -1; $i < 4; $i++) { $mod = $this->_generateModelLiteral(3, $i); $lit = new Literal('http://www.example.org/obj2'); $lit->setDatatype('notTest'); $stat = new Statement(new Resource('http://www.example.org/sub2'), new Resource('http://www.example.org/pred2'), $lit); $this->assertFalse($mod->contains($stat)); } }
public function accept($extractionResult) { $model1 = ModelFactory::getDefaultModel("http://dbpedia.org/"); // RAP model $count = 0; foreach (new ArrayObject($extractionResult->getTriples()) as $triple) { $count++; $tripleString = explode(">", $triple->toString()); $s = str_replace("<", "", $tripleString[0]); $p = str_replace("<", "", $tripleString[1]); // $s = preg_replace("/<|>/","",$triple->getSubject()); // $p = preg_replace("/<|>/","",$triple->getPredicate()); $o = $tripleString[2]; $subject = new Resource($s); $predicate = new Resource($p); if (strpos($o, "<")) { // echo "<br>" . $s. $p . $p; if (!strpos($o, "^^")) { // echo " a"; $o = str_replace("<", "", $o); $object = new Resource($o); } else { // echo " b"; $pos = strpos($o, "^^"); $literal = substr($o, 0, $pos); $object = new Literal($literal); $object->setDatatype(substr($o, $pos + 3, strlen($o) - $pos - 3)); } } else { // $lang = "en"; if (preg_match("/(.*)(@)([a-zA-Z]+) \\.\$/", $o, $match)) { $o = $match[1]; $lang = $match[3]; $object = new Literal($o, $lang); } else { $object = new Literal($o); } } $statement = new Statement($subject, $predicate, $object); $model1->add($statement); } if ($count > 0) { echo "<br><br><h3>" . $extractionResult->getExtractorID() . "</h3>"; echo $model1->writeAsHtmlTable(); } }
private function renderWidgetList($value, $choices, $attributes) { $name = $this->getAttribute('name'); $flags = $this->getAttribute('flags'); $hiddenWidget = new Hidden($this->view); $hiddenWidget->setAttribute('flags', $flags)->setAttribute('value', '')->setAttribute('class', 'Hidden')->setAttribute('name', $name); $contentWidget = new Literal($this->view); $contentWidget->setAttribute('data', $this->generateSelectorContentWidgetList($name, $value, $choices, $flags)); $panelWidget = new Panel($this->view); $panelWidget->setAttribute('class', $attributes['class'])->setAttribute('name', $name)->insert($hiddenWidget)->insert($contentWidget); if ($this->getAttribute('flags') & \Nethgui\Renderer\WidgetFactoryInterface::LABEL_NONE) { return $panelWidget->renderContent(); } $fieldsetWidget = new Fieldset($this->view); $fieldsetWidget->setAttribute('template', $this->getAttribute('label', $this->getTranslateClosure($name . '_label')))->setAttribute('flags', $this->getAttribute('flags')); if ($this->hasAttribute('icon-before')) { $fieldsetWidget->setAttribute('icon-before', $this->getAttribute('icon-before')); } $fieldsetWidget->insert($panelWidget); return $fieldsetWidget->renderContent(); }
function generateResultStatementsFromVarResult(&$result, $parsedq, &$outm, $closure, &$model) { foreach ($parsedq['patterns'] as $n => $pattern) { if (substr($pattern['subject']['value'], 0, 1) == '?') { $subj = $result[$pattern['subject']['value']]; } else { $subj = new Resource($pattern['subject']['value']); } if (substr($pattern['predicate']['value'], 0, 1) == '?') { $pred = $result[$pattern['predicate']['value']]; } else { $pred = new Resource($pattern['predicate']['value']); } if (substr($pattern['object']['value'], 0, 1) == '?') { $obj = $result[$pattern['object']['value']]; } else { if ($pattern['object']['is_literal']) { $obj = new Literal($pattern['object']['value']); $obj->setDatatype($pattern['object']['l_dtype']); $obj->setLanguage($pattern['object']['l_lang']); } else { $obj = new Resource($pattern['object']['value']); } } $stmt = new Statement($subj, $pred, $obj); // bNode closure if (is_a($stmt->object(), 'BlankNode') && $closure == True) { getBNodeClosure($stmt->object(), $model, $outm); } if (is_a($stmt->subject(), 'BlankNode') && $closure == True) { getBNodeClosure($stmt->subject(), $model, $outm); } // Add statement to model $outm->add($stmt); } }
/** * Convert an ADORecordSet to a memory Model. * * Every successful database query returns an ADORecordSet object which is actually * a cursor that holds the current row in the array fields[]. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! * !!! This method can only be applied to a RecordSet with array fields[] * !!! containing a representation of the database table: statements, * !!! with an index corresponding to following table columns: * !!! [0] - subject, [1] - predicate, [2] - object, [3] - l_language, * !!! [4] - l_datatype, [5] - subject_is, [6] - object_is * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! * * @param object ADORecordSet * @return object MemModel * @access private */ function _convertRecordSetToMemModel(&$recordSet) { $res = new MemModel($this->baseURI); while (!$recordSet->EOF) { // subject if ($recordSet->fields[5] == 'r') { $sub = new Resource($recordSet->fields[0]); } else { $sub = new BlankNode($recordSet->fields[0]); } // predicate $pred = new Resource($recordSet->fields[1]); // object if ($recordSet->fields[6] == 'r') { $obj = new Resource($recordSet->fields[2]); } elseif ($recordSet->fields[6] == 'b') { $obj = new BlankNode($recordSet->fields[2]); } else { $obj = new Literal($recordSet->fields[2], $recordSet->fields[3]); if ($recordSet->fields[4]) { $obj->setDatatype($recordSet->fields[4]); } } $statement = new Statement($sub, $pred, $obj); $res->add($statement); $recordSet->moveNext(); } $res->addParsedNamespaces($this->getParsedNamespaces()); return $res; }
/** * Returns the current item. * * @return mixed * @access public */ function ¤t() { if ($this->dbResultSet === false) { return null; } // subject if ($this->dbResultSet->fields[5] == 'r') { $sub = new Resource($this->dbResultSet->fields[0]); } else { $sub = new BlankNode($this->dbResultSet->fields[0]); } // predicate $pred = new Resource($this->dbResultSet->fields[1]); // object if ($this->dbResultSet->fields[6] == 'r') { $obj = new Resource($this->dbResultSet->fields[2]); } elseif ($this->dbResultSet->fields[6] == 'b') { $obj = new BlankNode($this->dbResultSet->fields[2]); } else { $obj = new Literal($this->dbResultSet->fields[2], $this->dbResultSet->fields[3]); if ($this->dbResultSet->fields[4]) { $obj->setDatatype($this->dbResultSet->fields[4]); } } if ($this->returnAsTriples) { return new Statement($sub, $pred, $obj); } return new Quad(new Resource($this->dbResultSet->fields[7]), $sub, $pred, $obj); }
/** Add a literal value as a property of a resource * * The resource can either be a resource or the URI of a resource. * The value can either be a single value or an array of values. * * Example: * $graph->add("http://www.example.com", 'dc:title', 'Title of Page'); * * @param mixed $resource The resource to add data to * @param mixed $property The property name * @param mixed $value The value or values for the property * @param string $lang The language of the literal * * @return integer The number of values added */ public function addLiteral($resource, $property, $value, $lang = null) { $this->checkResourceParam($resource); $this->checkSinglePropertyParam($property, $inverse); if (is_array($value)) { $added = 0; foreach ($value as $v) { $added += $this->addLiteral($resource, $property, $v, $lang); } return $added; } elseif (!is_object($value) or !$value instanceof Literal) { $value = Literal::create($value, $lang); } return $this->add($resource, $property, $value); }
private function body_for_pattern_matcher($patterns, $context) { $body = ''; foreach ($patterns as $regex => $native_method) { $regex = new Literal($regex); $body .= 'if (preg_match(' . $regex->to_php() . ', $method, $matches)) {'; $body .= ' $args[] = $matches;'; $body .= ' return call_user_func_array(array(' . $context . ', "' . $native_method . '"), $args);'; $body .= '}'; } $body .= 'throw new \\Exception("Unknown method \'$method\'");'; return $body; }
function tagData($parser, $tagData) { switch ($this->mode) { case 0: $this->arrOutput[$this->counter][$this->varname] = new Resource($tagData); $this->mode = -1; break; case 1: $lit = new Literal($tagData); if ($this->lang) { $lit->setLanguage($this->lang); } if ($this->dtype) { $lit->setDatatype($this->dtype); } $this->arrOutput[$this->counter][$this->varname] = $lit; $this->mode = -1; break; case 2: if ($tagData == "/") { $bn = "bNode" . $this->bncounter; $this->bncounter++; } else { $bn = $tagData; } $this->arrOutput[$this->counter][$this->varname] = new BlankNode($bn); $this->mode = -1; break; case 3: $this->arrOutput = $tagData; $this->mode = -1; break; } }
public function __toString() { return parent::__toString() . "^^" . $this->getDataType(); }
function tagClosed($parser, $name) { if ($this->mode == 1) { $lit = new Literal($this->current_literal); if ($this->lang) { $lit->setLanguage($this->lang); } if ($this->dtype) { $lit->setDatatype($this->dtype); } $this->arrOutput[$this->counter][$this->varname] = $lit; $this->mode = -1; } }
function _generateModelLiteral() { $model = new MemModel(); $model->setBaseURI('http://www.example.org'); $sub = new Resource('http://www.example.org/subject1'); $pred = new Resource('http://www.example.org/predicate1'); $obj = new Literal('http://www.example.org/object1'); $obj->setDatatype('test'); $obj->setLanguage('DE'); $model->add(new Statement($sub, $pred, $obj)); return $model; }
/** * Filter tuples containing variables matching all patterns from the WHERE clause * of an RDQL query. As a result of a database query using ADOdb these tuples * are returned as an ADORecordSet object, which is then passed to this function. * * @param object ADORecordSet &$recordSet * @return array [][?VARNAME]['value'] = string * ['nType'] = string * ['l_lang'] = string * ['l_dtype'] = string * @access private */ function filterQueryResult(&$recordSet) { $queryResult = array(); if (isset($this->parsedQuery['filters'])) { while (!$recordSet->EOF) { foreach ($this->parsedQuery['filters'] as $filter) { $evalFilterStr = $filter['evalFilterStr']; // evaluate regex equality expressions of each filter foreach ($filter['regexEqExprs'] as $i => $expr) { preg_match($expr['regex'], $recordSet->fields[$this->rsIndexes[$expr['var']]['value']], $match); $op = substr($expr['operator'], 0, 1); if ($op != '!' && !isset($match[0]) || $op == '!' && isset($match[0])) { $evalFilterStr = str_replace("##RegEx_{$i}##", 'FALSE', $evalFilterStr); } else { $evalFilterStr = str_replace("##RegEx_{$i}##", 'TRUE', $evalFilterStr); } } // evaluate string equality expressions foreach ($filter['strEqExprs'] as $i => $expr) { $exprBoolVal = 'FALSE'; switch ($expr['value_type']) { case 'variable': if ($recordSet->fields[$this->rsIndexes[$expr['var']]['value']] == $recordSet->fields[$this->rsIndexes[$expr['value']]['value']] && $expr['operator'] == 'eq' || $recordSet->fields[$this->rsIndexes[$expr['var']]['value']] != $recordSet->fields[$this->rsIndexes[$expr['value']]['value']] && $expr['operator'] == 'ne') { $exprBoolVal = 'TRUE'; } break; case 'URI': if (isset($this->rsIndexes[$expr['var']]['nType']) && $recordSet->fields[$this->rsIndexes[$expr['var']]['nType']] == 'l') { if ($expr['operator'] == 'ne') { $exprBoolVal = 'TRUE'; } break; } if ($recordSet->fields[$this->rsIndexes[$expr['var']]['value']] == $expr['value'] && $expr['operator'] == 'eq' || $recordSet->fields[$this->rsIndexes[$expr['var']]['value']] != $expr['value'] && $expr['operator'] == 'ne') { $exprBoolVal = 'TRUE'; } break; case 'Literal': if (!isset($this->rsIndexes[$expr['var']]['nType']) || $recordSet->fields[$this->rsIndexes[$expr['var']]['nType']] != 'l') { if ($expr['operator'] == 'ne') { $exprBoolVal = 'TRUE'; } break; } $filterLiteral = new Literal($expr['value'], $expr['value_lang']); $filterLiteral->setDatatype($expr['value_dtype']); $resultLiteral = new Literal($recordSet->fields[$this->rsIndexes[$expr['var']]['value']]); $resultLiteral->setDatatype($recordSet->fields[$this->rsIndexes[$expr['var']]['l_dtype']]); $resultLiteral->setLanguage($recordSet->fields[$this->rsIndexes[$expr['var']]['l_lang']]); $equal = $resultLiteral->equals($filterLiteral); if ($equal && $expr['operator'] == 'eq' || !$equal && $expr['operator'] == 'ne') { $exprBoolVal = 'TRUE'; } else { $exprBoolVal = 'FALSE'; } } $evalFilterStr = str_replace("##strEqExpr_{$i}##", $exprBoolVal, $evalFilterStr); } // evaluate numerical expressions foreach ($filter['numExprVars'] as $varName) { $varValue = "'" . $recordSet->fields[$this->rsIndexes[$varName]['value']] . "'"; $evalFilterStr = str_replace($varName, $varValue, $evalFilterStr); } eval("\$filterBoolVal = {$evalFilterStr}; \$eval_filter_ok = TRUE;"); if (!isset($eval_filter_ok)) { trigger_error(RDQL_AND_ERR . "'" . htmlspecialchars($filter['string']) . "'", E_USER_ERROR); } if (!$filterBoolVal) { $recordSet->MoveNext(); continue 2; } } $queryResult[] = $this->_convertRsRowToQueryResultRow($recordSet->fields); $recordSet->MoveNext(); } } else { while (!$recordSet->EOF) { $queryResult[] = $this->_convertRsRowToQueryResultRow($recordSet->fields); $recordSet->MoveNext(); } } return $queryResult; }
function MwRdfLiteral($str, $type = null, $lang = null) { static $cache = array(); if (isset($cache["{$type}:{$lang}:{$str}"])) { return $cache["{$type}:{$lang}:{$str}"]; } else { if (isset($lang)) { $lit = new Literal($str, $lang); } else { $lit = new Literal($str); } if (isset($type)) { $lit->setDatatype($type); } $cache["{$type}:{$lang}:{$str}"] = $lit; return $lit; } }
/** * Comparison function for sorting literals by their id * * @param Literal $a * @param Literal $b * @return int 0 if the literals are equal, 1 if b is larger than a, -1 else */ private function compareLiteralsById(Literal $a, Literal $b) { if ($a->getId() === $b->getId()) { return 0; } return $a->getId() < $b->getId() ? -1 : 1; }
/** * Constructs a RAP RDFNode from URI/Literal/Bnode * @access private * @param string $s * @returns object RDFNode **/ function toRDFNode($s, $state) { $ins = substr($s, 1, -1); if ($s[0] == "\"") { $lang = NULL; if (count($state) > 3) { for ($i = 3; $i < count($state); $i++) { if ($state[$i][0] == '@') { $lang = substr($state[3], 1); } if (substr($state[$i], 0, 2) == '^^') { $dtype = substr($state[$i], 2); if ($dtype[0] == '<') { $dtype = substr($dtype, 1, -1); } } } } if (UNIC_RDF) { $ins = $this->str2unicode_nfc($ins); } $new_Literal = new Literal($ins, $lang); if (isset($dtype)) { $new_Literal->setDatatype($dtype); } return $new_Literal; } if (strstr($s, '_' . BNODE_PREFIX)) { if ($this->FixBnodes or !array_search($s, $this->bNodeMap)) { return new BlankNode($ins); } else { return new BlankNode(trim(substr(array_search($s, $this->bNodeMap), 2))); } } return new Resource($ins); }
/** * converts a ResResource, ResProperty, or ResLiteral into a Resource, Blanknode, or Literal * * @param object ResNode $resNode * @return object Node * @access private * @throws phpErrpr */ function _resNode2Node($resNode) { if (is_a($resNode, 'ResResource')) { if ($resNode->getIsAnon()) { $return = new BlankNode($resNode->getURI()); } else { $return = new Resource($resNode->getURI()); } return $return; } if (is_a($resNode, 'ResLiteral')) { $literal = new Literal($resNode->getLabel(), $resNode->getLanguage()); if ($resNode->getDatatype() != null) { $literal->setDatatype($resNode->getDatatype()); } return $literal; } }
/** * return a mathing resource tyoe * * @param string * @param object simpleXMLNode $value * @access private */ function _element2Resource($tag, $value) { switch ($tag) { case 'uri': return new Resource((string) $value); break; case 'id': return new BlankNode((string) $value); break; case 'typedLiteral': $literal = new Literal((string) $value); $literal->setDatatype((string) $value['datatype']); return $literal; break; case 'plainLiteral': $literal = new Literal((string) $value); if (isset($value['xml:lang'])) { $literal->setLanguage((string) $value['xml:lang']); } return $literal; break; } }
/** * Checks if the Node is a typed Literal. * * @param String $node * * @return boolean TRUE if typed FALSE if not */ protected function dtypeCheck(&$node) { $patternInt = "/^-?[0-9]+\$/"; $match = preg_match($patternInt, $node, $hits); if ($match > 0) { $node = new Literal($hits[0]); $node->setDatatype(XML_SCHEMA . 'integer'); return true; } $patternBool = "/^(true|false)\$/"; $match = preg_match($patternBool, $node, $hits); if ($match > 0) { $node = new Literal($hits[0]); $node->setDatatype(XML_SCHEMA . 'boolean'); return true; } $patternType = "/^a\$/"; $match = preg_match($patternType, $node, $hits); if ($match > 0) { $node = new Resource(RDF_NAMESPACE_URI . 'type'); return true; } $patternDouble = "/^-?[0-9]+.[0-9]+[e|E]?-?[0-9]*/"; $match = preg_match($patternDouble, $node, $hits); if ($match > 0) { $node = new Literal($hits[0]); $node->setDatatype(XML_SCHEMA . 'double'); return true; } return false; }
/** * Constructs a RAP RDFNode from URI/Literal/Bnode * @access private * @param string $s * @returns object RDFNode **/ function toRDFNode($s, $state) { $ins = substr($s, 1, -1); if ($s[0] == '"' || $s[0] == '\'') { $lang = NULL; if (count($state) > 3) { for ($i = 3; $i < count($state); $i++) { if ($state[$i][0] == '@') { $lang = substr($state[3], 1); } if (substr($state[$i], 0, 2) == '^^') { $dtype = substr($state[$i], 2); if ($dtype[0] == '<') { $dtype = substr($dtype, 1, -1); } } } } if (UNIC_RDF) { $ins = $this->str2unicode_nfc($ins); } $new_Literal = new Literal($ins, $lang); if (isset($dtype)) { $new_Literal->setDatatype($dtype); } return $new_Literal; } else { if (is_int($s)) { $value = new Literal($s); $value->setDatatype(XML_SCHEMA . 'integer'); return $value; } else { if (is_float($s)) { $value = new Literal($s); $value->setDatatype(XML_SCHEMA . 'double'); return $value; } else { if ($s == '@true') { $value = new Literal(true); $value->setDatatype(XML_SCHEMA . 'boolean'); return $value; } else { if ($s == '@false') { $value = new Literal(false); $value->setDatatype(XML_SCHEMA . 'boolean'); return $value; } } } } } if (strstr($s, '_' . BNODE_PREFIX)) { if ($this->FixBnodes || !array_search($s, $this->bNodeMap)) { return new BlankNode($ins); } else { return new BlankNode(trim(substr(array_search($s, $this->bNodeMap), 2))); } } return new Resource($ins); }
/** * Constructor * You have to supply a string. * * @param string $str label of the literal * @param string $language optional language identifier */ function ResLiteral($str, $language = null) { parent::Literal($str, $language); }
/** * Perform an RDQL query on this Model. Should work with all types of models. * This method returns a MemModel containing the result statements. * If $closure is set to TRUE, the result will additionally contain * statements found by the findForward-method for blank nodes. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! * WARNING: If called with $closure = TRUE this method * can be slow with large models. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! * * @author Anton K�tlbacher <*****@*****.**> * @author code snippets taken from the RAP Netapi by Phil Dawes and Chris Bizer * @access public * @param string $queryString * @param boolean $closure * @return object MemModel * */ function &getMemModelByRDQL($queryString, $closure = FALSE) { require_once RDFAPI_INCLUDE_DIR . PACKAGE_RDQL; $parser = new RdqlParser(); $parsedQuery =& $parser->parseQuery($queryString); // If there are variables used in the pattern but not // in the select clause, add them to the select clause foreach ($parsedQuery['patterns'] as $n => $pattern) { foreach ($pattern as $key => $val_1) { if ($val_1['value'][0] == '?') { if (!in_array($val_1['value'], $parsedQuery['selectVars'])) { array_push($parsedQuery['selectVars'], $val_1['value']); } } } } if (is_a($this, "DbModel")) { $engine = new RdqlDbEngine(); $model = $this; } elseif (is_a($this, "MemModel")) { $engine = new RdqlMemEngine(); $model = $this; } elseif (is_a($this, "ResModel")) { $engine = new RdqlMemEngine(); $model = $this->model; } $res = $engine->queryModel($model, $parsedQuery, TRUE); $rdqlIter = new RdqlResultIterator($res); $newModel = new MemModel(); // Build statements from RdqlResultIterator while ($rdqlIter->hasNext()) { $result = $rdqlIter->next(); foreach ($parsedQuery['patterns'] as $n => $pattern) { if (substr($pattern['subject']['value'], 0, 1) == '?') { $subj = $result[$pattern['subject']['value']]; } else { $subj = new Resource($pattern['subject']['value']); } if (substr($pattern['predicate']['value'], 0, 1) == '?') { $pred = $result[$pattern['predicate']['value']]; } else { $pred = new Resource($pattern['predicate']['value']); } if (substr($pattern['object']['value'], 0, 1) == '?') { $obj = $result[$pattern['object']['value']]; } else { if (isset($pattern['object']['is_literal'])) { $obj = new Literal($pattern['object']['value']); $obj->setDatatype($pattern['object']['l_dtype']); $obj->setLanguage($pattern['object']['l_lang']); } else { $obj = new Resource($pattern['object']['value']); } } $statement = new Statement($subj, $pred, $obj); $newModel->add($statement); // findForward() Statements containing an eventually given blank node // and add them to the result, if closure = true if (is_a($statement->object(), 'BlankNode') && $closure == True) { $newModel = $model->findForward($statement->object(), NULL, NULL, $newModel); } if (is_a($statement->subject(), 'BlankNode') && $closure == True) { $newModel = $model->findForward($statement->subject(), NULL, NULL, $newModel); } } } return $newModel; }
/** * tests _node2ResNode() */ function test_node2ResNodeTest() { $_SESSION['test'] = 'ResModel _node2ResNode test'; $model1 = new MemModel(); $resmodel = new ResModel($model1); $literal = new Literal('Literal', 'DE'); $literal->setDatatype('type'); $result = $resmodel->_node2ResNode($literal); $this->assertIsA($result, 'ResLiteral'); $this->assertEqual($result->getLanguage(), 'DE'); $this->assertEqual($result->getDatatype(), 'type'); $model1->close(); }
/** * tests if equals() works according to the RDF specifications * */ function testEquals() { $_SESSION['test'] = 'Litras Equals test'; $literal1 = new Literal('test'); $literal2 = new Literal('test'); $this->assertTrue($literal1->equals($literal2)); $literal2->setLanguage('DE'); $this->assertFalse($literal1->equals($literal2)); $literal1->setLanguage('FR'); $this->assertFalse($literal1->equals($literal2)); $literal1->setLanguage('DE'); $this->assertTrue($literal1->equals($literal2)); $literal1->setDatatype("http://www.w3.org/TR/xmlschema-2/integer"); $this->assertFalse($literal1->equals($literal2)); $literal2->setDatatype("http://www.w3.org/TR/xmlschema-2/integer1"); $this->assertFalse($literal1->equals($literal2)); $literal2->setDatatype("http://www.w3.org/TR/xmlschema-2/integer"); $this->assertTrue($literal1->equals($literal2)); }
/** * Creates an RDF object object * contained in the given $dbRecordSet object. * * @see convertFromDbResult() to understand $strVarBase necessity * * @param ADORecordSet $dbRecordSet Record set returned from ADOConnection::Execute() * @param string $strVarBase Prefix of the columns the recordset fields have. * * @return Resource RDF triple object resource object */ protected function createObjectFromDbRecordSetPart(ADORecordSet $dbRecordSet, $strVarBase, $strVar) { $strVarName = (string) $strVar; if ($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_value']] === null) { //FIXME: should be NULL, but doesn't pass test return ''; } switch ($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_is']]) { case 'r': $object = new Resource($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_value']]); break; case 'b': $object = new BlankNode($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_value']]); break; default: $object = new Literal($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_value']], $dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_lang']]); if ($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_type']]) { $object->setDatatype($dbRecordSet->fields[$strVarBase . '.' . $this->sg->arVarAssignments[$strVarName]['sql_type']]); } } return $object; }
/** * Filter the result-set of query variables by evaluating each filter from the * AND clause of the RDQL query. * * @param array &$finalRes [][?VARNAME] = object Node * @return array [][?VARNAME] = object Node * @access private */ function filterTuples(&$finalRes) { foreach ($this->parsedQuery['filters'] as $filter) { foreach ($finalRes as $n => $fRes) { $evalFilterStr = $filter['evalFilterStr']; // evaluate regex equality expressions of each filter foreach ($filter['regexEqExprs'] as $i => $expr) { preg_match($expr['regex'], $fRes[$expr['var']]->getLabel(), $match); $op = substr($expr['operator'], 0, 1); if ($op != '!' && !isset($match[0]) || $op == '!' && isset($match[0])) { $evalFilterStr = str_replace("##RegEx_{$i}##", 'FALSE', $evalFilterStr); } else { $evalFilterStr = str_replace("##RegEx_{$i}##", 'TRUE', $evalFilterStr); } } // evaluate string equality expressions foreach ($filter['strEqExprs'] as $i => $expr) { $exprBoolVal = 'FALSE'; switch ($expr['value_type']) { case 'variable': if ($fRes[$expr['var']] == $fRes[$expr['value']] && $expr['operator'] == 'eq' || $fRes[$expr['var']] != $fRes[$expr['value']] && $expr['operator'] == 'ne') { $exprBoolVal = 'TRUE'; } break; case 'URI': if (is_a($fRes[$expr['var']], 'Literal')) { if ($expr['operator'] == 'ne') { $exprBoolVal = 'TRUE'; } break; } if ($fRes[$expr['var']]->getLabel() == $expr['value'] && $expr['operator'] == 'eq' || $fRes[$expr['var']]->getLabel() != $expr['value'] && $expr['operator'] == 'ne') { $exprBoolVal = 'TRUE'; } break; case 'Literal': if (!is_a($fRes[$expr['var']], 'Literal')) { if ($expr['operator'] == 'ne') { $exprBoolVal = 'TRUE'; } break; } $filterLiteral = new Literal($expr['value'], $expr['value_lang']); $filterLiteral->setDatatype($expr['value_dtype']); $equal = $fRes[$expr['var']]->equals($filterLiteral); /* if ($fRes[$expr['var']]->getLabel() == $expr['value'] && $fRes[$expr['var']]->getDatatype() == $expr['value_dtype']) { $equal = TRUE; // Lang tags only differentiate literals in rdf:XMLLiterals and plain literals. // Therefore if a literal is datatyped ignore the language tag. if ((($expr['value_dtype'] == NULL) || ($expr['value_dtype'] == 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral') || ($expr['value_dtype'] == 'http://www.w3.org/2001/XMLSchema#string')) && (($fRes[$expr['var']]->getLanguage() != $expr['value_lang']))) $equal = FALSE; }else $equal = FALSE; */ if ($equal && $expr['operator'] == 'eq' || !$equal && $expr['operator'] == 'ne') { $exprBoolVal = 'TRUE'; } else { $exprBoolVal = 'FALSE'; } } $evalFilterStr = str_replace("##strEqExpr_{$i}##", $exprBoolVal, $evalFilterStr); } // evaluate numerical expressions foreach ($filter['numExprVars'] as $varName) { $varValue = "'" . $fRes[$varName]->getLabel() . "'"; $evalFilterStr = str_replace($varName, $varValue, $evalFilterStr); } eval("\$filterBoolVal = {$evalFilterStr}; \$eval_filter_ok = TRUE;"); if (!isset($eval_filter_ok)) { trigger_error(RDQL_AND_ERR . "'" . htmlspecialchars($filter['string']) . "'", E_USER_ERROR); } if (!$filterBoolVal) { unset($finalRes[$n]); } } } return $finalRes; }
if ($_POST['subject_kind'] == 'resource') { $query_subj = new Resource($_POST['query_subject']); } else { $query_subj = new BlankNode($_POST['query_subject']); } $comment_string .= "Subject = " . $_POST['query_subject'] . "<BR>"; } if ($_POST['query_predicate'] != '') { $query_pred = new Resource($_POST['query_predicate']); $comment_string .= "Predicate = " . $_POST['query_predicate'] . "<BR>"; } if ($_POST['query_object'] != '') { if ($_POST['object_kind'] == 'resource') { $query_obj = new Resource($_POST['query_object']); } elseif ($_POST['object_kind'] == 'literal') { $query_obj = new Literal($_POST['query_object']); if ($_POST['query_object_datatype'] != '') { $query_obj->setDatatype($_POST['query_object_datatype']); } } else { $query_obj = new BlankNode($_POST['query_object']); } $comment_string .= "Object = " . $_POST['query_object'] . "<BR>"; } // Execute query and display what has been done $model = $model->find($query_subj, $query_pred, $query_obj); echo $comment_string; } // Reify the model if checked in submitted form if (isset($_POST['reify']) and $_POST['reify'] == "1") { $model =& $model->reify();
/** * Adds a new statement to the model * This method is called by generateModel(). * * @access private * @param string &$user_data * @param string $subject_type * @param string $subject * @param string $predicate * @param string $ordinal * @param string $object_type * @param string $object * @param string $xml_lang ) * @return object MemModel */ function add_statement_to_model(&$user_data, $subject_type, $subject, $predicate, $ordinal, $object_type, $object, $xml_lang, $datatype) { // ParseUnicode if (UNIC_RDF) { $subject = $this->str2unicode_nfc($subject); $predicate = $this->str2unicode_nfc($predicate); $object = $this->str2unicode_nfc($object); } //create subject if ($subject_type == RDF_SUBJECT_TYPE_BNODE) { $objsub = new BlankNode($subject); } else { $objsub = new Resource($subject); } // create predicate $objpred = new Resource($predicate); // create object if ($object_type == RDF_OBJECT_TYPE_RESOURCE || $object_type == RDF_OBJECT_TYPE_BNODE) { if ($object_type == RDF_OBJECT_TYPE_BNODE) { $objobj = new BlankNode($object); } else { $objobj = new Resource($object); } } else { $objobj = new Literal($object); if ($datatype != '') { $objobj->setDatatype($datatype); } elseif ($xml_lang != '') { $objobj->setLanguage($xml_lang); } } // create statement $statement = new Statement($objsub, $objpred, $objobj); // add statement to model if (CREATE_MODEL_WITHOUT_DUPLICATES == TRUE) { $this->model->addWithoutDuplicates($statement); } else { $this->model->add($statement); } }