Skip to content

Commit 7e0321b

Browse files
committed
Changes for sqo 1.3
1 parent 2749214 commit 7e0321b

12 files changed

Lines changed: 425 additions & 153 deletions

File tree

server/src/main/java/org/diskproject/server/adapters/GraphDBAdapter.java

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,29 @@ private boolean login () {
6868
return false;
6969
}
7070

71+
public List<DataResult> getDataResultFromJsonString (String jsonString) {
72+
JsonObject json = (JsonObject) jsonParser.parse(jsonString);
73+
JsonArray variables = json.get("head").getAsJsonObject().get("vars").getAsJsonArray();
74+
JsonArray bindings = json.get("results").getAsJsonObject().get("bindings").getAsJsonArray();
75+
76+
List<DataResult> results = new ArrayList<DataResult>();
77+
78+
for (JsonElement binding : bindings) {
79+
DataResult curResult = new DataResult();
80+
for (JsonElement variable : variables) {
81+
String varName = variable.getAsString();
82+
JsonElement valueJson = binding.getAsJsonObject().get(varName);
83+
String value = valueJson == null ? null : valueJson.getAsJsonObject().get("value").getAsString();
84+
curResult.addValue(varName, value);
85+
}
86+
results.add(curResult);
87+
}
88+
return results;
89+
}
90+
7191
@Override
7292
public List<DataResult> query(String queryString) throws Exception {
93+
// queryString is not being checked
7394
String queryUrl = getEndpointUrl() + "repositories/" + this.repository;
7495
URIBuilder builder = new URIBuilder(queryUrl);
7596
builder.setParameter("query", queryString);
@@ -82,35 +103,18 @@ public List<DataResult> query(String queryString) throws Exception {
82103
try (CloseableHttpResponse response = httpClient.execute(request)) {
83104
HttpEntity entity = response.getEntity();
84105
String strResponse = EntityUtils.toString(entity);
85-
86106
EntityUtils.consume(entity);
87-
try {
88-
JsonObject json = (JsonObject) jsonParser.parse(strResponse);
89-
JsonArray variables = json.get("head").getAsJsonObject().get("vars").getAsJsonArray();
90-
JsonArray bindings = json.get("results").getAsJsonObject().get("bindings").getAsJsonArray();
91-
92-
List<DataResult> results = new ArrayList<DataResult>();
93-
94-
for (JsonElement binding : bindings) {
95-
DataResult curResult = new DataResult();
96-
for (JsonElement variable : variables) {
97-
String varName = variable.getAsString();
98-
JsonElement valueJson = binding.getAsJsonObject().get(varName);
99-
String value = valueJson == null ? null : valueJson.getAsJsonObject().get("value").getAsString();
100-
curResult.addValue(varName, value);
101-
}
102-
results.add(curResult);
103-
}
104-
return results;
105-
} catch (Exception e) {
106-
System.err.println("Error decoding " + strResponse);
107-
e.printStackTrace();
108-
throw new RuntimeException(e);
107+
108+
if (strResponse.startsWith("MALFORMED QUERY")) {
109+
System.out.println(strResponse);
110+
System.out.println(queryString);
111+
throw new RuntimeException("MALFORMED QUERY");
112+
} else {
113+
return getDataResultFromJsonString(strResponse);
109114
}
110115
} catch (IOException e) {
111116
e.printStackTrace();
112117
}
113-
114118
return null;
115119
}
116120

@@ -128,26 +132,18 @@ public List<DataResult> queryOptions(String varname, String constraintQuery) thr
128132
query += "\n OPTIONAL { " + varname + " rdfs:label " + labelVar + " . }";
129133
query += "\n}";
130134

131-
System.out.println("Q: " + query);
132-
133135
List<DataResult> solutions = this.query(query);
134136
List<DataResult> fixedSolutions = new ArrayList<DataResult>();
135137

136138
for (DataResult solution : solutions) {
137139
DataResult cur = new DataResult();
138-
String valUrl = solution.getValue(name);
139-
String valName = solution.getName(name);
140+
String uri = solution.getValue(name);
140141
String label = solution.getValue(name + "Label");
141-
if (label == null && valName != null) {
142-
label = valName;
143-
} else if (label == null) {
144-
label = valUrl.replaceAll("^.*\\/", "");
145-
// Try to remove mediawiki stuff
146-
label = label.replaceAll("Property-3A", "");
147-
label = label.replaceAll("-28E-29", "");
148-
label = label.replaceAll("_", " ");
142+
if (label == null) { //Theres no label, create one with the def value or extract it from the URI
143+
String defName = solution.getName(name);
144+
label = defName != null ? defName : uri.replaceAll("^.*\\/", "").replaceAll("_", " ");
149145
}
150-
cur.addValue(VARURI, valUrl);
146+
cur.addValue(VARURI, uri);
151147
cur.addValue(VARLABEL, label);
152148
fixedSolutions.add(cur);
153149
}

server/src/main/java/org/diskproject/server/adapters/SparqlAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static Set<String> interceptVariables(final String queryA, final String q
6565

6666
@Override
6767
public List<DataResult> query(String queryString) throws Exception, QueryParseException, QueryExceptionHTTP {
68-
//System.out.println("SparqlAdapter.query: " + queryString);
68+
System.out.println("SparqlAdapter.query: " + queryString);
6969
ArrayList<ArrayList<SparqlQuerySolution>> solutions = null;
7070
try {
7171
String user = this.getUsername(), pass = this.getPassword();

server/src/main/java/org/diskproject/server/api/impl/DiskResource.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.diskproject.shared.classes.loi.LineOfInquiry;
3030
import org.diskproject.shared.classes.loi.TriggeredLOI;
3131
import org.diskproject.shared.classes.question.Question;
32+
import org.diskproject.shared.classes.question.VariableOption;
3233
import org.diskproject.shared.classes.util.DataAdapterResponse;
3334
import org.diskproject.shared.classes.util.ExternalDataRequest;
3435
import org.diskproject.shared.classes.util.QuestionOptionsRequest;
@@ -124,7 +125,7 @@ public Hypothesis addHypothesis(
124125
@GET
125126
@Path("hypotheses")
126127
@Override
127-
public List<TreeItem> listHypotheses() {
128+
public List<Hypothesis> listHypotheses() {
128129
return this.repo.listHypotheses(USERNAME);
129130
}
130131

@@ -434,7 +435,7 @@ public List<Question> listQuestions() {
434435
@GET
435436
@Path("question/{id}/options")
436437
@Override
437-
public List<List<String>> listOptions(
438+
public List<VariableOption> listOptions(
438439
@PathParam("id") String id) {
439440
try {
440441
// return WingsAdapter.get().getWorkflowList();
@@ -463,7 +464,7 @@ public List<List<String>> listOptions(
463464

464465
@POST
465466
@Path("question/options")
466-
public Map<String, List<List<String>>> listDynamicOptions(
467+
public Map<String, List<VariableOption>> listDynamicOptions(
467468
@JsonProperty("config") QuestionOptionsRequest opts) {
468469
try {
469470
return this.repo.listDynamicOptions(opts);

0 commit comments

Comments
 (0)