[Mulgara-svn] r1854 - in projects/SparqlTester/trunk/src/org/duraspace: rdf rdf/impl rdf/parser rdf/xml sparql/test

pag at mulgara.org pag at mulgara.org
Tue Nov 24 19:57:03 UTC 2009


Author: pag
Date: 2009-11-24 11:57:02 -0800 (Tue, 24 Nov 2009)
New Revision: 1854

Modified:
   projects/SparqlTester/trunk/src/org/duraspace/rdf/Graph.scala
   projects/SparqlTester/trunk/src/org/duraspace/rdf/Literal.scala
   projects/SparqlTester/trunk/src/org/duraspace/rdf/ObjectNode.scala
   projects/SparqlTester/trunk/src/org/duraspace/rdf/PredicateNode.scala
   projects/SparqlTester/trunk/src/org/duraspace/rdf/SubjectNode.scala
   projects/SparqlTester/trunk/src/org/duraspace/rdf/impl/BlankNodeImpl.scala
   projects/SparqlTester/trunk/src/org/duraspace/rdf/impl/LiteralImpl.scala
   projects/SparqlTester/trunk/src/org/duraspace/rdf/impl/Uri.scala
   projects/SparqlTester/trunk/src/org/duraspace/rdf/parser/GraphReader.scala
   projects/SparqlTester/trunk/src/org/duraspace/rdf/xml/LiteralXML.scala
   projects/SparqlTester/trunk/src/org/duraspace/rdf/xml/SparqlResult.scala
   projects/SparqlTester/trunk/src/org/duraspace/sparql/test/Manifest.scala
   projects/SparqlTester/trunk/src/org/duraspace/sparql/test/QueryEvalTest.scala
   projects/SparqlTester/trunk/src/org/duraspace/sparql/test/TestHarness.scala
Log:
Now handling lots of edge cases I was unaware of on the first commit. All tests can now be run

Modified: projects/SparqlTester/trunk/src/org/duraspace/rdf/Graph.scala
===================================================================
--- projects/SparqlTester/trunk/src/org/duraspace/rdf/Graph.scala	2009-11-24 19:56:19 UTC (rev 1853)
+++ projects/SparqlTester/trunk/src/org/duraspace/rdf/Graph.scala	2009-11-24 19:57:02 UTC (rev 1854)
@@ -114,9 +114,8 @@
       out.print(triple.pred)
       out.print(" ")
       out.print(triple.obj)
-      out.print(" .")
+      out.print(" .\n")
     }
   }
 
-
 }

Modified: projects/SparqlTester/trunk/src/org/duraspace/rdf/Literal.scala
===================================================================
--- projects/SparqlTester/trunk/src/org/duraspace/rdf/Literal.scala	2009-11-24 19:56:19 UTC (rev 1853)
+++ projects/SparqlTester/trunk/src/org/duraspace/rdf/Literal.scala	2009-11-24 19:57:02 UTC (rev 1854)
@@ -26,8 +26,8 @@
 
   override def toString = {
     "'" + text + "'" +
-      (if (language == None) "^^" + datatype.toString
-      else "@" + language)
+      (if (datatype != None) "^^" + datatype.toString
+      else if (language != None) "@" + language else "")
   }
 
 }

Modified: projects/SparqlTester/trunk/src/org/duraspace/rdf/ObjectNode.scala
===================================================================
--- projects/SparqlTester/trunk/src/org/duraspace/rdf/ObjectNode.scala	2009-11-24 19:56:19 UTC (rev 1853)
+++ projects/SparqlTester/trunk/src/org/duraspace/rdf/ObjectNode.scala	2009-11-24 19:57:02 UTC (rev 1854)
@@ -19,3 +19,10 @@
 trait ObjectNode extends Node {
 
 }
+
+object ObjectNode {
+  def unapply(n: Node) = n match {
+    case o: ObjectNode => Some(o)
+    case _ => None
+  }
+}
\ No newline at end of file

Modified: projects/SparqlTester/trunk/src/org/duraspace/rdf/PredicateNode.scala
===================================================================
--- projects/SparqlTester/trunk/src/org/duraspace/rdf/PredicateNode.scala	2009-11-24 19:56:19 UTC (rev 1853)
+++ projects/SparqlTester/trunk/src/org/duraspace/rdf/PredicateNode.scala	2009-11-24 19:57:02 UTC (rev 1854)
@@ -16,6 +16,13 @@
 
 package org.duraspace.rdf
 
-trait PredicateNode extends Node {
+trait PredicateNode extends Node with SubjectNode with ObjectNode {
 
 }
+
+object PredicateNode {
+  def unapply(n: Node) = n match {
+    case p: PredicateNode => Some(p)
+    case _ => None
+  }
+}
\ No newline at end of file

Modified: projects/SparqlTester/trunk/src/org/duraspace/rdf/SubjectNode.scala
===================================================================
--- projects/SparqlTester/trunk/src/org/duraspace/rdf/SubjectNode.scala	2009-11-24 19:56:19 UTC (rev 1853)
+++ projects/SparqlTester/trunk/src/org/duraspace/rdf/SubjectNode.scala	2009-11-24 19:57:02 UTC (rev 1854)
@@ -16,6 +16,13 @@
 
 package org.duraspace.rdf
 
-trait SubjectNode extends Node {
+trait SubjectNode extends Node with ObjectNode {
 
 }
+
+object SubjectNode {
+  def unapply(n: Node) = n match {
+    case s: SubjectNode => Some(s)
+    case _ => None
+  }
+}
\ No newline at end of file

Modified: projects/SparqlTester/trunk/src/org/duraspace/rdf/impl/BlankNodeImpl.scala
===================================================================
--- projects/SparqlTester/trunk/src/org/duraspace/rdf/impl/BlankNodeImpl.scala	2009-11-24 19:56:19 UTC (rev 1853)
+++ projects/SparqlTester/trunk/src/org/duraspace/rdf/impl/BlankNodeImpl.scala	2009-11-24 19:57:02 UTC (rev 1854)
@@ -29,3 +29,12 @@
 
   override def hashCode() = label.hashCode
 }
+
+object BlankNodeImpl {
+  def apply(b: BlankNode):Node = b
+
+  def unapply(n: Node) = n match {
+    case b: BlankNode => Some(b)
+    case _ => None
+  }
+}
\ No newline at end of file

Modified: projects/SparqlTester/trunk/src/org/duraspace/rdf/impl/LiteralImpl.scala
===================================================================
--- projects/SparqlTester/trunk/src/org/duraspace/rdf/impl/LiteralImpl.scala	2009-11-24 19:56:19 UTC (rev 1853)
+++ projects/SparqlTester/trunk/src/org/duraspace/rdf/impl/LiteralImpl.scala	2009-11-24 19:57:02 UTC (rev 1854)
@@ -33,3 +33,13 @@
 
   override def hashCode = text.hashCode + language.hashCode * 3 + datatype.hashCode * 7
 }
+
+object LiteralImpl {
+
+  def apply(l: Literal): Node = l
+
+  def unapply(n: Node) = n match {
+    case l: Literal => Some(l)
+    case _ => None
+  }
+}

Modified: projects/SparqlTester/trunk/src/org/duraspace/rdf/impl/Uri.scala
===================================================================
--- projects/SparqlTester/trunk/src/org/duraspace/rdf/impl/Uri.scala	2009-11-24 19:56:19 UTC (rev 1853)
+++ projects/SparqlTester/trunk/src/org/duraspace/rdf/impl/Uri.scala	2009-11-24 19:57:02 UTC (rev 1854)
@@ -51,4 +51,10 @@
 
   implicit def toUri(u: String) = new Uri(u)
 
+  def apply(u: Uri):Node = u
+
+  def unapply(n: Node) = n match {
+    case u: Uri => Some(u)
+    case _ => None
+  }
 }
\ No newline at end of file

Modified: projects/SparqlTester/trunk/src/org/duraspace/rdf/parser/GraphReader.scala
===================================================================
--- projects/SparqlTester/trunk/src/org/duraspace/rdf/parser/GraphReader.scala	2009-11-24 19:56:19 UTC (rev 1853)
+++ projects/SparqlTester/trunk/src/org/duraspace/rdf/parser/GraphReader.scala	2009-11-24 19:57:02 UTC (rev 1854)
@@ -34,14 +34,8 @@
 import org.duraspace.rdf.vocab.OWL
 
 
-class GraphReader(val graphFile: File) extends N3ParserEventHandler {
+trait GraphReader {
 
-  /** The first identifier for anonymous nodes. */
-  val AnonTag1 = "_:"
-
-  /** The second identifier for anonymous nodes. */
-  val AnonTag2 = "=:"
-
   /** Map of <code>prefix</code> directives. */
   var prefixMap = Map[String,String]()
 
@@ -51,140 +45,29 @@
   // kick it all off
   parse
 
-  def parse {
-    try {
-      val inputStream = new FileInputStream(graphFile)
-      (new N3Parser(inputStream, this)).parse()
-    } catch {
-      case e => throw new ParseException("Error reading from file: " + graphFile, e)
-    }
-  }
+  def parse:Unit
 
   /**
    * Convenience method for logging debug data.
    * @param msg The message to log.
    */
-  private def debug(msg: String) { GraphReader.logger.log(Level.FINE, msg) }
+  protected def debug(msg: String) { GraphReader.logger.log(Level.FINE, msg) }
 
   /**
    * Convenience method for logging error data.
    * @param msg The message to log.
    */
-  private def error(msg: String) { GraphReader.logger.log(Level.WARNING, msg) }
+  protected def error(msg: String) { GraphReader.logger.log(Level.WARNING, msg) }
 
-  def endDocument { debug("Finishing N3 document") }
-
-  def endFormula(line: Int, arg1: String) { debug("End of formula. " + line + ": " + arg1) }
-
-  def error(arg0: Exception, arg1: String) { error("Error in data. " + arg0 + ": " + arg1) }
-
-  def startDocument() { debug("Starting N3 document") }
-
-  def startFormula(line: Int, arg1: String) { error("Unexpected formula. " + line + ": " + arg1) }
-
-  def directive(line: Int, directive: AST, args: Array[AST], context: String) {
-    directive.getType match {
-      case AT_PREFIX => {
-        debug("Adding prefix: " + args(0) + args(1))
-        prefixMap += (args(0).toString -> args(1).toString)
-      }
-      case _ => error("Ignoring directive. " + line + ": " + directive + " {" + context + "}")
-    }
-  }
-
-  def quad(line: Int, subj: AST, pred: AST, obj: AST, context: String) {
-    debug("line: " + line + ". " + List(subj.getText(), pred.getText(), obj.getText()).mkString(" ") + " :- " + context)
-    try {
-      graph.add(toNode(subj).asInstanceOf[SubjectNode], toNode(pred).asInstanceOf[PredicateNode], toNode(obj).asInstanceOf[ObjectNode])
-    } catch {
-      case e: Throwable => {
-        error("Error parsing node: " + e.getMessage() + " at line: " + line + " [" + subj + ", " + pred + ", " + obj + "]")
-      }
-    }
-  }
-
-  def toNode(ast: AST) = {
-    ast.getType() match {
-      case LITERAL => {
-        val a1 = ast.getNextSibling
-        val a2 = if (a1 == null) null else a1.getNextSibling
-        val lang = getLang(a1, a2)
-        val datatype = getDatatype(a1, a2)
-        new LiteralImpl(ast.toString, lang, datatype)
-      }
-      case NUMBER => new LiteralImpl(ast.toString, getNumberType(ast.toString))
-      case ANON => new BlankNodeImpl(ast.toString())
-      case QNAME => {
-        val s = ast.toString()
-        if (isAnonymous(ast)) new BlankNodeImpl(s)
-        else {
-          val colonIndex = s.indexOf(':')
-          val qnamePrefix = s.substring(0, colonIndex + 1)
-          val uriPrefix = prefixMap(qnamePrefix)
-          require (uriPrefix != null, "No @prefix for " + s)
-          new Uri(uriPrefix + s.substring(colonIndex + 1))
-        }
-      }
-      case URIREF => new Uri(ast.toString())
-      case KW_A => new Uri(RDF.Type)
-      case TK_LIST_FIRST => new Uri(RDF.First)
-      case TK_LIST_REST => new Uri(RDF.Rest)
-      case TK_LIST_NIL => new Uri(RDF.Nil)
-      case TK_LIST => new Uri(RDF.List)
-      case EQUAL => new Uri(OWL.SameAs)
-      case FORMULA => throw new ParseException("Formulas are not supported. file=[" + graphFile + "]")
-      case _ => throw new ParseException("Unsupported N3 parser token type: " + ast.getType() + " '" + ast + "' file=[" + graphFile + "]");
-    }
-  }
-
-  private def getLang(a1: AST, a2: AST): Option[String] = {
-    if (a1 != null) {
-      val lang = getLang(a1)
-      if (lang == None) getLang(a2) else lang
-    } else getLang(a2)
-  }
-
-  private def getLang(a: AST): Option[String] =
-    if (a == null || a.getType != AT_LANG) None else Some(a.getText.substring(1))
-
-  private def getDatatype(a1: AST, a2: AST): Option[URI] = {
-    val datatype = getDatatype(a1)
-    if (datatype == None) getDatatype(a2) else datatype
-  }
-
-  private def getDatatype(a: AST): Option[URI] = {
-    if (a == null || a.getType() != DATATYPE) None
-    else {
-      val dt = a.getFirstChild
-      try {
-        if (dt == null) None else Some(new URI(dt.toString()))
-      } catch {
-        case e: URISyntaxException => error("Error parsing N3 datatype: " + dt.toString())
-                                      None
-      }
-    }
-  }
-
-  private def getNumberType(n: String) = {
-    if (n.indexOf('.') >= 0) XSD.Double
-    else {
-      try {
-        val l = n.toLong
-        if (l <= Math.MAX_INT && l >= Math.MIN_INT) XSD.Int else XSD.Long
-      } catch {
-        case _ => XSD.Integer
-      }
-    }
-  }
-
-  def isAnonymous(node: AST) = {
-    val s = node.toString()
-    s.startsWith(AnonTag1) || s.startsWith(AnonTag2)
-  }
-
 }
 
 object GraphReader {
   /** The logging object. */
   val logger = Logger.getLogger("GraphReader")
+
+  def apply(file: File) = {
+    if (file.getName.endsWith(".rdf")) new RDFGraphReader(file)
+    else if (file.getName.endsWith(".ttl")) new N3GraphReader(file)
+    else throw new ParseException("Unknown file type")
+  }
 }
\ No newline at end of file

Modified: projects/SparqlTester/trunk/src/org/duraspace/rdf/xml/LiteralXML.scala
===================================================================
--- projects/SparqlTester/trunk/src/org/duraspace/rdf/xml/LiteralXML.scala	2009-11-24 19:56:19 UTC (rev 1853)
+++ projects/SparqlTester/trunk/src/org/duraspace/rdf/xml/LiteralXML.scala	2009-11-24 19:57:02 UTC (rev 1854)
@@ -50,6 +50,7 @@
    */
   def unapply(node: Node): Option[(String, Option[String], Option[URI])] = node match {
     case <literal>{lit}</literal> => Some((lit.text, findLang(node), findDatatype(node)))
+    case <literal></literal> => Some(("", findLang(node), findDatatype(node)))
     case _ => None
   }
 

Modified: projects/SparqlTester/trunk/src/org/duraspace/rdf/xml/SparqlResult.scala
===================================================================
--- projects/SparqlTester/trunk/src/org/duraspace/rdf/xml/SparqlResult.scala	2009-11-24 19:56:19 UTC (rev 1853)
+++ projects/SparqlTester/trunk/src/org/duraspace/rdf/xml/SparqlResult.scala	2009-11-24 19:57:02 UTC (rev 1854)
@@ -16,15 +16,20 @@
 
 package org.duraspace.rdf.xml
 
+import java.io.File
 import scala.xml.Node
 import scala.xml.Text
+import scala.xml.XML
 import org.duraspace.rdf.impl._
 
 /**
  * Represents a set of SPARQL results, coming from XML.
  */
-class SparqlResult(root: Node) {
+class SparqlResult(root: Node) extends org.duraspace.rdf.SparqlResult {
 
+  /** Overloaded constructor to work with a file */
+  def this(file: File) = this(XML.loadFile(file))
+
   /** The head node of the XML */
   val headNode = root \ "head"
   /** The results node of the XML */
@@ -33,9 +38,13 @@
   /** The list of variables */
   val variables = for (vnode <- headNode \ "variable") yield vnode \ "@name" text
 
-  /** A list of variable bindings, one binding per entry */
-  val results = Set() ++ (for (rnode <- resultsNode \ "result") yield bindingMap(rnode))
+  override val isBoolean = (root \ "boolean").text != ""
 
+  /** A collection of variable bindings, one binding per entry */
+  val results = List() ++ (for (rnode <- resultsNode \ "result") yield bindingMap(rnode))
+
+  override def booleanResult = (root \ "boolean").text.toBoolean
+
   /** A map of each variable to a single value */
   def bindingMap(rnode: Node) = Map() ++ (for (binding <- rnode \ "binding") yield ((binding \ "@name" text) -> bindingVal(binding)))
 
@@ -46,7 +55,7 @@
         case LiteralXML(text, lang, datatype) => new LiteralImpl(text, lang, datatype)
         case UriXML(u) => new Uri(u)
         case BlankNodeXML(label) => new BlankNodeImpl(label)
-        case _ => throw new Exception("Unexpected XML node in binding: " + b.text)
+        case _ => throw new Exception("Unexpected XML node in binding: " + value.toString)
       }
     )
     require (boundVals.size == 1)
@@ -59,12 +68,4 @@
     case _ => true
   }
 
-  /** String representation of the entire result set */
-  override def toString = results.toString
-
-  /** Compares this result set to another for equality */
-  override def equals(that: Any) = that match {
-    case r: SparqlResult => results == r.results
-    case _ => false
-  }
 }

Modified: projects/SparqlTester/trunk/src/org/duraspace/sparql/test/Manifest.scala
===================================================================
--- projects/SparqlTester/trunk/src/org/duraspace/sparql/test/Manifest.scala	2009-11-24 19:56:19 UTC (rev 1853)
+++ projects/SparqlTester/trunk/src/org/duraspace/sparql/test/Manifest.scala	2009-11-24 19:57:02 UTC (rev 1854)
@@ -39,7 +39,7 @@
 
   // constructor code
   val file = manifest.getAbsoluteFile
-  val graph = new GraphReader(file).graph
+  val graph = GraphReader(file).graph
   val tests = extractTests
 
   /**

Modified: projects/SparqlTester/trunk/src/org/duraspace/sparql/test/QueryEvalTest.scala
===================================================================
--- projects/SparqlTester/trunk/src/org/duraspace/sparql/test/QueryEvalTest.scala	2009-11-24 19:56:19 UTC (rev 1853)
+++ projects/SparqlTester/trunk/src/org/duraspace/sparql/test/QueryEvalTest.scala	2009-11-24 19:57:02 UTC (rev 1854)
@@ -22,7 +22,7 @@
 import scala.xml.XML
 import org.apache.http.HttpResponse
 
-import org.duraspace.rdf.xml.SparqlResult
+import org.duraspace.rdf.SparqlResult
 
 class QueryEvalTest(name: String, queryFile: File, val dataFile: File, val resultFile: File, approved: Boolean)
       extends TestInstance(name, queryFile, approved) {
@@ -49,9 +49,9 @@
 
   // compare the resulting stream to the data in the expected results file
   def expectedData(content: InputStream) = {
-    val results = new SparqlResult(XML.load(content))
-    val expectedResults = new SparqlResult(XML.loadFile(resultFile))
-    results == expectedResults
+    val results = SparqlResult(content)
+    val expectedResults = SparqlResult(resultFile)
+    expectedResults.equivalentTo(results)
   }
 
 }

Modified: projects/SparqlTester/trunk/src/org/duraspace/sparql/test/TestHarness.scala
===================================================================
--- projects/SparqlTester/trunk/src/org/duraspace/sparql/test/TestHarness.scala	2009-11-24 19:56:19 UTC (rev 1853)
+++ projects/SparqlTester/trunk/src/org/duraspace/sparql/test/TestHarness.scala	2009-11-24 19:57:02 UTC (rev 1854)
@@ -23,7 +23,7 @@
 object TestHarness {
   val DefaultEndpoint = "http://localhost:8080/sparql/"
 
-  def main(args : Array[String]) : Unit = {
+  def main(args : Array[String]) {
     if (args.size == 0) {
       System.err.println("File must be provided")
       return




More information about the Mulgara-svn mailing list