[TASK] Allow toggling whether to shorten uris using prefixs.

This commit is contained in:
Jan Philipp Timme 2016-10-02 23:21:32 +02:00
parent 5371fa6aaa
commit 744b9c0dc2
4 changed files with 105 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lu.jpt.csparqlproject.gui.SimulationControlWindow;
import lu.jpt.csparqlproject.util.PrefixManager;
/**
* This is the main entry point of the whole project. A logger is being set up
@ -16,6 +17,7 @@ import lu.jpt.csparqlproject.gui.SimulationControlWindow;
public class Main {
public static Logger logger = LoggerFactory.getLogger(Main.class);
public static PrefixManager prefixManager;
public static void main(String[] args) {
// Initialize a logger
@ -24,6 +26,13 @@ public class Main {
} catch(Throwable t) {
PropertyConfigurator.configure("/log4j.properties");
}
// Initialize PrefixManager which helps to shorten strings using prefixes
Main.prefixManager = new PrefixManager();
// Register generic prefixes
Main.prefixManager.registerPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
Main.prefixManager.registerPrefix("xsd", "http://www.w3.org/2001/XMLSchema#");
Main.prefixManager.registerPrefix("f", "http://larkc.eu/csparql/sparql/jena/ext#");
// Initialize SimulationContext and wire it up with its control window
final SimulationContext simulationContext = new SimulationContext();
// Fire up the control window

View File

@ -31,6 +31,7 @@ import eu.larkc.csparql.cep.api.RdfQuadruple;
import eu.larkc.csparql.common.RDFTable;
import eu.larkc.csparql.common.RDFTuple;
import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy;
import lu.jpt.csparqlproject.Main;
/**
* Multi-purpose text window to allow for easy viewing of RdfStream quadruples,
@ -44,7 +45,7 @@ public class TextObserverWindow extends JFrame implements Observer {
private JTextPane textPane;
private volatile boolean autoScrollToBottom = true;
private volatile boolean usePrefixManager = true;
/**
* Constructor
@ -63,6 +64,15 @@ public class TextObserverWindow extends JFrame implements Observer {
this.autoScrollToBottom = autoScrollToBottom;
}
/**
* Set whether or not the PrefixManager shall be used to shorten
* uris in strings to prefixes.
* @param usePrefixManager
*/
public void setUsePrefixManager(boolean usePrefixManager) {
this.usePrefixManager = usePrefixManager;
}
/**
* Show the given text in the window
* @param text
@ -111,7 +121,8 @@ public class TextObserverWindow extends JFrame implements Observer {
* @param color
*/
public void showQuadruple(RdfQuadruple quad, Color color) {
this.appendToTextPane(quad.toString(), true, color);
String quadruple = this.shortenUriToPrefix(quad.toString());
this.appendToTextPane(quadruple, true, color);
}
/**
@ -182,6 +193,20 @@ public class TextObserverWindow extends JFrame implements Observer {
});
bottomPanel.add(autoscrollCheckbox);
// Add a checkbox to toggle uri shortening using PrefixManager
JCheckBox usePrefixManagerCheckbox = new JCheckBox();
usePrefixManagerCheckbox.setText("Shorten URIs to prefixes");
usePrefixManagerCheckbox.setEnabled(true);
usePrefixManagerCheckbox.setSelected(true);
usePrefixManagerCheckbox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JCheckBox checkBox = (JCheckBox) e.getSource();
textObserverWindow.setUsePrefixManager(checkBox.isSelected());
}
});
bottomPanel.add(usePrefixManagerCheckbox);
// Add buttons to increase/decrease font size
JButton increaseFontSizeButton = new JButton("+");
increaseFontSizeButton.addActionListener(new ActionListener() {
@ -296,6 +321,7 @@ public class TextObserverWindow extends JFrame implements Observer {
int tokenIndex = 0;
while(tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
token = this.shortenUriToPrefix(token);
if(token == "") token = "[NO VALUE]";
if(tokenizer.hasMoreTokens()) {
token += "\t";
@ -330,4 +356,18 @@ public class TextObserverWindow extends JFrame implements Observer {
}
/**
* Helper method to shorten strings using the PrefixManager if
* this window uses it
* @param str input string
* @return output string, shortened or not depending on usePrefixManager variable
*/
private String shortenUriToPrefix(String str) {
if(this.usePrefixManager) {
return Main.prefixManager.applyPrefix(str);
} else {
return str;
}
}
}

View File

@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory;
import eu.larkc.csparql.cep.api.RdfQuadruple;
import eu.larkc.csparql.cep.api.RdfStream;
import lu.jpt.csparqlproject.Main;
import lu.jpt.csparqlproject.util.WindowLoggingRdfStream;
/**
@ -40,6 +41,7 @@ public class RentACarSimulation implements Runnable {
public RentACarSimulation() {
this.registerOwnPrefixes();
int numberOfCars = 1;
int numberOfCustomers = 1;
// Create a car pool and drivers
@ -58,6 +60,13 @@ public class RentACarSimulation implements Runnable {
this.pauseSimulation = false;
}
private void registerOwnPrefixes() {
Main.prefixManager.registerPrefix("event", RentACarSimulation.BASE_OBJECT_IRI+"/event#");
Main.prefixManager.registerPrefix("carOnt", RentACarSimulation.BASE_ONTOLOGY_IRI);
Main.prefixManager.registerPrefix("car", RentACarSimulation.BASE_OBJECT_IRI+"/Car#");
Main.prefixManager.registerPrefix("driver", RentACarSimulation.BASE_OBJECT_IRI+"/Driver#");
}
public RdfStream getCarStream() {
return this.carStream;
}

View File

@ -0,0 +1,45 @@
package lu.jpt.csparqlproject.util;
import java.util.HashMap;
import java.util.Map;
/**
* This is a small helper class which will be used on some occasions
* to make output easier to read by providing a method to replace known
* uris in (tuple) strings with their known prefix.
*/
public class PrefixManager {
/**
* Key is the uri, value is its prefix
*/
private Map<String, String> prefixTable;
public PrefixManager() {
this.prefixTable = new HashMap<String, String>();
}
/**
* Register a known prefix with its corresponding uri
* @param prefix
* @param uri
*/
public void registerPrefix(String prefix, String uri) {
this.prefixTable.put(uri, prefix);
}
/**
* Replace all uris within a tuple with their know prefix
* This is not efficient, but that is not the point
* @param tuple as a string
* @return shortened tuple string
*/
public String applyPrefix(String tuple) {
for (Map.Entry<String, String> entry : this.prefixTable.entrySet()) {
tuple = tuple.replace(entry.getKey(), entry.getValue() + ":");
}
return tuple;
}
}