View Javadoc

1   /*
2    * $Id: OWLActionProcessor.java,v 1.35 2005/07/11 20:31:07 jlerner Exp $
3    * Created on Apr 28, 2004
4    *
5    * Copyright (c) 1999-2004, BBN Technologies, LLC.
6    * All rights reserved.
7    * http://www.daml.org/legal/opensource/bbn_license.html
8    */
9   
10  package com.bbn.swede.ui.action;
11  
12  import java.lang.reflect.InvocationTargetException;
13  import java.util.ArrayList;
14  import java.util.Collection;
15  import java.util.Iterator;
16  import java.util.List;
17  
18  import org.eclipse.core.resources.IMarker;
19  import org.eclipse.core.resources.IResource;
20  import org.eclipse.core.runtime.CoreException;
21  import org.eclipse.core.runtime.IProgressMonitor;
22  import org.eclipse.core.runtime.IStatus;
23  import org.eclipse.core.runtime.Status;
24  import org.eclipse.core.runtime.SubProgressMonitor;
25  import org.eclipse.core.runtime.jobs.Job;
26  import org.eclipse.jface.action.IAction;
27  import org.eclipse.jface.viewers.ISelection;
28  import org.eclipse.jface.viewers.IStructuredSelection;
29  import org.eclipse.ui.IWorkbenchWindow;
30  import org.eclipse.ui.IWorkbenchWindowActionDelegate;
31  import org.eclipse.ui.actions.WorkspaceModifyOperation;
32  
33  import com.bbn.semweb.owl.vowlidator.Preferences;
34  import com.bbn.semweb.owl.vowlidator.ValidatorAPI;
35  import com.bbn.semweb.owl.vowlidator.indications.Indication;
36  import com.bbn.swede.core.IOWLDocument;
37  import com.bbn.swede.core.IOWLElement;
38  import com.bbn.swede.core.IOWLModel;
39  import com.bbn.swede.core.IOWLProject;
40  import com.bbn.swede.core.OWLCore;
41  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
42  import com.bbn.swede.core.dom.OASTNode;
43  import com.bbn.swede.core.resources.SWResourceManager;
44  import com.bbn.swede.library.vowlidator.VowlidatorPlugin;
45  import com.bbn.swede.ui.UIPlugin;
46  import com.hp.hpl.jena.rdf.model.Statement;
47  
48  /***
49   * Centralized handling for SWeDE actions.
50   * @author tself
51   */
52  public class OWLActionProcessor implements IWorkbenchWindowActionDelegate
53  {
54     private IStructuredSelection _currentSelection;
55  
56     /*
57      * (non-Javadoc)
58      * 
59      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
60      */
61     public void run(IAction action)
62     {
63        if(_currentSelection == null)
64        {
65           return;
66        }
67        String actionID = action.getId();
68        if(actionID.equals("com.bbn.swede.ui.action.validateOWLAction"))
69        {
70           runValidator(_currentSelection);
71           return;
72        }
73  
74        Iterator it = _currentSelection.iterator();
75        while (it.hasNext())
76        {
77           Object obj = (Object)it.next();
78           //         if (obj instanceof IOWLElement)
79           //            System.out.println(
80           //               "Resource: " + ((IOWLElement) obj).getElementName());
81           //         if (obj instanceof OWLExternalDocument)
82           //            System.out.println(
83           //               "...Imported from "
84           //                  + ((OWLExternalDocument) obj).getSourceURL());
85           //         if (obj instanceof OWLDocument)
86           //            System.out.println(
87           //               "...with xml:base=" + ((OWLDocument) obj).getURI());
88        }
89     }
90  
91     private void runValidator(IStructuredSelection sel)
92     {
93        Iterator it = sel.iterator();
94        while (it.hasNext())
95        {
96           Object obj = (Object)it.next();
97           if(obj instanceof IOWLDocument)
98           {
99              validateDocument((IOWLDocument)obj);
100          }
101       }
102    }
103 
104    private void validateDocument(final IOWLDocument doc)
105    {
106       final WorkspaceModifyOperation op = new WorkspaceModifyOperation()
107       {
108          protected void execute(IProgressMonitor monitor) throws CoreException,
109             InterruptedException
110          {
111             monitor.beginTask("Validating " + doc.getElementName(), 1000);
112             ValidatorAPI va = null;
113             Preferences prefs = null;
114             va = VowlidatorPlugin.getValidatorAPI();
115             monitor.worked(50);
116             if(monitor.isCanceled())
117             {
118                throw new InterruptedException();
119             }
120             // get the document's project
121             IOWLModel model = SWResourceManager.getModel();
122             IOWLProject proj = (IOWLProject)(model.getCorrespondingElement(doc
123                .getResource().getProject()));
124             // find imported OWL documents
125 //            IOWLExternalDocument[] importedDocs = model
126 //               .getImportedDocuments(proj);
127             IOWLElement[] importedDocs = SWResourceManager.findChildrenOfType(proj, IOWLDocument.class);
128 
129             // get models for imported documents
130             int workAmt = 350;
131             if(importedDocs.length > 0)
132             {
133                workAmt = 350 / importedDocs.length;
134                for(int i = 0; i < importedDocs.length; i++)
135                {
136                   if(monitor.isCanceled())
137                   {
138                      throw new InterruptedException();
139                   }
140                   trace("Adding imported document: " + importedDocs[i].getURI());
141                   va.addModel(((IOWLDocument) importedDocs[i]).getDocumentInfo().getModel());
142                   monitor.worked(workAmt);
143                }
144             }
145             else
146             {
147                monitor.worked(workAmt);
148             }
149             // get model for current document
150             trace("Validating: " + doc.getElementName() + " as " + doc.getURI());
151             if(monitor.isCanceled())
152             {
153                throw new InterruptedException();
154             }
155             List indicators = va.validateModel(
156                doc.getDocumentInfo().getModel(), doc.getURI());
157             monitor.worked(400);
158 
159             IResource res = doc.getCorrespondingResource();
160             IMarker mark = null;
161             IMarker[] vMarkers = null;
162             workAmt = 200;
163             if(indicators.size() > 0)
164             {
165                workAmt = 200 / indicators.size();
166             }
167             Iterator it = indicators.iterator();
168             // clear old markers and add new ones
169             res.deleteMarkers("com.bbn.swede.ui.validationmarker", true,
170                IResource.DEPTH_INFINITE);
171 
172             while (it.hasNext())
173             {
174                if(monitor.isCanceled())
175                {
176                   throw new InterruptedException();
177                }
178                Indication ind = (Indication)it.next();
179                if(ind.getLevel() == Indication.INFORMATION)
180                {
181                   // ignore silly information
182                   monitor.worked(workAmt);
183                   continue;
184                }
185                final IResource fRes = res;
186                final Indication fInd = ind;
187                final IOWLDocument fDoc = doc;
188                mark = fRes.createMarker("com.bbn.swede.ui.validationmarker");
189                switch(fInd.getLevel())
190                {
191                   case Indication.INFORMATION:
192                      mark.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
193                      break;
194                   case Indication.WARNING:
195                      mark.setAttribute(IMarker.SEVERITY,
196                         IMarker.SEVERITY_WARNING);
197                      break;
198                   case Indication.ERROR:
199                      mark
200                         .setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
201                      break;
202                   default:
203                      OWLCore.trace("OWLActionProcessor.validateDocument", 
204                                    "Unknown indication level.", false);
205                      monitor.worked(workAmt);
206                      continue;
207                }
208                mark.setAttribute(IMarker.MESSAGE, fInd.getMessage());
209                //mark.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_NORMAL);
210                mark.setAttribute(IMarker.LOCATION, fDoc.getElementName());
211                setMarkerOffsets(fDoc, mark, fInd);
212                monitor.worked(workAmt);
213             }
214             if(indicators.size() < 1)
215             {
216                monitor.worked(workAmt);
217             }
218             monitor.done();
219          }
220       };
221       Job validationJob = new Job("OWL Validation")
222       {
223          protected IStatus run(IProgressMonitor monitor)
224          {
225             IStatus toReturn = Status.OK_STATUS;
226             monitor.beginTask("Validating...", 110);
227             try
228             {
229                monitor.worked(5);
230                op.run(new SubProgressMonitor(monitor, 100));
231                monitor.worked(5);
232             }
233             catch (InvocationTargetException e)
234             {
235                toReturn = new Status(IStatus.ERROR, UIPlugin.getID(),
236                   IStatus.OK, "Error encountered during validation...", e);
237             }
238             catch (InterruptedException e)
239             {
240                toReturn = new Status(IStatus.ERROR, UIPlugin.getID(),
241                   IStatus.OK, "Error encountered during validation...", e);            
242             }
243             finally
244             {
245                monitor.done();
246             }
247             if(monitor.isCanceled())
248             {
249                toReturn = Status.CANCEL_STATUS;
250             }
251             return toReturn;
252          }
253       };
254 
255       validationJob.setUser(true);
256       validationJob.schedule();
257    }
258 
259    private static void setMarkerOffsets(IOWLDocument doc, IMarker mark,
260       Indication ind) throws CoreException
261    {
262       Collection stmts = null;
263       Statement stmt = null;
264       stmts = ind.getStatements();
265       stmt = ind.getStatement();
266       if(stmt == null && stmts == null)
267       {
268          return;
269       }
270       IOWLAbstractSyntaxTree oast = doc.getDocumentInfo().getOAST();
271       OASTNode node = null;
272       if(stmts == null)
273       {
274          stmts = new ArrayList(1);
275          stmts.add(stmt);
276       }
277       // statements has at least 1 thing in it now
278       Iterator it = stmts.iterator();
279       while (it.hasNext())
280       {
281          String error = "";
282          Statement st = (Statement)it.next();
283          node = oast.getNode(st);
284          if(node == null)
285          {
286             node = oast.getNode(st.getSubject());
287          }
288          error = "Getting OASTNode using URI: " + st.getSubject().getURI();
289          //OWLCore.trace("setMarkerOffsets():node", Boolean.toString(node ==
290          // null), false);
291          if(node == null)
292          {
293             OWLCore
294                .trace("setMarkerOffsets()", error + "\nResult = NULL", true);
295             continue;
296          }
297          mark.setAttribute(IMarker.CHAR_START, new Integer(node.getOffset()));
298          mark.setAttribute(IMarker.CHAR_END, new Integer(node.getOffset()
299             + node.getLength()));
300       }
301    }
302 
303    /*
304     * (non-Javadoc)
305     * 
306     * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
307     *      org.eclipse.jface.viewers.ISelection)
308     */
309    public void selectionChanged(IAction action, ISelection selection)
310    {
311       if(selection instanceof IStructuredSelection)
312       {
313          _currentSelection = (IStructuredSelection)selection;
314       }
315    }
316 
317    /*
318     *  (non-Javadoc)
319     * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
320     */
321    public void init(IWorkbenchWindow window)
322    {
323    }
324 
325    /*
326     *  (non-Javadoc)
327     * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
328     */
329    public void dispose()
330    {
331    }
332 
333    private void trace(String s)
334    {
335       trace(s, false);
336    }
337 
338    private void trace(String s, boolean error)
339    {
340       if(true)
341       {
342          return;
343       }
344       OWLCore.trace("OWLActionProcessor", s, error);
345    }
346 
347 }