-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4504 from inception-project/feature/4503-SPARQL-q…
…uery-UI-for-developers #4503 - SPARQL query UI for developers
- Loading branch information
Showing
6 changed files
with
267 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
inception/inception-ui-kb/src/main/java/de/tudarmstadt/ukp/inception/ui/kb/SparqlPanel.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<!DOCTYPE html> | ||
<!-- | ||
Licensed to the Technische Universität Darmstadt under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The Technische Universität Darmstadt | ||
licenses this file to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
<html xmlns:wicket="http://wicket.apache.org"> | ||
<wicket:head> | ||
<style> | ||
.results-table tr:hover { | ||
background-color: #f5f5f5; | ||
} | ||
</style> | ||
</wicket:head> | ||
<body> | ||
<wicket:panel> | ||
<div class="modal-header"> | ||
<h5 class="modal-title"> | ||
SPARQL Query | ||
</h5> | ||
<button wicket:id="closeDialog" type="button" class="btn-close" aria-label="Close"></button> | ||
</div> | ||
<div class="modal-body overflow-auto" style="width: 79vw;"> | ||
<form wicket:id="form" class="container"> | ||
<div> | ||
<textarea wicket:id=query class="w-100" rows="10"> | ||
</textarea> | ||
</div> | ||
<button wicket:id="runQuery">Query</button> | ||
</form> | ||
<div wicket:id="resultsContainer"> | ||
<table wicket:id="results" class="results-table w-100"/> | ||
</div> | ||
</div> | ||
<div class="modal-footer flex-column small"> | ||
Great power brings great responsibility. | ||
</div> | ||
</wicket:panel> | ||
</body> | ||
</html> |
111 changes: 111 additions & 0 deletions
111
inception/inception-ui-kb/src/main/java/de/tudarmstadt/ukp/inception/ui/kb/SparqlPanel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Licensed to the Technische Universität Darmstadt under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The Technische Universität Darmstadt | ||
* licenses this file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package de.tudarmstadt.ukp.inception.ui.kb; | ||
|
||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import org.apache.wicket.ajax.AjaxRequestTarget; | ||
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalDialog; | ||
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackHeadersToolbar; | ||
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxNavigationToolbar; | ||
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable; | ||
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn; | ||
import org.apache.wicket.extensions.markup.html.repeater.data.table.LambdaColumn; | ||
import org.apache.wicket.markup.html.WebMarkupContainer; | ||
import org.apache.wicket.markup.html.form.Form; | ||
import org.apache.wicket.markup.html.form.TextArea; | ||
import org.apache.wicket.markup.html.panel.GenericPanel; | ||
import org.apache.wicket.model.CompoundPropertyModel; | ||
import org.apache.wicket.model.IModel; | ||
import org.apache.wicket.model.Model; | ||
import org.apache.wicket.spring.injection.annot.SpringBean; | ||
|
||
import de.tudarmstadt.ukp.inception.kb.KnowledgeBaseService; | ||
import de.tudarmstadt.ukp.inception.kb.model.KnowledgeBase; | ||
import de.tudarmstadt.ukp.inception.support.lambda.LambdaAjaxLink; | ||
import de.tudarmstadt.ukp.inception.support.lambda.LambdaAjaxSubmitLink; | ||
|
||
public class SparqlPanel | ||
extends GenericPanel<KnowledgeBase> | ||
{ | ||
private static final long serialVersionUID = -8435410869046010021L; | ||
|
||
private @SpringBean KnowledgeBaseService knowledgeBaseService; | ||
|
||
private WebMarkupContainer resultsContainer; | ||
private DataTable<Map<String, String>, Void> results; | ||
|
||
public SparqlPanel(String aId, IModel<KnowledgeBase> aModel) | ||
{ | ||
super(aId, aModel); | ||
|
||
queue(new Form<QueryModel>("form", new CompoundPropertyModel<>(new QueryModel()))); | ||
queue(new TextArea<>("query")); | ||
queue(new LambdaAjaxSubmitLink<QueryModel>("runQuery", this::actionQuery)); | ||
queue(new LambdaAjaxLink("closeDialog", this::actionCancel)); | ||
resultsContainer = new WebMarkupContainer("resultsContainer"); | ||
resultsContainer.setOutputMarkupId(true); | ||
queue(resultsContainer); | ||
results = new DataTable<>("results", Collections.emptyList(), | ||
new TupleQueryResultDataProvider(), 10); | ||
queue(results); | ||
} | ||
|
||
private void actionQuery(AjaxRequestTarget aTarget, Form<QueryModel> aForm) | ||
{ | ||
var model = aForm.getModelObject(); | ||
|
||
var columns = new ArrayList<IColumn<Map<String, String>, Void>>(); | ||
|
||
var dataProvider = knowledgeBaseService.read(getModelObject(), conn -> { | ||
var tupleQuery = conn.prepareTupleQuery(model.query); | ||
|
||
var tupleResult = tupleQuery.evaluate(); | ||
|
||
for (var bindingName : tupleResult.getBindingNames()) { | ||
columns.add(new LambdaColumn<>(Model.of(bindingName), $ -> $.get(bindingName))); | ||
} | ||
|
||
return new TupleQueryResultDataProvider(tupleResult); | ||
}); | ||
|
||
var table = new DataTable<>("results", columns, dataProvider, 10); | ||
table.setOutputMarkupId(true); | ||
table.addTopToolbar(new AjaxNavigationToolbar(table)); | ||
table.addTopToolbar(new AjaxFallbackHeadersToolbar<Void>(table, dataProvider)); | ||
results = (DataTable<Map<String, String>, Void>) results.replaceWith(table); | ||
|
||
aTarget.add(resultsContainer); | ||
} | ||
|
||
protected void actionCancel(AjaxRequestTarget aTarget) | ||
{ | ||
findParent(ModalDialog.class).close(aTarget); | ||
} | ||
|
||
private static class QueryModel | ||
implements Serializable | ||
{ | ||
private static final long serialVersionUID = -1407131006099282400L; | ||
|
||
String query; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...-ui-kb/src/main/java/de/tudarmstadt/ukp/inception/ui/kb/TupleQueryResultDataProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Licensed to the Technische Universität Darmstadt under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The Technische Universität Darmstadt | ||
* licenses this file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package de.tudarmstadt.ukp.inception.ui.kb; | ||
|
||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider; | ||
import org.apache.wicket.model.IModel; | ||
import org.apache.wicket.model.Model; | ||
import org.eclipse.rdf4j.query.TupleQueryResult; | ||
|
||
public class TupleQueryResultDataProvider | ||
extends SortableDataProvider<Map<String, String>, Void> | ||
implements Serializable | ||
{ | ||
private static final long serialVersionUID = -4546564722894830886L; | ||
|
||
private List<Map<String, String>> data; | ||
|
||
public TupleQueryResultDataProvider() | ||
{ | ||
data = Collections.emptyList(); | ||
} | ||
|
||
public TupleQueryResultDataProvider(TupleQueryResult aTupleResult) | ||
{ | ||
data = new ArrayList<Map<String, String>>(); | ||
|
||
for (var bindingSet : aTupleResult) { | ||
var map = new LinkedHashMap<String, String>(); | ||
|
||
for (var binding : bindingSet) { | ||
map.put(binding.getName(), String.valueOf(binding.getValue())); | ||
} | ||
|
||
data.add(map); | ||
} | ||
} | ||
|
||
@Override | ||
public Iterator<? extends Map<String, String>> iterator(long aFirst, long aCount) | ||
{ | ||
return data.subList((int) aFirst, (int) (aFirst + aCount)).iterator(); | ||
} | ||
|
||
@Override | ||
public long size() | ||
{ | ||
return data.size(); | ||
} | ||
|
||
@Override | ||
public IModel<Map<String, String>> model(Map<String, String> aObject) | ||
{ | ||
return Model.ofMap(aObject); | ||
} | ||
} |