private static object Execute(string name, Dictionary <string, string> values)
        {
            var          endpoint     = Resources.GetApiPathItem(name);
            EndpointType endpointType = Resources.GetEndpointType(endpoint);

            if (endpointType == EndpointType.HardCoded)
            {
                return(FixedQueryController.ExecuteHardCoded(name, values));
            }
            else
            {
                return(FixedQueryController.ExecuteNamedSparql(name, values));
            }
        }
        internal static object ExecuteNamedSparql(string name, Dictionary <string, string> values)
        {
            var endpoint    = Resources.DB.Endpoints[name];
            var queryString = Resources.GetSparql(name);
            var query       = new SparqlParameterizedString(queryString);

            query.SetUri("schemaUri", Global.SchemaUri);

            if (endpoint.Parameters != null)
            {
                FixedQueryController.SetParameters(query, endpoint.Parameters, values);
            }

            return(FixedQueryController.ExecuteQuery(query, endpoint.Type));
        }
        public static void SetParameters(SparqlParameterizedString query, IEnumerable <OpenApiParameter> parameters, Dictionary <string, string> values)
        {
            foreach (var parameterDefinition in parameters)
            {
                var name = parameterDefinition.Name;
                if (!values.ContainsKey(name))
                {
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                    {
                        Content = new StringContent($"missing parameter {name}")
                    });
                }

                var value = values[name];
                if (string.IsNullOrEmpty(value))
                {
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                    {
                        Content = new StringContent($"missing value for parameter {name}")
                    });
                }

                var type = Resources.GetParameterType(parameterDefinition);

                switch (type)
                {
                case ParameterType.Uri:
                    query.SetUri(name, new Uri(value));
                    break;

                case ParameterType.InstanceUri:
                    query.SetUri(name, new Uri(Global.InstanceUri, value));
                    break;

                case ParameterType.InstanceUris:
                    FixedQueryController.SetUris(query, name, value);
                    break;

                case ParameterType.SchemaUri:
                    query.SetUri(name, new Uri(Global.SchemaUri, value));
                    break;

                case ParameterType.Literal:
                    query.SetLiteral(name, value);
                    break;
                }
            }
        }
        internal static object ExecuteNamedSparql(string name, Dictionary <string, string> values)
        {
            var          endpoint     = Resources.GetApiPathItem(name);
            var          queryString  = Resources.GetSparql(name);
            var          query        = new SparqlParameterizedString(queryString);
            EndpointType endpointType = Resources.GetEndpointType(endpoint);

            query.SetUri("schemaUri", Global.SchemaUri);

            IEnumerable <OpenApiParameter> parameters = Resources.GetSparqlParameters(endpoint);

            if ((parameters != null) && (parameters.Any()))
            {
                FixedQueryController.SetParameters(query, parameters, values);
            }

            return(FixedQueryController.ExecuteQuery(query, endpointType));
        }
        public static IGraph webarticle_by_id(Dictionary <string, string> values)
        {
            var webarticle_id = values["webarticle_id"];

            var uri           = new Uri(Global.InstanceUri, webarticle_id).AbsoluteUri;
            var articleExists = FixedQueryController.ExecuteNamedSparql("exists", new Dictionary <string, string> {
                { "uri", uri }
            }) as SparqlResultSet;

            if (!articleExists.Result)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            try
            {
                return(Contentful.Engine.GetArticle(webarticle_id));
            }
            catch (Contentful.EntryNotFoundException)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }
        public static IGraph with_schema(Dictionary <string, string> values)
        {
            // Run the original query
            var endpoint_name = values["endpoint_name"];
            var originalGraph = FixedQueryController.ExecuteNamedSparql(endpoint_name, values) as IGraph;

            // Collect types and predicates from the result of the original query
            // and get ontology information about them.
            var predicates        = originalGraph.Triples.PredicateNodes;
            var types             = originalGraph.GetTriplesWithPredicate(new Uri(RdfSpecsHelper.RdfType)).Select(t => t.Object);
            var ontologyResources = string.Join(" ", predicates.Union(types).Distinct().Cast <IUriNode>().Select(p => "<" + p.Uri.AbsoluteUri + ">"));
            var ontologyQuery     = @"
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

CONSTRUCT {
	?ontology ?ontologyProperty ?ontologyValue .

	?ontologyResource ?resourceProperty ?resourceValue .
}
FROM <http://www.ontotext.com/explicit>
WHERE {
    VALUES ?ontologyResource {@ontologyResources}

	?ontology ?ontologyProperty ?ontologyValue .

	?ontologyResource
		?resourceProperty ?resourceValue ;
		rdfs:isDefinedBy ?ontology.

    FILTER(?resourceProperty != owl:inverseOf)
}".Replace("@ontologyResources", ontologyResources);

            var ontologyGraph = BaseController.ExecuteList(new SparqlParameterizedString(ontologyQuery)) as IGraph;

            // Collect classes from across both the result of the original query and the ontology generated for it
            // (could be types from the original, classes from the ontology or domains and ranges from the ontology)
            // and see if any are sub-classes of any other
            var domains        = ontologyGraph.GetTriplesWithPredicate(ontologyGraph.CreateUriNode(new Uri(OntologyHelper.PropertyDomain))).Select(t => t.Object);
            var ranges         = ontologyGraph.GetTriplesWithPredicate(ontologyGraph.CreateUriNode(new Uri(OntologyHelper.PropertyRange))).Select(t => t.Object);
            var classes        = ontologyGraph.GetTriplesWithPredicateObject(ontologyGraph.CreateUriNode(new Uri(RdfSpecsHelper.RdfType)), ontologyGraph.CreateUriNode(new Uri(OntologyHelper.OwlClass))).Select(t => t.Subject);
            var classResources = string.Join(" ", domains.Union(ranges).Union(classes).Union(types).Distinct().Cast <IUriNode>().Select(p => "<" + p.Uri.AbsoluteUri + ">"));
            var subClassQuery  = @"
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

CONSTRUCT {
	?class rdfs:subClassOf ?superClass .
}
FROM <http://www.ontotext.com/explicit>
WHERE {
    VALUES ?class {@classResources}
    VALUES ?superClass {@classResources}

	?class rdfs:subClassOf ?superClass .
}".Replace("@classResources", classResources);

            var subClassGraph = BaseController.ExecuteList(new SparqlParameterizedString(subClassQuery)) as IGraph;

            // The result is all three graphs above merged.
            var resultGraph = new Graph();

            resultGraph.Merge(originalGraph);
            resultGraph.Merge(ontologyGraph);
            resultGraph.Merge(subClassGraph);
            return(resultGraph);
        }