Skip to content

Commit

Permalink
Merge pull request #10 from p2m2/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ofilangi authored Feb 14, 2023
2 parents 780d1b3 + 3a962a2 commit 8df55fd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 60 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# facade-scalajs-comunica-query-sparql
[![Scala.js](https://www.scala-js.org/assets/badges/scalajs-1.13.0.svg)](https://www.scala-js.org)
[![p2m2](https://circleci.com/gh/p2m2/facade-scalajs-comunica-query-sparql.svg?style=shield)](https://app.circleci.com/pipelines/github/p2m2)
[![codecov](https://codecov.io/gh/p2m2/facade-scalajs-comunica-query-sparql.js/branch/develop/graph/badge.svg)](https://codecov.io/gh/p2m2/facade-scalajs-comunica-query-sparql)

[![codecov](https://codecov.io/gh/p2m2/facade-scalajs-comunica-query-sparql/branch/main/graph/badge.svg?token=GowU3tYOri)](https://codecov.io/gh/p2m2/facade-scalajs-comunica-query-sparql)
Scala.js Facade of the [Comunica SPARQL query engine for JavaScript for querying over decentralized RDF knowledge graphs on the Web](hhttps://www.npmjs.com/package/@comunica/query-sparql)

## Install
Expand Down
9 changes: 8 additions & 1 deletion src/main/scala/com/github/p2m2/facade/QueryEngine.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ import scala.scalajs.js.|
class QueryEngine extends js.Object {
def queryBindings( request : String , context : QueryEngineOptions = null) : js.Promise[stream.Transform] = js.native
def queryQuads( request : String , context : QueryEngineOptions = null) : js.Promise[stream.Transform] = js.native
def query( request : String , context : QueryEngineOptions = null) : js.Promise[stream.Transform] = js.native
def resultToString(s :stream.Transform,mediaType: String ) : js.Promise[IActorSparqlSerializeOutput] = js.native
}

@js.native
@JSImport("@comunica/query-sparql", "IActorSparqlSerializeOutput")
class IActorSparqlSerializeOutput extends js.Object {
val data : stream.Readable = js.native
}
/**
* https://comunica.dev/docs/query/advanced/context/
*/
Expand Down Expand Up @@ -50,7 +57,7 @@ object QueryEngineOptions {
queryFormat : QueryFormatType = QueryFormatType("sparql","1.1")
): QueryEngineOptions = js.Dynamic.literal(
"sources" -> (sources match {
case l if l.length>0 => l.toJSArray
case l if l.nonEmpty => l.toJSArray
case _ => js.undefined
}),
"lenient" -> lenient,
Expand Down
110 changes: 53 additions & 57 deletions src/test/scala/com/github/p2m2/facade/QueryEngineTest.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.github.p2m2.facade

import io.scalajs.nodejs.process.Process.stdout
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import utest.{assert, _}

import scala.concurrent.Promise
import scala.language.implicitConversions
import scala.scalajs.js.JSConverters._
import scala.scalajs.js
import scala.util.{Failure, Success}

object QueryEngineTest extends TestSuite {
Expand All @@ -19,7 +21,7 @@ object QueryEngineTest extends TestSuite {
DataFactory.namedNode("http://dbpedia.org/resource/Ghent")))
store
}
val tests = Tests {
val tests: Tests = Tests {
test("newEngine bindings - N3Store - bindings ") {

new QueryEngine().queryBindings(
Expand All @@ -37,7 +39,7 @@ object QueryEngineTest extends TestSuite {

case Failure(t) =>
println("An error has occurred: " + t.getMessage)
println(t.getStackTrace.foreach(println _))
println(t.getStackTrace.foreach(println))
assert(false)
}
}
Expand All @@ -46,81 +48,75 @@ object QueryEngineTest extends TestSuite {
new QueryEngine().queryQuads("CONSTRUCT WHERE { ?s ?p ?o } LIMIT 100",
QueryEngineOptions(sources = List(initStore())))
.toFuture onComplete {
case Success(results) => {
case Success(results) =>
results.on("data", (v : Quad) => {
assert(v.subject.value == "a")
assert(v.predicate.value == "b")
})
}
case Failure(t) =>
println("An error has occurred: " + t.getMessage)
println(t.getStackTrace.foreach( println _ ))
println(t.getStackTrace.foreach( println ))
assert(false)
}
}
/*
test("Serializing to a specific result format") {

test("Serializing to a specific result format") {
new QueryEngine().query("SELECT * { ?s ?p ?o . VALUES ?o { <http://dbpedia.org/resource/Belgium> } . } LIMIT 100",
QueryEngineOptions(sources = List(initStore())))
.toFuture onComplete {
case Success(results: IQueryResult) => {
case Success(results) =>
val data = new QueryEngine().resultToString(results,"application/sparql-results+json")
data.toFuture onComplete {
case Success(r) => r.data.pipe( stdout )
case Failure(t) => println("message :"+t)
}
}
case Failure(t) => println("An error has occurred: " + t.getMessage)
}
}

test("Serializing to a specific result format 2 ") {
new QueryEngine().query("SELECT * { ?s ?p ?o . VALUES ?o { <http://dbpedia.org/resource/Belgium> } . } LIMIT 100",
QueryEngineOptions(sources = List(initStore())))
.toFuture onComplete {
case Success(results: IQueryResult) => {
val data = new QueryEngine().resultToString(results,"application/sparql-results+json")
data.toFuture onComplete {
case Success(r) => r.data.on( "data" , (chunk : js.Object) => {
println("chunk :" + chunk.toString)
} )
case Failure(t) => println("message :"+t)
}
}
case Failure(t) => println("An error has occurred: " + t.getMessage)
}
}
test("Serializing to a specific result format 2 ") {

new QueryEngine().query("SELECT * { ?s ?p ?o . VALUES ?o { <http://dbpedia.org/resource/Belgium> } . } LIMIT 100",
QueryEngineOptions(sources = List(initStore())))
.toFuture onComplete {
case Success(results) =>
val data = new QueryEngine().resultToString(results,"application/sparql-results+json")
data.toFuture onComplete {
case Success(r) => r.data.on( "data" , (chunk : js.Object) => {
println("chunk :" + chunk.toString)
} )
case Failure(t) => println("message :"+t)
}
case Failure(t) => println("An error has occurred: " + t.getMessage)
}
}

test("Serializing to a specific result format 3 ") {
new QueryEngine().query("SELECT * { ?s ?p ?o . VALUES ?o { <http://dbpedia.org/resource/Belgium> } . } LIMIT 100",
QueryEngineOptions(sources = List(initStore())))
.toFuture onComplete {
case Success(results: IQueryResult) => {
new QueryEngine().resultToString(results,"application/sparql-results+json")
.toFuture.map( v => {
val p = Promise[String]()
var sparql_results = ""
println("HELLO WORLD !!!!")
v.data.on("data", (chunk: js.Object) => {
println(chunk)
sparql_results += chunk.toString
}).on("end", () => {
p success sparql_results
}).on("error", (error: String) => {
p failure js.JavaScriptException(error)
})
p.future
}).recover(error => {
throw js.JavaScriptException(error.toString)
})
}
case Failure(t) => println("An error has occurred: " + t.getMessage)
}
}
*/
test("Serializing to a specific result format 3 ") {

new QueryEngine().query("SELECT * { ?s ?p ?o . VALUES ?o { <http://dbpedia.org/resource/Belgium> } . } LIMIT 100",
QueryEngineOptions(sources = List(initStore())))
.toFuture onComplete {
case Success(results) =>
new QueryEngine().resultToString(results,"application/sparql-results+json")
.toFuture.map( v => {
val p = Promise[String]()
var sparql_results = ""
println("HELLO WORLD !!!!")
v.data.on("data", (chunk: js.Object) => {
println(chunk)
sparql_results += chunk.toString
}).on("end", () => {
p success sparql_results
}).on("error", (error: String) => {
p failure js.JavaScriptException(error)
})
p.future
}).recover(error => {
throw js.JavaScriptException(error.toString)
})
case Failure(t) => println("An error has occurred: " + t.getMessage)
}
}

test("newEngine bindings - SOURCES = List(N3Store + hypermedia) ") {
new QueryEngine().queryBindings("SELECT ?s { ?s ?p ?o . } LIMIT 100",
Expand All @@ -137,15 +133,15 @@ object QueryEngineTest extends TestSuite {

results.on("data", (v : Bindings) => {
println("test....................")
println("?s store+hypermedia ->" );
println("?s store+hypermedia ->" )
println(v.toString)
}).on("end", () => {
println(" ======== FIN store+hypermedia ============== ")
})

case Failure(t) =>
println("An error has occurred: " + t.getMessage)
println(t.getStackTrace.foreach( println _ ))
println(t.getStackTrace.foreach( println ))
assert(false)
}
}
Expand Down

0 comments on commit 8df55fd

Please sign in to comment.