diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala | 85 |
1 files changed, 39 insertions, 46 deletions
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala index f54884f..0554dbc 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala | |||
| @@ -1,37 +1,41 @@ | |||
| 1 | package uk.ac.ox.cs.rsacomb | 1 | package uk.ac.ox.cs.rsacomb |
| 2 | 2 | ||
| 3 | /* Java imports */ | ||
| 4 | import java.io.File | 3 | import java.io.File |
| 5 | import java.util.HashMap | 4 | import java.util.HashMap |
| 6 | import scala.collection.JavaConverters._ | 5 | import scala.collection.JavaConverters._ |
| 7 | |||
| 8 | import tech.oxfordsemantic.jrdfox.client.UpdateType | 6 | import tech.oxfordsemantic.jrdfox.client.UpdateType |
| 9 | import tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery | ||
| 10 | import tech.oxfordsemantic.jrdfox.logic.expression.{IRI, Term} | 7 | import tech.oxfordsemantic.jrdfox.logic.expression.{IRI, Term} |
| 8 | import tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery | ||
| 11 | 9 | ||
| 12 | /* Local imports */ | ||
| 13 | import util.{Logger, RDFoxUtil, RSA} | 10 | import util.{Logger, RDFoxUtil, RSA} |
| 14 | import sparql.ConjunctiveQuery | 11 | import sparql.ConjunctiveQuery |
| 15 | 12 | ||
| 13 | /** Entry point of the program. | ||
| 14 | * | ||
| 15 | * The executable expects a SPARQL query and a non-empty sequence of | ||
| 16 | * ontology files as arguments. The query file is expected to contain | ||
| 17 | * exactly one query, while the ontology files will be programmatically | ||
| 18 | * merged in a single ontology. | ||
| 19 | * | ||
| 20 | * @todo better arguments handling is needed. Look into some library | ||
| 21 | * for this. | ||
| 22 | * @todo at the moment the input ontology is assumed to be Horn-ALCHOIQ. | ||
| 23 | * This might not be the case. | ||
| 24 | */ | ||
| 16 | object RSAComb extends App { | 25 | object RSAComb extends App { |
| 17 | 26 | ||
| 18 | val help: String = """ | 27 | val help: String = """ |
| 19 | rsacomb - combined approach for CQ answering for RSA ontologies. | 28 | rsacomb - combined approach for CQ answering for RSA ontologies. |
| 20 | 29 | ||
| 21 | USAGE | 30 | USAGE |
| 22 | rsacomb <query> <ontology> ... | 31 | rsacomb <query> <ontology> [...] |
| 23 | 32 | ||
| 24 | where | 33 | where |
| 25 | - query: a (single) SPARQL query file. | 34 | - query: path to a file containing a single SPARQL query |
| 26 | - ontology: one or more ontologies. | 35 | - ontology: one or more ontology files |
| 27 | 36 | ||
| 28 | """ | 37 | """ |
| 29 | 38 | ||
| 30 | /* Simple arguments handling | ||
| 31 | * | ||
| 32 | * TODO: use something better later on | ||
| 33 | */ | ||
| 34 | |||
| 35 | if (args.length < 2) { | 39 | if (args.length < 2) { |
| 36 | println(help) | 40 | println(help) |
| 37 | sys.exit; | 41 | sys.exit; |
| @@ -46,52 +50,41 @@ object RSAComb extends App { | |||
| 46 | sys.exit; | 50 | sys.exit; |
| 47 | } | 51 | } |
| 48 | 52 | ||
| 49 | /* TODO: It might be required to check if the ontology in input is | ||
| 50 | * Horn-ALCHOIQ. At the moment we are assuming this is always the | ||
| 51 | * case. | ||
| 52 | */ | ||
| 53 | |||
| 54 | val ontology = RSAOntology(ontoPaths: _*) | 53 | val ontology = RSAOntology(ontoPaths: _*) |
| 55 | if (ontology.isRSA) { | 54 | if (ontology.isRSA) { |
| 56 | 55 | ||
| 57 | Logger print "Ontology is RSA!" | 56 | Logger print "Ontology is RSA!" |
| 58 | 57 | ||
| 59 | /** Read SPARQL query from file */ | 58 | val query = RDFoxUtil.loadQueryFromFile(queryPath.getAbsoluteFile) |
| 60 | val strQuery = RDFoxUtil.loadQueryFromFile(queryPath.getAbsoluteFile) | ||
| 61 | val query = ConjunctiveQuery parse strQuery | ||
| 62 | 59 | ||
| 63 | query match { | 60 | ConjunctiveQuery.parse(query) match { |
| 64 | case Some(query) => { | 61 | case Some(query) => { |
| 65 | val answers = ontology ask query | 62 | val answers = ontology ask query |
| 66 | Logger.print(s"$answers", Logger.QUIET) | 63 | Logger.print(s"$answers", Logger.QUIET) |
| 67 | Logger print s"Number of answer: ${answers.length} (${answers.lengthWithMultiplicity})" | 64 | Logger print s"Number of answer: ${answers.length} (${answers.lengthWithMultiplicity})" |
| 68 | 65 | ||
| 69 | val unfiltered = ontology askUnfiltered query | 66 | /* Additional DEBUG information */ |
| 70 | unfiltered map { u => | 67 | if (Logger.level >= Logger.DEBUG) { |
| 71 | Logger.print( | 68 | /* Unfiltered rules */ |
| 72 | s"Number of unfiltered answers: ${u.length} (${u.map(_._1).sum}).", | 69 | val unfiltered = ontology askUnfiltered query |
| 73 | Logger.DEBUG | 70 | unfiltered map { u => |
| 74 | ) | 71 | Logger print s"Number of unfiltered answers: ${u.length} (${u.map(_._1).sum})." |
| 75 | //u foreach println | 72 | |
| 76 | val spurious = { | 73 | /* Spurious answers */ |
| 77 | val sp = | 74 | val spurious = { |
| 78 | RDFoxUtil.buildDescriptionQuery("SP", query.variables.length) | 75 | val variables = query.variables.length |
| 79 | ontology.queryDataStore(query, sp, RSA.Prefixes) | 76 | val sp = RDFoxUtil.buildDescriptionQuery("SP", variables) |
| 80 | } | 77 | ontology.queryDataStore(query, sp, RSA.Prefixes) |
| 81 | spurious map { s => | 78 | } |
| 82 | Logger.print( | 79 | spurious map { s => |
| 83 | s"Number of spurious answers: ${s.length} (${s.map(_._1).sum})", | 80 | Logger print s"Number of spurious answers: ${s.length} (${s.map(_._1).sum})" |
| 84 | Logger.DEBUG | 81 | |
| 85 | ) | 82 | /* Spurious/unfiltered percentage */ |
| 86 | //s foreach println | 83 | val perc = |
| 87 | val perc = | 84 | if (u.length > 0) (s.length / u.length.toFloat) * 100 else 0 |
| 88 | if (u.length > 0) (s.length / u.length.toFloat) * 100 else 0 | 85 | Logger print s"Percentage of spurious answers: $perc%" |
| 89 | Logger.print( | 86 | } |
| 90 | s"Percentage of spurious answers: $perc%", | ||
| 91 | Logger.DEBUG | ||
| 92 | ) | ||
| 93 | } | 87 | } |
| 94 | |||
| 95 | } | 88 | } |
| 96 | } | 89 | } |
| 97 | case None => | 90 | case None => |
