3
3
toMappedQueryResults ,
4
4
} from "@/connector/useGEFetchTypes" ;
5
5
import { RawValue , rdfTypeUri } from "../types" ;
6
- import { createEdge , createVertex } from "@/core" ;
6
+ import { createEdge , createVertex , Vertex } from "@/core" ;
7
7
import { createRdfEdgeId } from "../createRdfEdgeId" ;
8
8
9
9
export type RawOneHopNeighborsResponse = {
@@ -17,81 +17,46 @@ export type RawOneHopNeighborsResponse = {
17
17
} ;
18
18
type Bindings = RawOneHopNeighborsResponse [ "results" ] [ "bindings" ] ;
19
19
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
-
35
20
export function mapToResults ( bindings : Bindings ) : MappedQueryResults {
36
21
// Get types map
37
22
const typesMap = getTypesMap ( bindings ) ;
38
23
39
24
// Get node related triples
40
- const drafts = bindings
25
+ const vertices = bindings
41
26
. values ( )
27
+ // Filter out type triples
42
28
. 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
45
33
const uri = current . subject . value ;
46
34
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 ;
59
35
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
+ }
61
40
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
+ } ) ;
72
50
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 ;
76
56
77
- const vertices = drafts
57
+ return vertices . set ( uri , vertex ) ;
58
+ } , new Map < string , Vertex > ( ) )
78
59
. 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 )
95
60
. toArray ( ) ;
96
61
97
62
// Get edge related triples
@@ -103,31 +68,29 @@ export function mapToResults(bindings: Bindings): MappedQueryResults {
103
68
( triple . subject . type === "uri" || triple . subject . type === "bnode" ) &&
104
69
( triple . value . type === "uri" || triple . value . type === "bnode" )
105
70
)
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 ) {
118
76
return null ;
119
77
}
78
+
79
+ const source = triple . subject . value ;
80
+ const target = triple . value . value ;
81
+ const uri = triple . p . value ;
82
+
120
83
return createEdge ( {
121
- id : createRdfEdgeId ( draft . source , draft . uri , draft . target ) ,
84
+ id : createRdfEdgeId ( source , uri , target ) ,
122
85
source : createVertex ( {
123
- id : draft . source ,
124
- types : draft . sourceClasses ,
86
+ id : source ,
87
+ types : sourceClasses ,
125
88
} ) ,
126
89
target : createVertex ( {
127
- id : draft . target ,
128
- types : draft . targetClasses ,
90
+ id : target ,
91
+ types : targetClasses ,
129
92
} ) ,
130
- type : draft . uri ,
93
+ type : uri ,
131
94
attributes : { } ,
132
95
} ) ;
133
96
} )
0 commit comments