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.ontology.AllValuesFromRestriction;
22  import com.hp.hpl.jena.ontology.HasValueRestriction;
23  import com.hp.hpl.jena.ontology.OntClass;
24  import com.hp.hpl.jena.ontology.OntProperty;
25  import com.hp.hpl.jena.ontology.Restriction;
26  import com.hp.hpl.jena.ontology.SomeValuesFromRestriction;
27  import com.hp.hpl.jena.rdf.model.RDFNode;
28  import ubic.basecode.ontology.model.OntologyClassRestriction;
29  import ubic.basecode.ontology.model.OntologyTerm;
30  
31  import java.util.Set;
32  
33  /**
34   * @author pavlidis
35   */
36  class OntologyClassRestrictionImpl extends OntologyRestrictionImpl implements OntologyClassRestriction {
37      private final RDFNode value;
38      private final OntologyTerm restrictedTo;
39      private final Set<Restriction> additionalRestrictions;
40  
41      public OntologyClassRestrictionImpl( Restriction term, Set<Restriction> additionalRestrictions ) {
42          super( term, additionalRestrictions );
43          this.additionalRestrictions = additionalRestrictions;
44  
45          if ( term.isAllValuesFromRestriction() ) {
46              AllValuesFromRestriction a = term.asAllValuesFromRestriction();
47              OntProperty onProp = a.getOnProperty();
48              convertProperty( onProp );
49              value = null;
50              restrictedTo = new OntologyTermImpl( ( OntClass ) a.getAllValuesFrom(), additionalRestrictions );
51          } else if ( term.isSomeValuesFromRestriction() ) {
52              SomeValuesFromRestriction s = term.asSomeValuesFromRestriction();
53              OntProperty onProp = s.getOnProperty();
54              convertProperty( onProp );
55              value = null;
56              restrictedTo = new OntologyTermImpl( ( OntClass ) s.getSomeValuesFrom(), additionalRestrictions );
57          } else if ( term.isHasValueRestriction() ) {
58              HasValueRestriction h = term.asHasValueRestriction();
59              OntProperty onProp = h.getOnProperty();
60              convertProperty( onProp );
61              value = h.getHasValue();
62              restrictedTo = null;
63          } else if ( term.isMaxCardinalityRestriction() || term.isMinCardinalityRestriction() ) {
64              log.warn( "Can't handle cardinality restrictions" );
65              value = null;
66              restrictedTo = null;
67          } else {
68              throw new IllegalArgumentException( "Can't handle that type of restriction for: " + term );
69          }
70  
71      }
72  
73      @Override
74      public OntologyTerm getRestrictedTo() {
75          return restrictedTo;
76      }
77  
78      //
79      // public RDFNode getRestrictedToValue() {
80      // return value;
81      // }
82  
83      @Override
84      public String toString() {
85          assert restrictionOn != null;
86          if ( restrictedTo != null ) {
87              return "Class restriction: " + this.getRestrictionOn() + " class=" + this.getRestrictedTo();
88          } else if ( value != null ) {
89              return "Class restriction: " + this.getRestrictionOn() + " = " + this.value.toString() + "[value]";
90          } else {
91              throw new IllegalStateException( "Value or restriction class must be non-null" );
92          }
93      }
94  
95      private void convertProperty( OntProperty restrictionOnProperty ) {
96          this.restrictionOn = PropertyFactory.asProperty( restrictionOnProperty, additionalRestrictions );
97      }
98  
99  }