View Javadoc
1   /*
2    * The basecode project
3    *
4    * Copyright (c) 2007-2019 University of British Columbia
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   */
19  package ubic.basecode.ontology.jena;
20  
21  import com.hp.hpl.jena.rdf.model.*;
22  import com.hp.hpl.jena.vocabulary.RDFS;
23  import ubic.basecode.ontology.model.AnnotationProperty;
24  
25  import org.jspecify.annotations.Nullable;
26  import java.util.Objects;
27  
28  /**
29   * Note that this is a concrete instance of the annotation.
30   *
31   * @author pavlidis
32   */
33  class AnnotationPropertyImpl extends AbstractOntologyResource implements AnnotationProperty {
34  
35      private final com.hp.hpl.jena.ontology.AnnotationProperty property;
36      @Nullable
37      private final RDFNode object;
38  
39      /**
40       * @param prop   property for the statement
41       * @param object of the statement
42       */
43      public AnnotationPropertyImpl( com.hp.hpl.jena.ontology.AnnotationProperty prop, @Nullable RDFNode object ) {
44          super( prop );
45          this.property = prop;
46          this.object = object;
47      }
48  
49      @Override
50      public String getProperty() {
51          if ( property.getLabel( null ) != null ) {
52              return property.getLabel( null );
53          } else if ( property.getLocalName() != null ) {
54              return property.getLocalName();
55          } else {
56              return property.toString();
57          }
58      }
59  
60      @Nullable
61      @Override
62      public String getContents() {
63          if ( this.object == null ) {
64              return null;
65          } else if ( object.isResource() ) {
66              // need a level of indirection...
67              Resource r = ( Resource ) object;
68              Statement s = r.getProperty( RDFS.label );
69              if ( s != null ) {
70                  return s.getObject().toString();
71              } else {
72                  return null;
73              }
74          } else {
75              return ( String ) object.visitWith( new RDFVisitor() {
76  
77                  @Override
78                  public Object visitBlank( Resource r, AnonId id ) {
79                      return r.getLocalName();
80                  }
81  
82                  @Override
83                  public Object visitLiteral( Literal l ) {
84                      return l.toString().replaceAll( "\\^\\^.+", "" );
85                  }
86  
87                  @Override
88                  public Object visitURI( Resource r, String uri ) {
89                      return r.getLocalName();
90                  }
91              } );
92          }
93      }
94  
95      @Override
96      public boolean isObsolete() {
97          return super.isObsolete() || property.hasSuperProperty( OBO.ObsoleteProperty, false );
98      }
99  
100     @Override
101     public boolean equals( @Nullable Object obj ) {
102         if ( this == obj ) return true;
103         if ( !( obj instanceof AnnotationProperty ) ) {
104             return false;
105         }
106         final AnnotationProperty other = ( AnnotationProperty ) obj;
107         if ( other instanceof AnnotationPropertyImpl ) {
108             return super.equals( other )
109                 && Objects.equals( property, ( ( AnnotationPropertyImpl ) other ).property )
110                 && Objects.equals( object, ( ( AnnotationPropertyImpl ) other ).object );
111         } else {
112             return super.equals( other )
113                 && Objects.equals( getProperty(), other.getProperty() )
114                 && Objects.equals( getContents(), other.getContents() );
115         }
116     }
117 
118     @Override
119     public int hashCode() {
120         return Objects.hash( super.hashCode(), property, object );
121     }
122 
123     @Override
124     public String toString() {
125         return property.getLocalName() + " " + object;
126     }
127 }