<commit_before>package org.rspql.spin.utils;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.UUID;

import org.rspql.lang.rspql.ParserRSPQL;
import org.topbraid.spin.arq.ARQ2SPIN;
import org.topbraid.spin.arq.ARQFactory;
import org.topbraid.spin.model.Template;
import org.topbraid.spin.system.SPINModuleRegistry;
import org.topbraid.spin.vocabulary.ARG;
import org.topbraid.spin.vocabulary.SP;
import org.topbraid.spin.vocabulary.SPIN;
import org.topbraid.spin.vocabulary.SPL;

import com.hp.hpl.jena.query.ParameterizedSparqlString;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolutionMap;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.NodeIterator;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;

public class TemplateManager {
	public Model model = ModelFactory.createDefaultModel();
	public String NS = "http://w3id.org/rsp/spin/template#";
	public SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd'T'hh:mm:ss");
	
	/**
	 * Setup a new template manager
	 * @param loadTemplates
	 */
	public TemplateManager() {
		// Initialize
		SPINModuleRegistry.get().init();
		ParserRSPQL.register();
		ARQFactory.get().setSyntax(ParserRSPQL.rspqlSyntax);
		// Eager literal validation active, required
		com.hp.hpl.jena.shared.impl.JenaParameters.enableEagerLiteralValidation = true;
	}

	/**
	 * Load templates from input stream.
	 * @param is
	 * @param format
	 */
	public void loadTemplates(InputStream is, String format) {
		model.read(is, format);
	}
	
	/**
	 * Set the default namespace.
	 * @param ns
	 */
	public void setNS(String ns){
		this.NS = ns;
	}

	/**
	 * Add a new query template.
	 * 
	 * @param qString
	 * @param handle
	 * @param comment
	 */
	public Template createTemplate(String qString, String handle, String comment) {
		handle = handle.startsWith("http://") ? handle : NS + handle;

		Query query = QueryFactory.create(qString, ParserRSPQL.rspqlSyntax);
		Resource queryType;
		switch (query.getQueryType()) {
		case Query.QueryTypeAsk:
			// TODO: Not tested
			queryType = SPIN.AskTemplate;
			break;
		case Query.QueryTypeConstruct:
			queryType = SPIN.ConstructTemplate;
			break;
		case Query.QueryTypeSelect:
			queryType = SPIN.SelectTemplate;
			break;
		default:
			System.err.println("Unrecognized");
			return null;
		}
		
		ARQ2SPIN arq2SPIN = new ARQ2SPIN(model);
		org.topbraid.spin.model.Query spinQuery = arq2SPIN.createQuery(query, null);

		// Create a template
		Template template = model.createResource(handle, queryType).as(Template.class);
		template.addProperty(SPIN.body, spinQuery);
		template.addProperty(RDFS.comment, comment);
		return template;
	}

	/**
	 * Create a template argument.
	 * 
	 * @param template
	 * @param varName
	 * @param valueType
	 * @param defaultValue
	 * @param optional
	 * @param comment
	 */
	public void createArgument(Template template, String varName, RDFNode valueType, RDFNode defaultValue,
			boolean optional, String comment) {
		Model model = template.getModel();
		Resource arg = model.createResource(SPL.Argument);
		template.addProperty(SPIN.constraint, arg);

		arg.addProperty(SPL.predicate, model.getProperty(ARG.NS + varName));
		arg.addProperty(SPL.valueType, valueType);
		if (defaultValue != null) {
			arg.addProperty(SPL.defaultValue, defaultValue);
		}
		arg.addProperty(SPL.optional, model.createTypedLiteral(optional));
		arg.addProperty(RDFS.comment, comment);
	}

	/**
	 * Return a template from the template model or null.
	 * 
	 * @param handle
	 * @return
	 */
	public Template getTemplate(String handle) {
		handle = handle.startsWith("http://") ? handle : NS + handle;

		// Find template type
		NodeIterator iter = model.listObjectsOfProperty(model.createResource(handle), RDF.type);
		if (iter.hasNext()) {
			Resource r = iter.next().asResource();
			if (r.equals(SPIN.SelectTemplate)) {
				return model.createResource(handle, SPIN.SelectTemplate).as(Template.class);
			} else if (r.equals(SPIN.ConstructTemplate)) {
				return model.createResource(handle, SPIN.ConstructTemplate).as(Template.class);
			} else if (r.equals(SPIN.AskTemplate)) {<commit_msg>Create argument now returns created argument<commit_after>package org.rspql.spin.utils;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;

import org.rspql.lang.rspql.ParserRSPQL;
import org.topbraid.spin.arq.ARQ2SPIN;
import org.topbraid.spin.arq.ARQFactory;
import org.topbraid.spin.model.Template;
import org.topbraid.spin.system.SPINModuleRegistry;
import org.topbraid.spin.vocabulary.ARG;
import org.topbraid.spin.vocabulary.SP;
import org.topbraid.spin.vocabulary.SPIN;
import org.topbraid.spin.vocabulary.SPL;

import com.hp.hpl.jena.query.ParameterizedSparqlString;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolutionMap;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.NodeIterator;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;

public class TemplateManager {
	public Model model = ModelFactory.createDefaultModel();
	public String NS = "http://w3id.org/rsp/spin/template#";
	public SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd'T'hh:mm:ss");
	
	/**
	 * Setup a new template manager
	 * @param loadTemplates
	 */
	public TemplateManager() {
		// Initialize
		SPINModuleRegistry.get().init();
		ParserRSPQL.register();
		ARQFactory.get().setSyntax(ParserRSPQL.rspqlSyntax);
		// Eager literal validation active, required
		com.hp.hpl.jena.shared.impl.JenaParameters.enableEagerLiteralValidation = true;
	}

	/**
	 * Load templates from input stream.
	 * @param is
	 * @param format
	 */
	public void loadTemplates(InputStream is, String format) {
		model.read(is, format);
	}
	
	/**
	 * Set the default namespace.
	 * @param ns
	 */
	public void setNS(String ns){
		this.NS = ns;
	}

	/**
	 * Add a new query template.
	 * 
	 * @param qString
	 * @param handle
	 * @param comment
	 */
	public Template createTemplate(String qString, String handle, String comment) {
		handle = handle.startsWith("http://") ? handle : NS + handle;

		Query query = QueryFactory.create(qString, ParserRSPQL.rspqlSyntax);
		Resource queryType;
		switch (query.getQueryType()) {
		case Query.QueryTypeAsk:
			// TODO: Not tested
			queryType = SPIN.AskTemplate;
			break;
		case Query.QueryTypeConstruct:
			queryType = SPIN.ConstructTemplate;
			break;
		case Query.QueryTypeSelect:
			queryType = SPIN.SelectTemplate;
			break;
		default:
			System.err.println("Unrecognized");
			return null;
		}
		
		ARQ2SPIN arq2SPIN = new ARQ2SPIN(model);
		org.topbraid.spin.model.Query spinQuery = arq2SPIN.createQuery(query, null);

		// Create a template
		Template template = model.createResource(handle, queryType).as(Template.class);
		template.addProperty(SPIN.body, spinQuery);
		template.addProperty(RDFS.comment, comment);
		return template;
	}

	/**
	 * Create a template argument.
	 * @param template
	 * @param varName
	 * @param valueType
	 * @param defaultValue
	 * @param optional
	 * @param comment
	 * @return arg
	 */
	public Resource createArgument(Template template, String varName, RDFNode valueType, RDFNode defaultValue,
			boolean optional, String comment) {
		Model model = template.getModel();
		Resource arg = model.createResource(SPL.Argument);
		template.addProperty(SPIN.constraint, arg);

		arg.addProperty(SPL.predicate, model.getProperty(ARG.NS + varName));
		arg.addProperty(SPL.valueType, valueType);
		if (defaultValue != null) {
			arg.addProperty(SPL.defaultValue, defaultValue);
		}
		arg.addProperty(SPL.optional, model.createTypedLiteral(optional));
		arg.addProperty(RDFS.comment, comment);
		return arg;
	}

	/**
	 * Return a template from the template model or null.
	 * 
	 * @param handle
	 * @return
	 */
	public Template getTemplate(String handle) {
		handle = handle.startsWith("http://") ? handle : NS + handle;

		// Find template type
		NodeIterator iter = model.listObjectsOfProperty(model.createResource(handle), RDF.type);
		if (iter.hasNext()) {
			Resource r = iter.next().asResource();
			if (r.equals(SPIN.SelectTemplate)) {
				return model.createResource(handle, SPIN.SelectTemplate).as(Template.class);
			} else if (r.equals(SPIN.ConstructTemplate)) {
				return model.createResource(handle, SPIN.ConstructTemplate).as(Template.class);
			} else if (r.equals(SPIN.AskTemplate)) {
{'fraction_non_alphanumeric': 0.12493034659534158, 'fraction_numerical': 0.0013373453694416582, 'mean_word_length': 4.306918982850385, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2977548', 'n_tokens_mistral': 3200, 'n_tokens_neox': 2954, 'n_words': 593}