-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathSemanticTests.cs
More file actions
106 lines (88 loc) · 2.56 KB
/
SemanticTests.cs
File metadata and controls
106 lines (88 loc) · 2.56 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text;
using Waher.Content.Semantic.Model;
using Waher.Runtime.Collections;
namespace Waher.Content.Semantic.Test
{
public abstract class SemanticTests
{
protected static void CompareTriples(ISemanticModel Result, ISemanticModel Expected)
{
Dictionary<string, string> NodeIdMap = [];
ChunkedList<ISemanticTriple> Triples = [.. Result];
ChunkedList<ISemanticTriple> NotFound = [];
int i, c = Triples.Count;
foreach (ISemanticTriple T in Expected)
{
bool Match = false;
for (i = 0; i < c; i++)
{
ISemanticTriple T2 = Triples[i];
if (Matches(Triples[i], T, NodeIdMap, false))
{
Assert.IsTrue(Matches(Triples[i], T, NodeIdMap, true), "Blank node inconsistency.");
Match = true;
Triples.RemoveAt(i);
c--;
break;
}
}
if (!Match)
NotFound.Add(T);
}
if (NotFound.Count > 0 || Triples.Count > 0)
{
StringBuilder sb = new();
sb.AppendLine("Unexpected result.");
if (NotFound.Count > 0)
{
sb.AppendLine();
sb.AppendLine("Expected Triples not found in result: ");
sb.AppendLine("=======================================");
foreach (ISemanticTriple T in NotFound)
{
sb.Append(T.Subject.ToString());
sb.Append('\t');
sb.Append(T.Predicate.ToString());
sb.Append('\t');
sb.AppendLine(T.Object.ToString());
}
}
if (Triples.Count > 0)
{
sb.AppendLine();
sb.AppendLine("Generated Triples not expected: ");
sb.AppendLine("==================================");
foreach (ISemanticTriple T in Triples)
{
sb.Append(T.Subject.ToString());
sb.Append('\t');
sb.Append(T.Predicate.ToString());
sb.Append('\t');
sb.AppendLine(T.Object.ToString());
}
}
Assert.Fail(sb.ToString());
}
}
private static bool Matches(ISemanticTriple T1, ISemanticTriple T2, Dictionary<string, string> NodeIdMap, bool AddToMap)
{
return Matches(T1.Subject, T2.Subject, NodeIdMap, AddToMap) &&
Matches(T1.Predicate, T2.Predicate, NodeIdMap, AddToMap) &&
Matches(T1.Object, T2.Object, NodeIdMap, AddToMap);
}
private static bool Matches(ISemanticElement E1, ISemanticElement E2, Dictionary<string, string> NodeIdMap, bool AddToMap)
{
if (E1 is BlankNode B1 && E2 is BlankNode B2)
{
if (NodeIdMap.TryGetValue(B1.NodeId, out string? NodeId))
return B2.NodeId == NodeId;
if (AddToMap)
NodeIdMap[B1.NodeId] = B2.NodeId;
return true;
}
else
return E1.Equals(E2);
}
}
}