Search in sources :

Example 16 with ResultSetRewindable

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

the class TestResultSet method test_RS_5.

// JSON
@Test
public void test_RS_5() {
    ResultSetRewindable rs1 = new ResultSetMem();
    ByteArrayOutputStream arr = new ByteArrayOutputStream();
    ResultSetFormatter.outputAsJSON(arr, rs1);
    rs1.reset();
    ByteArrayInputStream ins = new ByteArrayInputStream(arr.toByteArray());
    ResultSet rs2 = ResultSetFactory.fromJSON(ins);
    assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BuilderResultSet(org.apache.jena.sparql.sse.builders.BuilderResultSet) ResultSet(org.apache.jena.query.ResultSet) ResultSetRewindable(org.apache.jena.query.ResultSetRewindable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BaseTest(org.apache.jena.atlas.junit.BaseTest) Test(org.junit.Test)

Example 17 with ResultSetRewindable

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

the class TestResultSet method test_RS_6.

@Test
public void test_RS_6() {
    ResultSetRewindable rs1 = make2Rewindable("x", org.apache.jena.graph.NodeFactory.createURI("tag:local"));
    ByteArrayOutputStream arr = new ByteArrayOutputStream();
    ResultSetFormatter.outputAsJSON(arr, rs1);
    rs1.reset();
    ByteArrayInputStream ins = new ByteArrayInputStream(arr.toByteArray());
    // Test using the DAWG examples
    ResultSet rs2 = ResultSetFactory.fromJSON(ins);
    assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BuilderResultSet(org.apache.jena.sparql.sse.builders.BuilderResultSet) ResultSet(org.apache.jena.query.ResultSet) ResultSetRewindable(org.apache.jena.query.ResultSetRewindable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BaseTest(org.apache.jena.atlas.junit.BaseTest) Test(org.junit.Test)

Example 18 with ResultSetRewindable

use of org.apache.jena.query.ResultSetRewindable 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 19 with ResultSetRewindable

use of org.apache.jena.query.ResultSetRewindable 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 20 with ResultSetRewindable

use of org.apache.jena.query.ResultSetRewindable 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

ResultSetRewindable (org.apache.jena.query.ResultSetRewindable)32 Test (org.junit.Test)18 BaseTest (org.apache.jena.atlas.junit.BaseTest)17 ResultSet (org.apache.jena.query.ResultSet)11 BuilderResultSet (org.apache.jena.sparql.sse.builders.BuilderResultSet)11 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 QuerySolution (org.apache.jena.query.QuerySolution)5 Extractor (infoeval.main.WikiData.Extractor)3 RDFNode (org.apache.jena.rdf.model.RDFNode)3 Model (org.apache.jena.rdf.model.Model)2 SqlTablesFiller (infoeval.main.WikiData.SqlTablesFiller)1 Date (java.sql.Date)1 ArrayList (java.util.ArrayList)1 Item (org.apache.jena.sparql.sse.Item)1 Ignore (org.junit.Ignore)1