View Javadoc

1   /*
2    * $Id: ModelLoadJob.java,v 1.11 2005/07/07 19:40:26 aperezlo 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.editor.visualeditors.restriction;
10  
11  import java.util.ArrayList;
12  import java.util.Arrays;
13  import java.util.Iterator;
14  import java.util.List;
15  
16  import org.eclipse.core.runtime.IProgressMonitor;
17  import org.eclipse.core.runtime.IStatus;
18  import org.eclipse.core.runtime.Status;
19  import org.eclipse.core.runtime.SubProgressMonitor;
20  import org.eclipse.core.runtime.jobs.Job;
21  
22  import com.bbn.swede.core.IOWLDocument;
23  import com.bbn.swede.core.dom.AttributeNode;
24  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
25  import com.bbn.swede.core.dom.OASTNode;
26  import com.bbn.swede.core.dom.owl.Imports;
27  import com.hp.hpl.jena.ontology.OntModel;
28  import com.hp.hpl.jena.ontology.OntModelSpec;
29  import com.hp.hpl.jena.rdf.model.ModelFactory;
30  import com.hp.hpl.jena.rdf.model.Statement;
31  
32  /***
33   * A job to load the Jena model used by the Restriction Editor in a 
34   * different thread so as to keep the UI responsive.  It will create a 
35   * inferencing OntModel containing all the statements of each document 
36   * referenced by either assigning a namespace, or by an owl:import statement, 
37   * as available.
38   *  
39   * 
40   * @author aperezlo
41   */
42  
43  public class ModelLoadJob extends Job implements Runnable
44  {
45     private IProgressMonitor _monitor;
46     private IOWLAbstractSyntaxTree _oast;
47     private OntModel _model;
48     
49     /***
50      * Creates a model load job for a specific OAST.
51      * @param oast the OAST of the document to use for loading the model
52      */
53     public ModelLoadJob(IOWLAbstractSyntaxTree oast)
54     {
55        super("Loading model...");
56        _oast = oast;
57        setUser(true);
58        setPriority(Job.INTERACTIVE);
59     }
60     
61     /***
62      * Returns the model loaded by this job.  Only call once the job 
63      * has finished or the result will be null.
64      * 
65      * @return the loaded model  
66      */
67     public OntModel getModel()
68     {
69        return _model;
70     }
71     
72     /***
73      * Loads the model as described in the class documentation.
74      * 
75      * @see java.lang.Runnable#run()
76      */
77     public void run()
78     {
79        _monitor.beginTask("Loading model...", 100);
80        _monitor.subTask("Loading initial model...");
81        try
82        {
83           Iterator i = null;
84           String sUri = null;
85           List namespaces = new ArrayList();
86           namespaces.addAll(Arrays.asList(_oast.getRoot().getNodesOfType(OASTNode.NAMESPACE)));
87           namespaces.addAll(Arrays.asList(_oast.getRoot().getNodesOfType(OASTNode.OWL_IMPORTS))); 
88           
89           int numSpaces = namespaces.size();
90           
91           _model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RDFS_INF, _oast.getJenaModel());
92  //         _model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_TRANS_INF, _oast.getJenaModel());
93           
94           i = namespaces.iterator();
95           SubProgressMonitor monty = null;
96           OASTNode node = null;
97           
98  //         
99  //         _model.add(_model.createStatement(_model
100 //            .createResource(OASTNode.S_OWL_URI + "#Class"), _model
101 //            .createProperty(OASTNode.S_RDFS_URI + "#subClassOf"), _model
102 //            .createResource(OASTNode.S_RDFS_URI + "#Class")));
103          
104          while(i.hasNext() && !_monitor.isCanceled())
105          {
106             node = (OASTNode) i.next();
107             if(node instanceof AttributeNode)
108             {
109                sUri = ((AttributeNode) node).getValue();
110             }
111             else if (node instanceof Imports)
112             {
113                Statement s = ((Imports) node).getAssociatedStatement();
114                sUri =  s.getObject().asNode().getURI();
115             }
116             
117             monty = new SubProgressMonitor(_monitor, 100 / numSpaces);
118             monty.beginTask("Loading namespace:" + sUri + "...", 1);
119             IOWLDocument doc = _oast.getNamespace(sUri);
120             if(doc != null)
121             {
122                _model.add(doc.getDocumentInfo().getModel());
123             }
124             monty.done();
125          }               
126 
127       }
128       finally
129       {
130          _monitor.done();      
131          if(_monitor.isCanceled())
132          {
133             done(Status.CANCEL_STATUS);
134          }
135          else
136          {
137             done(Status.OK_STATUS);
138          }
139       }
140    }
141    
142    /***
143     * Runs on the calling thread.
144     * 
145     * @param monitor A progress monitor
146     * @return A status object indicating the success or failure of the job.
147     */
148    protected IStatus runBlocking(IProgressMonitor monitor)
149    {
150       _monitor = monitor;
151       run();
152       return Status.OK_STATUS;
153    }
154    
155    /*** 
156     * Default behaviour, runs asynchronously on a separate thread.
157     * 
158     * @see org.eclipse.core.internal.jobs.InternalJob#run(org.eclipse.core.runtime.IProgressMonitor)
159     */
160    protected IStatus run(IProgressMonitor monitor)
161    {
162       _monitor = monitor;
163       Thread t = new Thread(this);
164       setThread(t);
165       t.start();
166       return Job.ASYNC_FINISH;
167    } 
168 }
169