Why this SPARQL query don't work into Prolog? - prolog

I am trying to integrate SPARQL queries into Prolog program (I am using SWI-Prolog)
To test it I am doing the following operations:
Into SWI-Prolog shell I execute this command: use_module(library(semweb/sparql_client)). that load the sparql client library
Then, in the SWI-Prolog shell, I execute the following SPARQL query:
?- sparql_query('select count(*) where { ?person a http://dbpedia.org/ontology/Person .?person http://it.dbpedia.org/property/nome ?name.filter regex(?name,"Leonardo"). }', Row, [ host('dbpedia.org'), path('/sparql/')]).
Row = row(literal(type('http://www.w3.org/2001/XMLSchema#integer', '0'))).
I don't know well SPARQL but I think that this is pretty simple and that work in this way:
This query ask about the instances number of objects that are considered personal names on a RDF ontology called dbpedia, passing the input parameter "Leonardo".
As you can see the problem seems that don't find any instance of this type (I have tried also with others personal name)
Why? What are am I missing?

Your query does not return what you want it to return. You can use SPARQL interactively with the DBpedia SPARQL endpoint. I suggest that you use that to debug your query and make sure it returns what you would like first, and then copy it into your Prolog program. You can build it up in pieces to make sure it's giving suitable results. For instance, start with something like the following:
select * where {
?person a <http://dbpedia.org/ontology/Person> .
}
LIMIT 10
SPARQL results
Then expand it piece by piece:
select * where {
?person a <http://dbpedia.org/ontology/Person> .
?person <http://it.dbpedia.org/property/nome> ?name .
}
LIMIT 10
SPARQL results
This query doesn't return anything, so it would be best to debug it before continuing on the the FILTER or COUNT.
After playing with the endpoint for a while, it seems that using REGEX is going to be kind of expensive, as are other string operations. If you can query for the strings you want directly, you might be better off. Unfortunately, you'll need to start working with language tags, too. For instance, here is a query that counts people with the given name or surname "Leonardo".
select COUNT(?person) where {
?person a dbpedia-owl:Person .
{ ?person foaf:givenName "Leonardo" }
UNION
{ ?person foaf:surname "Leonardo" }
}
SPARQL results
It returns 0. However, if we add (English) language tags to those strings, we get different results (133):
select COUNT(?person) where {
?person a dbpedia-owl:Person .
{ ?person foaf:givenName "Leonardo"#en }
UNION
{ ?person foaf:surname "Leonardo"#en }
}
SPARQL results

Related

Filtering a list of values by a field value in GraphQL

So I'm doing some tests with GraphQL, and I'm failing in doing something that I believe is fairly simple.
When going to the GraphQL demo site (https://graphql.org/swapi-graphql) I'm presented with a default query which goes like this:
{
allFilms {
films {
title,
director,
releaseDate
}
}
}
This works as expected and returns a list of films.
Now - I would like to modify this query to return only the films where the director is George Lucas, and for the life of me - I can't figure out how to do that.
I've tried using the where and filter expressions, and also change the second line to films: (director: "George Lucas") but keep getting error messages.
What's the correct syntax for doing that?
Thanks!
If you check the docs of the provided GraphQL schema, you'll see that this is not possible. Following is the definition of the allFilms field:
allFilms(
after: String
first: Int
before: String
last: Int
): FilmsConnection
As per the doc, it has 4 input arguments, which are after, first, before, and last. There is no way to filter this out using the director's name.
GraphQL is not SQL. You cannot use expressions like WHERE or FILTER in GraphQL. The schema is already defined and the filters are pre-defined too. If the schema does not allow you to filter values using a certain field, you just can't do it.
You can to see the graphql schema here https://github.com/graphql/swapi-graphql/blob/master/schema.graphql
The allFilms query does not contain a filter for the field director. Also i can't find other query with this filter.
Most likely you need to write a filter on the result of the query.

How to properly perform like queries with Quickbase

I am working with quicktable queries and everything seems to be fine.
Now I want to perform queries using like operators. For instance in PHP I can do something like:
$data ='content to search';
$stmt = $db->prepare('SELECT * FROM members where name like :name OR email like :email limit 20');
$stmt->execute(array(
':name' => '%'.$data.'%',
':email' => '%'.$data.'%',
));
Now in quick table, I have tried using CT, EX or HAS parameter etc with OR Operators. Only CT gives nearby result but not exact as per code below.
//Email = 7
//name =8
{
"from": "tableId",
"where": "{7.CT.'nancy#gmail.com'}OR{8.CT.'nancy'}"
}
Is there any way I can obtain a better search with like operators with Quickbase. The documentation here does not cover that.
CT is the closest string comparison operator in Quick Base to LIKE in SQL, but since you can't use wildcards in Quick Base queries you might need to group multiple query strings to achieve the same result. The is also a SW operator that can sometimes come in helpful for comparing parts of a strings.

GraphQL - How do I use the return value as a variable for the next query?

How do I take the return result from the first query and assign that result to a variable to use in the second query? I need the first value result before I can make the second call, but would like to make only 1 call to accomplish this. My query:
query {
test1(sku: "12345"){
price
}
test2(itemPrice: $price){
isSuccessful
}
}
Your Query:
query {
test1(sku: "12345"){
price
}
test2(itemPrice: $price){
isSuccessful
}
}
When a query is parsed, it’s converted to an AST, or a tree. In your case your query will look like below:
Now there are 3 phases involved with every graphQl query :
1.Parse — A query is parsed into an abstract syntax tree (or AST). ASTs are incredibly powerful and behind tools like ESLint, babel, etc.
2.Validate — The AST is validated against the schema. Checks for correct query syntax and if the fields exist.
3.Execute — The runtime walks through the AST, starting from the root of the tree, invokes resolvers, collects up results, and emits JSON.
The root Query type is the entry point to the tree and contains our
two root fields, test1 and test2. The test1 and test2 resolvers
are executed in parallel (which is typical among all runtimes). The
tree is executed breadth-first, meaning user must be resolved before
its children price and isSuccessful are executed.
So basically in a single query you cannot solve your problem as of now.

Two "identical" answers to a request (utf8 and non-utf8)

I am having problems with a live.dbpedia SPARQL request, for it returns some entries twice (once as an utf8 URI, once as a non-utf8 URI : Here are the results.
Is it something that needs to be fixed inside of dbpedia (where should it be reported)?
Is there a way to keep only one version of these duplicated urls? (I do not want to ignore a non-utf8 URI if there is no utf8 counterpart)
P.S.: The actual request
select distinct ?name where {
?name <http://purl.org/dc/terms/subject><http://dbpedia.org/resource/Category:Individual_graphs>.
} ORDER BY desc(?name) LIMIT 2
Even though there are multiple URIs that can identify the article, they all have the same article title, so you can extract the title (it's the value of the rdfs:label property), group by that, and then sample the URIs. Doing that, along with using the built-in DBpedia namespaces, I end up with this query:
select distinct (sample(?name_) as ?name) where {
?name_ dcterms:subject category:Individual_graphs ;
rdfs:label ?label
}
group by ?label
order by desc(?name)
SPARQL results

Linq to NHibernate generating 3,000+ SQL statements in one request!

I've been developing a webapp using Linq to NHibernate for the past few months, but haven't profiled the SQL it generates until now. Using NH Profiler, it now seems that the following chunk of code hits the DB more than 3,000 times when the Linq expression is executed.
var activeCaseList = from c in UserRepository.GetCasesByProjectManagerID(consultantId)
where c.CompletionDate == null
select new { c.PropertyID, c.Reference, c.Property.Address, DaysOld = DateTime.Now.Subtract(c.CreationDate).Days, JobValue = String.Format("£{0:0,0}", c.JobValue), c.CurrentStatus };
Where the Repository method looks like:
public IEnumerable<Case> GetCasesByProjectManagerID(int projectManagerId)
{
return from c in Session.Linq<Case>()
where c.ProjectManagerID == projectManagerId
select c;
}
It appears to run the initial Repository query first, then iterates through all of the results checking to see if the CompletionDate is null, but issuing a query to get c.Property.Address first.
So if the initial query returns 2,000 records, even if only five of them have no CompletionDate, it still fires off an SQL query to bring back the address details for the 2,000 records.
The way I had imagined this would work, is that it would evaluate all of the WHERE and SELECT clauses and simply amalgamate them, so the inital query would be like:
SELECT ... WHERE ProjectManager = #p1 AND CompleteDate IS NOT NULL
Which would yield 5 records, and then it could fire the further 5 queries to obtain the addresses. Am I expecting too much here, or am I simply doing something wrong?
Anthony
Change the declaration of GetCasesByProjectManagerID:
public IQueryable<Case> GetCasesByProjectManagerID(int projectManagerId)
You can't compose queries with IEnumerable<T> - they're just sequences. IQueryable<T> is specifically designed for composition like this.
Since I can't add a comment yet. Jon Skeet is right you'll want to use IQueryable, this is allows the Linq provider to Lazily construct the SQL. IEnumerable is the eager version.

Resources