0

I'm trying to get movies in dbpedia that has subjects, directors and actors in common with a given movie. The search is made by a movie's name. Here is my final query:

SELECT (SUM(?quant) as ?final) ?movie {
    {
    SELECT distinct ?quant ?movie
    WHERE {
      SELECT ?movie ?name  (4*count(?movie) as ?quant)
      WHERE {
        ?movie rdf:type <http://dbpedia.org/ontology/Film>;
        foaf:name ?name;
        rdfs:label ?film_title;
        dbo:director ?director.
        {
          SELECT DISTINCT ?director
          WHERE {
            ?movie rdf:type <http://dbpedia.org/ontology/Film>;
            rdfs:label ?film_title;
            foaf:name ?name;
            dbo:director ?director.
            FILTER (lang(?film_title) = 'en')
            FILTER regex(?name, 'Fast and Furious')
          }
        }
        FILTER (lang(?film_title) = 'en')
      }
      GROUP BY ?movie ?name
      ORDER BY DESC(?quant)
    }
  }
  UNION {
    SELECT distinct ?quant ?movie
    WHERE{
      SELECT ?movie ?name (count(?movie) as ?quant)
      WHERE {
        ?movie rdf:type <http://dbpedia.org/ontology/Film>;
        foaf:name ?name;
        rdfs:label ?film_title;
        dct:subject ?subject.
        {
          SELECT DISTINCT ?subject
          WHERE {
            ?movie rdf:type <http://dbpedia.org/ontology/Film>;
            rdfs:label ?film_title;
            foaf:name ?name;
            dct:subject ?subject.
            FILTER (lang(?film_title) = 'en')
            FILTER regex(?name, 'Fast and Furious')
          }
        }
        FILTER (lang(?film_title) = 'en')
      }
      GROUP BY ?movie ?name
      ORDER BY DESC(?quant)
    }
  }
  UNION {
    SELECT distinct ?quant ?movie
    WHERE{
      SELECT ?movie ?name (2*count(?movie) as ?quant)
      WHERE {
        ?movie rdf:type <http://dbpedia.org/ontology/Film>;
        foaf:name ?name;
        rdfs:label ?film_title;
        dbo:starring ?starring.
        {
          SELECT DISTINCT ?starring
          WHERE {
            ?movie rdf:type <http://dbpedia.org/ontology/Film>;
            rdfs:label ?film_title;
            foaf:name ?name;
            dbo:starring ?starring.
            FILTER (lang(?film_title) = 'en')
            FILTER regex(?name, 'Fast and Furious')
          }
        }
        FILTER (lang(?film_title) = 'en')
      }
      GROUP BY ?movie ?name
      ORDER BY DESC(?quant)
    }
  }
}
GROUP BY ?movie
ORDER BY DESC(?final)
LIMIT 11

It works here: https://dbpedia.org/sparql

And don't return any errors when I validate here: http://www.sparql.org/query-validator.html

However, when I try to run it in Jena, it returns nothing. Here is my method:

public List<String> getRankedMovies(final String movieName) {
    List<String> rankedMovies = new ArrayList<>();

    final String header = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
                          "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
                          "PREFIX foaf: <http://xmlns.com/foaf/0.1/> "+
                          "PREFIX dct: <http://purl.org/dc/terms/> "+
                          "PREFIX dbo: <http://dbpedia.org/ontology/> ";
    final String selectSum = "SELECT (SUM(?quant) as ?final) ?movie {";
    final String selectDirectorOccurency = "{ "+
                                           "    SELECT distinct ?quant ?movie "+
                                           "    WHERE {"+
                                           "        SELECT ?movie ?name (4*count(?movie) as ?quant) "+
                                           "        WHERE {"+
                                           "            ?movie rdf:type <http://dbpedia.org/ontology/Film>;"+
                                           "            foaf:name ?name;"+
                                           "            rdfs:label ?film_title;"+
                                           "            dbo:director ?director."+
                                           "            {"+
                                           "                SELECT DISTINCT ?director "+
                                           "                WHERE {"+
                                           "                    ?movie rdf:type <http://dbpedia.org/ontology/Film>;"+
                                           "                    rdfs:label ?film_title;"+
                                           "                    foaf:name ?name;"+
                                           "                    dbo:director ?director."+
                                           "                    FILTER (lang(?film_title) = 'en')"+
                                           "                    FILTER regex(?name, \""+movieName+"\")"+
                                           "                }"+
                                           "            }"+
                                           "            FILTER (lang(?film_title) = 'en')"+
                                           "        }"+
                                           "        GROUP BY ?movie ?name "+
                                           "        ORDER BY DESC(?quant)"+
                                           "    }"+
                                           "}";
    final String union = "UNION {";
    final String selectSubjectOccurency = "SELECT distinct ?quant ?movie "+
                                          "WHERE {"+
                                          "     SELECT ?movie ?name (count(?movie) as ?quant) "+
                                          "     WHERE { "+
                                          "         ?movie rdf:type <http://dbpedia.org/ontology/Film>;"+
                                          "         foaf:name ?name;"+
                                          "         rdfs:label ?film_title;"+
                                          "         dct:subject ?subject."+
                                          "         {"+
                                          "             SELECT DISTINCT ?subject "+
                                          "             WHERE { "+
                                          "                 ?movie rdf:type <http://dbpedia.org/ontology/Film>;"+
                                          "                 rdfs:label ?film_title;"+
                                          "                 foaf:name ?name;"+
                                          "                 dct:subject ?subject."+
                                          "                 FILTER (lang(?film_title) = 'en')"+
                                          "                 FILTER regex(?name, \""+movieName+"\")"+
                                          "             }"+
                                          "         }"+
                                          "         FILTER (lang(?film_title) = 'en')"+
                                          "     }"+
                                          "     GROUP BY ?movie ?name "+
                                          "     ORDER BY DESC(?quant)"+
                                          "}"+
                                          "}";
    final String selectStarOccurency = "SELECT distinct ?quant ?movie "+
                                       "WHERE {"+
                                       "    SELECT ?movie ?name (2*count(?movie) as ?quant) "+
                                       "    WHERE { "+
                                       "        ?movie rdf:type <http://dbpedia.org/ontology/Film>;"+
                                       "        foaf:name ?name;"+
                                       "        rdfs:label ?film_title;"+
                                       "        dbo:starring ?starring."+
                                       "        {"+
                                       "            SELECT DISTINCT ?starring "+
                                       "            WHERE {"+
                                       "                ?movie rdf:type <http://dbpedia.org/ontology/Film>;"+
                                       "                rdfs:label ?film_title;"+
                                       "                foaf:name ?name;"+
                                       "                dbo:starring ?starring."+
                                       "                FILTER (lang(?film_title) = 'en')"+
                                       "                FILTER regex(?name, \""+movieName+"\")"+
                                       "            }"+
                                       "        }"+
                                       "        FILTER (lang(?film_title) = 'en')"+
                                       "    }"+
                                       "    GROUP BY ?movie ?name"+
                                       "    ORDER BY DESC(?quant)"+
                                       "}";
    final String queryFinal = "}"+
                              "}"+
                              "GROUP BY ?movie "+
                              "ORDER BY DESC(?final) "+
                              "LIMIT 11";

    final String finalQuery = header+selectSum+selectDirectorOccurency+union+selectSubjectOccurency+union+selectStarOccurency+queryFinal;

    Query query = QueryFactory.create(finalQuery);
    QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
    ResultSet results = qexec.execSelect();
    System.out.println(finalQuery);
    while(results.hasNext()) {
        final QuerySolution soln = results.nextSolution();
        System.out.println(soln.toString());
    }
    qexec.close();
    return rankedMovies;
}

There is nothing in ResultSet. I'm almost sure the query works, because it returns what I want in Virtuoso. I've made other queries in the same project and they works fine. So... What may be causing the issue? I'm bit new in SPARQL, and I would appreciate any help!

  • Your code works for me with e.g. "Star Wars": `( ?final = 59 ) ( ?movie = ) ( ?final = 47 ) ( ?movie = ) ( ?final = 43 ) ( ?movie = )` – UninformedUser Jun 27 '17 at 03:50
  • That leads me to the questions, does it never work for you? Or only if you search for some special titles? – UninformedUser Jun 27 '17 at 03:51
  • All Jena does is ship the query to the remote end. There is a difference between the HTML form and the SPARQL endpoint, despite them having the same URL. The form adds "?default-graph-uri=" for example. – AndyS Jun 27 '17 at 08:35
  • Thanks for the answer! Never worked. Today I try a simple query, just to get any movie with the given name, and it returns nothing. Both (the simple and the one in the topic) works in Virtuoso. When I try a query without filter, it works in Jena. If works for you, maybe I've missed something when install Jena on Eclipse... – Rafa_Domingos Jun 27 '17 at 16:20

0 Answers0