]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/SemanticWeb.php
Just comments
[SourceForge/phpwiki.git] / lib / SemanticWeb.php
1 <?php rcs_id('$Id: SemanticWeb.php,v 1.8 2007-05-27 19:46:11 rurban Exp $');
2 /**
3  * Search support and Import/Export
4  * Export: ?format=rdf, ?format=owl
5  *
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. 
12  *  OO predicates: 
13  *    is_a, has_a, ...
14  *  OWL predicates:
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.
21  *
22  * Purpose:
23  * - Another way to represent various KB models in various DL languages. 
24  *   (OWL/DAML/other DL)
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)
30  *
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).
37  *
38  * Rules: (may be represented by special content on a page)
39  *  - Syntax: reasoner backend specific, or common or ?
40  *
41  * RDF Triple: (representing facts)
42  *   Subject (page) -> Predicate (verb, relation) -> Object (links)
43  * Subject: a page
44  * Verb: 
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. 
47  *   (need your own DTD)
48  *   Example: page [Ape] isa:Animal, ...
49  * Object: special links on a page.
50  * Class: WikiCategory
51  * Model: Basepage for a KB. (parametrizeable pages or copies of modified snapshots?)
52  *
53  * DL: Description Logic
54  * KB: Knowledge Base
55  *
56  * Discussion:
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, ... 
61
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,
64  * ...) and forth.
65
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
68  * Common Lisp.
69  *
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)
76  *
77  * Links:
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)
82  *
83  *
84  * Author: Reini Urban <rurban@x-ray.at>
85  */
86 /*============================================================================*/
87 /*
88  Copyright 2004,2007 Reini Urban
89
90  This file is part of PhpWiki.
91
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.
96
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.
101
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
105  */
106
107 require_once('lib/RssWriter.php');
108 require_once('lib/TextSearchQuery.php');
109 require_once('lib/Units.php');
110
111
112 /**
113  * RdfWriter - A class to represent a single(?) wikipage as RDF. Supports ?format=rdf
114  *
115  * RdfWriter
116  *  - RssWriter
117  *    - RecentChanges (RecentChanges?format=rss)
118  *      channel: ... item: ...
119  */
120 class RdfWriter extends RssWriter // in fact it should be rewritten to be other way round.
121 {
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#'));
126
127         $this->_modules = array(
128             //Standards
129             'content'   => "http://purl.org/rss/1.0/modules/content/",
130             'dc'        => "http://purl.org/dc/elements/1.1/",
131             );
132
133         $this->_uris_seen = array();
134         $this->_items = array();
135     }
136
137     function output() {
138         echo "not yet supported!";
139     }
140 }
141
142 /**
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
146  *
147  * OwlWriter
148  *  - RdfWriter
149  *  - Reasoner
150 */
151 class OwlWriter extends RdfWriter {
152 };
153
154 /**
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, ...)
158  *
159  * ModelWriter
160  *  - OwlWriter
161  *  - ReasonerBackend
162 */
163 class ModelWriter extends OwlWriter {
164 };
165
166 /**
167  *  NumericSearchQuery can do: 
168  *         ("population < 20000 and area > 1000000", array("population", "area"))
169  *  ->match(array('population' => 100000, 'area' => 10000000)) 
170  * @see NumericSearchQuery
171  *
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))
175  *
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
181  */
182 class SemanticAttributeSearchQuery
183 extends NumericSearchQuery
184 {
185     /*
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'),
190                             );
191     */
192
193     /**
194      * We need to detect units from the freetext query:
195      * population > 1 million
196      */
197     function SemanticAttributeSearchQuery($search_query, $placeholders, $unit = '') {
198         $this->NumericSearchQuery($search_query, $placeholders);
199         $this->_units = new Units();
200         $this->unit = $unit;
201     }
202
203     /**
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)
208      *
209      * @access private
210      * @param $value number   A numerical value: integer, float or string.
211      * @param $x string       The variable name to be replaced in the query.
212      * @return string
213      */
214     function _bind($value, $x) {
215         $ori_value = $value;
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 == '') {
226                 ;
227             } else {
228                 // non-matching units are silently ignored
229                 $this->_workquery = '';
230                 return '';
231             }
232         }
233         $value = $val_base;
234         if (!is_numeric($value)) {
235             $this->_workquery = ''; //must return false
236             trigger_error("Cannot match against non-numeric attribute value $x := $ori_value", 
237                           E_USER_NOTICE);
238             return '';
239         }
240
241         $this->_workquery = preg_replace("/\b".preg_quote($x,"/")."\b/", $value, $this->_workquery);
242         return $this->_workquery;
243     }
244
245 }
246
247 /**
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
253  */
254 class SemanticSearchQuery
255 extends SemanticAttributeSearchQuery
256 {
257     function hasAttributes() { // TODO 
258     }
259     function hasRelations()  { // TODO 
260     }
261     function getLinkNames()  { // TODO 
262     }
263 }
264
265 /**
266  * ReasonerBackend - hooks to reasoner backends.
267  * via http as with DIG,
268  * or internally. sockets
269  */
270 class ReasonerBackend {
271     function ReasonerBackend () {
272         ;
273     }
274     /**
275      * transform to reasoner syntax
276      */
277     function transformTo () {
278         ;
279     }
280     /**
281      * transform from reasoner syntax
282      */
283     function transformFrom () {
284         ;
285     }
286     /**
287      * call the reasoner
288      */
289     function invoke () {
290         ;
291     }
292 };
293
294 class ReasonerBackend_LISA extends ReasonerBackend {
295 };
296
297 class ReasonerBackend_Racer extends ReasonerBackend {
298 };
299
300 class ReasonerBackend_KM extends ReasonerBackend {
301 };
302
303
304 // (c-file-style: "gnu")
305 // Local Variables:
306 // mode: php
307 // tab-width: 8
308 // c-basic-offset: 4
309 // c-hanging-comment-ender-p: nil
310 // indent-tabs-mode: nil
311 // End:   
312 ?>