I am trying to learn SPARQL to use with WikiData and I can't figure out how to perform an 'OR' statement, for instance find all taxons which are a subclasses of mammals OR a subclass of a subclass of mammals. I don't see how to use the VALUES method and if I use operator P171* with filter it takes too long. The following code provides an 'AND' statement I would like the equivalent 'OR' statement.
SELECT ?taxon
WHERE
{
?taxon wdt:P171 wd:Q7377. #taxon is a subclass of mammals
?taxonn wdt:P171/wdt:P171 wd:Q7377. #taxon is a subclass of a subclass of mammals
}
There are multiple ways to perform an 'OR' statement in SPARQL. A good overview is provided in the follwing answer: https://stackoverflow.com/a/30502737/12780418.
For your query the easiest way is to use |, i.e.
SELECT ?taxon WHERE {
?taxon wdt:P171|wdt:P171/wdt:P171 wd:Q7377.
}
Related
I'd like to be able to implement a search method that can take any arbitrary properties of my POCO class as arguments. This works well:
public static IEnumerable<iUser> Search(DataContext context, Func<iUser, bool> predicate)
{
return from i in context.GetTable<iUser>().Where(predicate) select i;
}
but in this case the filtering appears to take place after collecting all the rows in the table.
Is it possible to use Linq to generate an arbitrary query like this without filtering after the sql call? What approaches would you recommend?
Thanks!
LINQ to DB is an Object-Relational Mapper (ORM) that is capable of translating LINQ expressions into SQL. The word "expression" is important here. A Func is not an expression but a delegate, you have to use Expression<Func<>> in LINQ methods for LINQ to DB to be able to translate them. Otherwise the data will be pulled from the database first after which the Func filters them in memory.
So your function should look like:
public static IEnumerable<iUser> Search(DataContext context,
Expression<Func<iUser, bool>> predicate)
{
return context.GetTable<iUser>().Where(predicate);
}
The return type depends on the what you want the caller of this function to be capable of. If you return IQueryable<iUser> the caller will be able to extend the expression by their own expressions. That is, Search(context, somePredicate).Where(...) will be translated into SQL as a whole. Returning IEnumerable will apply any subsequent predicates (either as Func or as Expression) in memory.
Side note, in order to line up with common naming conventions, if iUser is an interface (I have no idea if LINQ to DB supports interfaces) then you should rename it into IUser, otherwise name it User.
I have A, B and C as classes related by the transitive property isSubClassOf.
So A isSuclassOF B and B isSubClassOf C. So by inference we have A isSubClassOf C.
My question: How can I write a SPARQL query to just return back for each Class its direct only subclass number. for example
A 0
B 1
C 1
Within the standard SPARQL language, you can do this by querying for those subclasses where no other subclass exists "in between", like so:
SELECT ?directSub ?super
WHERE { ?directSub rdfs:subClassOf ?super .
FILTER NOT EXISTS { ?otherSub rdfs:subClassOf ?super.
?directSub rdfs:subClassOf ?otherSub .
FILTER (?otherSub != ?directSub)
}
}
If you want to count the number of subclasses, you will need to adapt the above query using the COUNT and GROUP BY operators.
Many SPARQL engines offer some shortcuts for querying direct subclasses, however. For example in Sesame, when querying programmatically, you can disable inferencing for the duration of the query by setting a boolean property on the Query object to false. It also offers an additional reasoner which can be configured on top of a datastore and which allows you to query using a "virtual" property, sesame:directSubClassOf (as well as sesame:directType and sesame:directSubPropertyOf).
Other SPARQL engines have similar mechanisms.
How can I accomplish operator overloading in a way that is convenient to use?
As you can see, putting an operator overload in a class does nothing. Also, if I use "abstract", I can't even call the Bark() method on Dog.
Operator overloading shouldn't be so convoluted and unviable.
Operator overloading is only for abstracts at the moment. What you can do is to create and to apply a macro to your context (where your operations are executed) and transform the expression tree so that the operations are mapped to the right methods.
In haxe 3.1.3, you can add #:forward before the abstract to forward underlying attributes and methods to the abstract.
#:forward // add this!
abstract Dog2(Dog) to Dog from Dog
{
...
}
new Dog2().Bark(); // no more error!
You can also forward specific methods/attributes to the abstract. See Forwarding abstract fields
I am using Lucene 3.6.1. I have a BooleanQuery some clauses of which are marked as Occur.MUST_NOT. When I extract terms from this query, it happily extracts the terms that must not occur as well. This is so because of the following code in BooleanQuery.java
#Override
public void extractTerms(Set<Term> terms) {
for (BooleanClause clause : clauses) {
clause.getQuery().extractTerms(terms);
}
}
I am using these terms to present the user with a set of terms that can be added or removed from the query. If the user has explicitly specified that some term or phrase is not desired (e..g, by adding -"foo bar" to a query), I don't want to show these terms to him. What might make more sense is code like this:
#Override
public void extractTerms(Set<Term> terms) {
for (BooleanClause clause : clauses) {
if (!clause.isProhibited())
clause.getQuery().extractTerms(terms);
}
}
What is the design rationale for the existing implementation? When does it make sense? What's the best way to get around this problem, assuming I don't want negated terms, but don't know where in the query tree they occur?
Gene: maybe you can open a LUCENE Jira ticket for this?
I actually think extractTerms should do as you suggest. For example if i make a simple highlighter that uses this method (which I've done before), I don't want the negative portions either. I'm guessing in general this is the expected behavior for most uses of this method.
At the very least its currently inconsistent, e.g. SpanNotQuery is in the same boat and excludes its "negative" portions from extractTerms.
I wonder what prefix you guys use for methods that create or calculate a value based on given parameters. I tend to use "get", but that feels wrong, because it looks like a getter then.
For methods that fetch from the database I use "fetch", but for methods that create a value based on the given input I haven't found a satisfying prefix yet. ("create" feels a bit too generic). Are there guidelines for this or is everyone just thinking up something for themselves?
Pseudo code example:
class myClass
{
method getOrderFlowpoint(par1, par2, par3) {
// do stuff based on the parameters
return orderFlowpoint;
}
}
I tend to not use prefixes. I tend to name the method according to the exact function it fulfills.
If your function calculates the order flowpoint, that's exactly how you should name it. calculateOrderFlowpoint.
If your class is only about OrderFlowPoints, I have seen the following, all linked to the Factory Pattern:
OrderFlowPoint.of(par1, par2, par3);
OrderFlowPoint.valueOf(par1, par2, par3);
OrderFlowPoint.newInstance(par1, par2, par3);