diff --git a/Runtime/Vitrivr/UnityInterface/CineastApi/CineastWrapper.cs b/Runtime/Vitrivr/UnityInterface/CineastApi/CineastWrapper.cs index e169dfb..6a8a25c 100644 --- a/Runtime/Vitrivr/UnityInterface/CineastApi/CineastWrapper.cs +++ b/Runtime/Vitrivr/UnityInterface/CineastApi/CineastWrapper.cs @@ -37,6 +37,7 @@ public static async Task ExecuteQuery(SimilarityQuery query, int { var queryResults = await SegmentsApi.FindSegmentSimilarAsync(query); + // TODO: max results should be specified in the respective query config var querySegments = ResultUtils.ToSegmentData(queryResults, maxResults); var queryData = new QueryResponse(query, querySegments); @@ -48,6 +49,41 @@ public static async Task ExecuteQuery(SimilarityQuery query, int return queryData; } + /// + /// Executes a staged query. + /// + public static async Task ExecuteQuery(StagedSimilarityQuery query, int maxResults, int prefetch) + { + var queryResults = await SegmentsApi.FindSegmentSimilarStagedAsync(query); + + // TODO: max results should be specified in the respective query config + var querySegments = ResultUtils.ToSegmentData(queryResults, maxResults); + + var queryData = new QueryResponse(query, querySegments); + if (prefetch > 0) + { + await queryData.Prefetch(prefetch); + } + + return queryData; + } + + /// + /// Executes a temporal query. + /// + public static async Task ExecuteQuery(TemporalQuery query, int prefetch) + { + var queryResults = await SegmentsApi.FindSegmentSimilarTemporalAsync(query); + + var queryData = new TemporalQueryResponse(query, queryResults); + if (prefetch > 0) + { + await queryData.Prefetch(prefetch); + } + + return queryData; + } + /// /// Retrieves the unordered list of tags with names matching get given name. /// @@ -81,7 +117,7 @@ public async Task RequestThreaded(SimilarityQuery qu /// List of distinct values occuring in the specified column of the specified table public static async Task> GetDistinctTableValues(string table, string column) { - var columnSpec = new ColumnSpecification(column, table); + var columnSpec = new ColumnSpecification(column, table); var results = await Task.Run(() => MiscApi.FindDistinctElementsByColumn(columnSpec)); return results.DistinctElements; } diff --git a/Runtime/Vitrivr/UnityInterface/CineastApi/Model/Data/QueryResponse.cs b/Runtime/Vitrivr/UnityInterface/CineastApi/Model/Data/QueryResponse.cs index dbc905b..d28a58c 100644 --- a/Runtime/Vitrivr/UnityInterface/CineastApi/Model/Data/QueryResponse.cs +++ b/Runtime/Vitrivr/UnityInterface/CineastApi/Model/Data/QueryResponse.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using JetBrains.Annotations; using Org.Vitrivr.CineastApi.Model; using Vitrivr.UnityInterface.CineastApi.Model.Registries; using Vitrivr.UnityInterface.CineastApi.Utils; @@ -17,20 +18,28 @@ public class QueryResponse /// /// The executed query leading to the stored results. /// - public readonly SimilarityQuery query; + [CanBeNull] public readonly SimilarityQuery Query; + + [CanBeNull] public readonly StagedSimilarityQuery StagedQuery; /// /// Dictionary of result lists by result category. /// - public readonly Dictionary> results; + public readonly Dictionary> Results; /// Query to store data for /// Dictionary of results by result category public QueryResponse(SimilarityQuery query, Dictionary> results) { - this.query = query; - this.results = results; + Query = query; + Results = results; + } + + public QueryResponse(StagedSimilarityQuery query, Dictionary> results) + { + StagedQuery = query; + Results = results; } /// @@ -41,7 +50,7 @@ public async Task Prefetch(int number) { // TODO: Prevent more than the number of segments to be prefetched in total var segmentSet = new HashSet(); - foreach (var segmentList in results.Values) + foreach (var segmentList in Results.Values) { segmentList.Take(number).Select(item => item.segment).ToList().ForEach(segment => segmentSet.Add(segment)); } @@ -51,7 +60,7 @@ public async Task Prefetch(int number) public List GetMeanFusionResults() { - return ResultUtils.MeanScoreFusion(results); + return ResultUtils.MeanScoreFusion(Results); } } } \ No newline at end of file diff --git a/Runtime/Vitrivr/UnityInterface/CineastApi/Model/Data/TemporalQueryResponse.cs b/Runtime/Vitrivr/UnityInterface/CineastApi/Model/Data/TemporalQueryResponse.cs new file mode 100644 index 0000000..ebd9411 --- /dev/null +++ b/Runtime/Vitrivr/UnityInterface/CineastApi/Model/Data/TemporalQueryResponse.cs @@ -0,0 +1,29 @@ +using System.Linq; +using System.Threading.Tasks; +using Org.Vitrivr.CineastApi.Model; +using Vitrivr.UnityInterface.CineastApi.Model.Registries; + +namespace Vitrivr.UnityInterface.CineastApi.Model.Data +{ + public class TemporalQueryResponse + { + public readonly TemporalQuery Query; + public readonly TemporalQueryResult Results; + + public TemporalQueryResponse(TemporalQuery query, TemporalQueryResult results) + { + Query = query; + Results = results; + } + + public async Task Prefetch(int number) + { + var segmentSet = Results.Content.Take(number) + .SelectMany(result => result.Segments + .Select(SegmentRegistry.GetSegment) + ).ToHashSet(); + + await SegmentRegistry.BatchFetchSegmentData(segmentSet.ToList()); + } + } +} \ No newline at end of file diff --git a/Runtime/Vitrivr/UnityInterface/CineastApi/Model/Data/TemporalQueryResponse.cs.meta b/Runtime/Vitrivr/UnityInterface/CineastApi/Model/Data/TemporalQueryResponse.cs.meta new file mode 100644 index 0000000..2b19ba2 --- /dev/null +++ b/Runtime/Vitrivr/UnityInterface/CineastApi/Model/Data/TemporalQueryResponse.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 63969b7631b341858e4f26a146da7c92 +timeCreated: 1652957749 \ No newline at end of file diff --git a/Runtime/Vitrivr/UnityInterface/CineastApi/Utils/QueryBuilder.cs b/Runtime/Vitrivr/UnityInterface/CineastApi/Utils/QueryBuilder.cs index a7cfd77..8516f4f 100644 --- a/Runtime/Vitrivr/UnityInterface/CineastApi/Utils/QueryBuilder.cs +++ b/Runtime/Vitrivr/UnityInterface/CineastApi/Utils/QueryBuilder.cs @@ -5,17 +5,36 @@ namespace Vitrivr.UnityInterface.CineastApi.Utils { - public class QueryBuilder + public static class QueryBuilder { /// /// Generic similarity query for given terms /// - /// + /// Terms to use for query. public static SimilarityQuery BuildSimilarityQuery(params QueryTerm[] terms) { return new SimilarityQuery(terms.ToList()); } + /// + /// Staged similarity query for the given stages. + /// + /// Enumerable of stages, each containing the respective s. + public static StagedSimilarityQuery BuildStagedQuery(IEnumerable> stages) + { + return new StagedSimilarityQuery(stages.Select(terms => new QueryStage(terms)).ToList()); + } + + /// + /// Temporal similarity query for the given staged temporal contexts. + /// + /// Enumerable of temporally ordered enumerables containing stages, containing s. + /// + public static TemporalQuery BuildTemporalQuery(IEnumerable>> temporalContexts) + { + return new TemporalQuery(temporalContexts.Select(BuildStagedQuery).ToList()); + } + /// /// Convenience method to create spatial similarity query ///