Skip to content

Commit

Permalink
Further speedup TIME extension by introducing ImportCache
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesalvo committed Jan 6, 2025
1 parent 64bd6e9 commit 3cf9138
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public async Task InitializeAsync()
{
TestOntology = new OWLOntology(new Uri("ex:ont"));
await TestOntology.InitializeTIMEAsync(30000);

Assert.IsTrue(OWLOntologyHelper.ImportCache.Count > 0);
}
}
#endregion
Expand Down
2 changes: 2 additions & 0 deletions OWLSharp.Test/Extensions/TIME/TIMEHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public async Task InitializeAsync()
TestOntology = new OWLOntology(new Uri("ex:WorldWarIIOntology"));
await TestOntology.InitializeTIMEAsync(30000);

Assert.IsTrue(OWLOntologyHelper.ImportCache.Count > 0);

TestOntology.DeclareEntity(new OWLNamedIndividual(new RDFResource("ex:WorldWarII")));
TestOntology.DeclareEntity(new OWLNamedIndividual(new RDFResource("ex:WorldWarIITemporalDimension")));
}
Expand Down
2 changes: 1 addition & 1 deletion OWLSharp.Test/Ontology/OWLOntologyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5515,7 +5515,7 @@ public async Task ShouldResolveImportsAsync()
public async Task ShouldThrowExceptionOnImportingOntologyAsync()
{
OWLOntology ontology = new OWLOntology(new Uri("ex:ont"));
await Assert.ThrowsExceptionAsync<OWLException>(async () => await ontology.ImportAsync(new Uri(RDFVocabulary.SKOS.DEREFERENCE_URI), 10));
await Assert.ThrowsExceptionAsync<OWLException>(async () => await ontology.ImportAsync(new Uri("ex:ont"), 5));
}
#endregion

Expand Down
35 changes: 35 additions & 0 deletions OWLSharp/Ontology/Helpers/OWLOntologyHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2014-2025 Marco De Salvo
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System.Collections.Generic;

namespace OWLSharp.Ontology
{
public static class OWLOntologyHelper
{
#region Properties
internal static Dictionary<string, OWLOntology> ImportCache { get; set; }
#endregion

#region Ctors
static OWLOntologyHelper()
{
if (ImportCache == null)
ImportCache = new Dictionary<string, OWLOntology>();
}
#endregion
}
}
12 changes: 10 additions & 2 deletions OWLSharp/Ontology/OWLOntology.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2821,8 +2821,16 @@ internal Task ImportAsync(Uri ontologyIRI, int timeoutMilliseconds, bool shouldC

try
{
RDFAsyncGraph importedGraph = await RDFAsyncGraph.FromUriAsync(ontologyIRI, timeoutMilliseconds);
OWLOntology importedOntology = await FromRDFGraphAsync(importedGraph.WrappedGraph);
#region ImportCache
string ontologyIRIString = ontologyIRI.ToString();
if (!OWLOntologyHelper.ImportCache.ContainsKey(ontologyIRIString))
{
RDFAsyncGraph importGraph = await RDFAsyncGraph.FromUriAsync(ontologyIRI, timeoutMilliseconds, true);
OWLOntology importOntology = await FromRDFGraphAsync(importGraph.WrappedGraph);
OWLOntologyHelper.ImportCache.Add(ontologyIRIString, importOntology);
}
OWLOntology importedOntology = OWLOntologyHelper.ImportCache[ontologyIRIString];
#endregion

//Imports
if (shouldCollectImport)
Expand Down

0 comments on commit 3cf9138

Please sign in to comment.