1
- import groupBy from "lodash/groupBy" ;
2
- import { Edge , Vertex , VertexId } from "@/core" ;
3
1
import {
4
2
toMappedQueryResults ,
5
3
type NeighborsResponse ,
6
4
} from "@/connector/useGEFetchTypes" ;
7
- import mapIncomingToEdge , {
8
- IncomingPredicate ,
9
- isIncomingPredicate ,
10
- } from "../mappers/mapIncomingToEdge" ;
11
- import mapOutgoingToEdge , {
12
- OutgoingPredicate ,
13
- } from "../mappers/mapOutgoingToEdge" ;
14
- import mapRawResultToVertex from "../mappers/mapRawResultToVertex" ;
15
- import oneHopNeighborsTemplate from "./oneHopNeighborsTemplate" ;
16
- import subjectPredicatesTemplate from "./subjectPredicatesTemplate" ;
17
- import {
18
- RawResult ,
19
- RawValue ,
20
- SparqlFetch ,
21
- SPARQLNeighborsRequest ,
22
- } from "../types" ;
23
- import { logger } from "@/utils" ;
24
-
25
- type RawOneHopNeighborsResponse = {
26
- results : {
27
- bindings : Array < {
28
- subject : RawValue ;
29
- pred : RawValue ;
30
- value : RawValue ;
31
- subjectClass : RawValue ;
32
- pToSubject ?: RawValue ;
33
- pFromSubject ?: RawValue ;
34
- } > ;
35
- } ;
36
- } ;
37
-
38
- type RawNeighborsPredicatesResponse = {
39
- results : {
40
- bindings : Array < OutgoingPredicate | IncomingPredicate > ;
41
- } ;
42
- } ;
43
-
44
- const isBlank = ( result : RawValue ) => {
45
- return result . type === "bnode" ;
46
- } ;
5
+ import { oneHopNeighborsTemplate } from "./oneHopNeighborsTemplate" ;
47
6
48
- const fetchOneHopNeighbors = async (
49
- sparqlFetch : SparqlFetch ,
50
- req : SPARQLNeighborsRequest
51
- ) => {
52
- const oneHopTemplate = oneHopNeighborsTemplate ( req ) ;
53
- logger . log ( "[SPARQL Explorer] Fetching oneHopNeighbors..." , req ) ;
54
- const data = await sparqlFetch < RawOneHopNeighborsResponse > ( oneHopTemplate ) ;
55
-
56
- const groupBySubject = groupBy (
57
- data . results . bindings ,
58
- result => result . subject . value
59
- ) ;
60
-
61
- const mappedResults : Record < string , RawResult > = { } ;
62
- const bNodesEdges : Edge [ ] = [ ] ;
63
-
64
- Object . entries ( groupBySubject ) . forEach ( ( [ uri , result ] ) => {
65
- // Create outgoing predicates to blank nodes
66
- if ( isBlank ( result [ 0 ] . subject ) && result [ 0 ] . pToSubject ) {
67
- const edge = mapOutgoingToEdge ( req . resourceURI , req . resourceClasses , {
68
- subject : result [ 0 ] . subject ,
69
- subjectClass : result [ 0 ] . subjectClass ,
70
- predToSubject : result [ 0 ] . pToSubject ,
71
- } ) ;
72
- bNodesEdges . push ( edge ) ;
73
- }
74
-
75
- // Create incoming predicates from blank nodes
76
- if ( isBlank ( result [ 0 ] . subject ) && result [ 0 ] . pFromSubject ) {
77
- const edge = mapIncomingToEdge ( req . resourceURI , req . resourceClasses , {
78
- subject : result [ 0 ] . subject ,
79
- subjectClass : result [ 0 ] . subjectClass ,
80
- predFromSubject : result [ 0 ] . pFromSubject ,
81
- } ) ;
82
- bNodesEdges . push ( edge ) ;
83
- }
84
-
85
- mappedResults [ uri ] = {
86
- uri : uri ,
87
- class : result [ 0 ] . subjectClass . value ,
88
- isBlank : isBlank ( result [ 0 ] . subject ) ,
89
- attributes : { } ,
90
- } ;
91
-
92
- result . forEach ( attr => {
93
- mappedResults [ uri ] . attributes [ attr . pred . value ] = attr . value . value ;
94
- } ) ;
95
- } ) ;
96
-
97
- const vertices = Object . values ( mappedResults ) . map ( result => {
98
- return mapRawResultToVertex ( result ) ;
99
- } ) ;
100
-
101
- return {
102
- vertices,
103
- bNodesEdges,
104
- } ;
105
- } ;
106
-
107
- export const fetchNeighborsPredicates = async (
108
- sparqlFetch : SparqlFetch ,
109
- resourceURI : VertexId ,
110
- resourceClasses : Vertex [ "types" ] ,
111
- subjectURIs : VertexId [ ]
112
- ) => {
113
- const template = subjectPredicatesTemplate ( {
114
- resourceURI,
115
- subjectURIs,
116
- } ) ;
117
-
118
- logger . log ( "[SPARQL Explorer] Fetching neighbor predicates..." , {
119
- resourceURI,
120
- resourceClasses,
121
- subjectURIs,
122
- } ) ;
123
- const response = await sparqlFetch < RawNeighborsPredicatesResponse > ( template ) ;
124
- return response . results . bindings . map ( result => {
125
- if ( isIncomingPredicate ( result ) ) {
126
- return mapIncomingToEdge ( resourceURI , resourceClasses , result ) ;
127
- }
128
-
129
- return mapOutgoingToEdge ( resourceURI , resourceClasses , result ) ;
130
- } ) ;
131
- } ;
7
+ import { SparqlFetch , SPARQLNeighborsRequest } from "../types" ;
8
+ import { logger } from "@/utils" ;
9
+ import { mapToResults , RawOneHopNeighborsResponse } from "./mapToResults" ;
132
10
133
11
/**
134
12
* Given a subject URI, it returns a set of subjects (with their properties)
@@ -144,26 +22,20 @@ export const fetchNeighborsPredicates = async (
144
22
*
145
23
* It does not return neighbors counts.
146
24
*/
147
- const fetchNeighbors = async (
25
+ export default async function fetchNeighbors (
148
26
sparqlFetch : SparqlFetch ,
149
27
req : SPARQLNeighborsRequest
150
- ) : Promise < NeighborsResponse > => {
151
- const { vertices, bNodesEdges } = await fetchOneHopNeighbors (
152
- sparqlFetch ,
153
- req
154
- ) ;
155
- const subjectsURIs = vertices . map ( v => v . id ) ;
156
- const edges = await fetchNeighborsPredicates (
157
- sparqlFetch ,
158
- req . resourceURI ,
159
- req . resourceClasses ,
160
- subjectsURIs
161
- ) ;
28
+ ) : Promise < NeighborsResponse > {
29
+ const oneHopTemplate = oneHopNeighborsTemplate ( req ) ;
30
+ logger . log ( "[SPARQL Explorer] Fetching oneHopNeighbors..." , req ) ;
31
+ const data = await sparqlFetch < RawOneHopNeighborsResponse > ( oneHopTemplate ) ;
32
+ logger . log ( "[SPARQL Explorer] Fetched oneHopNeighbors" , data ) ;
33
+
34
+ const results = mapToResults ( data . results . bindings ) ;
162
35
163
36
return toMappedQueryResults ( {
164
- vertices,
165
- edges : [ ...edges , ...bNodesEdges ] ,
37
+ // Filter out the source vertex since it is already in the graph and this one is missing the attributes
38
+ vertices : results . vertices . filter ( v => v . id !== req . resourceURI ) ,
39
+ edges : results . edges ,
166
40
} ) ;
167
- } ;
168
-
169
- export default fetchNeighbors ;
41
+ }
0 commit comments