Java tutorial
/** * This file is part of combo-obda. * * combo-obda is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * combo-obda is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR * A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * combo-obda. If not, see <http://www.gnu.org/licenses/>. */ package de.unibremen.informatik.tdki.combo.ui; import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter; import com.beust.jcommander.ParameterException; import com.beust.jcommander.Parameters; import de.unibremen.informatik.tdki.combo.data.DBConfig; import de.unibremen.informatik.tdki.combo.data.DBConnPool; import de.unibremen.informatik.tdki.combo.data.DBLayout; import de.unibremen.informatik.tdki.combo.data.RowCallbackHandler; import de.unibremen.informatik.tdki.combo.rewriting.FilterRewriterDB2; import de.unibremen.informatik.tdki.combo.syntax.query.*; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.dbutils.QueryRunner; /** * * @author nan Seylan */ @Parameters(commandDescription = "Completes the ABox assertions w.r.t. the TBox") class CommandComplete { @Parameter(description = "A list of project names") private List<String> projects; public List<String> getProjects() { return projects; } } /** * * @author nan Seylan */ @Parameters(commandDescription = "Creates a project") class CommandCreate { @Parameter(description = "A list of project names") private List<String> projects; public List<String> getProjects() { return projects; } } /** * * @author nan Seylan */ @Parameters(commandDescription = "Deletes a project") class CommandDelete { @Parameter(description = "A list of project names") private List<String> projects; public List<String> getProjects() { return projects; } } /** * * @author nan Seylan */ @Parameters(commandDescription = "Drops all filters") class CommandDropFilters { } /** * * @author nan Seylan */ @Parameters(commandDescription = "Exports the given project into the given combo bulk file") class CommandExport { @Parameter(names = "--project", description = "Name of the project") private String project; @Parameter(names = "--file", description = "Path of the file") private String file; public String getProject() { return project; } public void setProject(String project) { this.project = project; } public String getFile() { return file; } public void setFile(String file) { this.file = file; } } /** * * @author nan Seylan */ @Parameters(commandDescription = "Prints the usage of the program") class CommandHelp { } /** * * @author nan Seylan */ @Parameters(commandDescription = "Initializes the database. Note that all previous projects are deleted.") class CommandInit { } /** * * @author nan Seylan */ @Parameters(commandDescription = "Lists all the projects") class CommandList { } /** * * @author nan Seylan */ @Parameters(commandDescription = "Loads the given combo bulk load file into the given project") class CommandLoad { @Parameter(names = "--file", description = "Path of the file") private String file; @Parameter(names = "--project", description = "Name of the project") private String project; public String getFile() { return file; } public void setFile(String file) { this.file = file; } public String getProject() { return project; } public void setProject(String project) { this.project = project; } } /** * * @author nan Seylan */ @Parameters(commandDescription = "Rewrites the Datalog program in the given file to SQL") class CommandRewriteDatalog { @Parameter(names = "--uri", description = "URI to prepend for the concept/role names used in the query") private String uri; @Parameter(names = "--file", description = "Path of the file") private String filename; @Parameter(names = "--project", description = "The project to query") private String project; @Parameter(names = "--count", arity = 1, description = "With this option, a count query asking for the number of individuals is generated") private boolean count = true; @Parameter(names = "--names", arity = 1, description = "With this option, a query asking for the names of individuals (instead of their ids) is generated") private boolean names = false; public String getUri() { return uri; } public void setUri(String uri) { this.uri = uri; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } public String getProject() { return project; } public void setProject(String project) { this.project = project; } public boolean isCount() { return count; } public void setCount(boolean count) { this.count = count; } public boolean isNames() { return names; } public void setNames(boolean names) { this.names = names; } } /** * * @author nan Seylan */ @Parameters(commandDescription = "Rewrites the CQ in the given file to SQL by generating a filter") class CommandRewriteFilter { @Parameter(names = "--uri", description = "URI to prepend for the concept/role names used in the query") private String uri; @Parameter(names = "--file", description = "Path of the file") private String filename; @Parameter(names = "--avo", arity = 1, description = "Optimizes answer variable matching by adding extra conditions to the WHERE clause") private boolean avo = true; @Parameter(names = "--count", arity = 1, description = "With this option, a count query asking for the number of individuals is generated") private boolean count = true; @Parameter(names = "--names", arity = 1, description = "With this option, a query asking for the names of individuals (instead of their ids) is generated") private boolean names = false; @Parameter(names = "--project", description = "The project to query") private String project; public String getUri() { return uri; } public void setUri(String uri) { this.uri = uri; } public boolean isCount() { return count; } public void setCount(boolean count) { this.count = count; } public boolean isNames() { return names; } public void setNames(boolean names) { this.names = names; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } public boolean isAvo() { return avo; } public void setAvo(boolean answerVar) { this.avo = answerVar; } public String getProject() { return project; } public void setProject(String project) { this.project = project; } } /** * * @author nan Seylan */ @Parameters(commandDescription = "Updates the table statistics of the given project") class CommandRunStats { @Parameter(description = "A list of project names") private List<String> projects; public List<String> getProjects() { return projects; } public void setProjects(List<String> projects) { this.projects = projects; } } /** * * @author nan Seylan */ public class CombinedAppInterface { private static Connection connection; public static void main(String[] args) { JCommander jc = new JCommander(); CommandComplete complete = new CommandComplete(); jc.addCommand("complete", complete); CommandCreate create = new CommandCreate(); jc.addCommand("create", create); CommandDelete delete = new CommandDelete(); jc.addCommand("delete", delete); CommandDropFilters dropFilter = new CommandDropFilters(); jc.addCommand("dropf", dropFilter); CommandExport export = new CommandExport(); jc.addCommand("export", export); CommandHelp help = new CommandHelp(); jc.addCommand("help", help); CommandInit init = new CommandInit(); jc.addCommand("init", init); CommandList list = new CommandList(); jc.addCommand("list", list); CommandLoad load = new CommandLoad(); jc.addCommand("load", load); CommandRewriteDatalog rwDatalog = new CommandRewriteDatalog(); jc.addCommand("rwd", rwDatalog); CommandRewriteFilter rwFilter = new CommandRewriteFilter(); jc.addCommand("rwf", rwFilter); CommandRunStats runStats = new CommandRunStats(); jc.addCommand("runstats", runStats); DBConnPool pool = null; try { jc.parse(args); String command = jc.getParsedCommand(); if (command.equals("help")) { jc.usage(); return; } pool = new DBConnPool(DBConfig.fromPropertyFile()); connection = pool.getConnection(); DBLayout layout = new DBLayout(false, connection); if (command.equals("complete")) { for (String name : complete.getProjects()) { layout.completeData(name); } } else if (command.equals("create")) { for (String name : create.getProjects()) { if (layout.createProject(name)) { System.out.println(name + " already exists; not creating."); } } } else if (command.equals("delete")) { for (String name : delete.getProjects()) { layout.dropProject(name); } } else if (command.equals("dropf")) { dropFilter(); } else if (command.equals("export")) { boolean projectExists = layout.exportProject(export.getProject(), new File(export.getFile())); if (!projectExists) { System.err.println("Project " + export.getProject() + " does not exist."); } } else if (command.equals("init")) { layout.initialize(); } else if (command.equals("list")) { List<String> projects = layout.getProjects(); for (String p : projects) { System.out.println(p); } } else if (command.equals("load")) { boolean projectExists = layout.loadProject(new File(load.getFile()), load.getProject()); if (!projectExists) { System.err.println("Project " + load.getProject() + " does not exist."); } } else if (command.equals("rwd")) { generateDatalog(rwDatalog); } else if (command.equals("rwf")) { generateFilter(rwFilter); } else if (command.equals("runstats")) { for (String name : runStats.getProjects()) { layout.updateStatistics(name); } } } catch (ParameterException e) { System.err.println(e.getMessage()); System.err.println("Try the 'help' command for usage."); } finally { if (pool != null) pool.releaseConnection(connection); } } private static void generateFilter(CommandRewriteFilter rw) { try { NRDatalogProgram program = new QueryParser(rw.getUri()).parseDatalogFile(new File(rw.getFilename())); assert (program.getRules().size() == 1); ConjunctiveQuery q = program.getRules().get(0); FilterRewriterDB2 qCompiler = new FilterRewriterDB2(rw.getProject(), connection); String sql = qCompiler.filter(q, rw.isNames(), rw.isCount(), rw.isAvo()).toString(); System.out.println(sql); } catch (FileNotFoundException ex) { Logger.getLogger(CombinedAppInterface.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(CombinedAppInterface.class.getName()).log(Level.SEVERE, null, ex); } } private static void generateDatalog(CommandRewriteDatalog rw) { try { NRDatalogProgram program = new QueryParser(rw.getUri()).parseDatalogFile(new File(rw.getFilename())); program.setHeadPredicate("Q"); NRDatalogToPEQConverter converter = new NRDatalogToPEQConverter(); Query q = converter.convert(program); FilterRewriterDB2 qCompiler = new FilterRewriterDB2(rw.getProject(), connection); String sql = qCompiler.rewrite(q, rw.isNames(), rw.isCount(), true).toString(); System.out.println(sql); } catch (FileNotFoundException ex) { Logger.getLogger(CombinedAppInterface.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(CombinedAppInterface.class.getName()).log(Level.SEVERE, null, ex); } } private static void dropFilter() { dropFilter("IS_REAL_TUPLE"); dropFilter("FAKE_FILTER"); } private static void dropFilter(String namePattern) { // TODO: add a stored procedure for filter dropping try { final QueryRunner qRunner = new QueryRunner(); qRunner.query( "SELECT funcname, parm_count FROM Syscat.Functions WHERE funcname LIKE '" + namePattern + "%'", new RowCallbackHandler() { @Override public void processRow(ResultSet rs) throws SQLException { StringBuilder query = new StringBuilder(); query.append("DROP FUNCTION ").append(rs.getString(1)).append("("); int noOfParams = rs.getInt(2); for (int i = 0; i < noOfParams; i++) { query.append("INTEGER"); if (i != noOfParams - 1) { query.append(","); } } query.append(")"); qRunner.update(connection, query.toString()); } }); } catch (SQLException ex) { throw new RuntimeException(ex); } } }