Skip to content

Commit

Permalink
Merge branch 'master' of github.build.ge.com:semtk/semtk-opensource
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Cuddihy committed Nov 8, 2022
2 parents 95d2c1a + 4f29f94 commit 7ed8f25
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,26 @@ public static String tabOutdent(String tab) {
}
}

/**
* Returns a query to get graph names.
* @return the query
*/
public static String generateSelectGraphNames() {
return "SELECT ?g WHERE { GRAPH ?g { }} ORDER BY ?g";
}

/**
* Returns a query to get graph info (names and triple counts)
* @param sei the SPARQL endpoint interface
* @param defaultGraphOnly true to only return info about the default graph
* @return the query
*/
public static String generateSelectGraphInfo(SparqlEndpointInterface sei, boolean defaultGraphOnly) {
if(!defaultGraphOnly) {
return "SELECT DISTINCT ?graph (COUNT(?s) AS ?triples) { GRAPH ?graph { ?s ?p ?o } } GROUP BY ?graph ORDER BY ?graph";
}else {
return "SELECT (\"" + sei.getDefaultGraphName() + "\" AS ?graph) (COUNT(?s) AS ?triples) {GRAPH <" + sei.getLocalDefaultGraphName() + "> { ?s ?p ?o } }";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -200,7 +202,15 @@ public JSONObject query(@RequestBody SparqlQueryRequestBody requestBody, @Reques

}


/**
* Gets names (and optionally triple counts) of graphs in the triple store.
* Returns a table result set with either 1) graph names or 2) graph names and triple counts
*
* skipSemtkGraphs true to omit the SemTK utility graphs
* graphNamesOnly true to only return graph names (e.g. no counts)
*
* For graphNamesOnly, could use simpler query if performance turns out to be an issue
*/
@CrossOrigin
@RequestMapping(value="/selectGraphNames", method= RequestMethod.POST)
public JSONObject selectGraphNames(@RequestBody GraphNameRequestBody requestBody, @RequestHeader HttpHeaders headers) {
Expand All @@ -213,33 +223,47 @@ public JSONObject selectGraphNames(@RequestBody GraphNameRequestBody requestBody

try{

String query = SparqlToXUtils.generateSelectGraphNames();
sei = SparqlEndpointInterface.getInstance(requestBody.getServerType(), requestBody.getServerAndPort(), "");

// get info for graphs
String query = SparqlToXUtils.generateSelectGraphInfo(sei, false);
resultSet = sei.executeQueryAndBuildResultSet(query, SparqlResultTypes.TABLE);

// add default graph name
Table tab = ((TableResultSet) resultSet).getResults();
tab.addRow(0, new String [] {sei.getDefaultGraphName()});

Table table = ((TableResultSet) resultSet).getResults();

// get info for default graph, append it
query = SparqlToXUtils.generateSelectGraphInfo(sei, true);
GeneralResultSet resultSet2 = sei.executeQueryAndBuildResultSet(query, SparqlResultTypes.TABLE);
table.append(((TableResultSet) resultSet2).getResults());

// optionally exclude SemTK graphs
if (requestBody.getSkipSemtkGraphs()) {
// get list of graphs, then remove the semtk graphs
ArrayList<String> graphNames = new ArrayList<String>(Arrays.asList(tab.getColumn(0)));
graphNames.remove(servicesgraph_prop.getEndpointDataset());
graphNames.remove(store_prop.getSparqlConnDataDataset());
graphNames.remove(store_prop.getSparqlConnModelDataset());
graphNames.remove(JavaApiDemo.CONN_GRAPH);
graphNames.remove(DemoSetupThread.GRAPH);

// build a new table with updated graph names
Table tabSmaller = new Table(tab.getColumnNames(), tab.getColumnTypes());
for (String g : graphNames) {
tabSmaller.addRow( new String [] { g });

final Set<String> SEMTK_GRAPHS = new HashSet<>(Arrays.asList(
servicesgraph_prop.getEndpointDataset(),
store_prop.getSparqlConnDataDataset(),
store_prop.getSparqlConnModelDataset(),
JavaApiDemo.CONN_GRAPH,
DemoSetupThread.GRAPH));

// build a new table excluding the SemTK graphs
Table tableSubset = new Table(table.getColumnNames(), table.getColumnTypes());
for (int i = 0; i < table.getNumRows(); i++) {
if(!SEMTK_GRAPHS.contains(table.getCell(i, 0))){
tableSubset.addRow(table.getRow(i));
}
}
((TableResultSet) resultSet).addResults(tabSmaller);
} else {
((TableResultSet) resultSet).addResults(tab);
table = tableSubset;
}


// optionally return only the graph names
if (requestBody.getGraphNamesOnly()) {
String[] colList = new String[1];
colList[0] = table.getColumnNames()[0]; // assumes first column is the graph name
table.toSubTable(colList);
}

((TableResultSet) resultSet).addResults(table);

} catch (Exception e) {
LocalLogger.printStackTrace(e);
resultSet = new SimpleResultSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ public class GraphNameRequestBody {
public String serverType; // e.g. virtuoso

@Schema(required = false)
public boolean skipSemtkGraphs = false;
public boolean skipSemtkGraphs = false;

@Schema(required = false)
public boolean graphNamesOnly = true;

public String getServerAndPort() {
return serverAndPort;
}


public String getServerType() {
return serverType;
}


public boolean getSkipSemtkGraphs() {
return this.skipSemtkGraphs;
}

public boolean getGraphNamesOnly() {
return this.graphNamesOnly;
}
}

0 comments on commit 7ed8f25

Please sign in to comment.