Package model.query

Source Code of model.query.OntologyQueryer

package model.query;

import model.array.ActorArray;
import model.array.MovieArray;
import model.movie.Actor;
import model.movie.Movie;
import model.ontology.OntologyReader;
import model.query.interfaces.QueryInterface;

import com.clarkparsia.pellet.sparqldl.jena.SparqlDLExecutionFactory;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QueryParseException;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;

public class OntologyQueryer implements QueryInterface {
  public static Model describeQuery(String uri) {
    Model model = OntologyReader.read();

    Query query = QueryFactory.create("DESCRIBE <" + uri + ">");
    QueryExecution execute = SparqlDLExecutionFactory.create(query, model);

    Model result = execute.execDescribe();

    return result;
  }

  public static ResultSet resultQuery(String queryText) {
    Model model = OntologyReader.read();

    try {
      Query query = QueryFactory.create(PREFIXES + queryText);
      QueryExecution execute = SparqlDLExecutionFactory.create(query, model);
      ResultSet result = execute.execSelect();
     
      return result;
    } catch (QueryParseException e) {
      return null;
    }
  }
 
  public static MovieArray getOntologoyMovieList() {
    MovieArray movieList = new MovieArray();
    String queryText =   "SELECT ?uri WHERE { " +
                "?uri rdf:type h2mdb:Film . " +
              "} ";
    ResultSet resultSet = resultQuery(queryText);
   
    if (resultSet != null)
      while (resultSet.hasNext())
        movieList.add(new Movie(resultSet.next().get("uri").toString()));
   
    return movieList;
  }
 
  public static ActorArray getOntologoyActorList() {
    ActorArray actorList = new ActorArray();
    MovieArray movieList = getOntologoyMovieList();
    for (Movie m : movieList) {
      String queryText =   "SELECT ?actor WHERE { " +
                  "<" + m.getUri() + "> dbpedia-owl:starring ?actor  . " +
                "} ";
      ResultSet resultSet = resultQuery(queryText);
      if (resultSet != null)
        while (resultSet.hasNext())
          actorList.add(new Actor(resultSet.next().get("actor").toString()));
    }
   
    return actorList;
  }
}
TOP

Related Classes of model.query.OntologyQueryer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.