55import java .util .ArrayList ;
66import java .util .List ;
77import java .util .concurrent .Callable ;
8+ import java .util .function .Function ;
9+ import java .util .stream .Stream ;
810
911import org .aksw .commons .io .util .StdIo ;
1012import org .aksw .jena_sparql_api .rx .script .SparqlScriptProcessor ;
1113import org .aksw .jenax .dataaccess .sparql .datasource .RDFDataSource ;
1214import org .aksw .jenax .dataaccess .sparql .factory .datasource .RDFDataSources ;
1315import org .aksw .jenax .graphql .schema .generator .GraphQlSchemaGenerator ;
1416import org .aksw .jenax .graphql .schema .generator .GraphQlSchemaGenerator .TypeInfo ;
17+ import org .aksw .jenax .graphql .util .GraphQlUtils ;
1518import org .aksw .jenax .stmt .core .SparqlStmt ;
1619import org .aksw .jenax .stmt .util .SparqlStmtUtils ;
1720import 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 ;
1825import org .apache .jena .query .Dataset ;
1926import org .apache .jena .query .DatasetFactory ;
2027import org .apache .jena .rdfconnection .RDFConnection ;
28+ import org .apache .jena .riot .RDFDataMgr ;
29+ import org .apache .jena .vocabulary .RDFS ;
2130
2231import graphql .language .AstPrinter ;
2332import graphql .language .Document ;
24- import graphql .parser .Parser ;
2533import picocli .CommandLine .Command ;
2634import picocli .CommandLine .Mixin ;
2735import picocli .CommandLine .Option ;
@@ -37,11 +45,18 @@ public class CmdGraphQlSchemaGen
3745 @ Mixin
3846 public CmdMixinSparqlDataset sparqlDataset = new CmdMixinSparqlDataset ();
3947
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+
4051 @ Parameters (arity = "0..*" , description = "Input files" )
4152 public List <String > nonOptionArgs = new ArrayList <>();
4253
4354 @ Override
4455 public Integer call () throws Exception {
56+ Graph labelGraph = labelSource == null
57+ ? null
58+ : RDFDataMgr .loadGraph (labelSource );
59+
4560 SparqlScriptProcessor processor = SparqlScriptProcessor .createWithEnvSubstitution (null );
4661 processor .process (nonOptionArgs );
4762
@@ -55,7 +70,19 @@ public Integer call() throws Exception {
5570 RDFDataSource dataSource = RDFDataSources .of (dataset );
5671 List <TypeInfo > types = GraphQlSchemaGenerator .summarize (dataSource );
5772
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 );
5986 Document doc = generator .process (types );
6087 String str = AstPrinter .printAst (doc );
6188
@@ -65,8 +92,8 @@ public Integer call() throws Exception {
6592
6693 boolean validateOutput = true ;
6794 if (validateOutput ) {
68- Parser parser = new Parser ();
69- parser . parse (str );
95+ @ SuppressWarnings ( "unused" )
96+ Document reparsedDoc = GraphQlUtils . parseUnrestricted (str );
7097 }
7198
7299 return 0 ;
0 commit comments