<commit_before># 
#    Portions Copyright (c) 2009-10 Zepheira LLC, Some Rights Reserved
#    Portions Copyright (c) 2010-11 Talis Inc, Some Rights Reserved
# 
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
# 
#        http://www.apache.org/licenses/LICENSE-2.0
# 
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

@prefix xsd:<http://www.w3.org/2001/XMLSchema#>.
@prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>.
@prefix owl:<http://www.w3.org/2002/07/owl#>.
@prefix sd:<http://www.w3.org/ns/sparql-service-description#>.
@prefix msg:<http://www.openrdf.org/rdf/2011/messaging#>.
@prefix calli:<http://callimachusproject.org/rdf/2009/framework#>.
@prefix :<#>.

<> a <SchemaGraph>.

<SparqlService> a <Local>, owl:Class;
	rdfs:subClassOf <Viewable>, sd:Service;
	rdfs:isDefinedBy </callimachus>;
	calli:realm </>;
	calli:view <sparql-view.xhtml>.

:EvaluateSparql rdfs:subClassOf msg:Message;
	rdfs:subClassOf [owl:onProperty msg:target; owl:allValuesFrom <SparqlService>];
	rdfs:subClassOf [owl:onProperty msg:object; owl:allValuesFrom <java:org.apache.http.HttpResponse>];
	msg:imports <java:org.callimachusproject.server.exceptions.BadRequest>;
	msg:imports <java:org.openrdf.query.QueryLanguage>;
	msg:imports <java:org.openrdf.query.parser.QueryParserRegistry>;
	msg:imports <java:org.openrdf.query.parser.ParsedBooleanQuery>;
	msg:imports <java:org.openrdf.query.parser.ParsedGraphQuery>;
	msg:imports <java:org.openrdf.query.parser.ParsedTupleQuery>;
	msg:imports <java:org.openrdf.query.GraphQueryResult>;
	msg:imports <java:org.openrdf.query.TupleQueryResult>;
	msg:imports <java:org.apache.http.ProtocolVersion>;
	msg:imports <java:org.apache.http.message.BasicHttpResponse>;
	msg:imports <java:org.openrdf.query.MalformedQueryException>;
	msg:imports <java:org.callimachusproject.server.writers.AggregateWriter>;
	msg:imports <java:org.callimachusproject.server.util.MessageType>;
	msg:imports <java:org.callimachusproject.server.model.ReadableHttpEntityChannel>;
	msg:imports <java:org.callimachusproject.server.exceptions.InternalServerError>;
	msg:script """
		if (!qry)
			throw new BadRequest("Missing query");
		try {
			var query;
			var con = this.objectConnection;
			if (qry.search(/\\bPREFIX\\b/i) >= 0) {
				query = qry;
			} else {
				var sb = new java.lang.StringBuilder();
				var namespaces = con.namespaces.asList();
				for (var i = 0; i < namespaces.size(); i++) {
					var prefix = namespaces.get(i).prefix;
					var uri = namespaces.get(i).name;
					if (qry.indexOf(prefix) >= 0) {
						sb.append("PREFIX ").append(prefix);
						sb.append(":<").append(uri).append(">\\n");
					}
				}
				query = sb.append(qry).toString();
			}
			var mime;
			var rs;
			var type;
			var base = this.resource.stringValue();
			var reg = QueryParserRegistry.getInstance();
			var parser = reg.get(QueryLanguage.SPARQL).getParser();
			var parsed = parser.parseQuery(query, base);
			if (parsed instanceof ParsedBooleanQuery) {
				mime = "application/sparql-results+xml";
				rs = con.prepareBooleanQuery(QueryLanguage.SPARQL, query, base).evaluate();
				type = java.lang.Boolean.TYPE;
			} else if (parsed instanceof ParsedGraphQuery) {
				mime = "application/rdf+xml";
				rs = con.prepareGraphQuery(QueryLanguage.SPARQL, query, base).evaluate();
				type = rs.getClass();
			} else if (parsed instanceof ParsedTupleQuery) {
				mime = "application/sparql-results+xml";
				rs = con.prepareTupleQuery(QueryLanguage.SPARQL, query, base).evaluate();
				type = rs.getClass();
			} else {
				throw new InternalServerError("Unknown query type: "
						+ parsed.getClass());
			}
			var http11 = new ProtocolVersion("HTTP", 1, 1);
			var resp = new BasicHttpResponse(http11, 200, "OK");
			var writer = AggregateWriter.getInstance();
			var body = writer.write(new MessageType(mime, type, type, con), rs, base, null);
			resp.setEntity(new ReadableHttpEntityChannel(mime, -1, body));
			return resp;
		} catch (e if e.javaException instanceof MalformedQueryException) {
			throw new BadRequest(e.javaException.toString());
		} catch (e if e.javaException instanceof java.lang.IllegalArgumentException) {
			throw new BadRequest("Missing accept header: " + e.javaException.getMessage());
		}
	""".

:qry a owl:DatatypeProperty, owl:FunctionalProperty;
	rdfs:domain :EvaluateSparql;
	rdfs:range xsd:string.

# POST application/x-www-form-urlencoded
:PostForm rdfs:subClassOf msg:Message;
	rdfs:subClassOf [owl:onProperty msg:target; owl:allValuesFrom <SparqlService>];
	rdfs:subClassOf [owl:onProperty msg:object; owl:allValuesFrom <java:org.apache.http.HttpResponse>];
	calli:method "POST";
	calli:type "message/x-response";
	calli:transform :TransformResult;
	msg:imports <java:org.apache.http.ProtocolVersion>;
	msg:imports <java:org.apache.http.message.BasicHttpResponse>;
	msg:imports <java:org.apache.http.entity.StringEntity>;
	msg:imports <java:org.openrdf.query.MalformedQueryException>;
	msg:imports <java:org.callimachusproject.server.exceptions.BadRequest>;
	msg:imports <java:org.openrdf.query.QueryLanguage>;
	msg:script """
		if (map && map.containsKey("query"))
			return this.EvaluateSparql(map.get("query")[0]);
		if (!map || !map.containsKey("update"))
			throw new org.callimachusproject.server.exceptions.BadRequest("Missing query");
		var qry = map.get("update")[0];
		try {
			var query;
			var con = this.objectConnection;
			if (qry.search(/\\bPREFIX\\b/i) >= 0) {
				query = qry;
			} else {
				var sb = new java.lang.StringBuilder();
				var namespaces = con.namespaces.asList();
				for (var i = 0; i < namespaces.size(); i++) {
					var prefix = namespaces.get(i).prefix;
					var uri = namespaces.get(i).name;
					if (qry.indexOf(prefix) >= 0) {
						sb.append("PREFIX ").append(prefix);
						sb.append(":<").append(uri).append(">\\n");
					}
				}
				query = sb.append(qry).toString();
			}
			var base = this.resource.stringValue();
			con.prepareUpdate(QueryLanguage.SPARQL, query, base).execute();
			return null;
		} catch (e if e.javaException instanceof MalformedQueryException) {
			throw new BadRequest(e.javaException.toString());
		}
	""".

:map a owl:ObjectProperty, owl:FunctionalProperty;
	rdfs:domain :PostForm;
	rdfs:range <java:java.util.Map>;
	calli:type "application/x-www-form-urlencoded".

# GET query parameter
:GetQueryResult rdfs:subClassOf msg:Message;
	rdfs:subClassOf [owl:onProperty msg:target; owl:allValuesFrom <SparqlService>];
	rdfs:subClassOf [owl:onProperty msg:object; owl:allValuesFrom <java:org.apache.http.HttpResponse>];
	calli:method "GET";
	calli:query "query";
	calli:type "message/x-response";
	calli:header "cache-control:no-validate";
	msg:imports <java:org.apache.http.ProtocolVersion>;
	msg:imports <java:org.apache.http.message.BasicHttpResponse>;
	msg:imports <java:org.apache.http.entity.StringEntity>;
	msg:script """
		if (queryStr)
			return this.EvaluateSparql(queryStr);
		var http11 = new ProtocolVersion("HTTP", 1, 1);
		var resp = new BasicHttpResponse(http11, 302, "see-other");
		resp.setHeader("Location", this.toString() + "?view");
		resp.setEntity(new StringEntity(""));
		return resp;
	""".

:queryStr a owl:DatatypeProperty, owl:FunctionalProperty;
	rdfs:domain :GetQueryResult;
	rdfs:range xsd:string;
	calli:query "query".

# Transformers
:TransformResult rdfs:subClassOf msg:Message;
	rdfs:subClassOf [owl:onProperty msg:target; owl:allValuesFrom <SparqlService>];
	rdfs:subClassOf [owl:onProperty msg:object; owl:allValuesFrom <java:java.io.InputStream>];
	calli:type "application/xml";
	calli:transform :TransformXml;
	msg:script "return msg && msg.entity ? msg.entity.content : null".

:msg a owl:ObjectProperty, owl:FunctionalProperty;
	rdfs:domain :TransformResult;
	rdfs:range <java:org.apache.http.HttpResponse>;
	calli:type "message/x-response".

:TransformXml rdfs:subClassOf msg:Message;
	rdfs:subClassOf [owl:onProperty msg:target; owl:allValuesFrom <SparqlService>];
	rdfs:subClassOf [owl:onProperty msg:object; owl:allValuesFrom <java:java.io.InputStream>];
	calli:type "application/xml";
	calli:transform <transforms/TransformLayout>;
	msg:xslt <transforms/sparql.xsl>.

:inputStream a owl:ObjectProperty, owl:FunctionalProperty;<commit_msg>Support direct sparql POSTs to sparql service<commit_after>#
#    Portions Copyright (c) 2009-10 Zepheira LLC, Some Rights Reserved
#    Portions Copyright (c) 2010-11 Talis Inc, Some Rights Reserved
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

@prefix xsd:<http://www.w3.org/2001/XMLSchema#>.
@prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#>.
@prefix owl:<http://www.w3.org/2002/07/owl#>.
@prefix sd:<http://www.w3.org/ns/sparql-service-description#>.
@prefix msg:<http://www.openrdf.org/rdf/2011/messaging#>.
@prefix calli:<http://callimachusproject.org/rdf/2009/framework#>.
@prefix :<#>.

<> a <SchemaGraph>.

<SparqlService> a <Local>, owl:Class;
	rdfs:subClassOf <Viewable>, sd:Service;
	rdfs:isDefinedBy </callimachus>;
	calli:realm </>;
	calli:view <sparql-view.xhtml>.

:EvaluateSparql rdfs:subClassOf msg:Message;
	rdfs:subClassOf [owl:onProperty msg:target; owl:allValuesFrom <SparqlService>];
	rdfs:subClassOf [owl:onProperty msg:object; owl:allValuesFrom <java:org.apache.http.HttpResponse>];
	msg:imports <java:org.callimachusproject.server.exceptions.BadRequest>;
	msg:imports <java:org.openrdf.query.QueryLanguage>;
	msg:imports <java:org.openrdf.query.parser.QueryParserRegistry>;
	msg:imports <java:org.openrdf.query.parser.ParsedBooleanQuery>;
	msg:imports <java:org.openrdf.query.parser.ParsedGraphQuery>;
	msg:imports <java:org.openrdf.query.parser.ParsedTupleQuery>;
	msg:imports <java:org.openrdf.query.GraphQueryResult>;
	msg:imports <java:org.openrdf.query.TupleQueryResult>;
	msg:imports <java:org.apache.http.ProtocolVersion>;
	msg:imports <java:org.apache.http.message.BasicHttpResponse>;
	msg:imports <java:org.openrdf.query.MalformedQueryException>;
	msg:imports <java:org.callimachusproject.server.writers.AggregateWriter>;
	msg:imports <java:org.callimachusproject.server.util.MessageType>;
	msg:imports <java:org.callimachusproject.server.model.ReadableHttpEntityChannel>;
	msg:imports <java:org.callimachusproject.server.exceptions.InternalServerError>;
	msg:script """
		if (!qry)
			throw new BadRequest("Missing query");
		try {
			var query = this.AddPrefix(qry);
			var con = this.objectConnection;
			var mime;
			var rs;
			var type;
			var base = this.resource.stringValue();
			var reg = QueryParserRegistry.getInstance();
			var parser = reg.get(QueryLanguage.SPARQL).getParser();
			var parsed = parser.parseQuery(query, base);
			if (parsed instanceof ParsedBooleanQuery) {
				mime = "application/sparql-results+xml";
				rs = con.prepareBooleanQuery(QueryLanguage.SPARQL, query, base).evaluate();
				type = java.lang.Boolean.TYPE;
			} else if (parsed instanceof ParsedGraphQuery) {
				mime = "application/rdf+xml";
				rs = con.prepareGraphQuery(QueryLanguage.SPARQL, query, base).evaluate();
				type = rs.getClass();
			} else if (parsed instanceof ParsedTupleQuery) {
				mime = "application/sparql-results+xml";
				rs = con.prepareTupleQuery(QueryLanguage.SPARQL, query, base).evaluate();
				type = rs.getClass();
			} else {
				throw new InternalServerError("Unknown query type: "
						+ parsed.getClass());
			}
			var http11 = new ProtocolVersion("HTTP", 1, 1);
			var resp = new BasicHttpResponse(http11, 200, "OK");
			var writer = AggregateWriter.getInstance();
			var body = writer.write(new MessageType(mime, type, type, con), rs, base, null);
			resp.setEntity(new ReadableHttpEntityChannel(mime, -1, body));
			return resp;
		} catch (e if e.javaException instanceof MalformedQueryException) {
			throw new BadRequest(e.javaException.toString());
		} catch (e if e.javaException instanceof java.lang.IllegalArgumentException) {
			throw new BadRequest("Missing accept header: " + e.javaException.getMessage());
		}
	""".

:qry a owl:DatatypeProperty, owl:FunctionalProperty;
	rdfs:domain :EvaluateSparql;
	rdfs:range xsd:string.

# POST application/sparql-query
:PostQuery rdfs:subClassOf msg:Message;
	rdfs:subClassOf [owl:onProperty msg:target; owl:allValuesFrom <SparqlEndPoint>];
	rdfs:subClassOf [owl:onProperty msg:object; owl:allValuesFrom <java:org.apache.http.HttpResponse>];
	msg:method "POST";
	msg:type "message/x-response";
	msg:transform :TransformResult;
	msg:imports <java:org.openrdf.http.object.exceptions.BadRequest>;
	msg:script """
		if (!query)
			throw new BadRequest("Missing query body");
		return this.EvaluateSparql(new java.lang.String(query, "UTF-8"));
	""".

:query a owl:DatatypeProperty, owl:FunctionalProperty;
	rdfs:domain :PostQuery;
	rdfs:range xsd:hexBinary;
	msg:type "application/sparql-query".

# POST application/sparql-update
:PostUpdate rdfs:subClassOf msg:Message;
	rdfs:subClassOf [owl:onProperty msg:target; owl:allValuesFrom <SparqlEndPoint>];
	rdfs:subClassOf [owl:onProperty msg:object; owl:allValuesFrom owl:Nothing];
	msg:method "POST";
	msg:imports <java:org.openrdf.http.object.exceptions.BadRequest>;
	msg:script """
		if (!update)
			throw new BadRequest("Missing update body");
		try {
			var query = this.AddPrefix(update);
			var con = this.objectConnection;
			var base = this.resource.stringValue();
			con.prepareUpdate(QueryLanguage.SPARQL, query, base).execute();
			return null;
		} catch (e if e.javaException instanceof MalformedQueryException) {
			throw new BadRequest(e.javaException.toString());
		}
	""".

:update a owl:DatatypeProperty, owl:FunctionalProperty;
	rdfs:domain :PostUpdate;
	rdfs:range xsd:hexBinary;
	msg:type "application/sparql-update".

# POST application/x-www-form-urlencoded
:PostForm rdfs:subClassOf msg:Message;
	rdfs:subClassOf [owl:onProperty msg:target; owl:allValuesFrom <SparqlService>];
	rdfs:subClassOf [owl:onProperty msg:object; owl:allValuesFrom <java:org.apache.http.HttpResponse>];
	calli:method "POST";
	calli:type "message/x-response";
	calli:transform :TransformResult;
	msg:imports <java:org.apache.http.ProtocolVersion>;
	msg:imports <java:org.apache.http.message.BasicHttpResponse>;
	msg:imports <java:org.apache.http.entity.StringEntity>;
	msg:imports <java:org.openrdf.query.MalformedQueryException>;
	msg:imports <java:org.callimachusproject.server.exceptions.BadRequest>;
	msg:imports <java:org.openrdf.query.QueryLanguage>;
	msg:script """
		if (map && map.containsKey("query"))
			return this.EvaluateSparql(map.get("query")[0]);
		if (!map || !map.containsKey("update"))
			throw new org.callimachusproject.server.exceptions.BadRequest("Missing query");
		var qry = map.get("update")[0];
		try {
			var query = this.AddPrefix(qry);
			var con = this.objectConnection;
			var base = this.resource.stringValue();
			con.prepareUpdate(QueryLanguage.SPARQL, query, base).execute();
			return null;
		} catch (e if e.javaException instanceof MalformedQueryException) {
			throw new BadRequest(e.javaException.toString());
		}
	""".

:map a owl:ObjectProperty, owl:FunctionalProperty;
	rdfs:domain :PostForm;
	rdfs:range <java:java.util.Map>;
	calli:type "application/x-www-form-urlencoded".

# GET query parameter
:GetQueryResult rdfs:subClassOf msg:Message;
	rdfs:subClassOf [owl:onProperty msg:target; owl:allValuesFrom <SparqlService>];
	rdfs:subClassOf [owl:onProperty msg:object; owl:allValuesFrom <java:org.apache.http.HttpResponse>];
	calli:method "GET";
	calli:query "query";
	calli:type "message/x-response";
	calli:header "cache-control:no-validate";
	msg:imports <java:org.apache.http.ProtocolVersion>;
	msg:imports <java:org.apache.http.message.BasicHttpResponse>;
	msg:imports <java:org.apache.http.entity.StringEntity>;
	msg:script """
		if (queryStr)
			return this.EvaluateSparql(queryStr);
		var http11 = new ProtocolVersion("HTTP", 1, 1);
		var resp = new BasicHttpResponse(http11, 302, "see-other");
		resp.setHeader("Location", this.toString() + "?view");
		resp.setEntity(new StringEntity(""));
		return resp;
	""".

:queryStr a owl:DatatypeProperty, owl:FunctionalProperty;
	rdfs:domain :GetQueryResult;
	rdfs:range xsd:string;
	calli:query "query".

:AddPrefix rdfs:subClassOf msg:Message;
	rdfs:subClassOf [owl:onProperty msg:target; owl:allValuesFrom <SparqlService>];
	rdfs:subClassOf [owl:onProperty msg:literal; owl:allValuesFrom xsd:string];
	msg:script """
		if (inputString.search(/^[^#]*\\bPREFIX\\b/im) >= 0)
			return inputString;
		var sb = new java.lang.StringBuilder();
		var con = this.objectConnection;
		var namespaces = con.namespaces.asList();
		for (var i = 0; i < namespaces.size(); i++) {
			var prefix = namespaces.get(i).prefix;
			var uri = namespaces.get(i).name;
			if (qry.indexOf(prefix) >= 0) {
				sb.append("PREFIX ").append(prefix);
				sb.append(":<").append(uri).append(">\\n");
			}
		}
		return sb.append(inputString).toString();
	""".

:inputString a owl:FunctionalProperty, owl:DatatypeProperty;
	rdfs:domain :AddPrefix;
	rdfs:rang xsd:string.

# Transformers
:TransformResult rdfs:subClassOf msg:Message;
	rdfs:subClassOf [owl:onProperty msg:target; owl:allValuesFrom <SparqlService>];
	rdfs:subClassOf [owl:onProperty msg:object; owl:allValuesFrom <java:java.io.InputStream>];
	calli:type "application/xml";
	calli:transform :TransformXml;
	msg:script "return msg && msg.entity ? msg.entity.content : null".

:msg a owl:ObjectProperty, owl:FunctionalProperty;
	rdfs:domain :TransformResult;
	rdfs:range <java:org.apache.http.HttpResponse>;
	calli:type "message/x-response".

:TransformXml rdfs:subClassOf msg:Message;
	rdfs:subClassOf [owl:onProperty msg:target; owl:allValuesFrom <SparqlService>];
	rdfs:subClassOf [owl:onProperty msg:object; owl:allValuesFrom <java:java.io.InputStream>];
	calli:type "application/xml";
	calli:transform <transforms/TransformLayout>;
	msg:xslt <transforms/sparql.xsl>.

:inputStream a owl:ObjectProperty, owl:FunctionalProperty;
{'fraction_non_alphanumeric': 0.14147143008586877, 'fraction_numerical': 0.008374854235131983, 'mean_word_length': 5.310033444816053, 'pattern_counts': {'":': 3, '<': 123, '<?xml version=': 0, '>': 126, 'https://': 0, 'lorem ipsum': 0, 'www.': 18, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3493457', 'n_tokens_mistral': 6979, 'n_tokens_neox': 6410, 'n_words': 1257}