| 1 | package RDF::Redland::DIG::Tutorial | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Tutorial - RDF::Redland::DIG::Tutorial | ||
| 8 | |||
| 9 | =head1 Introduction | ||
| 10 | |||
| 11 | L<RDF::Redland> is a Perl wrapper around the Redland RDF libraries. L<RDF::Redland::DIG> is a | ||
| 12 | (Perl-only) extension to exchange information with a I<DIG reasoner>, i.e. one which can be reached | ||
| 13 | via the DIG protocol. | ||
| 14 | |||
| 15 | The following gives an overview over the API. To learn more you may want to consult the | ||
| 16 | manpages | ||
| 17 | |||
| 18 | man RDF::Redland::DIG | ||
| 19 | |||
| 20 | for the reasoner itself, or | ||
| 21 | |||
| 22 | man RDF::Redland::DIG::KB | ||
| 23 | |||
| 24 | for the knowledge base hosted inside the reasoner. | ||
| 25 | |||
| 26 | =head1 Test Ontology | ||
| 27 | |||
| 28 | Before we can start with using the reasoning services, we have to create an | ||
| 29 | RDF::Redland::Model to host the data. In the following, I created a simple test ontology inspired by | ||
| 30 | the Protég©pizza ontology (L<http://protege.stanford.edu>). | ||
| 31 | |||
| 32 | @prefix owl <http://www.w3.org/2002/07/owl#> . | ||
| 33 | @prefix xsd <http://www.w3.org/2001/XMLSchema#> . | ||
| 34 | @prefix rdfs <http://www.w3.org/2000/01/rdf-schema#>. | ||
| 35 | @prefix rdf <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. | ||
| 36 | |||
| 37 | Pizza a owl:Class. | ||
| 38 | PizzaTopping a owl:Class. | ||
| 39 | |||
| 40 | CheesePizza a owl:Class; | ||
| 41 | rdfs:subClassOf Pizza. | ||
| 42 | ProsciuttoPizza a owl:Class; | ||
| 43 | rdfs:subClassOf Pizza. | ||
| 44 | |||
| 45 | CheeseTopping a owl:Class; | ||
| 46 | rdfs:subClassOf PizzaTopping. | ||
| 47 | HamTopping a owl:Class; | ||
| 48 | rdfs:subClassOf PizzaTopping. | ||
| 49 | TomatoTopping a owl:Class; | ||
| 50 | rdfs:subclassOf PizzaTopping. | ||
| 51 | |||
| 52 | hasIngredient a owl:ObjectProperty; | ||
| 53 | owl:inverseOf isIngredientOf. | ||
| 54 | hasTopping rdfs:subPropertyOf hasIngredient; | ||
| 55 | owl:inverseOf isToppingOf; | ||
| 56 | rdfs:domain Pizza; | ||
| 57 | rdfs:range PizzaTopping. | ||
| 58 | isIngredientOf a owl:ObjectProperty; | ||
| 59 | owl:inverseOf hasIngredient. | ||
| 60 | isToppingOf rdfs:subPropertyOf isIngredientOf; | ||
| 61 | owl:inverseOf hasTopping; | ||
| 62 | rdfs:domain PizzaTopping; | ||
| 63 | rdfs:range Pizza. | ||
| 64 | |||
| 65 | Apart from the ontological information, we can also add some instance data to play with: | ||
| 66 | |||
| 67 | Margharita a CheesePizza; | ||
| 68 | hasTopping MozarellaTopping. | ||
| 69 | MozarellaTopping a CheeseTopping; | ||
| 70 | isToppingOf Margharita. | ||
| 71 | |||
| 72 | =head1 The Model | ||
| 73 | |||
| 74 | If we assume that the above ontology and instance data is stored in an RDF file | ||
| 75 | C<pizza.n3>, the we can load this file into a model: | ||
| 76 | |||
| 77 | use RDF::Redland::Parser; | ||
| 78 | my $parser = new RDF::Redland::Parser (undef, "application/rdf+xml") | ||
| 79 | or die "Failed to find parser"; | ||
| 80 | |||
| 81 | use RDF::Redland::Storage; | ||
| 82 | my $storage = new RDF::Redland::Storage ( | ||
| 83 | "hashes", | ||
| 84 | "test", | ||
| 85 | "new='yes', hash-type='bdb', dir='/where/ever/'") | ||
| 86 | or die "Failed to create RDF::Redland::Storage"; | ||
| 87 | |||
| 88 | use RDF::Redland::Model; | ||
| 89 | my $model = new RDF::Redland::Model ($storage, "") | ||
| 90 | or die "Failed to create RDF::Redland::Model for storage"; | ||
| 91 | |||
| 92 | my $uri = new RDF::Redland::URI ("file:///where/ever/pizza.n3"); | ||
| 93 | $parser->parse_into_model ($uri, $uri, $model); | ||
| 94 | |||
| 95 | A detailed step-by-step tutorial for L<RDF::Redland> can | ||
| 96 | be found at L<http://kill.devc.at/internet/semantic-web/rdf/redland/tutorial> . | ||
| 97 | |||
| 98 | =head1 The Reasoner and KB Handles | ||
| 99 | |||
| 100 | A DIG reasoner is a separate process, popular choices being pellet, Fact++. As the | ||
| 101 | communication occurs via HTTP, we have to specify the URL when we create a handle | ||
| 102 | to the reasoner: | ||
| 103 | |||
| 104 | use RDF::Redland::DIG; | ||
| 105 | my $digreasoner = new RDF::Redland::DIG ( "http://localhost:8081" ) | ||
| 106 | or die "Failed to create RDF::Redland::DIG"; | ||
| 107 | |||
| 108 | From then on we are able to create our own knowledge base. This can be easily done by | ||
| 109 | requesting such from our $digreasoner: | ||
| 110 | |||
| 111 | my $kb = $digreasoner->kb; | ||
| 112 | |||
| 113 | This way, we can create as many knowledge bases as we need. | ||
| 114 | |||
| 115 | To fill our knowledge base $kb with data we tell it about our $model: | ||
| 116 | |||
| 117 | $kb->tell ($model) or die "Tell failed"; | ||
| 118 | |||
| 119 | After having executed C<tell>, our C<$model> is stored in the knowledge base, we can start | ||
| 120 | with the reasoning. In the following I will describe a few stereotypical methods. For | ||
| 121 | further details on all available methods please have a look at the manual page of | ||
| 122 | L<RDF::Redland::DIG::KB>. | ||
| 123 | |||
| 124 | =head1 Concept Retrieval | ||
| 125 | |||
| 126 | For pure concept retrieval there are three methods available. One of them is the function | ||
| 127 | C<allRoleNames> that returns all roles from our C<$model>: | ||
| 128 | |||
| 129 | warn Dumper $kb->allRoleNames; | ||
| 130 | |||
| 131 | Result: | ||
| 132 | 'hasIngredient', | ||
| 133 | 'hasTopping', | ||
| 134 | 'isIngredientOf', | ||
| 135 | 'isToppingOf' | ||
| 136 | |||
| 137 | The methods C<allConceptNames> and C<allIndividuals> work analogous. | ||
| 138 | |||
| 139 | =head1 Satisfiability | ||
| 140 | |||
| 141 | To check, whether or not the relations between classes are satisfied, there are three | ||
| 142 | methods available: | ||
| 143 | |||
| 144 | With C<unsatisfiable> we can retrieve a list of all those concepts, which are not | ||
| 145 | satisfiable. Because there are no unsatisfiable concepts in our pizza ontology, we would | ||
| 146 | get back an empty list: | ||
| 147 | |||
| 148 | warn Dumper $kb->unsatisfiable; | ||
| 149 | |||
| 150 | Result: | ||
| 151 | |||
| 152 | We can also query whether or not one concept subsumes another. To do that we have to | ||
| 153 | provide a hash (reference) that contains the classes we want to check: | ||
| 154 | |||
| 155 | warn Dumper $kb->subsumes (\ %('Pizza' => ['PizzaTopping', 'CheesePizza']) ); | ||
| 156 | |||
| 157 | Result: | ||
| 158 | 'Pizza' => 'CheesePizza' | ||
| 159 | |||
| 160 | The result contains all those classes as values that indeed subsume the concept that is | ||
| 161 | provided as key. | ||
| 162 | |||
| 163 | The third method C<disjoint> works the same way. | ||
| 164 | |||
| 165 | =head1 Concept Hierarchy | ||
| 166 | |||
| 167 | Furthermore, there are some methods regarding the hierarchy of concepts. If you want to | ||
| 168 | know, for instance, the parents from a specific concept of our C<$model>, you can simply | ||
| 169 | write: | ||
| 170 | |||
| 171 | warn Dumper $kb->parents ('CheesePizza', 'HamTopping'); | ||
| 172 | |||
| 173 | Result: | ||
| 174 | 'CheesePizza' => 'Pizza', | ||
| 175 | 'HamTopping' => 'PizzaTopping' | ||
| 176 | |||
| 177 | You can also request information on children, descendants or ancestors from classes. | ||
| 178 | |||
| 179 | =head3 Role Hierarchy | ||
| 180 | |||
| 181 | Similar to the concept hierarchy methods, there are also role hierarchy functions | ||
| 182 | available. This way you are able to retrieve information regarding the parents, children, | ||
| 183 | ancestors or descendants of a specific role. If we want to know all children for a | ||
| 184 | specific role in our C<$model>, we write: | ||
| 185 | |||
| 186 | warn Dumper $kb->rchildren ('isIngredientOf'); | ||
| 187 | |||
| 188 | Result: | ||
| 189 | 'isIngredientOf' => 'isToppingOf' | ||
| 190 | |||
| 191 | =head1 Queries about Individuals | ||
| 192 | |||
| 193 | For retrieving information about the relations between concepts, roles or individuals, | ||
| 194 | there is also a bunch of methods available. We can query the instances from a specific | ||
| 195 | class: | ||
| 196 | |||
| 197 | warn Dumper $kb->instances ('CheesePizza') | ||
| 198 | |||
| 199 | Result: | ||
| 200 | 'CheesePizza' => 'Margharita' | ||
| 201 | |||
| 202 | We can retrieve information about individuals that are asserted to a specific | ||
| 203 | (individual,role)-pair with the C<roleFillers> method. All we have to provide is one | ||
| 204 | individual and one role as parameter and in return we get all individuals that are | ||
| 205 | asserted as statement involving this pair: | ||
| 206 | |||
| 207 | warn Dumper $kb->roleFillers ('Margharita', 'hasTopping'); | ||
| 208 | |||
| 209 | Result: | ||
| 210 | 'MozarellaTopping' | ||
| 211 | |||
| 212 | If we want to know which pairs of individuals are asserted to a given role, we use the | ||
| 213 | method C<relatedIndividuals>: | ||
| 214 | |||
| 215 | warn Dumper $kb->relatedIndividuals ('hasTopping'); | ||
| 216 | |||
| 217 | Result: | ||
| 218 | ('Margharita', 'MozarellaTopping') | ||
| 219 | |||
| 220 | |||
| 221 | =head1 Variation: Procure your own User Agent | ||
| 222 | |||
| 223 | Sometimes you will want to use an HTTP user agent of your own making, be it for testing, | ||
| 224 | caching, etc. In any case it will have to be a subclass of L<LWP::UserAgent>. | ||
| 225 | |||
| 226 | If we assume that it is stored in C<$ua>, then you will need to provide it as a additional | ||
| 227 | parameter when the reasoner handle is created: | ||
| 228 | |||
| 229 | my $digreasoner = new RDF::Redland::DIG ( "http://localhost:8081", ua => $ua ) | ||
| 230 | or die "Failed to create RDF::Redland::DIG"; | ||
| 231 | |||
| 232 | =head1 Variation: Concept Hierarchy Parameters | ||
| 233 | |||
| 234 | There are some alternatives regarding parameters when using the concept and role hierarchy | ||
| 235 | methods. | ||
| 236 | |||
| 237 | As shown above, you can provide a specific concept or role as parameter and get all the | ||
| 238 | information you want for those specified concepts/roles. If you want to create a general | ||
| 239 | query on all existing concepts and roles from your model, you can leave the parameter | ||
| 240 | blank. | ||
| 241 | |||
| 242 | For instance, if you want to have the parents from all concepts of our C<$model>, you | ||
| 243 | can write: | ||
| 244 | |||
| 245 | warn Dumper $kb->parents; | ||
| 246 | |||
| 247 | Result: | ||
| 248 | 'Pizza' => '', | ||
| 249 | 'PizzaTopping' => '', | ||
| 250 | 'CheesePizza' => 'Pizza', | ||
| 251 | 'ProsciuttoPizza' => 'Pizza', | ||
| 252 | 'CheeseTopping' => 'PizzaTopping', | ||
| 253 | 'TomatoTopping' => 'PizzaTopping', | ||
| 254 | 'HamTopping' => 'PizzaTopping' | ||
| 255 | |||
| 256 | =head1 COPYRIGHT AND LICENCE | ||
| 257 | |||
| 258 | Copyright 2008 by Lara Spendier and Robert Barta | ||
| 259 | |||
| 260 | Creative Commons Licence L<http://creativecommons.org/licenses/by/2.0/at/> | ||
| 261 | |||
| 262 | =cut | ||
| 263 | |||
| 264 | our $VERSION = '0.03'; | ||
| 265 | |||
| 266 | 1; |