1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
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  
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      
80      
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  }