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