2424import com .hp .hpl .jena .rdf .arp .ParseException ;
2525import com .hp .hpl .jena .rdf .model .Property ;
2626import com .hp .hpl .jena .rdf .model .Resource ;
27+ import com .hp .hpl .jena .rdf .model .ResourceFactory ;
2728import com .hp .hpl .jena .util .iterator .ExtendedIterator ;
2829import org .apache .commons .lang3 .RandomStringUtils ;
2930import org .apache .commons .lang3 .StringUtils ;
@@ -65,18 +66,20 @@ public abstract class AbstractOntologyService implements OntologyService {
6566 /**
6667 * Properties through which propagation is allowed for {@link #getParents(Collection, boolean, boolean)}}
6768 */
68- private static final Set <Property > additionalProperties ;
69+ private static final Set <String > DEFAULT_ADDITIONAL_PROPERTIES ;
6970
7071 static {
71- additionalProperties = new HashSet <>();
72- additionalProperties .add ( BFO .partOf );
73- additionalProperties .add ( RO .properPartOf );
72+ DEFAULT_ADDITIONAL_PROPERTIES = new HashSet <>();
73+ DEFAULT_ADDITIONAL_PROPERTIES .add ( BFO .partOf . getURI () );
74+ DEFAULT_ADDITIONAL_PROPERTIES .add ( RO .properPartOf . getURI () );
7475 }
7576
7677 /* settings (applicable for next initialization) */
78+ private LanguageLevel nextLanguageLevel = LanguageLevel .FULL ;
7779 private InferenceMode nextInferenceMode = InferenceMode .TRANSITIVE ;
7880 private boolean nextProcessImports = true ;
7981 private boolean nextSearchEnabled = true ;
82+ private Set <String > nextAdditionalPropertyUris = DEFAULT_ADDITIONAL_PROPERTIES ;
8083
8184 /**
8285 * Lock used to prevent reads while the ontology is being initialized.
@@ -91,11 +94,31 @@ public abstract class AbstractOntologyService implements OntologyService {
9194 private Set <Restriction > additionalRestrictions ;
9295 private boolean isInitialized = false ;
9396 @ Nullable
97+ private LanguageLevel languageLevel = null ;
98+ @ Nullable
9499 private InferenceMode inferenceMode = null ;
95100 @ Nullable
96101 private Boolean processImports = null ;
97102 @ Nullable
98103 private Boolean searchEnabled = null ;
104+ @ Nullable
105+ private Set <String > additionalPropertyUris = null ;
106+
107+ @ Override
108+ public LanguageLevel getLanguageLevel () {
109+ Lock lock = rwLock .readLock ();
110+ try {
111+ lock .lock ();
112+ return this .languageLevel != null ? this .languageLevel : nextLanguageLevel ;
113+ } finally {
114+ lock .unlock ();
115+ }
116+ }
117+
118+ @ Override
119+ public void setLanguageLevel ( LanguageLevel languageLevel ) {
120+ this .nextLanguageLevel = languageLevel ;
121+ }
99122
100123 @ Override
101124 public InferenceMode getInferenceMode () {
@@ -145,6 +168,22 @@ public void setSearchEnabled( boolean searchEnabled ) {
145168 this .nextSearchEnabled = searchEnabled ;
146169 }
147170
171+ @ Override
172+ public Set <String > getAdditionalPropertyUris () {
173+ Lock lock = rwLock .readLock ();
174+ try {
175+ lock .lock ();
176+ return additionalPropertyUris != null ? additionalPropertyUris : nextAdditionalPropertyUris ;
177+ } finally {
178+ lock .unlock ();
179+ }
180+ }
181+
182+ @ Override
183+ public void setAdditionalPropertyUris ( Set <String > additionalPropertyUris ) {
184+ this .nextAdditionalPropertyUris = additionalPropertyUris ;
185+ }
186+
148187 public void initialize ( boolean forceLoad , boolean forceIndexing ) {
149188 initialize ( null , forceLoad , forceIndexing );
150189 }
@@ -159,9 +198,13 @@ private void initialize( @Nullable InputStream stream, boolean forceLoad, boolea
159198 return ;
160199 }
161200
201+ // making a copy of all we need
162202 String ontologyUrl = getOntologyUrl ();
163203 String ontologyName = getOntologyName ();
164204 String cacheName = getCacheName ();
205+ Set <Property > additionalProperties = nextAdditionalPropertyUris .stream ()
206+ .map ( ResourceFactory ::createProperty ).collect ( Collectors .toSet () );
207+ LanguageLevel languageLevel = nextLanguageLevel ;
165208 InferenceMode inferenceMode = nextInferenceMode ;
166209 boolean processImports = nextProcessImports ;
167210 boolean searchEnabled = nextSearchEnabled ;
@@ -196,7 +239,7 @@ private void initialize( @Nullable InputStream stream, boolean forceLoad, boolea
196239 return ;
197240
198241 try {
199- OntologyModel m = stream != null ? loadModelFromStream ( stream , processImports , inferenceMode ) : loadModel ( processImports , inferenceMode ); // can take a while.
242+ OntologyModel m = stream != null ? loadModelFromStream ( stream , processImports , languageLevel , inferenceMode ) : loadModel ( processImports , languageLevel , inferenceMode ); // can take a while.
200243 if ( m instanceof OntologyModelImpl ) {
201244 model = ( ( OntologyModelImpl ) m ).getOntModel ();
202245 } else {
@@ -254,9 +297,11 @@ private void initialize( @Nullable InputStream stream, boolean forceLoad, boolea
254297 this .additionalRestrictions = additionalRestrictions ;
255298 this .index = index ;
256299 this .isInitialized = true ;
300+ this .languageLevel = languageLevel ;
257301 this .inferenceMode = inferenceMode ;
258302 this .processImports = processImports ;
259303 this .searchEnabled = searchEnabled ;
304+ this .additionalPropertyUris = additionalProperties .stream ().map ( Property ::getURI ).collect ( Collectors .toSet () );
260305 if ( cacheName != null ) {
261306 // now that the terms have been replaced, we can clear old caches
262307 try {
@@ -615,13 +660,13 @@ public void waitForInitializationThread() throws InterruptedException {
615660 * Delegates the call as to load the model into memory or leave it on disk. Simply delegates to either
616661 * OntologyLoader.loadMemoryModel( url ); OR OntologyLoader.loadPersistentModel( url, spec );
617662 */
618- protected abstract OntologyModel loadModel ( boolean processImports , InferenceMode inferenceMode ) throws IOException ;
663+ protected abstract OntologyModel loadModel ( boolean processImports , LanguageLevel languageLevel , InferenceMode inferenceMode ) throws IOException ;
619664
620665
621666 /**
622667 * Load a model from a given input stream.
623668 */
624- protected abstract OntologyModel loadModelFromStream ( InputStream stream , boolean processImports , InferenceMode inferenceMode ) throws IOException ;
669+ protected abstract OntologyModel loadModelFromStream ( InputStream stream , boolean processImports , LanguageLevel languageLevel , InferenceMode inferenceMode ) throws IOException ;
625670
626671 /**
627672 * A name for caching this ontology, or null to disable caching.
@@ -633,17 +678,6 @@ protected String getCacheName() {
633678 return getOntologyName ();
634679 }
635680
636- private OntModelSpec getSpec ( InferenceMode inferenceMode ) {
637- switch ( inferenceMode ) {
638- case TRANSITIVE :
639- return OntModelSpec .OWL_MEM_TRANS_INF ;
640- case NONE :
641- return OntModelSpec .OWL_MEM ;
642- default :
643- throw new UnsupportedOperationException ( String .format ( "Unsupported inference level %s." , inferenceMode ) );
644- }
645- }
646-
647681 @ Override
648682 public void index ( boolean force ) {
649683 String cacheName = getCacheName ();
@@ -743,7 +777,8 @@ public void loadTermsInNameSpace( InputStream is, boolean forceIndex ) {
743777
744778 @ Override
745779 public String toString () {
746- return String .format ( "%s [%s]" , getOntologyName (), getOntologyUrl () );
780+ return String .format ( "%s [url=%s] [language level=%s] [inference mode=%s] [imports=%b] [search=%b]" ,
781+ getOntologyName (), getOntologyUrl (), getLanguageLevel (), getInferenceMode (), getProcessImports (), isSearchEnabled () );
747782 }
748783
749784 private Set <OntClass > getOntClassesFromTerms ( Collection <OntologyTerm > terms ) {
0 commit comments