From 744b9c0dc2c2b635d28e4554065c0f53b3f84a72 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sun, 2 Oct 2016 23:21:32 +0200 Subject: [PATCH] [TASK] Allow toggling whether to shorten uris using prefixs. --- src/main/java/lu/jpt/csparqlproject/Main.java | 9 ++++ .../gui/TextObserverWindow.java | 44 +++++++++++++++++- .../rentacar/RentACarSimulation.java | 9 ++++ .../csparqlproject/util/PrefixManager.java | 45 +++++++++++++++++++ 4 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 src/main/java/lu/jpt/csparqlproject/util/PrefixManager.java diff --git a/src/main/java/lu/jpt/csparqlproject/Main.java b/src/main/java/lu/jpt/csparqlproject/Main.java index ef6d355..cb9fe91 100644 --- a/src/main/java/lu/jpt/csparqlproject/Main.java +++ b/src/main/java/lu/jpt/csparqlproject/Main.java @@ -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 diff --git a/src/main/java/lu/jpt/csparqlproject/gui/TextObserverWindow.java b/src/main/java/lu/jpt/csparqlproject/gui/TextObserverWindow.java index b1ee952..bea6f53 100644 --- a/src/main/java/lu/jpt/csparqlproject/gui/TextObserverWindow.java +++ b/src/main/java/lu/jpt/csparqlproject/gui/TextObserverWindow.java @@ -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"; @@ -329,5 +355,19 @@ public class TextObserverWindow extends JFrame implements Observer { return result; } + + /** + * 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; + } + } } diff --git a/src/main/java/lu/jpt/csparqlproject/rentacar/RentACarSimulation.java b/src/main/java/lu/jpt/csparqlproject/rentacar/RentACarSimulation.java index 171b647..ae5ecda 100644 --- a/src/main/java/lu/jpt/csparqlproject/rentacar/RentACarSimulation.java +++ b/src/main/java/lu/jpt/csparqlproject/rentacar/RentACarSimulation.java @@ -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; } diff --git a/src/main/java/lu/jpt/csparqlproject/util/PrefixManager.java b/src/main/java/lu/jpt/csparqlproject/util/PrefixManager.java new file mode 100644 index 0000000..f3a54d2 --- /dev/null +++ b/src/main/java/lu/jpt/csparqlproject/util/PrefixManager.java @@ -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 prefixTable; + + + public PrefixManager() { + this.prefixTable = new HashMap(); + } + + /** + * 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 entry : this.prefixTable.entrySet()) { + tuple = tuple.replace(entry.getKey(), entry.getValue() + ":"); + } + return tuple; + } + +}