1 <?php rcs_id('$Id: SemanticWeb.php,v 1.8 2007-05-27 19:46:11 rurban Exp $');
3 * Search support and Import/Export
4 * Export: ?format=rdf, ?format=owl
6 * Map relations on a wikipage to a RDF ressource to build a "Semantic Web"
7 * - a web ontology frontend compatible to OWL (web ontology language).
8 * http://www.w3.org/2001/sw/Activity
9 * Simple RDF ontologies contain facts and rules, expressed by RDF triples:
10 * Subject (page) -> Predicate (verb, relation) -> Object (links)
11 * OWL extents that to represent a typical OO framework.
15 * subClassOf, restrictedBy, onProperty, intersectionOf, allValuesFrom, ...
16 * someValuesFrom, unionOf, equivalentClass, disjointWith, ...
17 * plus the custom vocabulary (ontology): canRun, canBite, smellsGood, ...
18 * OWL Subjects: Class, Restriction, ...
19 * OWL Properties: type, label, comment, ...
20 * DAML should also be supported.
23 * - Another way to represent various KB models in various DL languages.
25 * - Frontend to various KB model reasoners and representations.
26 * - Generation/update of static wiki pages based on external OWL/DL/KB
27 * (=> ModelTest/Categories)
28 * KB Blackboard and Visualization.
29 * - OWL generation based on static wiki pages (ModelTest?format=owl)
31 * Facts: (may be represented by special links on a page)
32 * - Each page must be representable with an unique URL.
33 * - Each fact must be representable with an unique RDF triple.
34 * - A class is represented by a category page.
35 * - To represent more expressive description logic, "enriched"
36 * links will not be enough (? variable symbolic objects).
38 * Rules: (may be represented by special content on a page)
39 * - Syntax: reasoner backend specific, or common or ?
41 * RDF Triple: (representing facts)
42 * Subject (page) -> Predicate (verb, relation) -> Object (links)
45 * Special link qualifiers represent RDF triples, based on RDF standard notation.
46 * See RDF standard DTD's on daml.org and w3.org, plus your custom predicates.
48 * Example: page [Ape] isa:Animal, ...
49 * Object: special links on a page.
51 * Model: Basepage for a KB. (parametrizeable pages or copies of modified snapshots?)
53 * DL: Description Logic
57 * Of course *real* expert systems ("reasoners") will help/must be used in
58 * optimization and maintainance of the SemanticWeb KB (Knowledge
59 * Base). Hooks will be tested to KM (an interactive KB playground),
60 * LISA (standard unifier), FaCT, RACER, ...
62 * Maybe also ZEBU (parser generator) is needed to convert the wiki KB
63 * syntax to the KB reasoner backend (LISA, KM, CLIPS, JESS, FaCT,
66 * pOWL is a simple php backend with some very simple AI logic in PHP,
67 * though I strongly doubt the usefulness of reasoners not written in
70 * SEAL (omntoweb.org) is similar to that, just on top of the Zope CMF.
71 * FaCT uses e.g. this KB DTD:
72 <!ELEMENT KNOWLEDGEBASE (DEFCONCEPT|DEFROLE|IMPLIESC|EQUALC|IMPLIESR|EQUALR|TRANSITIVE|FUNCTIONAL)*>
73 <!ELEMENT CONCEPT (PRIMITIVE|TOP|BOTTOM|AND|OR|NOT|SOME|ALL|ATMOST|ATLEAST)>
74 <!ELEMENT ROLE (PRIMROLE|INVROLE)>
75 ... (facts and rules described in XML)
78 * http://phpwiki.org/SemanticWeb,
79 * http://en.wikipedia.org/wiki/Knowledge_representation
80 * http://www.ontoweb.org/
81 * http://www.semwebcentral.org/ (OWL on top of GForge)
84 * Author: Reini Urban <rurban@x-ray.at>
86 /*============================================================================*/
88 Copyright 2004,2007 Reini Urban
90 This file is part of PhpWiki.
92 PhpWiki is free software; you can redistribute it and/or modify
93 it under the terms of the GNU General Public License as published by
94 the Free Software Foundation; either version 2 of the License, or
95 (at your option) any later version.
97 PhpWiki is distributed in the hope that it will be useful,
98 but WITHOUT ANY WARRANTY; without even the implied warranty of
99 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
100 GNU General Public License for more details.
102 You should have received a copy of the GNU General Public License
103 along with PhpWiki; if not, write to the Free Software
104 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
107 require_once('lib/RssWriter.php');
108 require_once('lib/TextSearchQuery.php');
109 require_once('lib/Units.php');
113 * RdfWriter - A class to represent a single(?) wikipage as RDF. Supports ?format=rdf
117 * - RecentChanges (RecentChanges?format=rss)
118 * channel: ... item: ...
120 class RdfWriter extends RssWriter // in fact it should be rewritten to be other way round.
122 function RdfWriter () {
123 $this->XmlElement('rdf:RDF',
124 array('xmlns' => "http://purl.org/rss/1.0/",
125 'xmlns:rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'));
127 $this->_modules = array(
129 'content' => "http://purl.org/rss/1.0/modules/content/",
130 'dc' => "http://purl.org/dc/elements/1.1/",
133 $this->_uris_seen = array();
134 $this->_items = array();
138 echo "not yet supported!";
143 * OwlWriter - A class to represent a set of wiki pages (a DL model) as OWL.
144 * Requires an actionpage returning a pagelist.
145 * Supports ?format=owl
151 class OwlWriter extends RdfWriter {
155 * ModelWriter - Export a KB as set of wiki pages.
156 * Requires an actionpage returning a pagelist.
157 * Probably based on some convenient DL expression syntax. (deffact, defrule, ...)
163 class ModelWriter extends OwlWriter {
167 * NumericSearchQuery can do:
168 * ("population < 20000 and area > 1000000", array("population", "area"))
169 * ->match(array('population' => 100000, 'area' => 10000000))
170 * @see NumericSearchQuery
172 * SemanticAttributeSearchQuery can detect and unify units in numbers.
173 * ("population < 2million and area > 100km2", array("population", "area"))
174 * ->match(array('population' => 100000, 'area' => 10000000))
176 * Do we need a real parser or can we just regexp over some allowed unit
177 * suffixes to detect the numbers?
178 * See man units(1) and /usr/share/units.dat
179 * base units: $ units "1 million miles"
180 * Definition: 1.609344e+09 m
182 class SemanticAttributeSearchQuery
183 extends NumericSearchQuery
186 // base_units are extracted dynamicly via /usr/bin/units
187 var $base_units = array('m' => explode(',','km,miles,cm,dm,mm,ft,inch,inches,meter'),
188 'm^2' => explode(',','km^2,ha,cm^2,mi^2'),
189 'm^3' => explode(',','km^3,lit,cm^3,dm^3,gallons'),
194 * We need to detect units from the freetext query:
195 * population > 1 million
197 function SemanticAttributeSearchQuery($search_query, $placeholders, $unit = '') {
198 $this->NumericSearchQuery($search_query, $placeholders);
199 $this->_units = new Units();
204 * Strip non-numeric chars from the variable (as the groupseperator) and replace
205 * it in the symbolic query for evaluation.
206 * This version unifies the attribute values from the database to a
207 * numeric basevalue before comparison. (area:=963.6km^2 => 9.366e+08 m^2)
210 * @param $value number A numerical value: integer, float or string.
211 * @param $x string The variable name to be replaced in the query.
214 function _bind($value, $x) {
216 $value = preg_replace("/,/", "", $value);
217 $this->_bound[] = array('linkname' => $x,
218 'linkvalue' => $value);
219 // We must ensure that the same baseunits are matched against.
220 // We cannot compare m^2 to m or ''
221 $val_base = $this->_units->basevalue($value);
222 if (!DISABLE_UNITS and $this->_units->baseunit($value) != $this->unit) {
223 // Poor user has selected an attribute, but no unit.
224 // Assume he means the baseunit
225 if (count($this->getVars() == 1) and $this->unit == '') {
228 // non-matching units are silently ignored
229 $this->_workquery = '';
234 if (!is_numeric($value)) {
235 $this->_workquery = ''; //must return false
236 trigger_error("Cannot match against non-numeric attribute value $x := $ori_value",
241 $this->_workquery = preg_replace("/\b".preg_quote($x,"/")."\b/", $value, $this->_workquery);
242 return $this->_workquery;
248 * SemanticSearchQuery can do:
249 * (is_a::city and population < 20000) and (*::city and area > 1000000)
250 * ->match(array('is_a' => 'city', 'linkfrom' => array(),
251 * population' => 100000, 'area' => 10000000))
252 * @return array A list of found and bound matches
254 class SemanticSearchQuery
255 extends SemanticAttributeSearchQuery
257 function hasAttributes() { // TODO
259 function hasRelations() { // TODO
261 function getLinkNames() { // TODO
266 * ReasonerBackend - hooks to reasoner backends.
267 * via http as with DIG,
268 * or internally. sockets
270 class ReasonerBackend {
271 function ReasonerBackend () {
275 * transform to reasoner syntax
277 function transformTo () {
281 * transform from reasoner syntax
283 function transformFrom () {
294 class ReasonerBackend_LISA extends ReasonerBackend {
297 class ReasonerBackend_Racer extends ReasonerBackend {
300 class ReasonerBackend_KM extends ReasonerBackend {
304 // (c-file-style: "gnu")
309 // c-hanging-comment-ender-p: nil
310 // indent-tabs-mode: nil