diff options
| author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2022-05-10 18:17:06 +0100 |
|---|---|---|
| committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2022-05-11 12:34:47 +0100 |
| commit | 17bd9beaf7f358a44e5bf36a5855fe6727d506dc (patch) | |
| tree | 47e9310a0cff869d9ec017dcb2c81876407782c8 /src/main/java/uk/ac/ox/cs/pagoda/Pagoda.java | |
| parent | 8651164cd632a5db310b457ce32d4fbc97bdc41c (diff) | |
| download | ACQuA-17bd9beaf7f358a44e5bf36a5855fe6727d506dc.tar.gz ACQuA-17bd9beaf7f358a44e5bf36a5855fe6727d506dc.zip | |
[pagoda] Move project to Scala
This commit includes a few changes:
- The repository still uses Maven to manage dependency but it is now a
Scala project.
- The code has been ported from OWLAPI 3.4.10 to 5.1.20
- A proof of concept program using both RSAComb and PAGOdA has been
added.
Diffstat (limited to 'src/main/java/uk/ac/ox/cs/pagoda/Pagoda.java')
| -rw-r--r-- | src/main/java/uk/ac/ox/cs/pagoda/Pagoda.java | 259 |
1 files changed, 259 insertions, 0 deletions
diff --git a/src/main/java/uk/ac/ox/cs/pagoda/Pagoda.java b/src/main/java/uk/ac/ox/cs/pagoda/Pagoda.java new file mode 100644 index 0000000..6a1946e --- /dev/null +++ b/src/main/java/uk/ac/ox/cs/pagoda/Pagoda.java | |||
| @@ -0,0 +1,259 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda; | ||
| 2 | |||
| 3 | import org.apache.commons.cli.*; | ||
| 4 | import org.apache.commons.io.FilenameUtils; | ||
| 5 | import uk.ac.ox.cs.pagoda.query.QueryRecord; | ||
| 6 | import uk.ac.ox.cs.pagoda.reasoner.QueryReasoner; | ||
| 7 | import uk.ac.ox.cs.pagoda.util.PagodaProperties; | ||
| 8 | import uk.ac.ox.cs.pagoda.util.Timer; | ||
| 9 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 10 | |||
| 11 | import java.io.BufferedWriter; | ||
| 12 | import java.io.IOException; | ||
| 13 | import java.nio.file.Files; | ||
| 14 | import java.nio.file.Path; | ||
| 15 | import java.nio.file.Paths; | ||
| 16 | import java.util.Collection; | ||
| 17 | import java.util.HashMap; | ||
| 18 | import java.util.Map; | ||
| 19 | |||
| 20 | /** | ||
| 21 | * Executable command line user interface. | ||
| 22 | */ | ||
| 23 | public class Pagoda implements Runnable { | ||
| 24 | |||
| 25 | private static final String OPTION_ONTOLOGY = "o"; | ||
| 26 | private static final String OPTION_DATA = "t"; | ||
| 27 | private static final String OPTION_QUERY = "q"; | ||
| 28 | private static final String OPTION_ANSWER = "a"; | ||
| 29 | private static final String OPTION_CLASSIFY = "c"; | ||
| 30 | private static final String OPTION_HERMIT = "f"; | ||
| 31 | private static final String OPTION_SKOLEM = "s"; | ||
| 32 | private static final String OPTION_SKOLEM_DEPTH = "d"; | ||
| 33 | private final PagodaProperties properties; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * Do not use it | ||
| 37 | */ | ||
| 38 | private Pagoda() { | ||
| 39 | properties = new PagodaProperties(); | ||
| 40 | } | ||
| 41 | |||
| 42 | public static void main(String... args) { | ||
| 43 | |||
| 44 | // TODO treat the mandatory options as simple args | ||
| 45 | Options options = new Options(); | ||
| 46 | options.addOption(Option.builder(OPTION_ONTOLOGY) | ||
| 47 | .argName(OPTION_ONTOLOGY) | ||
| 48 | .required() | ||
| 49 | .hasArg() | ||
| 50 | .desc("The ontology path") | ||
| 51 | .build()); | ||
| 52 | options.addOption(Option.builder(OPTION_DATA).argName(OPTION_DATA).hasArg().desc("The data path").build()); | ||
| 53 | options.addOption(Option.builder(OPTION_QUERY) | ||
| 54 | .argName(OPTION_QUERY) | ||
| 55 | .required() | ||
| 56 | .hasArg() | ||
| 57 | .desc("The query path") | ||
| 58 | .build()); | ||
| 59 | options.addOption(Option.builder(OPTION_ANSWER) | ||
| 60 | .argName(OPTION_ANSWER) | ||
| 61 | .hasArg() | ||
| 62 | .desc("The answer path") | ||
| 63 | .build()); | ||
| 64 | options.addOption(Option.builder(OPTION_CLASSIFY) | ||
| 65 | .argName(OPTION_CLASSIFY) | ||
| 66 | .desc("Tell whether to classify") | ||
| 67 | .type(Boolean.class) | ||
| 68 | .build()); | ||
| 69 | options.addOption(Option.builder(OPTION_HERMIT) | ||
| 70 | .argName(OPTION_HERMIT) | ||
| 71 | .desc("Tell whether to call Hermit") | ||
| 72 | .type(Boolean.class) | ||
| 73 | .build()); | ||
| 74 | options.addOption(Option.builder(OPTION_SKOLEM) | ||
| 75 | .argName(OPTION_SKOLEM) | ||
| 76 | .hasArg() | ||
| 77 | .desc("Tell if and how to apply Skolemisation") | ||
| 78 | .build()); | ||
| 79 | options.addOption(Option.builder(OPTION_SKOLEM_DEPTH) | ||
| 80 | .argName(OPTION_SKOLEM_DEPTH) | ||
| 81 | .hasArg() | ||
| 82 | .desc("Tell how deep to skolemise") | ||
| 83 | .build()); | ||
| 84 | |||
| 85 | CommandLineParser parser = new DefaultParser(); | ||
| 86 | try { | ||
| 87 | CommandLine cmd = parser.parse(options, args); | ||
| 88 | PagodaBuilder pagodaBuilder = Pagoda.builder() | ||
| 89 | .ontology(cmd.getOptionValue(OPTION_ONTOLOGY)) | ||
| 90 | .query(cmd.getOptionValue(OPTION_QUERY)); | ||
| 91 | if(cmd.hasOption(OPTION_DATA)) pagodaBuilder.data(cmd.getOptionValue(OPTION_DATA)); | ||
| 92 | if(cmd.hasOption(OPTION_ANSWER)) pagodaBuilder.answer(cmd.getOptionValue(OPTION_ANSWER)); | ||
| 93 | if(cmd.hasOption(OPTION_CLASSIFY)) | ||
| 94 | pagodaBuilder.classify(Boolean.parseBoolean(cmd.getOptionValue(OPTION_CLASSIFY))); | ||
| 95 | if(cmd.hasOption(OPTION_HERMIT)) | ||
| 96 | pagodaBuilder.hermit(Boolean.parseBoolean(cmd.getOptionValue(OPTION_HERMIT))); | ||
| 97 | if(cmd.hasOption(OPTION_SKOLEM)) | ||
| 98 | pagodaBuilder.skolem(PagodaProperties.SkolemUpperBoundOptions.valueOf(cmd.getOptionValue(OPTION_SKOLEM))); | ||
| 99 | if(cmd.hasOption(OPTION_SKOLEM_DEPTH)) | ||
| 100 | pagodaBuilder.skolemDepth(Integer.parseInt(cmd.getOptionValue(OPTION_SKOLEM_DEPTH))); | ||
| 101 | |||
| 102 | pagodaBuilder.build().run(); | ||
| 103 | } catch(ParseException exp) { | ||
| 104 | HelpFormatter formatter = new HelpFormatter(); | ||
| 105 | formatter.printHelp("PAGOdA", options); | ||
| 106 | Utility.logError("Parsing failed. Reason: " + exp.getMessage()); | ||
| 107 | System.exit(0); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | /** | ||
| 112 | * Get a builder. | ||
| 113 | */ | ||
| 114 | public static PagodaBuilder builder() { | ||
| 115 | return new PagodaBuilder(); | ||
| 116 | } | ||
| 117 | |||
| 118 | @Override | ||
| 119 | public void run() { | ||
| 120 | Utility.logInfo("Ontology file: " + properties.getOntologyPath()); | ||
| 121 | Utility.logInfo("Data files: " + properties.getDataPath()); | ||
| 122 | Utility.logInfo("Query files: " + properties.getQueryPath()); | ||
| 123 | Utility.logInfo("Answer file: " + properties.getAnswerPath()); | ||
| 124 | Utility.logInfo("Skolemisation: " + properties.getSkolemUpperBound()); | ||
| 125 | Utility.logInfo("Skolemisation depth: " + properties.getSkolemDepth()); | ||
| 126 | |||
| 127 | QueryReasoner pagoda = null; | ||
| 128 | |||
| 129 | try { | ||
| 130 | Timer t = new Timer(); | ||
| 131 | pagoda = QueryReasoner.getInstance(properties); | ||
| 132 | if(pagoda == null) return; | ||
| 133 | |||
| 134 | Utility.logInfo("Preprocessing Done in " + t.duration() + " seconds."); | ||
| 135 | |||
| 136 | if(properties.getQueryPath() != null) { | ||
| 137 | for(String queryFile : properties.getQueryPath().split(";")) { | ||
| 138 | Collection<QueryRecord> queryRecords = pagoda.getQueryManager().collectQueryRecords(queryFile); | ||
| 139 | pagoda.evaluate(queryRecords); | ||
| 140 | |||
| 141 | if(PagodaProperties.isDebuggingMode()) { | ||
| 142 | HashMap<String, Map<String, String>> statistics = new HashMap<>(); | ||
| 143 | for(QueryRecord queryRecord : queryRecords) { | ||
| 144 | statistics.put(queryRecord.getQueryID(), queryRecord.getStatistics()); | ||
| 145 | } | ||
| 146 | String statisticsFilename = getStatisticsFilename(properties, queryFile); | ||
| 147 | try(BufferedWriter writer = Files.newBufferedWriter(Paths.get(statisticsFilename))) { | ||
| 148 | QueryRecord.GsonCreator.getInstance().toJson(statistics, writer); | ||
| 149 | } catch(IOException e) { | ||
| 150 | Utility.logError("Unable to save statistics"); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | } finally { | ||
| 156 | if(pagoda != null) pagoda.dispose(); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | private String getStatisticsFilename(PagodaProperties properties, String queryFile) { | ||
| 161 | String statisticsFilename = "statistics"; | ||
| 162 | statisticsFilename += "_" + FilenameUtils.removeExtension(FilenameUtils.getName(properties.getOntologyPath().replaceAll("_", "-"))); | ||
| 163 | if(properties.getDataPath() != null) | ||
| 164 | statisticsFilename += "_" + FilenameUtils.removeExtension(FilenameUtils.getName(properties.getDataPath().replaceAll("_", "-"))); | ||
| 165 | statisticsFilename += "_" + FilenameUtils.removeExtension(FilenameUtils.getName(queryFile).replaceAll("_", "-")); | ||
| 166 | statisticsFilename += "_" + ((properties.getSkolemUpperBound() == PagodaProperties.SkolemUpperBoundOptions.DISABLED) | ||
| 167 | ? "" : (properties.getSkolemUpperBound() == PagodaProperties.SkolemUpperBoundOptions.BEFORE_SUMMARISATION) | ||
| 168 | ? "before" : "after"); | ||
| 169 | statisticsFilename += "_skd" + properties.getSkolemDepth(); | ||
| 170 | statisticsFilename += "_maxtrpl" + properties.getMaxTriplesInSkolemStore(); | ||
| 171 | statisticsFilename += ".json"; | ||
| 172 | statisticsFilename = FilenameUtils.concat(properties.getStatisticsDir().toString(), | ||
| 173 | statisticsFilename); | ||
| 174 | return statisticsFilename; | ||
| 175 | } | ||
| 176 | |||
| 177 | /** | ||
| 178 | * Allows to set the parameters before creating a Pagoda instance. | ||
| 179 | */ | ||
| 180 | public static class PagodaBuilder { | ||
| 181 | |||
| 182 | private Pagoda instance; | ||
| 183 | |||
| 184 | private PagodaBuilder() { | ||
| 185 | instance = new Pagoda(); | ||
| 186 | } | ||
| 187 | |||
| 188 | public PagodaBuilder ontology(String ontologyPath) { | ||
| 189 | if(instance == null) return null; | ||
| 190 | instance.properties.setOntologyPath(ontologyPath); | ||
| 191 | return this; | ||
| 192 | } | ||
| 193 | |||
| 194 | public PagodaBuilder ontology(Path ontologyPath) { | ||
| 195 | return ontology(ontologyPath.toString()); | ||
| 196 | } | ||
| 197 | |||
| 198 | public PagodaBuilder data(String dataPath) { | ||
| 199 | if(instance == null) return null; | ||
| 200 | instance.properties.setDataPath(dataPath); | ||
| 201 | return this; | ||
| 202 | } | ||
| 203 | |||
| 204 | public PagodaBuilder data(Path dataPath) { | ||
| 205 | return data(dataPath.toString()); | ||
| 206 | } | ||
| 207 | |||
| 208 | public PagodaBuilder query(String queryPath) { | ||
| 209 | if(instance == null) return null; | ||
| 210 | instance.properties.setQueryPath(queryPath); | ||
| 211 | return this; | ||
| 212 | } | ||
| 213 | |||
| 214 | public PagodaBuilder query(Path queryPath) { | ||
| 215 | return query(queryPath.toString()); | ||
| 216 | } | ||
| 217 | |||
| 218 | public PagodaBuilder answer(String answerPath) { | ||
| 219 | if(instance == null) return null; | ||
| 220 | instance.properties.setAnswerPath(answerPath); | ||
| 221 | return this; | ||
| 222 | } | ||
| 223 | |||
| 224 | public PagodaBuilder answer(Path answerPath) { | ||
| 225 | return answer(answerPath.toString()); | ||
| 226 | } | ||
| 227 | |||
| 228 | public PagodaBuilder classify(Boolean toClassify) { | ||
| 229 | if(instance == null) return null; | ||
| 230 | instance.properties.setToClassify(toClassify); | ||
| 231 | return this; | ||
| 232 | } | ||
| 233 | |||
| 234 | public PagodaBuilder hermit(Boolean callHermit) { | ||
| 235 | if(instance == null) return null; | ||
| 236 | instance.properties.setToCallHermiT(callHermit); | ||
| 237 | return this; | ||
| 238 | } | ||
| 239 | |||
| 240 | public PagodaBuilder skolem(PagodaProperties.SkolemUpperBoundOptions option) { | ||
| 241 | if(instance == null) return null; | ||
| 242 | instance.properties.setSkolemUpperBound(option); | ||
| 243 | return this; | ||
| 244 | } | ||
| 245 | |||
| 246 | public PagodaBuilder skolemDepth(int depth) { | ||
| 247 | if(instance == null) return null; | ||
| 248 | instance.properties.setSkolemDepth(depth); | ||
| 249 | return this; | ||
| 250 | } | ||
| 251 | |||
| 252 | public Pagoda build() { | ||
| 253 | Pagoda builtInstance = instance; | ||
| 254 | instance = null; | ||
| 255 | return builtInstance; | ||
| 256 | } | ||
| 257 | |||
| 258 | } | ||
| 259 | } | ||
