diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/Pagoda.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/Pagoda.java | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/Pagoda.java b/src/uk/ac/ox/cs/pagoda/Pagoda.java new file mode 100644 index 0000000..aeb85a7 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/Pagoda.java | |||
| @@ -0,0 +1,187 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda; | ||
| 2 | |||
| 3 | import org.apache.commons.cli.*; | ||
| 4 | import uk.ac.ox.cs.pagoda.reasoner.QueryReasoner; | ||
| 5 | import uk.ac.ox.cs.pagoda.util.PagodaProperties; | ||
| 6 | import uk.ac.ox.cs.pagoda.util.Timer; | ||
| 7 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 8 | |||
| 9 | import java.nio.file.Path; | ||
| 10 | |||
| 11 | /** | ||
| 12 | * Executable command line user interface. | ||
| 13 | */ | ||
| 14 | public class Pagoda implements Runnable { | ||
| 15 | |||
| 16 | private static final String OPTION_ONTOLOGY = "o"; | ||
| 17 | private static final String OPTION_DATA = "d"; | ||
| 18 | private static final String OPTION_QUERY = "q"; | ||
| 19 | private static final String OPTION_ANSWER = "a"; | ||
| 20 | private static final String OPTION_CLASSIFY = "c"; | ||
| 21 | private static final String OPTION_HERMIT = "f"; | ||
| 22 | private final PagodaProperties properties; | ||
| 23 | |||
| 24 | /** | ||
| 25 | * Do not use it | ||
| 26 | * */ | ||
| 27 | private Pagoda() { | ||
| 28 | properties = new PagodaProperties(); | ||
| 29 | } | ||
| 30 | |||
| 31 | public static void main(String... args) { | ||
| 32 | |||
| 33 | // TODO treat the mandatory options as simple args | ||
| 34 | Options options = new Options(); | ||
| 35 | options.addOption(Option.builder(OPTION_ONTOLOGY) | ||
| 36 | .argName(OPTION_ONTOLOGY) | ||
| 37 | .required() | ||
| 38 | .hasArg() | ||
| 39 | .desc("The ontology path") | ||
| 40 | .build()); | ||
| 41 | options.addOption(Option.builder(OPTION_DATA).argName(OPTION_DATA).hasArg().desc("The data path").build()); | ||
| 42 | options.addOption(Option.builder(OPTION_QUERY) | ||
| 43 | .argName(OPTION_QUERY) | ||
| 44 | .required() | ||
| 45 | .hasArg() | ||
| 46 | .desc("The query path") | ||
| 47 | .build()); | ||
| 48 | options.addOption(Option.builder(OPTION_ANSWER) | ||
| 49 | .argName(OPTION_ANSWER) | ||
| 50 | .hasArg() | ||
| 51 | .desc("The answer path") | ||
| 52 | .build()); | ||
| 53 | options.addOption(Option.builder(OPTION_CLASSIFY) | ||
| 54 | .argName(OPTION_CLASSIFY) | ||
| 55 | .desc("Tell whether to classify") | ||
| 56 | .type(Boolean.class) | ||
| 57 | .build()); | ||
| 58 | options.addOption(Option.builder(OPTION_HERMIT) | ||
| 59 | .argName(OPTION_HERMIT) | ||
| 60 | .desc("Tell whether to call Hermit") | ||
| 61 | .type(Boolean.class) | ||
| 62 | .build()); | ||
| 63 | |||
| 64 | CommandLineParser parser = new DefaultParser(); | ||
| 65 | try { | ||
| 66 | CommandLine cmd = parser.parse(options, args); | ||
| 67 | PagodaBuilder pagodaBuilder = Pagoda.builder() | ||
| 68 | .ontology(cmd.getOptionValue(OPTION_ONTOLOGY)) | ||
| 69 | .query(cmd.getOptionValue(OPTION_QUERY)); | ||
| 70 | if(cmd.hasOption(OPTION_DATA)) pagodaBuilder.data(cmd.getOptionValue(OPTION_DATA)); | ||
| 71 | if(cmd.hasOption(OPTION_ANSWER)) pagodaBuilder.answer(cmd.getOptionValue(OPTION_ANSWER)); | ||
| 72 | if(cmd.hasOption(OPTION_CLASSIFY)) | ||
| 73 | pagodaBuilder.classify(Boolean.parseBoolean(cmd.getOptionValue(OPTION_CLASSIFY))); | ||
| 74 | if(cmd.hasOption(OPTION_HERMIT)) | ||
| 75 | pagodaBuilder.hermit(Boolean.parseBoolean(cmd.getOptionValue(OPTION_HERMIT))); | ||
| 76 | |||
| 77 | pagodaBuilder.build().run(); | ||
| 78 | } catch(ParseException exp) { | ||
| 79 | HelpFormatter formatter = new HelpFormatter(); | ||
| 80 | formatter.printHelp("PAGOdA", options); | ||
| 81 | Utility.logError("Parsing failed. Reason: " + exp.getMessage()); | ||
| 82 | System.exit(0); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | /** | ||
| 87 | * Get a builder. | ||
| 88 | * */ | ||
| 89 | public static PagodaBuilder builder() { | ||
| 90 | return new PagodaBuilder(); | ||
| 91 | } | ||
| 92 | |||
| 93 | @Override | ||
| 94 | public void run() { | ||
| 95 | Utility.logInfo("Ontology file: " + properties.getOntologyPath()); | ||
| 96 | Utility.logInfo("Data files: " + properties.getDataPath()); | ||
| 97 | Utility.logInfo("Query files: " + properties.getQueryPath()); | ||
| 98 | Utility.logInfo("Answer file: " + properties.getAnswerPath()); | ||
| 99 | |||
| 100 | QueryReasoner pagoda = null; | ||
| 101 | |||
| 102 | try { | ||
| 103 | Timer t = new Timer(); | ||
| 104 | pagoda = QueryReasoner.getInstance(properties); | ||
| 105 | if (pagoda == null) return; | ||
| 106 | |||
| 107 | Utility.logInfo("Preprocessing Done in " + t.duration() + " seconds."); | ||
| 108 | |||
| 109 | if (properties.getQueryPath() != null) | ||
| 110 | for (String queryFile: properties.getQueryPath().split(";")) | ||
| 111 | pagoda.evaluate(pagoda.getQueryManager().collectQueryRecords(queryFile)); | ||
| 112 | } finally { | ||
| 113 | if (pagoda != null) pagoda.dispose(); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | /** | ||
| 118 | * Allows to set the parameters before creating a Pagoda instance. | ||
| 119 | * */ | ||
| 120 | public static class PagodaBuilder { | ||
| 121 | |||
| 122 | private Pagoda instance; | ||
| 123 | |||
| 124 | private PagodaBuilder() { | ||
| 125 | instance = new Pagoda(); | ||
| 126 | } | ||
| 127 | |||
| 128 | public PagodaBuilder ontology(String ontologyPath) { | ||
| 129 | if(instance == null) return null; | ||
| 130 | instance.properties.setOntologyPath(ontologyPath); | ||
| 131 | return this; | ||
| 132 | } | ||
| 133 | |||
| 134 | public PagodaBuilder ontology(Path ontologyPath) { | ||
| 135 | return ontology(ontologyPath.toString()); | ||
| 136 | } | ||
| 137 | |||
| 138 | public PagodaBuilder data(String dataPath) { | ||
| 139 | if(instance == null) return null; | ||
| 140 | instance.properties.setDataPath(dataPath); | ||
| 141 | return this; | ||
| 142 | } | ||
| 143 | |||
| 144 | public PagodaBuilder data(Path dataPath) { | ||
| 145 | return data(dataPath.toString()); | ||
| 146 | } | ||
| 147 | |||
| 148 | public PagodaBuilder query(String queryPath) { | ||
| 149 | if(instance == null) return null; | ||
| 150 | instance.properties.setQueryPath(queryPath); | ||
| 151 | return this; | ||
| 152 | } | ||
| 153 | |||
| 154 | public PagodaBuilder query(Path queryPath) { | ||
| 155 | return query(queryPath.toString()); | ||
| 156 | } | ||
| 157 | |||
| 158 | public PagodaBuilder answer(String answerPath) { | ||
| 159 | if(instance == null) return null; | ||
| 160 | instance.properties.setAnswerPath(answerPath); | ||
| 161 | return this; | ||
| 162 | } | ||
| 163 | |||
| 164 | public PagodaBuilder answer(Path answerPath) { | ||
| 165 | return answer(answerPath.toString()); | ||
| 166 | } | ||
| 167 | |||
| 168 | public PagodaBuilder classify(Boolean toClassify) { | ||
| 169 | if(instance == null) return null; | ||
| 170 | instance.properties.setToClassify(toClassify); | ||
| 171 | return this; | ||
| 172 | } | ||
| 173 | |||
| 174 | public PagodaBuilder hermit(Boolean callHermit) { | ||
| 175 | if(instance == null) return null; | ||
| 176 | instance.properties.setToCallHermiT(callHermit); | ||
| 177 | return this; | ||
| 178 | } | ||
| 179 | |||
| 180 | public Pagoda build() { | ||
| 181 | Pagoda builtInstance = instance; | ||
| 182 | instance = null; | ||
| 183 | return builtInstance; | ||
| 184 | } | ||
| 185 | |||
| 186 | } | ||
| 187 | } | ||
