5
5
import java .util .ArrayList ;
6
6
import java .util .List ;
7
7
import java .util .concurrent .Callable ;
8
+ import java .util .function .Function ;
9
+ import java .util .stream .Stream ;
8
10
9
11
import org .aksw .commons .io .util .StdIo ;
10
12
import org .aksw .jena_sparql_api .rx .script .SparqlScriptProcessor ;
11
13
import org .aksw .jenax .dataaccess .sparql .datasource .RDFDataSource ;
12
14
import org .aksw .jenax .dataaccess .sparql .factory .datasource .RDFDataSources ;
13
15
import org .aksw .jenax .graphql .schema .generator .GraphQlSchemaGenerator ;
14
16
import org .aksw .jenax .graphql .schema .generator .GraphQlSchemaGenerator .TypeInfo ;
17
+ import org .aksw .jenax .graphql .util .GraphQlUtils ;
15
18
import org .aksw .jenax .stmt .core .SparqlStmt ;
16
19
import org .aksw .jenax .stmt .util .SparqlStmtUtils ;
17
20
import org .aksw .rdf_processing_toolkit .cli .cmd .CmdMixinSparqlDataset ;
21
+ import org .apache .jena .graph .Graph ;
22
+ import org .apache .jena .graph .Node ;
23
+ import org .apache .jena .graph .NodeFactory ;
24
+ import org .apache .jena .graph .Triple ;
18
25
import org .apache .jena .query .Dataset ;
19
26
import org .apache .jena .query .DatasetFactory ;
20
27
import org .apache .jena .rdfconnection .RDFConnection ;
28
+ import org .apache .jena .riot .RDFDataMgr ;
29
+ import org .apache .jena .vocabulary .RDFS ;
21
30
22
31
import graphql .language .AstPrinter ;
23
32
import graphql .language .Document ;
24
- import graphql .parser .Parser ;
25
33
import picocli .CommandLine .Command ;
26
34
import picocli .CommandLine .Mixin ;
27
35
import picocli .CommandLine .Option ;
@@ -37,11 +45,18 @@ public class CmdGraphQlSchemaGen
37
45
@ Mixin
38
46
public CmdMixinSparqlDataset sparqlDataset = new CmdMixinSparqlDataset ();
39
47
48
+ @ Option (names = { "-l" , "--label-source" }, description = "An RDF dataset with labels for the classes and properties. Local names will be used as fallback." )
49
+ public String labelSource ;
50
+
40
51
@ Parameters (arity = "0..*" , description = "Input files" )
41
52
public List <String > nonOptionArgs = new ArrayList <>();
42
53
43
54
@ Override
44
55
public Integer call () throws Exception {
56
+ Graph labelGraph = labelSource == null
57
+ ? null
58
+ : RDFDataMgr .loadGraph (labelSource );
59
+
45
60
SparqlScriptProcessor processor = SparqlScriptProcessor .createWithEnvSubstitution (null );
46
61
processor .process (nonOptionArgs );
47
62
@@ -55,7 +70,19 @@ public Integer call() throws Exception {
55
70
RDFDataSource dataSource = RDFDataSources .of (dataset );
56
71
List <TypeInfo > types = GraphQlSchemaGenerator .summarize (dataSource );
57
72
58
- GraphQlSchemaGenerator generator = new GraphQlSchemaGenerator ();
73
+ Function <String , String > iriToLabel = labelGraph == null
74
+ ? null
75
+ : iriStr -> {
76
+ try (Stream <String > stream = labelGraph .stream (
77
+ NodeFactory .createURI (iriStr ), RDFS .label .asNode (), Node .ANY )
78
+ .map (Triple ::getObject )
79
+ .filter (Node ::isLiteral )
80
+ .map (Node ::getLiteralLexicalForm )) {
81
+ return stream .findFirst ().orElse (null );
82
+ }
83
+ };
84
+
85
+ GraphQlSchemaGenerator generator = new GraphQlSchemaGenerator (iriToLabel );
59
86
Document doc = generator .process (types );
60
87
String str = AstPrinter .printAst (doc );
61
88
@@ -65,8 +92,8 @@ public Integer call() throws Exception {
65
92
66
93
boolean validateOutput = true ;
67
94
if (validateOutput ) {
68
- Parser parser = new Parser ();
69
- parser . parse (str );
95
+ @ SuppressWarnings ( "unused" )
96
+ Document reparsedDoc = GraphQlUtils . parseUnrestricted (str );
70
97
}
71
98
72
99
return 0 ;
0 commit comments