Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rdf-based prefix completer #72

Merged
merged 2 commits into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/com/github/imas/rdflint/DatasetLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ static Model loadRdfSet(RdfLintParameters params, String targetDir) throws IOExc
List<Triple> lst = gf.find().toList();
gf.close();
lst.forEach(g::add);

gf.getPrefixMapping().getNsPrefixMap()
.forEach((k, v) -> g.getPrefixMapping().setNsPrefix(k, v));
});

return ModelFactory.createModelForGraph(g);
Expand Down
35 changes: 18 additions & 17 deletions src/main/java/com/github/imas/rdflint/InteractiveMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -53,7 +55,7 @@ void execute(RdfLintParameters params, String targetDir) throws IOException {
.build();
LineReader lineReader = LineReaderBuilder.builder()
.terminal(terminal)
.completer(new InteractiveCompleter())
.completer(new InteractiveCompleter(m))
.parser(new InteractiveParser())
.build();

Expand Down Expand Up @@ -235,15 +237,12 @@ public static class InteractiveCompleter implements Completer {
"true",
"false"
};
private static final String[] PREFIX_MAP = {
"rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>",
"rdfs: <http://www.w3.org/2000/01/rdf-schema#>",
"xsd: <http://www.w3.org/2001/XMLSchema#>",
"fn: <http://www.w3.org/2005/xpath-functions#>",
"schema: <http://schema.org/>",
"foaf: <http://xmlns.com/foaf/0.1/>",
"dc: <http://purl.org/dc/elements/1.1/>",
};
Model model;

public InteractiveCompleter(Model model) {
super();
this.model = model;
}

@Override
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
Expand All @@ -258,21 +257,23 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
}

// prefix completer
Map<String, String> prefixMap = this.model.getNsPrefixMap();
int idxBefore1 = line.words().size() - 2;
int idxBefore2 = line.words().size() - 3;
if (idxBefore1 >= 0 && "PREFIX".equals(line.words().get(idxBefore1).toUpperCase())) {
Stream.of(PREFIX_MAP)
.map(s -> s.split(" ")[0])
prefixMap.keySet().stream()
.filter(s -> s.startsWith(line.word()))
foooomio marked this conversation as resolved.
Show resolved Hide resolved
.forEach(s -> candidates.add(new Candidate(s)));
.sorted()
.forEach(s -> candidates.add(new Candidate(s + ":")));
return;
}
if (idxBefore2 >= 0 && "PREFIX".equals(line.words().get(idxBefore2).toUpperCase())) {
String alias = line.words().get(idxBefore1);
Stream.of(PREFIX_MAP)
.filter(s -> s.split(" ")[0].equals(alias))
.map(s -> s.split(" ")[1])
.forEach(s -> candidates.add(new Candidate(s)));

prefixMap.entrySet().stream()
.filter(e -> alias.equals(e.getKey() + ":"))
.map(Entry::getValue)
.forEach(s -> candidates.add(new Candidate("<" + s + ">")));
return;
}

Expand Down
22 changes: 18 additions & 4 deletions src/test/java/com/github/imas/rdflint/InteractiveModeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.jena.rdf.model.Model;
import org.hamcrest.CoreMatchers;
import org.jline.reader.Candidate;
Expand Down Expand Up @@ -108,7 +110,9 @@ public void interactiveCompleterCommand() throws Exception {
ParsedLine pl = mock(ParsedLine.class);
when(pl.line()).thenReturn(":ex");
when(pl.word()).thenReturn(":ex");
InteractiveMode.InteractiveCompleter completer = new InteractiveMode.InteractiveCompleter();
Model m = mock(Model.class);
when(m.getNsPrefixMap()).thenReturn(new HashMap<String, String>());
InteractiveMode.InteractiveCompleter completer = new InteractiveMode.InteractiveCompleter(m);
List<Candidate> candidates = new LinkedList<>();
completer.complete(null, pl, candidates);
assertEquals(":exit", candidates.get(0).value());
Expand All @@ -119,7 +123,9 @@ public void interactiveCompleterQuery() throws Exception {
ParsedLine pl = mock(ParsedLine.class);
when(pl.line()).thenReturn("se");
when(pl.word()).thenReturn("se");
InteractiveMode.InteractiveCompleter completer = new InteractiveMode.InteractiveCompleter();
Model m = mock(Model.class);
when(m.getNsPrefixMap()).thenReturn(new HashMap<String, String>());
InteractiveMode.InteractiveCompleter completer = new InteractiveMode.InteractiveCompleter(m);
List<Candidate> candidates = new LinkedList<>();
completer.complete(null, pl, candidates);
assertEquals("SELECT", candidates.get(0).value());
Expand All @@ -131,7 +137,11 @@ public void interactiveCompleterPrefixAlias() throws Exception {
when(pl.line()).thenReturn("PREFIX foa");
when(pl.words()).thenReturn(Arrays.asList("PREFIX", "foa"));
when(pl.word()).thenReturn("foa");
InteractiveMode.InteractiveCompleter completer = new InteractiveMode.InteractiveCompleter();
Model m = mock(Model.class);
Map<String, String> prefixMap = new HashMap<String, String>();
prefixMap.put("foaf", "http://xmlns.com/foaf/spec/");
when(m.getNsPrefixMap()).thenReturn(prefixMap);
InteractiveMode.InteractiveCompleter completer = new InteractiveMode.InteractiveCompleter(m);
List<Candidate> candidates = new LinkedList<>();
completer.complete(null, pl, candidates);
assertEquals("foaf:", candidates.get(0).value());
Expand All @@ -143,7 +153,11 @@ public void interactiveCompleterPrefixUri() throws Exception {
when(pl.line()).thenReturn("PREFIX rdfs: ");
when(pl.words()).thenReturn(Arrays.asList("PREFIX", "rdfs:", ""));
when(pl.word()).thenReturn("");
InteractiveMode.InteractiveCompleter completer = new InteractiveMode.InteractiveCompleter();
Model m = mock(Model.class);
Map<String, String> prefixMap = new HashMap<String, String>();
prefixMap.put("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
when(m.getNsPrefixMap()).thenReturn(prefixMap);
InteractiveMode.InteractiveCompleter completer = new InteractiveMode.InteractiveCompleter(m);
List<Candidate> candidates = new LinkedList<>();
completer.complete(null, pl, candidates);
assertEquals("<http://www.w3.org/2000/01/rdf-schema#>", candidates.get(0).value());
Expand Down