-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathQueryDisplay.cs
77 lines (69 loc) · 2.32 KB
/
QueryDisplay.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.Linq;
using System.Text;
using Org.Vitrivr.CineastApi.Model;
using Vitrivr.UnityInterface.CineastApi.Model.Data;
using UnityEngine;
using Vitrivr.UnityInterface.CineastApi;
using Vitrivr.UnityInterface.CineastApi.Utils;
namespace VitrivrVR.Query.Display
{
/// <summary>
/// Abstract class for displaying queries.
/// </summary>
public abstract class QueryDisplay : MonoBehaviour
{
public virtual int NumberOfResults => -1;
public QueryResponse QueryData { get; private set; }
public CineastClient QueryClient { get; private set; }
public void Initialize(QueryResponse queryResult, CineastClient queryClient)
{
QueryData = queryResult;
QueryClient = queryClient;
Initialize();
}
/// <summary>
/// Returns a string representation of the query that resulted in this query display.
/// </summary>
/// <returns>String representation of the associated query.</returns>
public virtual string GetQueryStringRepresentation()
{
var stringBuilder = new StringBuilder();
stringBuilder.Append("{");
if (QueryData.Query != null)
{
stringBuilder.Append(string.Join(", ", QueryData.Query.Terms.Select(TermToString)));
}
else if (QueryData.StagedQuery != null)
{
stringBuilder.Append("{");
stringBuilder.Append(string.Join("}, {",
QueryData.StagedQuery.Stages.Select(stage => string.Join(", ", stage.Terms.Select(TermToString)))));
stringBuilder.Append("}");
}
stringBuilder.Append("}");
return stringBuilder.ToString();
}
protected abstract void Initialize();
/// <summary>
/// Turns the given <see cref="QueryTerm"/> into a string representation.
/// </summary>
protected static string TermToString(QueryTerm term)
{
var categories = string.Join(", ", term.Categories);
var baseString = $"{term.Type} ({categories})";
switch (term.Type)
{
case QueryTerm.TypeEnum.IMAGE:
return baseString;
case QueryTerm.TypeEnum.BOOLEAN:
case QueryTerm.TypeEnum.TAG:
{
var data = Base64Converter.StringFromBase64(term.Data[Base64Converter.JsonPrefix.Length..]);
return $"{baseString}: {data}";
}
default:
return $"{baseString}: {term.Data}";
}
}
}
}