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 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
84
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 }