11package net .sansa_stack .test .conformance
22
33import java .io .File
4+ import java .nio .file .{FileSystem , Path }
45
5- import org .apache .jena .riot .RDFDataMgr
6-
6+ import org .apache .jena .riot .{Lang , RDFDataMgr }
77import scala .collection .mutable .ListBuffer
88import scala .xml .XML
99
10+ import org .apache .commons .io .IOUtils
11+ import org .apache .jena .rdf .model .ModelFactory
12+ import org .scalatest .path
13+
1014/**
1115 * Test cases loader.
1216 *
1317 * @author Lorenz Buehmann
1418 */
1519object TestCases {
1620
21+ val logger = com.typesafe.scalalogging.Logger (" TestCases" )
22+
23+ var fs : FileSystem = null
24+
1725 /**
1826 * Loads test cases from the given root folder.
1927 *
@@ -25,6 +33,8 @@ object TestCases {
2533
2634 val testCases = new ListBuffer [TestCase ]()
2735
36+ println(directory)
37+
2838 directory.listFiles().filter(f => f.isDirectory && (ids.isEmpty || ids.contains(f.getName))).foreach { subDirectory =>
2939
3040 // the files in the directory
@@ -61,4 +71,88 @@ object TestCases {
6171
6272 testCases
6373 }
74+
75+ /**
76+ * Loads test cases from the given root folder.
77+ *
78+ * @param directory the root folder containing sub-folders for each test case
79+ * @return test cases
80+ */
81+ def loadTestCasesJar (directory : String , ids : Set [String ] = Set .empty): Seq [TestCase ] = {
82+ logger.info(s " loading test cases from ${directory}... " )
83+
84+ val testCases = new ListBuffer [TestCase ]()
85+
86+ listFiles(directory).filter(f => ids.isEmpty || ids.contains(f.getFileName.toString.replace(" /" , " " ))).map { p =>
87+
88+ // the files in the directory
89+ val files = listFiles(
90+ if (p.toUri.getScheme == " jar" ) p.toString.substring(1 ) else p.toString, true )
91+
92+ // get the metadata file
93+ val metadataFile = files.filter(_.toString.endsWith(" .metadata.properties" )).head
94+
95+ // load metadata XML
96+ val metadata = XML .load(metadataFile.toUri.toURL.openStream())
97+
98+ // id
99+ val id = (metadata \\ " entry" ).filter(n => n.attribute(" key" ).get.text == " testcase.id" ).text
100+
101+ // description
102+ val description = (metadata \\ " entry" ).filter(n => n.attribute(" key" ).get.text == " testcase.description" ).text
103+
104+ // test case type
105+ val entailmentType = (metadata \\ " entry" ).filter(n => n.attribute(" key" ).get.text == " testcase.type" ).text
106+
107+ // currently we support only entailment test cases
108+ if (entailmentType == " POSITIVE_ENTAILMENT" ) {
109+ // load input data
110+
111+ val inputGraph = ModelFactory .createDefaultModel()
112+ inputGraph.read(files.filter(_.toString.endsWith(" .premisegraph.ttl" )).head.toUri.toURL.openStream(), null , " Turtle" )
113+
114+ // load output data
115+ val outputGraph = ModelFactory .createDefaultModel()
116+ outputGraph.read(files.filter(_.toString.endsWith(" .conclusiongraph.ttl" )).head.toUri.toURL.openStream(), null , " Turtle" )
117+
118+ testCases += TestCase (id, description, entailmentType, inputGraph, outputGraph)
119+ }
120+ }
121+ // directory.listFiles().filter(f => f.isDirectory && (ids.isEmpty || ids.contains(f.getName))).foreach { subDirectory =>
122+
123+
124+ println(s " loaded ${testCases.size} test cases " )
125+
126+ if (fs != null ) fs.close()
127+
128+ testCases
129+ }
130+
131+ private def listFiles (path : String , subDir : Boolean = false ): Seq [Path ] = {
132+ import java .nio .file .FileSystems
133+ import java .nio .file .Files
134+ import java .nio .file .Paths
135+ import java .util .Collections
136+
137+ // println(s"path: $path")
138+ val uri = if (path.startsWith(" /" )) new File (path).toURI else classOf [TestCase ].getClassLoader.getResource(path).toURI
139+ // println(s"uri: $uri")
140+ var myPath : Path = null
141+ if (uri.getScheme == " jar" && ! subDir) {
142+ fs = FileSystems .newFileSystem(uri, Collections .emptyMap[String , Any ])
143+ myPath = fs.getPath(path)
144+ }
145+ else myPath = Paths .get(uri)
146+ val walk = Files .walk(myPath, 1 )
147+ val it = walk.iterator
148+ var files = Seq [Path ]()
149+ while ({it.hasNext}) {
150+ val subPath = it.next()
151+ if (! subPath.equals(myPath)) {
152+ files :+= subPath
153+ }
154+ }
155+
156+ files
157+ }
64158}
0 commit comments