Skip to content

Commit

Permalink
query viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jazzpirate committed Oct 23, 2023
1 parent 297b0e1 commit 316c8c2
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 10 deletions.
55 changes: 55 additions & 0 deletions src/mmt-api/resources/mmt-web/stex/queries.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en" ng-app="QueryApp">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<script src="/script/angularjs/angular-1.5.3/angular.min.js"></script>
<script type="text/javascript" src="/script/jquery/jquery.js"></script>
<script type="text/javascript" src="/stex/queries.js">
</script>
<link rel="stylesheet" href="/css/bootstrap3/css/bootstrap.min.css"/>
<style>
button {
background-color: darkslategray;
}
.query-return {
font-size:x-small;
background-color: black;
color: red;
/* width: min-content; */
height: 300px;
/* max-width: 95%; */
overflow-y: scroll;
overflow-x: hidden;
}
.my-separator {
display:inline-block;
width:5px;
border-right: 1px solid;
height: 26px;
margin-right: 5px;
vertical-align: bottom;
}
input, select, textarea {
background-color: black;
}
</style>
</head>
<body style="background-color: #2a2a2a;color: aliceblue">
<div class="container-fluid" ng-controller="QueryViewer">
<h2 style="margin-top: 5px;margin-bottom: 3px">sTeX Queries</h2>
<hr/>
<b>Queries:</b>
<button ng-click="query_all()">All sTeX Symbols</button>
<button ng-click="query_all_undefined()">All <i>undefined</i> sTeX Symbols</button>
<button ng-click="query_all_with_data()">All sTeX Symbols with their macro names</button>
<hr/>
<b>Custom SPARQL Query:</b><br/>(See <a href="https://hackmd.io/@X5Y0GrH0RY-KPMg2uH7o5Q/ByvrtDbFn" target="_blank">here</a> for the ontology used)
<textarea ng-model="queryString" style="display: block;width:100%;height:150px;" rows="10" cols="50"></textarea>
<button ng-click="query_custom()">Send</button>
<hr/>
<b>Result:</b>
<pre class="query-return" id="query_return">{{n.short}}&#10;{{n.long}}</pre>
</div>

</body>
42 changes: 42 additions & 0 deletions src/mmt-api/resources/mmt-web/stex/queries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
angular.module('QueryApp', []).controller('QueryViewer', [ '$scope', '$http', function($scope, $http) {



$scope.query_test = function() {
document.getElementById("query_return").innerHTML = "Loading...";
$http.get('/:sTeX/query/test').success(function(data) {
document.getElementById("query_return").innerHTML = JSON.stringify(data,null,2);
});
};



$scope.query_all = function() {
document.getElementById("query_return").innerHTML = "Loading...";
$http.get('/:sTeX/query/all').success(function(data) {
document.getElementById("query_return").innerHTML = JSON.stringify(data,null,2);
});
};
$scope.query_all_undefined = function() {
document.getElementById("query_return").innerHTML = "Loading...";
$http.get('/:sTeX/query/all_undefined').success(function(data) {
document.getElementById("query_return").innerHTML = JSON.stringify(data,null,2);
});
};
$scope.query_all_with_data = function() {
document.getElementById("query_return").innerHTML = "Loading...";
$http.get('/:sTeX/query/all_with_data').success(function(data) {
document.getElementById("query_return").innerHTML = JSON.stringify(data,null,2);
});
};
$scope.do_query = function(s) {
document.getElementById("query_return").innerHTML = "Loading...";
$http.post('/:query/sparql',s).success(function(data) {
document.getElementById("query_return").innerHTML = JSON.stringify(data,null,2);
});
};
$scope.queryString ="SELECT ?x WHERE { \n <http://mathhub.info/MiKoMH/AI/course/notes/notes.omdoc#> (<http://mathhub.info/ulo#crossrefs>|<http://mathhub.info/ulo#specifies>|<http://mathhub.info/ulo#contains>|<http://mathhub.info/ulo#has-language-module>)+ ?x .\n ?x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://mathhub.info/ulo#constant> .\n}";
$scope.query_custom = function() {
$scope.do_query($scope.queryString)
};
}]);
19 changes: 11 additions & 8 deletions src/mmt-api/src/main/info/kwarc/mmt/api/ontology/ULO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,8 @@ object ULO {

object RDFStore {
val memory = URI.scheme("mmt") colon "memory"
def archive(id:String) = iri(memory.toString + "/archive#" + id)
def archiveId(iri: IRI) = if (iri.getNamespace == memory.toString + "/archive") {
def archive(id:String) = iri(memory.toString + "archive#" + id)
def archiveId(iri: IRI) = if (iri.getNamespace == memory.toString + "archive") {
Some(iri.getLocalName)
} else None
import org.eclipse.rdf4j.rio.RDFFormat
Expand Down Expand Up @@ -976,7 +976,10 @@ object SPARQL {
sealed trait Object {
def toObjString : String
}
sealed trait Subject extends this.Object
sealed trait SubjectT extends this.Object
case class Subject(s:String) extends SubjectT {
def toObjString = "<" + s + ">"
}

private case class Trans(p:Predicate) extends Predicate {
def predString: String = p.predString + "+"
Expand All @@ -998,17 +1001,17 @@ object SPARQL {
}
implicit def asPred(u:ULOPredicate): Predicate = UloPred(u)

private case class PathO(p:Path) extends Subject {
private case class PathO(p:Path) extends SubjectT {
override def toObjString: String = "<" + pathToIri(p).toString + ">"
}
private case class IriO(i:IRI) extends Object {
override def toObjString: String = "<" + i.toString + ">"
}
case class V(s:String) extends Subject with Predicate {
case class V(s:String) extends SubjectT with Predicate {
def toObjString = s"?$s"
def predString: String = s"?$s"
}
implicit def pathtosubject(p:Path): Subject = PathO(p)
implicit def pathtosubject(p:Path): SubjectT = PathO(p)

implicit def classtoobject(p: ULOClass): Object = IriO(p.toIri)
private case class SelectWhere(vars:List[String],where:SparqlQuery) extends SparqlQuery {
Expand All @@ -1018,10 +1021,10 @@ object SPARQL {
def WHERE(q : SparqlQuery): SparqlQuery = SelectWhere(`var`.toList,q)
}
import org.eclipse.rdf4j.sparqlbuilder.core.query.Queries._
case class T(subject: Subject,predicate: this.Predicate,`object`:this.Object) extends SparqlQuery {
case class T(subject: SubjectT, predicate: this.Predicate, `object`:this.Object) extends SparqlQuery {
def queryString: String = s"${subject.toObjString} ${predicate.predString} ${`object`.toObjString} ."
}
case class HASTYPE(subject: Subject,`object`:this.Object) extends SparqlQuery {
case class HASTYPE(subject: SubjectT, `object`:this.Object) extends SparqlQuery {
override def queryString: String = s"${subject.toObjString} <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ${`object`.toObjString} ."
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package info.kwarc.mmt.stex.Extensions

import info.kwarc.mmt.api.{GlobalName, Path}
import info.kwarc.mmt.api.ontology.SPARQL.T
import info.kwarc.mmt.api.ontology.ULO
import info.kwarc.mmt.api.utils.{JSONArray, JSONNull, JSONObject, JSONString}
import info.kwarc.mmt.api.web.ServerResponse.{JsonResponse, ResourceResponse}
import info.kwarc.mmt.api.web.{ServerRequest, ServerResponse}
import info.kwarc.mmt.stex.STeXServer

trait QueryExtension { this : STeXServer =>
protected def queryRequest(request: ServerRequest): ServerResponse = {
request.path.lastOption match {
case Some("all") =>
JsonResponse(JSONArray(getAll.map(p => JSONString(p.toString)) :_*))
case Some("all_undefined") =>
JsonResponse(JSONArray(undefineds.map(p => JSONString(p.toString)): _*))
case Some("all_with_data") =>
JsonResponse(JSONArray(symbols_with_stuff.map(p =>
JSONObject(
("path", JSONString(p._1.toString)),
("macro", p._2.map(s => JSONString("\\" + s)).getOrElse(JSONNull)),
//("notations", JSONArray(p._3.map(JSONString): _*))
)):_*))
case Some("test") =>
JsonResponse(JSONArray(test.map(p => JSONString(p.toString)): _*))
case _ =>
ResourceResponse("stex/queries.html")
}
}

private lazy val archives = controller.backend.getArchives.filter { a =>
a.properties.get("format").contains("stex")
}

private lazy val getAll: List[Path] = { // TODO hacky; should be more systematic
archives.flatMap(a => {
import info.kwarc.mmt.api.ontology.SPARQL._
controller.depstore.query(
SELECT("x") WHERE (
(
T(Subject(s"mmt://memory/archive#${a.id}"), ULO.contains, V("y")) UNION
T(Subject(s"mmt://memory//archive#${a.id}"), ULO.contains, V("y"))
) AND
T(V("y"),(ULO.contains | ULO.declares | ULO.specifies)+,V("x")) AND
HASTYPE(V("x"),ULO.constant)
)
).getPaths.filter(p => !p.asInstanceOf[GlobalName].module.parent.last.endsWith(".omdoc"))
})
}

private def symbols_with_stuff: List[(Path,Option[String])] = {
getAll.map{p =>
(p,
controller.getO(p).flatMap(SHTMLContentManagement.getMacroName),
//SHTMLContentManagement.getNotations(p.asInstanceOf[GlobalName])(controller).map{n => n.notation.toString}
)
}
}

private def undefineds: List[Path] = {
getAll.filter{p =>
import info.kwarc.mmt.api.ontology.SPARQL._
controller.depstore.query(
SELECT("x") WHERE (
T(p,ULO.docref,V("x")) UNION T(V("x"),ULO.defines,p)
)
).getPaths.isEmpty
}
}

private def test: List[Path] = {
archives.flatMap(a => {
import info.kwarc.mmt.api.ontology.SPARQL._
controller.depstore.query(
SELECT("x") WHERE (
T(Subject(s"mmt://memory/archive#${a.id}"), ULO.contains, V("x")) UNION
T(Subject(s"mmt://memory//archive#${a.id}"), ULO.contains, V("x"))
)
).getPaths
})
}
}
6 changes: 4 additions & 2 deletions src/mmt-stex/src/info/kwarc/mmt/stex/STeXServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import info.kwarc.mmt.api.presentation.Presenter
import info.kwarc.mmt.api.utils.time.Time
import info.kwarc.mmt.api.utils.{File, FilePath, JSON, JSONArray, JSONObject, JSONString, MMTSystem, XMLEscaping}
import info.kwarc.mmt.api.web.{ServerExtension, ServerRequest, ServerResponse}
import info.kwarc.mmt.stex.Extensions.{Definienda, ExportExtension, FrontendExtension, NotationExtractor, OMDocHTML, OMDocSHTMLRules, SHTMLBrowser, SHTMLContentManagement, SHTMLDocumentServer, STeXRelationals}
import info.kwarc.mmt.stex.Extensions.{Definienda, ExportExtension, FrontendExtension, NotationExtractor, OMDocHTML, OMDocSHTMLRules, QueryExtension, SHTMLBrowser, SHTMLContentManagement, SHTMLDocumentServer, STeXRelationals}
import info.kwarc.mmt.stex.lsp.{MathHubServer, RemoteLSP, STeXLSPServer, SearchResultServer}
import info.kwarc.mmt.stex.rules.MathStructureFeature
import info.kwarc.mmt.stex.vollki.{FullsTeXGraph, JupyterBookArchive, VirtualArchive, VollKi}
Expand All @@ -24,7 +24,8 @@ case class ErrorReturn(s : String) extends Throwable {
}


class STeXServer extends ServerExtension("sTeX") with OMDocSHTMLRules with SHTMLDocumentServer with SHTMLBrowser with OMDocHTML with ExportExtension with FrontendExtension {
class STeXServer extends ServerExtension("sTeX") with OMDocSHTMLRules with SHTMLDocumentServer with SHTMLBrowser
with OMDocHTML with ExportExtension with FrontendExtension with QueryExtension {
def ctrl = controller
def getArchives = controller.backend.getStores.collect {
case a : Archive if a.properties.get("format").contains("stex") => a
Expand Down Expand Up @@ -101,6 +102,7 @@ class STeXServer extends ServerExtension("sTeX") with OMDocSHTMLRules with SHTML
lazy val htmlpres = new MMTsTeXPresenter(texPresenter,xhtmlPresenter)

override def apply(request: ServerRequest): ServerResponse = try {
if (request.path.startsWith(List(":sTeX","query"))) return queryRequest(request.copy(path=request.path.drop(2)))
request.path.lastOption match {
case Some("document" | "pdf" | "fullhtml" | "documentTop" | "fulldocument" | "fragment" | "rawfragment" | "symbol" | "declaration" |
"variable" | "css" | "sections" | "definienda" | "lo" | "loraw") =>
Expand Down

0 comments on commit 316c8c2

Please sign in to comment.