View Javadoc

1   /*
2    * $Id: OWLDocumentInfo.java,v 1.19 2005/06/01 17:38:38 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8   
9   package com.bbn.swede.core;
10  
11  import java.util.ArrayList;
12  
13  import org.eclipse.core.resources.IFile;
14  
15  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
16  import com.bbn.swede.core.dom.OAST;
17  import com.bbn.swede.core.resources.SWResourceManager;
18  import com.hp.hpl.jena.rdf.model.Model;
19  import com.hp.hpl.jena.rdf.model.Property;
20  import com.hp.hpl.jena.rdf.model.ResIterator;
21  import com.hp.hpl.jena.rdf.model.Resource;
22  import com.hp.hpl.jena.vocabulary.OWL;
23  import com.hp.hpl.jena.vocabulary.RDF;
24  
25  /***
26   * A specialization of OWL element info for OWL documents.  Document info
27   * adds delayed loading of a document's OAST and Jena model.
28   * @author jlerner
29   * @author tself
30   */
31  public class OWLDocumentInfo extends OWLElementInfo
32  {
33     private Model _model;
34     /***
35      * Creates a new info object for an OWL document.
36      * @param doc The document
37      */
38     public OWLDocumentInfo(IOWLDocument doc)
39     {
40        super(doc);
41     }
42  
43     /***
44      * <p>Returns the OWL document associated with this info object.</p>
45      * 
46      * <p>This is a convenience method, fully equivalent to:
47      * <blockquote>(IOWLDocument)getElement()</blockquote></p>
48      * @return The OWL document this info object is assocaited with.
49      */
50     public IOWLDocument getDocument()
51     {
52        return (IOWLDocument) _elem;
53     }
54  
55     /***
56      * Indicates whether or not the Jena model for this document has been built.
57      * @return <code>true</code> if the Jena model already exists,
58      *         <code>false</code> if not.
59      */
60     public boolean modelExists()
61     {
62        return (_model != null);
63     }
64  
65     /***
66      * Returns the Jena model representing the OWL document.  If the model does
67      * not already exist, it will be built.  Furthermore, if the OAST does not
68      * already exist, it will be built.  Once the Jena model has been built once,
69      * the OAST keeps it in synch with the document structure, so the model
70      * returned by this method is always current.
71      * @return The Jena model for this document.
72      */
73     public Model getModel()
74     {
75        if (_model == null)
76        {
77           _model = getOAST().getJenaModel();
78        }
79        return _model;
80     }
81     
82     /***
83      * Produces a list of all named individuals in the document.  This method 
84      * will cause the Jena model for the document to be built, if it does not 
85      * already exist.
86      * @return An unordered array of Jena resources representing all the 
87      *         non-anonymous individuals in the document.
88      */
89     public Resource[] getNamedIndividuals()
90     {
91        ArrayList al = new ArrayList();
92        
93        Model model = getModel();
94        ResIterator ri = model.listSubjects();
95        while (ri.hasNext())
96        {
97           Resource res = (Resource)ri.next();
98           //Skip anonymous resources and the resource representing the document
99           if (!res.isAnon() && !res.getURI().equals(getDocument().getURI()))
100          {
101             al.add(res);
102          }
103       }
104       ri.close();
105       
106       return (Resource[])al.toArray(new Resource[0]);
107    }
108 
109    /***
110     * Populates the internal list of OWL classes and properties that are
111     * contained in this OWL document.  This method relies on the Jena model,
112     * which relies on the OAST; if one or both of these does not yet exist when
113     * it is called, they will be created now.
114     */
115    public void buildChildren()
116    {
117       Model model = getModel();
118       ArrayList alChildren = new ArrayList();
119       ResIterator ri = model.listSubjectsWithProperty(RDF.type, OWL.Class);
120       while (ri.hasNext())
121       {
122          com.hp.hpl.jena.rdf.model.Resource res =
123             (com.hp.hpl.jena.rdf.model.Resource) ri.next();
124          IOWLClass oc = SWResourceManager.createOWLClass(res);
125          alChildren.add(oc);
126          oc.setParent(getElement());
127       }
128       ri = model.listSubjectsWithProperty(RDF.type, OWL.DatatypeProperty);
129       while (ri.hasNext())
130       {
131          com.hp.hpl.jena.rdf.model.Resource res =
132             (com.hp.hpl.jena.rdf.model.Resource) ri.next();
133          if(!res.isAnon())
134          {
135             Property prop = (Property) res.as(Property.class);
136             IOWLProperty op = SWResourceManager.createOWLProperty(prop);
137             alChildren.add(op);
138             op.setParent(getElement());
139          }
140       }
141       ri = model.listSubjectsWithProperty(RDF.type, OWL.ObjectProperty);
142       while (ri.hasNext())
143       {
144          com.hp.hpl.jena.rdf.model.Resource res =
145             (com.hp.hpl.jena.rdf.model.Resource) ri.next();
146          if(!res.isAnon())
147          {
148             Property prop = (Property) res.as(Property.class);
149             IOWLProperty op = SWResourceManager.createOWLProperty(prop);
150             alChildren.add(op);
151             op.setParent(getElement());
152          }
153       }
154       ri.close();
155 
156       _aelemChildren = new IOWLElement[alChildren.size()];
157       System.arraycopy(
158          alChildren.toArray(),
159          0,
160          _aelemChildren,
161          0,
162          alChildren.size());
163    }
164 
165    private IOWLAbstractSyntaxTree _tree;
166    /***
167     * Returns the OAST representing the OWL document.  If it does not already
168     * exist, this method causes the OAST to be built.
169     * @return The document's OAST.
170     */
171    public IOWLAbstractSyntaxTree getOAST()
172    {
173       if (_tree == null)
174       {
175          OAST oast = new OAST();
176          IFile file = (IFile) getDocument().getResource();
177          oast.parseDocument(file);
178          _tree = oast;
179       }
180       return _tree;
181    }
182 
183    /*
184     *  (non-Javadoc)
185     * @see com.bbn.swede.core.OWLElementInfo#getChildren()
186     */
187    public IOWLElement[] getChildren()
188    {
189       buildChildren();
190       return super.getChildren();
191    }
192 
193 }