86613

SPARQL query RDF ontology

Question:

Im trying this sparql query, but Im having trouble to find out whats wrong. I would like to take all the receipes that has those ingredients or less. ie: receipe 1: Tomato receipe 2: Tomato and salt receipe 3: Tomato, salt, onion.

And I have the following query:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX rec:<http://www.receta.org#> SELECT reduced ?r (count (?i) as ?cuantos) WHERE { ?x rdf:type rec:Receta . ?x rdfs:label ?r. filter not exists { ?x rec:Ingrediente ?i filter( ?i not in (rec:Tomato, rec:Salt) ) } } GROUP BY ?r

I would to show only the first one. Not the second one and not the last one.

Its just an example but I actually want to do it with more that 3 receipes, lets say a huge database, I want to take the receipes that have less ingredients than tomato and salt for example. So any ideas of how can I fix my query to do so?

Im going to try to explain myself:

For example:

Lets say I have a huge database with hundreds recipes. Some of those are: Recipe1: tomato and salt Receipe2: Onion and meat Receipe3: Tomato and vinegar Receipe4: chocolate and milk

And in my query I say that I want the recipes with tomato, salt and vinegar. So in that case I would like to get the Recipe1 and the Recipe3, because the recipe1 has tomato and salt and the recipe3 has tomato and vinegar, one ingredient less than what I say. Lets say now that I add more recipes:

Recipe4: Tomato Recipe5: Salt Recipe6: Tomato, vinegar, salt and onion Recipe6: Tomato, vinegar, salt

If I execute my query again I would like to have the recipe1, recipe3, recipe4 and recipe5. Cause those recipes are those that has those ingredients or less. I dont want to have the recipe6 cause has more ingredients than what I asked and not even the recipe7.

Thank you!!!

Answer1:

If I understand you correctly, you're trying to specify a set of ingredients, and then select the recipes whose ingredients are a property subset of that set of ingredients (or maybe those whose ingredients are just a subset; the examples in your question don't make this clear). At any rate, rather than writing examples out in natural language where it's easy to make mistakes, it's better to provide data we can work with, and in this case it's not hard at all to do that. For instance, here's a small Turtle file with nine recipes, each of which has some ingredients:

<pre class="lang-none prettyprint-override">@prefix : <urn:ex:> . :recipe1 :ingredient :tomato, :salt . :recipe2 :ingredient :onion, :meat . :recipe3 :ingredient :tomato, :vinegar . :recipe4 :ingredient :chocolate, :milk . :recipe5 :ingredient :tomato . :recipe6 :ingredient :salt . :recipe7 :ingredient :tomato, :vinegar, :salt, :onion . :recipe8 :ingredient :tomato, :vinegar, :salt . :recipe9 :ingredient :vinegar, :salt .

Now, we can write a query that retrieves recipes all of whose ingredients are in a specified set, and keeps only those that have less than (or less than or equal to) some specific number of distinct ingredients. (You don't have to include the <strong>group_concat</strong> stuff; I just did that to get an ingredient list for each recipe.)

<pre class="lang-sql prettyprint-override">prefix : <urn:ex:> select ?recipe (group_concat(?ingredient;separator=', ') as ?ingredients) { ?recipe :ingredient ?ingredient filter not exists { ?recipe :ingredient ?other_ingredient filter( ?other_ingredient not in ( :salt, :tomato, :vinegar ) ) } } group by ?recipe having (count(distinct ?ingredient) < 3) <pre class="lang-none prettyprint-override">---------------------------------------------- | recipe | ingredients | ============================================== | :recipe9 | "urn:ex:salt, urn:ex:vinegar" | | :recipe5 | "urn:ex:tomato" | | :recipe6 | "urn:ex:salt" | | :recipe3 | "urn:ex:tomato, urn:ex:vinegar" | | :recipe1 | "urn:ex:tomato, urn:ex:salt" | ----------------------------------------------

If you wanted to allow recipes with all three ingredients, you could just change the line

<pre class="lang-sql prettyprint-override">having (count(distinct ?ingredient) < 3)

to either of:

<pre class="lang-sql prettyprint-override">having (count(distinct ?ingredient) <= 3)

<pre class="lang-sql prettyprint-override">having (count(distinct ?ingredient) < 4)

Recommend

  • When I instantiate a class in PHP, do I get a pointer to the object?
  • Can https fallback to http and security level of https
  • Ruby on Rails filtering attributes on returning model object
  • XCode, Authentication and NSUserDefaults
  • How to get intermediate colors from one to another? [duplicate]
  • Why doesn't >= (greater than or equals) comparison work in Javascript?
  • MySQL: “Unknown column in where clause” during Update Statement
  • Securely storing (encrypting) data in an ASP.Net application
  • Easier way to do non-recursive list on gcs bucket outside appengine
  • Extracting individual digits from a float
  • rewrite uppercase url to lowercase url htaccess
  • Access user's phone number on iOS 7
  • For loop with if condition on multiple R functions
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • Abort upload large uploads after reading headers
  • Getting last autonumber in access
  • Read text file and split every line in MSBuild
  • JavaScriptCore crash on iOS9
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • How to extract text from Word files using C#?
  • javascript inside java/jsp code
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • Importing jscolor library in angular 2
  • QuartzCore.framework for Mono Develop
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Arrays break string types in Julia
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Data Validation Drop Down Box Arrow Disappearing
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • embed rChart in Markdown
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Binding checkboxes to object values in AngularJs