From b7d9a8ffc8cc4a8949229b5bd3ada301878633d9 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Tue, 24 Nov 2020 11:41:08 +0000 Subject: Rework filtering program generation --- .../ac/ox/cs/rsacomb/FilteringProgramSpecs.scala | 343 ++++----------------- 1 file changed, 59 insertions(+), 284 deletions(-) (limited to 'src/test/scala/uk/ac/ox/cs') diff --git a/src/test/scala/uk/ac/ox/cs/rsacomb/FilteringProgramSpecs.scala b/src/test/scala/uk/ac/ox/cs/rsacomb/FilteringProgramSpecs.scala index e9a20db..71c9a99 100644 --- a/src/test/scala/uk/ac/ox/cs/rsacomb/FilteringProgramSpecs.scala +++ b/src/test/scala/uk/ac/ox/cs/rsacomb/FilteringProgramSpecs.scala @@ -1,313 +1,88 @@ package rsacomb -import java.io.File -import java.util.{ArrayList => JList} -import org.scalatest.LoneElement -import org.scalatest.Inspectors import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers - -import tech.oxfordsemantic.jrdfox.logic.datalog.TupleTableAtom -import tech.oxfordsemantic.jrdfox.logic.expression.{Variable, IRI} -import tech.oxfordsemantic.jrdfox.logic.sparql.statement.{Query, SelectQuery} -import tech.oxfordsemantic.jrdfox.Prefixes - -import scala.collection.JavaConverters._ - +import tech.oxfordsemantic.jrdfox.logic.expression.IRI import uk.ac.ox.cs.rsacomb.FilteringProgram -import uk.ac.ox.cs.rsacomb.util.RDFoxHelpers +import uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQuery object FilteringProgramSpec { - val prefixes = new Prefixes() - prefixes.declarePrefix( - ":", - "http://slegger.gitlab.io/slegge-obda/ontology/subsurface-exploration#" - ) - prefixes.declarePrefix("rdf:", "http://www.w3.org/1999/02/22-rdf-syntax-ns#") - prefixes.declarePrefix("rdfs:", "http://www.w3.org/2000/01/rdf-schema#") - prefixes.declarePrefix("owl:", "http://www.w3.org/2002/07/owl#") - - // DEBUG: Quick helper functions - def v(v: String): Variable = Variable.create(v) - def c(c: String): IRI = IRI.create(":" + c) - - // QUERY 0 - - val query0 = RDFoxHelpers - .parseSelectQuery(""" - SELECT ?subj - WHERE { - ?subj ?pred ?obj - } - """, prefixes) - .get - - // val query0 = Query.create( - // QueryType.SELECT, - // false, - // List(v("subj")).asJava, - // Atom.rdf(v("subj"), v("pred"), v("obj")) - // ) - - // QUERY 1 - - val query1 = RDFoxHelpers - .parseSelectQuery(""" - SELECT * - WHERE { - ?w a :Wellbore - } - """, prefixes) - .get - - // val query1 = Query.create( - // QueryType.SELECT, - // false, - // List(v("w")).asJava, - // Atom.rdf(v("w"), IRI.RDF_TYPE, c("Wellbore")) - // ) - - // QUERY 2 - - val query2 = RDFoxHelpers - .parseSelectQuery( - """ - SELECT * - WHERE { - ?w a :Wellbore ; - :wellboreDocument ?doc . - ?doc :hasURL ?document_hyperlink - } - """, - prefixes - ) - .get + val constants = + List(IRI.create("_:iri1"), IRI.create("_:iri2"), IRI.create("_:iri3")) - // val query2 = Query.create( - // QueryType.SELECT, - // false, - // List(v("w"), v("doc"), v("document_hyperlink")).asJava, - // Conjunction.create( - // Atom.rdf(v("w"), IRI.RDF_TYPE, c("Wellbore")), - // Atom.rdf(v("w"), c("wellboreDocument"), v("doc")), - // Atom.rdf(v("doc"), c("hasURL"), v("document_hyperlink")) - // ) - // ) + val cq0 = """ + PREFIX : - // QUERY 3 - - val query3 = RDFoxHelpers - .parseSelectQuery( - """ - SELECT ?wellbore ?formation_pressure + SELECT ?X WHERE { - ?w a :Wellbore ; - :name ?wellbore ; - :hasFormationPressure ?fp . - ?fp :valueInStandardUnit ?formation_pressure + ?X a :D ; + :R ?Y . + ?Y :S ?Z . + ?Z a :D . } - """, - prefixes - ) - .get - - // val query3 = Query.create( - // QueryType.SELECT, - // false, - // List(v("wellbore"), v("formation_pressure")).asJava, - // Conjunction.create( - // Atom.rdf(v("w"), IRI.RDF_TYPE, c("Wellbore")), - // Atom.rdf(v("w"), c("name"), v("wellbore")), - // Atom.rdf(v("w"), c("hasFormationPressure"), v("fp")), - // Atom.rdf(v("fp"), c("valueInStandardUnit"), v("formation_pressure")) - // ) - // ) + """ - // QUERY 4 + val cq1 = """ + PREFIX : - val query4 = RDFoxHelpers - .parseSelectQuery( - """ - SELECT * + SELECT * WHERE { - ?w a :Wellbore ; - :hasGeochemicalMeasurement ?measurement . - ?measurement :cgType ?cgtype ; - :peakName ?peakType ; - :peakHeight ?peak_height ; - :peakAmount ?peak_amount + ?X a :D ; + :R ?Y . + ?Y :S ?Z . + ?Z a :D . } - """, - prefixes - ) - .get + """ - // val query4 = Query.create( - // QueryType.SELECT, - // false, - // List( - // v("w"), - // v("measurement"), - // v("cgtype"), - // v("peakType"), - // v("peak_height"), - // v("peak_amount") - // ).asJava, - // Conjunction.create( - // Atom.rdf(v("w"), IRI.RDF_TYPE, c("Wellbore")), - // Atom.rdf(v("w"), c("hasGeochemicalMeasurement"), v("measurement")), - // Atom.rdf(v("measurement"), c("cgType"), v("cgtype")), - // Atom.rdf(v("measurement"), c("peakName"), v("peakType")), - // Atom.rdf(v("measurement"), c("peakHeight"), v("peak_height")), - // Atom.rdf(v("measurement"), c("peakAmount"), v("peak_amount")) - // ) - // ) + val cq2 = """ + PREFIX : - // QUERY 5 - - val query5 = RDFoxHelpers - .parseSelectQuery( - """ - SELECT ?wellbore ?unit_name ?discovery + SELECT ?X WHERE { - ?w a :Wellbore ; - :name ?wellbore ; - :hasWellboreInterval ?c_int ; - :hasWellboreInterval ?f_int . - ?c_int :hasUnit ?c_unit . - ?c_unit :name ?unit_name . - ?f_int a :FluidZone ; - :name ?discovery ; - :overlapsWellboreInterval ?c_int + ?X a :D ; + :R ?Y . + ?Y :S ?Z . + ?Y :T ?W . + ?Z a :D . + ?W a :D } - """, - prefixes - ) - .get - - // val query5 = Query.create( - // QueryType.SELECT, - // false, - // List(v("wellbore"), v("unit_name"), v("discovery")).asJava, - // Conjunction.create( - // Atom.rdf(v("w"), IRI.RDF_TYPE, c("Wellbore")), - // Atom.rdf(v("w"), c("name"), v("wellbore")), - // Atom.rdf(v("w"), c("hasWellboreInterval"), v("c_int")), - // Atom.rdf(v("w"), c("hasWellboreInterval"), v("f_int")), - // Atom.rdf(v("c_int"), c("hasUnit"), v("c_unit")), - // Atom.rdf(v("c_unit"), c("name"), v("unit_name")), - // Atom.rdf(v("f_int"), IRI.RDF_TYPE, c("FluidZone")), - // Atom.rdf(v("f_int"), c("name"), v("discovery")), - // Atom.rdf(v("f_int"), c("overlapsWellboreInterval"), v("c_int")) - // ) - // ) + """ - // QUERY 6 + val bcq0 = """ + PREFIX : - val query6 = RDFoxHelpers - .parseSelectQuery( - """ - SELECT DISTINCT ?wellbore ?content - WHERE { - ?w a :Wellbore ; - :name ?wellbore ; - :hasWellboreInterval ?int . - ?int a :FluidZone ; - :fluidZoneContent ?content + ASK { + ?X a :D ; + :R ?Y . + ?Y :S ?Z . + ?Z a :D . } - """, - prefixes - ) - .get - - // val query6 = Query.create( - // QueryType.SELECT, - // true, - // List(v("wellbore"), v("content")).asJava, - // Conjunction.create( - // Atom.rdf(v("w"), IRI.RDF_TYPE, c("Wellbore")), - // Atom.rdf(v("w"), c("name"), v("wellbore")), - // Atom.rdf(v("w"), c("hasWellboreInterval"), v("int")), - // Atom.rdf(v("int"), IRI.RDF_TYPE, c("FluidZone")), - // Atom.rdf(v("int"), c("fluidZoneContent"), v("content")) - // ) - // ) - - // QUERY 7 - - val query7 = RDFoxHelpers - .parseSelectQuery( - """ - SELECT ?wName ?sample ?porosity ?top_depth_md ?bot_depth_md - WHERE { - ?w a :Wellbore ; - :name ?wName ; - :hasWellboreInterval ?z . - ?z :hasUnit ?u . - ?u :name ?strat_unit_name . - ?wellbore :hasWellboreInterval ?cored_int . - ?c :extractedFrom ?cored_int ; - :hasCoreSample ?sample . - ?sample :hasDepth ?sample_depth . - ?sample_depth - :inWellboreInterval ?z . - ?sample :hasPorosity ?p . - ?p :valueInStandardUnit ?porosity . - ?z :hasTopDepth ?top . - ?top a :MeasuredDepth ; - :valueInStandardUnit ?top_depth_md . - ?z :hasBottomDepth ?bot . - ?bot a :MeasuredDepth ; - :valueInStandardUnit ?bot_depth_md - } - """, - prefixes - ) - .get - - // val query7 = Query.create( - // QueryType.SELECT, - // false, - // List( - // v("wName"), - // v("sample"), - // v("porosity"), - // v("top_depth_md"), - // v("bot_depth_md") - // ).asJava, - // Conjunction.create( - // Atom.rdf(v("w"), IRI.RDF_TYPE, c("Wellbore")), - // Atom.rdf(v("w"), c("name"), v("wName")), - // Atom.rdf(v("w"), c("hasWellboreInterval"), v("z")), - // Atom.rdf(v("z"), c("hasUnit"), v("u")), - // Atom.rdf(v("u"), c("name"), v("strat_unit_name")), - // Atom.rdf(v("wellbore"), c("hasWellboreInterval"), v("cored_int")), - // Atom.rdf(v("c"), c("extractedFrom"), v("cored_int")), - // Atom.rdf(v("c"), c("hasCoreSample"), v("sample")), - // Atom.rdf(v("sample"), c("hasDepth"), v("sample_depth")), - // Atom.rdf(v("sample_depth"), c("inWellboreInterval"), v("z")), - // Atom.rdf(v("sample"), c("hasPorosity"), v("p")), - // Atom.rdf(v("p"), c("valueInStandardUnit"), v("porosity")), - // Atom.rdf(v("z"), c("hasTopDepth"), v("top")), - // Atom.rdf(v("top"), IRI.RDF_TYPE, c("MeasuredDepth")), - // Atom.rdf(v("top"), c("valueInStandardUnit"), v("top_depth_md")), - // Atom.rdf(v("z"), c("hasBottomDepth"), v("bot")), - // Atom.rdf(v("bot"), IRI.RDF_TYPE, c("MeasuredDepth")), - // Atom.rdf(v("bot"), c("valueInStandardUnit"), v("bot_depth_md")) - // ) - // ) - - val queries = - List(query0, query1, query2, query3, query4, query5, query6, query7) + """ } -class FilteringProgramSpec - extends AnyFlatSpec - with Matchers - with LoneElement - with Inspectors { +class FilteringProgramSpec extends AnyFlatSpec with Matchers { import FilteringProgramSpec._ + "CQ 0" should "generate 30 rules" in { + val cq = ConjunctiveQuery(cq0).get + FilteringProgram(cq, constants).rules should have length 30 + } + + "CQ 1" should "generate 15 rules" in { + val cq = ConjunctiveQuery(cq1).get + FilteringProgram(cq, List()).rules should have length 15 + } + + "CQ 2" should "generate 51 rules" in { + val cq = ConjunctiveQuery(cq2).get + FilteringProgram(cq, List()).rules should have length 51 + } + + "BCQ 0" should "generate 46 rules" in { + val cq = ConjunctiveQuery(bcq0).get + FilteringProgram(cq, constants).rules should have length 46 + } + } -- cgit v1.2.3