KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > ontology > OwlParser


1 /* Copyright (c) 2004 michael j pan. All rights reserved. */
2 /* Use subject to the same conditions as Nutch. */
3 /* see http://www.nutch.org/LICENSE.txt. */
4
5 package net.nutch.ontology;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.regex.Matcher JavaDoc;
11 import java.util.regex.Pattern JavaDoc;
12 //import net.nutch.util.LogFormatter;
13

14 import com.hp.hpl.jena.ontology.OntClass;
15 import com.hp.hpl.jena.ontology.OntModel;
16 import com.hp.hpl.jena.ontology.Individual;
17 import com.hp.hpl.jena.rdf.model.Literal;
18
19 /**
20  * implementation of parser for w3c's OWL files
21  *
22  * @author michael j pan
23  */

24 public class OwlParser implements Parser {
25   public OwlParser () {
26   }
27
28   /**
29    * parse owl ontology files using jena
30    */

31   public void parse(OntModel m) {
32     for (Iterator JavaDoc i = rootClasses( m ); i.hasNext(); ) {
33       OntClass c = (OntClass) i.next();
34
35       //dont deal with anonymous classes
36
if (c.isAnon()) {
37         continue;
38       }
39
40       parseClass( c, new ArrayList JavaDoc(), 0 );
41     }
42   }
43
44   protected void parseClass( OntClass cls, List JavaDoc occurs, int depth ) {
45     //dont deal with anonymous classes
46
if (cls.isAnon()) {
47       return;
48     }
49
50     //add cls to Ontology searchterms
51
//list labels
52
Iterator JavaDoc labelIter = cls.listLabels(null);
53     //if has no labels
54
if (!labelIter.hasNext()) {
55         //add rdf:ID as a label
56
cls.addLabel(rdfidToLabel(cls.getLocalName()), null);
57     }
58     //reset the label iterator
59
labelIter = cls.listLabels(null);
60   
61     while(labelIter.hasNext()) {
62       Literal l = (Literal) labelIter.next();
63       OntologyImpl.addSearchTerm(l.toString(), cls);
64     }
65
66     // recurse to the next level down
67
if (cls.canAs( OntClass.class ) && !occurs.contains( cls )) {
68       //list subclasses
69
for (Iterator JavaDoc i = cls.listSubClasses( true ); i.hasNext(); ) {
70         OntClass sub = (OntClass) i.next();
71
72         // we push this expression on the occurs list before we recurse
73
occurs.add( cls );
74         parseClass(sub, occurs, depth+1);
75         occurs.remove( cls );
76       }
77
78       //list instances
79
for (Iterator JavaDoc i=cls.listInstances(); i.hasNext(); ) {
80         //add search terms for each instance
81

82         //list labels
83
Individual individual = (Individual) i.next();
84         for (Iterator JavaDoc j=individual.listLabels(null); j.hasNext();) {
85           Literal l = (Literal) j.next();
86           OntologyImpl.addSearchTerm(l.toString(), individual);
87         }
88       }
89     }
90   }
91
92   public Iterator JavaDoc rootClasses( OntModel m ) {
93     List JavaDoc roots = new ArrayList JavaDoc();
94     
95     for (Iterator JavaDoc i = m.listClasses(); i.hasNext(); ) {
96       OntClass c = (OntClass) i.next();
97         
98       try {
99       // too confusing to list all the restrictions as root classes
100
if (c.isAnon()) {
101           continue;
102         }
103     
104         if (c.hasSuperClass( m.getProfile().THING(), true ) ) {
105           // this class is directly descended from Thing
106
roots.add( c );
107         } else if (c.getCardinality( m.getProfile().SUB_CLASS_OF() ) == 0 ) {
108           // this class has no super-classes
109
// (can occur if we're not using the reasoner)
110
roots.add( c );
111         }
112       } catch (Exception JavaDoc e) {
113         //e.printStackTrace();
114
System.out.println(e.getMessage());
115       }
116     }
117     
118     return roots.iterator();
119   }
120
121   public String JavaDoc rdfidToLabel (String JavaDoc idString) {
122     Pattern JavaDoc p = Pattern.compile("([a-z0-9])([A-Z])");
123     Matcher JavaDoc m = p.matcher(idString);
124
125     String JavaDoc labelString = new String JavaDoc(idString);
126     while(m.find()) {
127       labelString = labelString.replaceAll(m.group(1)+m.group(2),
128              m.group(1)+" "+m.group(2));
129     }
130     return labelString;
131   }
132
133 }
134
Popular Tags