Search in sources :

Example 6 with QuerySolution

use of org.apache.jena.query.QuerySolution in project jena by apache.

the class TextOutput method write.

/** Textual representation : layout using given separator.
     *  Ensure the PrintWriter can handle UTF-8.
     *  @param pw         PrintWriter
     *  @param colSep      Column separator
     */
public void write(PrintWriter pw, ResultSet resultSet, String colStart, String colSep, String colEnd) {
    if (resultSet.getResultVars().size() == 0) {
        pw.println("==== No variables ====");
    // return ;
    }
    ResultSetRewindable resultSetRewindable = ResultSetFactory.makeRewindable(resultSet);
    int numCols = resultSetRewindable.getResultVars().size();
    int[] colWidths = colWidths(resultSetRewindable);
    String[] row = new String[numCols];
    int lineWidth = 0;
    for (int col = 0; col < numCols; col++) {
        String rVar = resultSet.getResultVars().get(col);
        row[col] = rVar;
        lineWidth += colWidths[col];
        if (col > 0)
            lineWidth += colSep.length();
    }
    if (colStart != null)
        lineWidth += colStart.length();
    if (colEnd != null)
        lineWidth += colEnd.length();
    for (int i = 0; i < lineWidth; i++) pw.print('-');
    pw.println();
    printRow(pw, row, colWidths, colStart, colSep, colEnd);
    for (int i = 0; i < lineWidth; i++) pw.print('=');
    pw.println();
    for (; resultSetRewindable.hasNext(); ) {
        QuerySolution rBind = resultSetRewindable.nextSolution();
        for (int col = 0; col < numCols; col++) {
            String rVar = resultSet.getResultVars().get(col);
            row[col] = this.getVarValueAsString(rBind, rVar);
        }
        printRow(pw, row, colWidths, colStart, colSep, colEnd);
    }
    for (int i = 0; i < lineWidth; i++) pw.print('-');
    pw.println();
    resultSetRewindable = null;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) ResultSetRewindable(org.apache.jena.query.ResultSetRewindable)

Example 7 with QuerySolution

use of org.apache.jena.query.QuerySolution in project jena by apache.

the class ExTDB_Txn1 method execQuery.

public static void execQuery(String sparqlQueryString, Dataset dataset) {
    Query query = QueryFactory.create(sparqlQueryString);
    QueryExecution qexec = QueryExecutionFactory.create(query, dataset);
    try {
        ResultSet results = qexec.execSelect();
        for (; results.hasNext(); ) {
            QuerySolution soln = results.nextSolution();
            int count = soln.getLiteral("count").getInt();
            System.out.println("count = " + count);
        }
    } finally {
        qexec.close();
    }
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 8 with QuerySolution

use of org.apache.jena.query.QuerySolution in project Info-Evaluation by TechnionYP5777.

the class SqlRunner method getPersonalInfo.

@SuppressWarnings("unchecked")
public TableEntry getPersonalInfo(String name) throws ClassNotFoundException, SQLException, IOException, ParseException {
    Object[] inp = new Object[] { name };
    ArrayList<Row> res = conn.runQuery("SELECT name FROM basic_info WHERE name = ?", inp);
    String newName = WordUtils.capitalize(name).replaceAll(" ", "_");
    if (res.isEmpty()) {
        return getPersonalInfoFromDBpedia(newName);
    }
    ArrayList<Row> id_result = conn.runQuery("SELECT serialized_id " + "FROM serialized_query_results " + "WHERE query_identifier LIKE CONCAT('getPersonalInfo','(',?,')')", inp);
    int serialized_id = -1;
    ArrayList<Row> rows = new ArrayList<>();
    String overviewStr = "";
    if (id_result.isEmpty()) {
        Extractor ext = new Extractor(newName);
        logger.log(Level.INFO, "abstract extraction query is being executed");
        ext.executeQuery(QueryTypes.ABSTRACT);
        ResultSetRewindable results = ext.getResults();
        results.reset();
        QuerySolution solution = results.nextSolution();
        RDFNode overview = solution.get("abstract");
        overviewStr = "No Abstract";
        if (overview != null)
            if (overview.isResource())
                overviewStr = (overview.asResource() + "").split("resource/")[1];
            else if (overview.isLiteral())
                overviewStr = (overview.asLiteral() + "").split("@")[0];
        final String personalInfoQuery = "SELECT SQL_CACHE filtered_info.*, WikiID.wikiPageId " + "FROM (SELECT * FROM basic_info WHERE name = ?) AS filtered_info " + "LEFT JOIN WikiID " + "ON WikiID.name = filtered_info.name " + "LIMIT 1";
        logger.log(Level.INFO, "personal info query is being executed");
        rows = conn.runQuery(personalInfoQuery, inp);
        Object[] toSerilaize = new Object[2];
        toSerilaize[0] = rows;
        toSerilaize[1] = overviewStr;
        String query_identifier = "getPersonalInfo(" + name + ")";
        serialized_id = resultsSer.serializeQueryResults(conn, query_identifier, toSerilaize);
    } else {
        serialized_id = (int) id_result.get(0).row.get(0).getKey();
        Object[] output = (Object[]) resultsSer.deSerializeQueryResults(conn, serialized_id);
        rows = (ArrayList<Row>) output[0];
        overviewStr = (String) output[1];
    }
    Row res_row = rows.get(0);
    String birthPlace = (String) res_row.row.get(1).getValue().cast(res_row.row.get(1).getKey());
    String deathPlace = (String) res_row.row.get(2).getValue().cast(res_row.row.get(2).getKey());
    Date birthDate = null;
    if (!"".equals(res_row.row.get(3).getKey())) {
        birthDate = (java.sql.Date) res_row.row.get(3).getValue().cast(res_row.row.get(3).getKey());
    }
    Date deathDate = null;
    if (!"".equals(res_row.row.get(4).getKey())) {
        deathDate = (java.sql.Date) res_row.row.get(4).getValue().cast(res_row.row.get(4).getKey());
    }
    String occupation = (String) res_row.row.get(5).getValue().cast(res_row.row.get(5).getKey());
    String spouseName = (String) res_row.row.get(6).getValue().cast(res_row.row.get(6).getKey());
    String spouseOccupation = (String) res_row.row.get(7).getValue().cast(res_row.row.get(7).getKey());
    String photoLink = (String) res_row.row.get(8).getValue().cast(res_row.row.get(8).getKey());
    photoLink.replaceAll("'", "\'");
    String wikiPageID = (String) res_row.row.get(9).getValue().cast(res_row.row.get(9).getKey());
    TableEntry te = new TableEntry(wikiURL + wikiPageID, name, birthPlace, deathPlace, birthDate, deathDate, occupation, spouseName, spouseOccupation, photoLink, overviewStr);
    return te;
}
Also used : ArrayList(java.util.ArrayList) Date(java.sql.Date) QuerySolution(org.apache.jena.query.QuerySolution) Extractor(infoeval.main.WikiData.Extractor) ResultSetRewindable(org.apache.jena.query.ResultSetRewindable) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 9 with QuerySolution

use of org.apache.jena.query.QuerySolution in project Info-Evaluation by TechnionYP5777.

the class SqlRunner method getPersonalInfoFromDBpedia.

public TableEntry getPersonalInfoFromDBpedia(String name) throws ClassNotFoundException, SQLException, IOException, ParseException {
    // Object[] inp = new Object[] { name };
    // ArrayList<Row> id_result = conn.runQuery("SELECT serialized_id " +
    // "FROM serialized_query_results "
    // + "WHERE query_identifier LIKE CONCAT('getPersonalInfo','(',?,')')",
    // inp);
    // int serialized_id = -1;
    TableEntry result;
    // if (id_result.isEmpty()) {
    Extractor ext = new Extractor(name);
    logger.log(Level.INFO, "abstract extraction query is being executed");
    ext.executeQuery(QueryTypes.ABSTRACT);
    ResultSetRewindable results = ext.getResults();
    results.reset();
    QuerySolution solution = results.nextSolution();
    RDFNode overview = solution.get("abstract");
    String overviewStr = "No Abstract";
    if (overview != null)
        if (overview.isResource())
            overviewStr = (overview.asResource() + "").split("resource/")[1];
        else if (overview.isLiteral())
            overviewStr = (overview.asLiteral() + "").split("@")[0];
    logger.log(Level.INFO, "basic Info By Name extraction query is being executed");
    ext.executeQuery(QueryTypes.BASIC_INFO_BY_NAME);
    ResultSetRewindable basicInfoByNameResults = ext.getResults();
    basicInfoByNameResults.reset();
    SqlTablesFiller filler = new SqlTablesFiller();
    TableEntry te = filler.getInfo(basicInfoByNameResults);
    filler.close();
    result = new TableEntry(te);
    String newName = name.replaceAll("_", " ");
    result.setName(newName);
    result.setUrl("");
    result.setOverview(overviewStr);
    String photoLink = result.getPhotoLink();
    photoLink.replaceAll("'", "\'");
    result.setPhotoLink(photoLink);
    Object[] toSerilaize = new Object[1];
    toSerilaize[0] = result;
    /*
		 * String query_identifier = "getPersonalInfo(" + name + ")";
		 * resultsSer.serializeQueryResults(conn, query_identifier,
		 * toSerilaize); } else { serialized_id = (int)
		 * id_result.get(0).row.get(0).getKey(); Object[] output = (Object[])
		 * resultsSer.deSerializeQueryResults(conn, serialized_id); result =
		 * (TableEntry) output[0]; }
		 */
    return result;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Extractor(infoeval.main.WikiData.Extractor) ResultSetRewindable(org.apache.jena.query.ResultSetRewindable) SqlTablesFiller(infoeval.main.WikiData.SqlTablesFiller) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 10 with QuerySolution

use of org.apache.jena.query.QuerySolution in project Info-Evaluation by TechnionYP5777.

the class SqlTablesFiller method fillWikiIdTable.

public void fillWikiIdTable() throws SQLException, ClassNotFoundException, IOException {
    connector.clearWikiIdTable();
    Extractor ext = new Extractor();
    ext.executeQuery(QueryTypes.WIKI_ID);
    ResultSetRewindable results = ext.getResults();
    results.reset();
    for (int i = 0; i < results.size(); ++i) {
        QuerySolution solution = results.nextSolution();
        Object[] inp = new Object[2];
        inp[0] = solution.getLiteral("name").getString();
        inp[1] = solution.getLiteral("wikiPageID").getString();
        connector.runUpdate("INSERT INTO WikiID VALUES(?,?)", inp);
    }
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) ResultSetRewindable(org.apache.jena.query.ResultSetRewindable)

Aggregations

QuerySolution (org.apache.jena.query.QuerySolution)23 ResultSet (org.apache.jena.query.ResultSet)11 RDFNode (org.apache.jena.rdf.model.RDFNode)9 QueryExecution (org.apache.jena.query.QueryExecution)8 ArrayList (java.util.ArrayList)5 Query (org.apache.jena.query.Query)5 ResultSetRewindable (org.apache.jena.query.ResultSetRewindable)5 Model (org.apache.jena.rdf.model.Model)5 Resource (org.apache.jena.rdf.model.Resource)5 Literal (org.apache.jena.rdf.model.Literal)4 Test (org.junit.Test)4 Extractor (infoeval.main.WikiData.Extractor)3 Date (java.sql.Date)3 AbstractRegexpBasedTest (org.apache.jena.arq.AbstractRegexpBasedTest)3 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 SimpleDateFormat (java.text.SimpleDateFormat)2 SqlTablesFiller (infoeval.main.WikiData.SqlTablesFiller)1 TableEntry (infoeval.main.mysql.TableEntry)1 HashMap (java.util.HashMap)1