Skip to content

Commit f7f90d1

Browse files
committed
Remove drafts
1 parent e916cfd commit f7f90d1

File tree

1 file changed

+43
-80
lines changed

1 file changed

+43
-80
lines changed

packages/graph-explorer/src/connector/sparql/fetchNeighbors/mapToResults.ts

Lines changed: 43 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
toMappedQueryResults,
44
} from "@/connector/useGEFetchTypes";
55
import { RawValue, rdfTypeUri } from "../types";
6-
import { createEdge, createVertex } from "@/core";
6+
import { createEdge, createVertex, Vertex } from "@/core";
77
import { createRdfEdgeId } from "../createRdfEdgeId";
88

99
export type RawOneHopNeighborsResponse = {
@@ -17,81 +17,46 @@ export type RawOneHopNeighborsResponse = {
1717
};
1818
type Bindings = RawOneHopNeighborsResponse["results"]["bindings"];
1919

20-
type DraftRdfNode = {
21-
uri: string;
22-
classes?: string[];
23-
isBlankNode?: boolean;
24-
attributes?: { predicate: string; value: string | number }[];
25-
};
26-
27-
type DraftRdfEdge = {
28-
uri: string;
29-
source?: string;
30-
sourceClasses?: string[];
31-
target?: string;
32-
targetClasses?: string[];
33-
};
34-
3520
export function mapToResults(bindings: Bindings): MappedQueryResults {
3621
// Get types map
3722
const typesMap = getTypesMap(bindings);
3823

3924
// Get node related triples
40-
const drafts = bindings
25+
const vertices = bindings
4126
.values()
27+
// Filter out type triples
4228
.filter(triple => triple.p.value !== rdfTypeUri)
43-
.reduce((draftsMap, current) => {
44-
const updated = new Map(draftsMap);
29+
// Filter out non-literal values
30+
.filter(triple => triple.value.type === "literal")
31+
.reduce((vertices, current) => {
32+
// Get the types for the current node
4533
const uri = current.subject.value;
4634
const classes = typesMap.get(uri);
47-
const isBlankNode = current.subject.type === "bnode";
48-
const attribute =
49-
current.value.type === "literal"
50-
? {
51-
predicate: current.p.value,
52-
value:
53-
current.value.datatype ===
54-
"http://www.w3.org/2001/XMLSchema#integer"
55-
? Number(current.value.value)
56-
: current.value.value,
57-
}
58-
: null;
5935

60-
const existingDraft = updated.get(uri);
36+
if (!classes?.length) {
37+
// Nodes are only valid if they include at least one class
38+
return vertices;
39+
}
6140

62-
const draft: DraftRdfNode = {
63-
...existingDraft,
64-
uri,
65-
classes,
66-
isBlankNode,
67-
attributes: [
68-
...(existingDraft?.attributes ?? []),
69-
...(attribute ? [attribute] : []),
70-
],
71-
};
41+
// Get existing vertex or create a new one
42+
const vertex =
43+
vertices.get(uri) ??
44+
createVertex({
45+
id: uri,
46+
types: classes,
47+
isBlankNode: current.subject.type === "bnode",
48+
attributes: {},
49+
});
7250

73-
updated.set(uri, draft);
74-
return updated;
75-
}, new Map<string, DraftRdfNode>());
51+
// Add the attribute to the vertex
52+
vertex.attributes[current.p.value] =
53+
current.value.datatype === "http://www.w3.org/2001/XMLSchema#integer"
54+
? Number(current.value.value)
55+
: current.value.value;
7656

77-
const vertices = drafts
57+
return vertices.set(uri, vertex);
58+
}, new Map<string, Vertex>())
7859
.values()
79-
.map(draft => {
80-
if (!draft.classes?.length) {
81-
return null;
82-
}
83-
const attributes: Record<string, string | number> = {};
84-
for (const attribute of draft.attributes ?? []) {
85-
attributes[attribute.predicate] = attribute.value;
86-
}
87-
return createVertex({
88-
id: draft.uri,
89-
types: draft.classes,
90-
isBlankNode: draft.isBlankNode,
91-
attributes: attributes,
92-
});
93-
})
94-
.filter(raw => raw != null)
9560
.toArray();
9661

9762
// Get edge related triples
@@ -103,31 +68,29 @@ export function mapToResults(bindings: Bindings): MappedQueryResults {
10368
(triple.subject.type === "uri" || triple.subject.type === "bnode") &&
10469
(triple.value.type === "uri" || triple.value.type === "bnode")
10570
)
106-
.map(
107-
triple =>
108-
({
109-
uri: triple.p.value,
110-
source: triple.subject.value,
111-
target: triple.value.value,
112-
sourceClasses: typesMap.get(triple.subject.value),
113-
targetClasses: typesMap.get(triple.value.value),
114-
}) satisfies DraftRdfEdge
115-
)
116-
.map(draft => {
117-
if (!draft.sourceClasses?.length || !draft.targetClasses?.length) {
71+
.map(triple => {
72+
const sourceClasses = typesMap.get(triple.subject.value);
73+
const targetClasses = typesMap.get(triple.value.value);
74+
75+
if (!sourceClasses?.length || !targetClasses?.length) {
11876
return null;
11977
}
78+
79+
const source = triple.subject.value;
80+
const target = triple.value.value;
81+
const uri = triple.p.value;
82+
12083
return createEdge({
121-
id: createRdfEdgeId(draft.source, draft.uri, draft.target),
84+
id: createRdfEdgeId(source, uri, target),
12285
source: createVertex({
123-
id: draft.source,
124-
types: draft.sourceClasses,
86+
id: source,
87+
types: sourceClasses,
12588
}),
12689
target: createVertex({
127-
id: draft.target,
128-
types: draft.targetClasses,
90+
id: target,
91+
types: targetClasses,
12992
}),
130-
type: draft.uri,
93+
type: uri,
13194
attributes: {},
13295
});
13396
})

0 commit comments

Comments
 (0)