1 <?php rcs_id('$Id: SemanticWeb.php,v 1.3 2007-01-03 21:25:21 rurban Exp $');
3 * What to do on ?format=rdf What to do on ?format=owl
5 * Map relations on a wikipage to a RDF ressource to build a "Semantic Web"
6 * - a web ontology frontend compatible to OWL (web ontology language).
7 * http://www.w3.org/2001/sw/Activity
8 * Simple RDF ontologies contain facts and rules, expressed by RDF triples:
9 * Subject (page) -> Predicate (verb, relation) -> Object (links)
10 * OWL extents that to represent a typical OO framework.
14 * subClassOf, restrictedBy, onProperty, intersectionOf, allValuesFrom, ...
15 * someValuesFrom, unionOf, equivalentClass, disjointWith, ...
16 * plus the custom vocabulary (ontology): canRun, canBite, smellsGood, ...
17 * OWL Subjects: Class, Restriction, ...
18 * OWL Properties: type, label, comment, ...
19 * DAML should also be supported.
22 * - Another way to represent various KB models in various DL languages.
24 * - Frontend to various KB model reasoners and representations.
25 * - Generation/update of static wiki pages based on external OWL/DL/KB
26 * (=> ModelTest/Categories)
27 * KB Blackboard and Visualization.
28 * - OWL generation based on static wiki pages (ModelTest?format=owl)
30 * Facts: (may be represented by special links on a page)
31 * - Each page must be representable with an unique URL.
32 * - Each fact must be representable with an unique RDF triple.
33 * - A class is represented by a category page.
34 * - To represent more expressive description logic, "enriched"
35 * links will not be enough (? variable symbolic objects).
37 * Rules: (may be represented by special content on a page)
38 * - Syntax: reasoner backend specific, or common or ?
40 * RDF Triple: (representing facts)
41 * Subject (page) -> Predicate (verb, relation) -> Object (links)
44 * Special link qualifiers represent RDF triples, based on RDF standard notation.
45 * See RDF standard DTD's on daml.org and w3.org, plus your custom predicates.
47 * Example: page [Ape] isa:Animal, ...
48 * Object: special links on a page.
50 * Model: Basepage for a KB. (parametrizeable pages or copies of modified snapshots?)
52 * DL: Description Logic
56 * Of course *real* expert systems ("reasoners") will help/must be used in
57 * optimization and maintainance of the SemanticWeb KB (Knowledge
58 * Base). Hooks will be tested to KM (an interactive KB playground),
59 * LISA (standard unifier), FaCT, RACER, ...
61 * Maybe also ZEBU (parser generator) is needed to convert the wiki KB
62 * syntax to the KB reasoner backend (LISA, KM, CLIPS, JESS, FaCT,
65 * pOWL is a simple php backend with some very simple AI logic in PHP,
66 * though I strongly doubt the usefulness of reasoners not written in
69 * SEAL (omntoweb.org) is similar to that, just on top of the Zope CMF.
70 * FaCT uses e.g. this KB DTD:
71 <!ELEMENT KNOWLEDGEBASE (DEFCONCEPT|DEFROLE|IMPLIESC|EQUALC|IMPLIESR|EQUALR|TRANSITIVE|FUNCTIONAL)*>
72 <!ELEMENT CONCEPT (PRIMITIVE|TOP|BOTTOM|AND|OR|NOT|SOME|ALL|ATMOST|ATLEAST)>
73 <!ELEMENT ROLE (PRIMROLE|INVROLE)>
74 ... (facts and rules described in XML)
77 * http://phpwiki.org/SemanticWeb,
78 * http://en.wikipedia.org/wiki/Knowledge_representation
79 * http://www.ontoweb.org/
80 * http://www.semwebcentral.org/ (OWL on top of GForge)
83 * Author: Reini Urban <rurban@x-ray.at>
85 /*============================================================================*/
87 Copyright 2004,2007 Reini Urban
89 This file is part of PhpWiki.
91 PhpWiki is free software; you can redistribute it and/or modify
92 it under the terms of the GNU General Public License as published by
93 the Free Software Foundation; either version 2 of the License, or
94 (at your option) any later version.
96 PhpWiki is distributed in the hope that it will be useful,
97 but WITHOUT ANY WARRANTY; without even the implied warranty of
98 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
99 GNU General Public License for more details.
101 You should have received a copy of the GNU General Public License
102 along with PhpWiki; if not, write to the Free Software
103 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
106 include_once('lib/RssWriter.php');
107 include_once('lib/Units.php');
108 include_once("lib/TextSearchQuery.lib");
111 * RdfWriter - A class to represent a wikipage as RDF. Supports ?format=rdf
115 * - RecentChanges (RecentChanges?format=rss)
116 * channel: ... item: ...
118 class RdfWriter extends RssWriter // in fact it should be rewritten to be other way round.
120 function RdfWriter () {
121 $this->XmlElement('rdf:RDF',
122 array('xmlns' => "http://purl.org/rss/1.0/",
123 'xmlns:rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'));
125 $this->_modules = array(
127 'content' => "http://purl.org/rss/1.0/modules/content/",
128 'dc' => "http://purl.org/dc/elements/1.1/",
131 $this->_uris_seen = array();
132 $this->_items = array();
137 * OwlWriter - A class to represent a set of wiki pages (a DL model) as OWL.
138 * Supports ?format=owl
144 class OwlWriter extends RdfWriter {
148 * ModelWriter - Export a KB as set of wiki pages.
149 * Probably based on some convenient DL expression syntax. (deffact, defrule, ...)
155 class ModelWriter extends OwlWriter {
159 * NumericSearchQuery can do:
160 * ("population < 20000 and area > 1000000", array("population", "area"))
161 * ->match(array('population' => 100000, 'area' => 10000000))
162 * @see NumericSearchQuery
164 * SemanticAttributeSearchQuery can detect and unify units in numbers.
165 * ("population < 2million and area > 100km2", array("population", "area"))
166 * ->match(array('population' => 100000, 'area' => 10000000))
168 * Do we need a real parser or can we just regexp over some allowed unit
169 * suffixes to detect the numbers?
170 * see man units(1) and /usr/share/units.dat
171 * base units: $ units "1 million miles"
172 * Definition: 1.609344e+09 m
174 class SemanticAttributeSearchQuery
175 extends NumericSearchQuery
178 var $base_units = array('m' => explode(',','km,miles,cm,dm,mm,ft,inch,inches,meter'),
179 'm^2' => explode(',','km^2,ha,cm^2,mi^2'),
180 'm^3' => explode(',','km^3,lit,cm^3,dm^3,gallons'),
185 * We need to detect units from the freetext query:
186 * population > 1 million
188 function SemanticAttributeSearchQuery($search_query, $placeholders, $unit = '') {
189 $this->NumericSearchQuery($search_query, $placeholders);
190 $this->_units = new Units();
195 * Strip non-numeric chars from the variable (as the groupseperator) and replace
196 * it in the symbolic query for evaluation.
197 * This version unifies the attribute values from the database to a
198 * numeric basevalue before comparison. (area:=963.6km^2 => 9.366e+08 m^2)
201 * @param $value number A numerical value: integer, float or string.
202 * @param $x string The variable name to be replaced in the query.
205 function _bind($value, $x) {
207 $value = preg_replace("/,/", "", $value);
208 $this->_bound[] = array('linkname' => $x,
209 'linkvalue' => $value);
210 // We must ensure that the same baseunits are matched against.
211 // We cannot compare m^2 to m or ''
212 $val_base = $this->_units->basevalue($value);
213 if (!DISABLE_UNITS and $this->_units->baseunit($value) != $this->unit) {
214 // Poor user has selected an attribute, but no unit. assume he means the baseunit
215 if (count($this->getVars() == 1) and $this->unit == '') {
218 // non-matching units are silently ignored
219 $this->_workquery = '';
224 if (!is_numeric($value)) {
225 $this->_workquery = ''; //must return false
226 trigger_error("Cannot match against non-numeric attribute value $x := $ori_value",
231 $this->_workquery = preg_replace("/\b".preg_quote($x,"/")."\b/", $value, $this->_workquery);
232 return $this->_workquery;
238 * SemanticSearchQuery can do:
239 * (is_a::city and population < 20000) and (*::city and area > 1000000)
240 * ->match(array('is_a' => 'city', 'linkfrom' => array(),
241 * population' => 100000, 'area' => 10000000))
242 * @return array A list of found and bound matches
244 class SemanticSearchQuery
245 extends SemanticAttributeSearchQuery
247 function hasAttributes() { // TODO
249 function hasRelations() { // TODO
251 function getLinkNames() { // TODO
256 * ReasonerBackend - hooks to reasoner backends.
257 * via http as with DIG,
260 class ReasonerBackend {
261 function ReasonerBackend () {
265 * transform to reasoner syntax
267 function transformTo () {
271 * transform from reasoner syntax
273 function transformFrom () {
284 class ReasonerBackend_LISA extends ReasonerBackend {
287 class ReasonerBackend_KM extends ReasonerBackend {
291 // (c-file-style: "gnu")
296 // c-hanging-comment-ender-p: nil
297 // indent-tabs-mode: nil