View Javadoc

1   /*
2    * $Id: RestrictionWrapper.java,v 1.8 2005/07/08 18:19:13 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.editor.visualeditors.restriction;
10  
11  import java.net.URISyntaxException;
12  
13  import org.eclipse.core.runtime.IStatus;
14  import org.eclipse.core.runtime.MultiStatus;
15  import org.eclipse.core.runtime.Status;
16  
17  import com.bbn.swede.core.OWLCore;
18  import com.bbn.swede.core.dom.AttributeNode;
19  import com.bbn.swede.core.dom.DefaultOASTRunnable;
20  import com.bbn.swede.core.dom.IOWLAbstractSyntaxTree;
21  import com.bbn.swede.core.dom.Literal;
22  import com.bbn.swede.core.dom.OAST;
23  import com.bbn.swede.core.dom.OASTException;
24  import com.bbn.swede.core.dom.OASTNode;
25  import com.bbn.swede.core.dom.PropertyNode;
26  import com.bbn.swede.core.dom.TagNode;
27  import com.bbn.swede.core.dom.owl.OnProperty;
28  import com.bbn.swede.core.dom.owl.Restriction;
29  import com.bbn.swede.editor.EditorPlugin;
30  import com.hp.hpl.jena.graph.Node;
31  import com.hp.hpl.jena.ontology.OntProperty;
32  import com.hp.hpl.jena.rdf.model.Property;
33  import com.hp.hpl.jena.rdf.model.RDFNode;
34  import com.hp.hpl.jena.rdf.model.Statement;
35  
36  /***
37   * A utility class which wraps a restriction (either a Jena 
38   * restriction, or an OASTNode of type Restriction), and allows for
39   * convenient access, and, in the case of an OAST Restriction, 
40   * editing.
41   * 
42   * @author aperezlo
43   */
44  public class RestrictionWrapper
45  {
46     /***
47      * Label for owl:someValuesFrom restrictions.
48      */
49     public static final String SOMEVALUES = "someValuesFrom";
50     /***
51      * Label for owl:allValuesFrom restrictions.
52      */
53     public static final String ALLVALUES = "allValuesFrom";
54     /***
55      * Label for owl:hasValue restrictions.
56      */
57     public static final String HASVALUE = "hasValue";
58     /***
59      * Label for owl:minCardinality restrictions.
60      */
61     public static final String MIN = "minCardinality";
62     /***
63      * Label for owl:maxCardinality restrictions.
64      */
65     public static final String MAX = "maxCardinality";
66     /***
67      * Label for owl:cardinality restrictions.
68      */
69     public static final String CARDINALITY = "cardinality";
70     
71     private IRestrictionWrapper _wrapper;
72     
73     /***
74      * Creates a restriction wrapper for a restriction in an OAST.
75      * @param wrapee the OAST Restriction to wrap
76      */
77     public RestrictionWrapper(Restriction wrapee)
78     {
79        _wrapper = new SWeDERestrictionWrapper(wrapee);
80     }
81     
82     /***
83      * Creates a restriction wrapper for a restriction in a Jena model.
84      * @param wrapee the Jena Restriction to wrap
85      */
86     public RestrictionWrapper(com.hp.hpl.jena.ontology.Restriction wrapee)
87     {
88        _wrapper = new JenaRestrictionWrapper(wrapee);
89     }
90  
91     /***
92      * Returns a URI (String form) or OAST Node, depending on 
93      * whether this is a Jena or OAST wrapper, respectively.
94      * 
95      * @return the value of the restriction's onProperty property
96      */
97     public Object getOnProperty()
98     {
99        return _wrapper.getOnProperty();
100    }
101    /***
102     * Retrieves a String representing the URI of the property upon which this 
103     * restriction applies.
104     * @return The onProperty URI
105     */
106    public String getOnPropertyURI()
107    {
108       return _wrapper.getOnPropertyURI();
109    }
110    /***
111     * Returns a string representing which type of restriction 
112     * this wrapper wraps.  
113     * <br />
114     * Will always return one of {@link #ALLVALUES }, {@link #SOMEVALUES }, 
115     * {@link #HASVALUE}, {@link #MIN}, {@link #MAX}, or {@link #CARDINALITY }.
116     * 
117     * 
118     * @return a string representation of the type of this restriction 
119     */
120    public String getRestrictionType()
121    {
122       return _wrapper.getRestrictionType();
123    }
124    /***
125     * Returns a Jena Statement, or an OAST Node, representing 
126     * this restriction type, depending on whether this is a 
127     * Jena or OAST restriction, respectively.
128     * 
129     * @return the restriction type
130     */
131    public Object getRestrictionTypeNode()
132    {
133       return _wrapper.getRestrictionTypeNode();
134    }
135    /***
136     * The text of the restriction. This is suitable for cardinality
137     * restrictions, as well as hasValue restrictions where the value is a
138     * literal.
139     * 
140     * @return the value of the restriction, or null if this is not a restriction
141     *         with a text value (i.e. it is a restriction with a resource value)
142     */
143    public String getRestrictionValueText()
144    {
145       return _wrapper.getRestrictionValueText();
146    }
147    /***
148     * Returns the node that holds the restriction value text, 
149     * either as a Jena Node, or as an OAST Literal Node, depending on 
150     * whether this is a Jena or OAST wrapper.
151     * 
152     * @return the value text node of this restriction.
153     */
154    public Object getRestrictionValueTextNode()
155    {
156       return _wrapper.getRestrictionValueTextNode();
157    }
158    /***
159     * Retrieves a URI containing the reference of the restriction value, or 
160     * <code>null</code> if it is not applicable.
161     * @return The restriction value as a URI
162     */
163    public java.net.URI getRestrictionValueURI()
164    {
165       return _wrapper.getRestrictionValueURI();
166    }
167    /***
168     * Set this restriction's owl:OnProperty value to the URI
169     * specified by the parameter.  Returns an IStatus object 
170     * indicating the result of the operation.
171     * <br />
172     * Note this method does nothing for Jena restrictions.
173     * 
174     * @param uri the new value of the owl:OnProperty property
175     * @return IStatus.
176     */
177    public IStatus setOnPropertyURI(java.net.URI uri)
178    {
179       return _wrapper.setOnPropertyURI(uri);
180    }
181    /***
182     * Set this restriction's type to the value 
183     * specified by the parameter.  Returns an IStatus object 
184     * indicating the result of the operation.
185     * <br />
186     * Note this method does nothing for Jena restrictions.
187     * 
188     * @param type the new type of this property
189     * @return IStatus.
190     */
191    public IStatus setRestictionType(String type)
192    {
193       return _wrapper.setRestictionType(type);
194    }
195    /***
196     * Set this restriction's text value to the text
197     * specified by the parameter.  Returns an IStatus object 
198     * indicating the result of the operation.
199     * <br />
200     * Note this method does nothing for Jena restrictions.
201     * 
202     * @param text the new text value of this restriction (e.g. 7)
203     * @return IStatus.
204     */
205    public IStatus setRestrictionValueText(String text)
206    {
207       return _wrapper.setRestrictionValueText(text);
208    }
209    /***
210     * Set this restriction's value to the URI
211     * specified by the parameter.  Returns an IStatus object 
212     * indicating the result of the operation.
213     * <br />
214     * Note this method does nothing for Jena restrictions.
215     * 
216     * @param uri the new value of the restriction (e.g. xsd:string)
217     */
218    public void setRestrictionValueURI(java.net.URI uri)
219    {
220       _wrapper.setRestrictionValueURI(uri);
221    }
222    /***
223     * A wrapper for interacting with restrictions.
224     * @author aperezlo
225     */
226    interface IRestrictionWrapper
227    {
228       /***
229        * Retrieves the onProperty property for the restriction.
230        * @return The restriction's onProperty property
231        */
232       Object getOnProperty();
233       
234       /***
235        * Retrieves the URI specified by the onProperty property of the 
236        * restriction.
237        * @return The onProperty URI, or <code>null</code> if there is no
238        *         onProperty property.
239        */
240       String getOnPropertyURI();
241       
242       /***
243        * Retrieves a node representing the restriction type.
244        * @return The restriction type
245        */
246       Object getRestrictionTypeNode();
247 
248       /***
249        * Retrieves the restriction type name.  See {@link IRestrictionWrapper}
250        * for restriction type labels.
251        * @return The restriction type name
252        */
253       String getRestrictionType();
254       
255       /***
256        * Retrieves the restriction value as a URI.
257        * @return The restruction value URI, or <code>null</code> if the 
258        *         restriction has no value or it is not a URI.
259        */
260       java.net.URI getRestrictionValueURI();
261       
262       /***
263        * Sets the restriction value to a URI.
264        * @param uri The URI
265        */
266       void setRestrictionValueURI(java.net.URI uri);
267       
268       /***
269        * Retrieves the restriction value as a text node.
270        * @return The restriction value text, or <code>null</code> if the
271        *         restriction has no value or it is not a text node.
272        */
273       Object getRestrictionValueTextNode();
274       
275       /***
276        * Retrieves the restriction value as text.
277        * @return The restriction value text, or <code>null</code> if the
278        *         restriction has no value or it is not a text node.
279        */
280       String getRestrictionValueText();
281       
282       /***
283        * Sets the restriction value to a text string.
284        * @param text The new value.
285        * @return A status object indicating the success or failure of the 
286        *         operation.
287        */
288       IStatus setRestrictionValueText(String text);
289       
290       /***
291        * Sets the URI referred to by the onProperty property of the restriction.
292        * @param uri The URI
293        * @return A status object indicating the success or failure of the 
294        *         operation.
295        */
296       IStatus setOnPropertyURI(java.net.URI uri);
297 
298       /***
299        * Sets the type of the restriction.  See {@link IRestrictionWrapper} for
300        * restriction type label constants.
301        * @param type The type.
302        * @return A status object indicating the success or failure of the 
303        *         operation.
304        */
305       IStatus setRestictionType(String type);
306 
307    }
308    
309    private final class JenaRestrictionWrapper implements IRestrictionWrapper
310    {
311       private com.hp.hpl.jena.ontology.Restriction _restriction;
312       
313       /***
314        * Creates a wrapper for a restriction in a Jena model.
315        * @param rest The restriction
316        */
317       public JenaRestrictionWrapper(com.hp.hpl.jena.ontology.Restriction rest)
318       {
319          _restriction = rest;
320       }
321       
322       public Object getOnProperty()
323       {
324          return _restriction.getOnProperty();
325       }
326       public String getOnPropertyURI()
327       {
328          String toReturn = null;
329          OntProperty op = _restriction.getOnProperty();
330          if(op != null)
331          {
332             toReturn = op.getURI();
333          }
334          return toReturn;
335       }
336       public String getRestrictionType()
337       {
338          String toReturn = "";
339          
340          if(_restriction.isAllValuesFromRestriction())
341          {
342             toReturn = RestrictionWrapper.ALLVALUES;
343          }
344          else if(_restriction.isSomeValuesFromRestriction())
345          {
346             toReturn = RestrictionWrapper.SOMEVALUES;
347          } 
348          else if(_restriction.isMinCardinalityRestriction())
349          {
350             toReturn = RestrictionWrapper.MIN;
351          }
352          else if(_restriction.isMaxCardinalityRestriction())
353          {
354             toReturn = RestrictionWrapper.MAX;
355          }
356          else if(_restriction.isCardinalityRestriction())
357          {
358             toReturn = RestrictionWrapper.CARDINALITY;
359          }
360          else if(_restriction.isHasValueRestriction())
361          {
362             toReturn = RestrictionWrapper.HASVALUE;
363          }
364          
365          return toReturn;
366       }
367       public Object getRestrictionTypeNode()
368       {
369          Object toReturn = null;
370          String sUri = OASTNode.S_OWL_URI + "#" + getRestrictionType();
371          Property prop = _restriction.getModel().createProperty(sUri);
372          toReturn = _restriction.getProperty(prop);
373          return toReturn;
374       }
375 
376       public Object getRestrictionValueTextNode()
377       {
378          Object toReturn = null;
379          Statement prop = (Statement) getRestrictionTypeNode(); 
380          RDFNode rdf = null;
381          if(prop != null)
382          {
383             rdf = prop.getObject();
384             if(rdf.asNode().isLiteral())
385             {
386                toReturn = rdf.asNode();
387             }
388          }
389          return toReturn;
390       }
391       public String getRestrictionValueText()
392       {
393          String toReturn = null;
394          Node lit = (Node) getRestrictionValueTextNode(); 
395          if(lit != null)
396          {
397             if(lit.isLiteral())
398             {
399                toReturn = lit.getLiteral().toString();
400             }
401          }
402          return toReturn;
403       }
404 
405       public java.net.URI getRestrictionValueURI()
406       {
407          java.net.URI toReturn = null;
408          Statement prop = (Statement) getRestrictionTypeNode();
409          RDFNode rdf = null;
410          if(prop != null)
411          {
412             rdf = prop.getObject();
413             if(rdf.asNode().isConcrete() && !rdf.asNode().isLiteral() && !rdf.asNode().isBlank())
414             {
415                try
416                {
417                   toReturn = new java.net.URI(rdf.asNode().getURI());
418                }
419                catch (URISyntaxException e)
420                {
421                   e.printStackTrace();
422                }
423             }
424          }
425          return toReturn;
426       }
427 
428       public IStatus setOnPropertyURI(java.net.URI uri)
429       {
430          return Status.OK_STATUS;
431       }
432       public IStatus setRestictionType(String type)
433       {
434          return Status.OK_STATUS;
435       }
436       public IStatus setRestrictionValueText(String text)
437       {
438          return Status.OK_STATUS;
439       }
440       public void setRestrictionValueURI(java.net.URI uri)
441       {
442 
443       }
444    }
445    
446    private final class SWeDERestrictionWrapper implements IRestrictionWrapper
447    {
448       private Restriction _restriction;
449       
450       /***
451        * Creates a wrappe for a restriction in an OWL abstract syntax tree.
452        * @param rest The restriction
453        */
454       public SWeDERestrictionWrapper(Restriction rest)
455       {
456          _restriction = rest;
457       }
458       
459       public Object getOnProperty()
460       {
461          OASTNode[] kids = _restriction.getNodesOfType(OASTNode.OWL_ONPROPERTY);
462          OnProperty toReturn = null;
463          if(kids.length > 0)
464          {
465             toReturn = (OnProperty)kids[0];
466          }
467          return toReturn;
468       }
469       
470       /***
471        * Retrieves an attribute value from a tag.
472        * @param iOast The OWL abstract syntax tree containing the tag.
473        * @param node The tag.
474        * @param sRdf The name of an element from the RDF namespace
475        * @return The value of the specified RDF attribute of the tag.
476        */
477       public String oastGetRDF(IOWLAbstractSyntaxTree iOast, TagNode node, String sRdf)
478       {
479          OAST oast = (OAST) iOast;
480          String toReturn = node.getName();
481          AttributeNode attr = null;
482          String qName = oast.getQNameForURI(OASTNode.S_RDF_URI + "#" + sRdf);
483          attr = node.getAttribute(qName);
484          if(attr != null)
485          {
486             toReturn = attr.getValue();
487          }
488          return toReturn;
489       }
490       
491       public String getOnPropertyURI()
492       {
493          String toReturn = null;
494          OnProperty onProp = (OnProperty) getOnProperty();
495          if(onProp != null)
496          {
497             toReturn = oastGetRDF(onProp.getOWLAbstractSyntaxTree(), onProp, "resource");
498          }
499          return toReturn;
500       }
501       
502       public Object getRestrictionTypeNode()
503       {
504          TagNode toReturn = null;
505          OASTNode[] kids = _restriction.getChildren();
506          OASTNode node = null;
507          if(kids != null)
508          {
509             for (int i = 0; i < kids.length; i++)
510             {
511                node = kids[i];
512                if(node.getNodeType() == OASTNode.OWL_ALLVALUESFROM)
513                {
514                   toReturn = (TagNode) node;
515                   break;
516                }
517                if(node.getNodeType() == OASTNode.OWL_SOMEVALUESFROM)
518                {
519                   toReturn = (TagNode) node;
520                   break;
521                }
522                if(node.getNodeType() == OASTNode.OWL_HASVALUE)
523                {
524                   toReturn = (TagNode) node;
525                   break;
526                }
527                if(node.getNodeType() == OASTNode.OWL_MINCARDINALITY)
528                {
529                   toReturn = (TagNode) node;
530                   break;
531                }
532                if(node.getNodeType() == OASTNode.OWL_CARDINALITY)
533                {
534                   toReturn = (TagNode) node;
535                   break;
536                }
537                if(node.getNodeType() == OASTNode.OWL_MAXCARDINALITY)
538                {
539                   toReturn = (TagNode) node;
540                   break;
541                }
542             }
543          }
544          
545          return toReturn;
546       }
547 
548       public String getRestrictionType()
549       {
550          TagNode tn = (TagNode) getRestrictionTypeNode();
551          String toReturn = null;
552          String temp = null;
553          if(tn != null)
554          {
555             temp = tn.getUri();
556             toReturn = temp.substring(temp.lastIndexOf("#") + 1);
557          }
558          return toReturn;
559       }
560       
561       public java.net.URI getRestrictionValueURI()
562       {
563          java.net.URI toReturn = null;
564          TagNode tn = (TagNode) getRestrictionTypeNode();
565          String temp = null;
566          if(tn != null)
567          {
568             temp = oastGetRDF(tn.getOWLAbstractSyntaxTree(), tn, "resource");
569             if(temp.indexOf("#") > -1)
570             {
571                try
572                {
573                   toReturn = new java.net.URI(temp);
574                }
575                catch (URISyntaxException e)
576                {
577                   e.printStackTrace();
578                }
579             }
580          }
581          return toReturn;
582       }
583       
584       public void setRestrictionValueURI(java.net.URI uri)
585       {
586          final TagNode tn = (TagNode) getRestrictionTypeNode();
587          IOWLAbstractSyntaxTree oast = tn.getOWLAbstractSyntaxTree();
588          final String qName = tn.toString();
589          final String uriString = uri.toString();
590          
591          try
592          {
593             oast.run(new DefaultOASTRunnable()
594             {
595                public void run() throws OASTException
596                {
597                   _restriction.removeProperty((PropertyNode) tn);
598                   _restriction.addPropertyResource(qName, uriString);
599                }
600             });
601          }
602          catch (OASTException e)
603          {
604             //This should never happen because the restriction editor disables the
605             //edit controls for read-only docs, but make sure it gets reported if 
606             //it does.
607             OWLCore.logWarning(EditorPlugin.getID(), e.getMessage(), e);
608          }
609       }
610       
611       public Object getRestrictionValueTextNode()
612       {
613          Literal toReturn = null;
614          TagNode tn = (TagNode) getRestrictionTypeNode();
615          OASTNode[] kids = null;
616          if(tn != null)
617          {
618             kids = tn.getNodesOfType(OASTNode.LITERAL);
619             if(kids.length > 0)
620             {
621                toReturn = (Literal)kids[0];
622             }
623          }
624          return toReturn;
625       }
626       
627       public String getRestrictionValueText()
628       {
629          String toReturn = null;
630          Literal l = (Literal) getRestrictionValueTextNode();
631          if(l != null)
632          {
633             toReturn = l.getText();
634          }
635          return toReturn;
636       }
637       
638       public IStatus setRestrictionValueText(final String text)
639       {
640          MultiStatus toReturn = new MultiStatus(EditorPlugin.getID(),
641             IStatus.OK, "Set restriction value text...", null);
642          String restrictionType = null;
643          try
644          {
645             IOWLAbstractSyntaxTree oast = _restriction.getOWLAbstractSyntaxTree();
646             restrictionType = getRestrictionType();
647             if(!restrictionType.equals(SOMEVALUES)
648                && !restrictionType.equals(ALLVALUES))
649             {
650                final TagNode type = (TagNode) getRestrictionTypeNode();
651                if(type == null)
652                {
653                   toReturn.add(new Status(IStatus.ERROR, EditorPlugin.getID(),
654                      IStatus.OK, "Could not find the restriction type node!",
655                      null));
656                }
657                else
658                {
659                   final String owlType = type.toString();
660                   oast.run(new DefaultOASTRunnable()
661                   {
662                      public void run() throws OASTException
663                      {
664                         _restriction.removeProperty((PropertyNode) type);
665                         _restriction.addPropertyLiteral(owlType, text);
666 //                        Literal l = (Literal) getRestrictionValueTextNode();
667 //                        if(l != null)
668 //                        {
669 //                           l.setText(text);
670 //                        }
671                      }
672                   });
673                   
674                }
675             }
676             else
677             {
678                toReturn.add(new Status(IStatus.ERROR, EditorPlugin.getID(),
679                   IStatus.OK,
680                   "Can't set a text value on this type of restriction ("
681                      + getRestrictionType() + ")", null));
682             }
683          }
684          catch (OASTException e)
685          {
686             //This should never happen because the restriction editor disables the
687             //edit controls for read-only docs, but make sure it gets reported if 
688             //it does.
689             OWLCore.logWarning(EditorPlugin.getID(), e.getMessage(), e);
690          }
691          catch(Exception e)
692          {
693             toReturn.add(new Status(IStatus.ERROR, EditorPlugin.getID(),
694                IStatus.OK, "Error setting restriction value text...", e));
695          }
696          return toReturn;
697       }
698       
699       public IStatus setOnPropertyURI(java.net.URI uri)
700       {
701          MultiStatus toReturn = new MultiStatus(EditorPlugin.getID(), IStatus.OK, "Set OnProperty URI...", null);
702          try
703          {
704             OnProperty onProp = (OnProperty) getOnProperty();
705             String qName = "rdf:resource";
706             if(onProp == null)
707             {
708                qName = "owl:onProperty";
709                _restriction.addPropertyResource(qName, uri.toString());
710             }
711             else
712             {
713                onProp.setAttribute(qName, uri.toString());
714             }
715          }
716          catch(Exception e)
717          {
718             toReturn.add(new Status(IStatus.ERROR, EditorPlugin.getID(),
719                IStatus.OK, "Error setting OnProperty URI...", e));
720          }
721          return toReturn;
722       }
723       
724       public IStatus setRestictionType(String type)
725       {
726          MultiStatus toReturn = new MultiStatus(EditorPlugin.getID(), IStatus.OK, "Set Restriction Type...", null);
727          try
728          {
729             final PropertyNode tn = (PropertyNode) getRestrictionTypeNode();
730             final java.net.URI uri = getRestrictionValueURI();
731             final String value = getRestrictionValueText();
732             final String finalType = "owl:" + type;
733             IOWLAbstractSyntaxTree oast = _restriction.getOWLAbstractSyntaxTree();
734 
735             oast.run(new DefaultOASTRunnable()
736             {
737                public void run()
738                {
739                   try
740                   {
741                      _restriction.removeProperty(tn);
742                      if(uri != null)
743                      {
744                         _restriction.addPropertyResource(finalType, uri.toString());
745                      }
746                      else
747                      {
748                         String newValue = (value == null) ? "" : value;
749                         _restriction.addPropertyLiteral(finalType, newValue);
750                      }
751                   }
752                   catch(Exception e)
753                   {
754                      e.printStackTrace();
755                   }
756                }
757             });
758          }
759          catch(Exception e)
760          {
761             toReturn.add(new Status(IStatus.ERROR, EditorPlugin.getID(),
762                IStatus.OK, "Error setting Restriction Type...", e));
763          }
764          return toReturn;
765       }      
766    }
767    
768    
769 }