Search in sources :

Example 1 with QuerySolution

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

the class SqlTablesFiller method getInfo.

public TableEntry getInfo(ResultSetRewindable r) throws ClassNotFoundException, SQLException, IOException, ParseException {
    QuerySolution solution = r.nextSolution();
    RDFNode sName = solution.get("sname");
    String spouseName = sName == null || !sName.isLiteral() ? "No Spouse" : sName.asLiteral().getString() + "";
    RDFNode bPlace = solution.get("birth");
    String birthPlace = "No Birth Place";
    if (bPlace != null)
        if (bPlace.isResource())
            birthPlace = (bPlace.asResource() + "").split("resource/")[1];
        else if (bPlace.isLiteral())
            birthPlace = (bPlace.asLiteral() + "").split("@")[0];
    RDFNode dPlace = solution.get("death");
    String deathPlace = "No Death Place";
    if (dPlace != null)
        if (dPlace.isResource())
            deathPlace = (dPlace.asResource() + "").split("resource/")[1];
        else if (dPlace.isLiteral())
            deathPlace = (dPlace.asLiteral() + "").split("@")[0];
    RDFNode occupation = solution.get("occup");
    String occup = "No Occupation";
    if (occupation != null)
        if (occupation.isResource())
            occup = !(occupation.asResource() + "").contains("resource") ? "No Occupation" : (occupation.asResource() + "").split("resource/")[1];
        else if (occupation.isLiteral())
            occup = (occupation.asLiteral() + "").split("@")[0];
    RDFNode spOcuup = solution.get("spOccu");
    String spouseOccupation = "No Spouse Occupation";
    if (spOcuup != null)
        if (spOcuup.isResource())
            spouseOccupation = !(spOcuup.asResource() + "").contains("resource") ? "No Spouse Occupation" : (spOcuup.asResource() + "").split("resource/")[1];
        else if (dPlace.isLiteral())
            spouseOccupation = (spOcuup.asLiteral() + "").split("@")[0];
    RDFNode bDate = solution.get("bDate");
    String birthDate = bDate == null ? "" : !bDate.isLiteral() ? null : bDate.asLiteral().getValue() + "";
    RDFNode dDate = solution.get("dDate");
    String deathDate = null;
    java.sql.Date sqlDeathDate = null;
    if (dDate == null)
        sqlDeathDate = null;
    else {
        deathDate = !dDate.isLiteral() ? null : dDate.asLiteral().getValue() + "";
        if (deathDate.contains(".") || deathDate.contains("c."))
            sqlDeathDate = null;
        else if (deathDate.split("-").length == 1 && deathDate.matches("[0-9]+") && deathDate.length() <= 4)
            sqlDeathDate = stringToSqlDate(deathDate, new SimpleDateFormat("yyyy"));
        else if (deathDate.matches("[0-9][0-9][0-9][0-9][-][0-9][0-9][-][0-9][0-9]"))
            sqlDeathDate = stringToSqlDate(deathDate, new SimpleDateFormat("yyyy-MM-dd"));
        else if (deathDate.matches("--[0-9][0-9][-][0-9][0-9]"))
            sqlDeathDate = stringToSqlDate(deathDate.substring(2, deathDate.length()), new SimpleDateFormat("MM-dd"));
        int monthNum = 1;
        for (String month : months) {
            if (deathDate.equals(month)) {
                sqlDeathDate = stringToSqlDate(monthNum + "", new SimpleDateFormat("MM"));
                break;
            }
            if (deathDate.startsWith(month)) {
                String parseDeathDate = deathDate.split(" ")[1] + "-" + monthNum;
                sqlDeathDate = stringToSqlDate(parseDeathDate, new SimpleDateFormat("yyyy-MM"));
                break;
            }
            ++monthNum;
        }
    }
    java.sql.Date sqlBirthDate = null;
    if (birthDate.contains(".") || birthDate.contains("c."))
        sqlBirthDate = null;
    else if (birthDate.split("-").length == 1 && birthDate.matches("[0-9]+") && birthDate.length() <= 4)
        sqlBirthDate = stringToSqlDate(birthDate, new SimpleDateFormat("yyyy"));
    else if (birthDate.matches("--[0-9][0-9][-][0-9][0-9]"))
        sqlDeathDate = stringToSqlDate(birthDate.substring(2, birthDate.length()), new SimpleDateFormat("MM-dd"));
    else if (birthDate.matches("[0-9][0-9][0-9][0-9][-][0-9][0-9][-][0-9][0-9]"))
        sqlBirthDate = stringToSqlDate(birthDate, new SimpleDateFormat("yyyy-MM-dd"));
    int monthNum = 1;
    for (String month : months) {
        if (birthDate.equals(month)) {
            sqlBirthDate = stringToSqlDate(monthNum + "", new SimpleDateFormat("MM"));
            break;
        }
        if (birthDate.startsWith(month)) {
            String parseBirthDate = birthDate.split(" ")[1] + "-" + monthNum;
            sqlBirthDate = stringToSqlDate(parseBirthDate, new SimpleDateFormat("yyyy-MM"));
            break;
        }
        ++monthNum;
    }
    return new TableEntry("", "", birthPlace, deathPlace, sqlBirthDate, sqlDeathDate, occup, spouseName, spouseOccupation, (solution.get("photo") == null ? "No Photo" : solution.get("photo") + ""), "");
}
Also used : TableEntry(infoeval.main.mysql.TableEntry) QuerySolution(org.apache.jena.query.QuerySolution) SimpleDateFormat(java.text.SimpleDateFormat) RDFNode(org.apache.jena.rdf.model.RDFNode) Date(java.sql.Date)

Example 2 with QuerySolution

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

the class SqlTablesFiller method fillBasicInfoTable.

public void fillBasicInfoTable(ResultSetRewindable r) throws ClassNotFoundException, SQLException, IOException, ParseException {
    QuerySolution solution = r.nextSolution();
    String name = solution.getLiteral("name").getString();
    RDFNode sName = solution.get("sname");
    String spouseName = sName == null || !sName.isLiteral() ? "No Spouse" : sName.asLiteral().getString() + "";
    RDFNode bPlace = solution.get("birth");
    String birthPlace = "No Birth Place";
    if (bPlace != null)
        if (bPlace.isResource())
            birthPlace = (bPlace.asResource() + "").split("resource/")[1];
        else if (bPlace.isLiteral())
            birthPlace = (bPlace.asLiteral() + "").split("@")[0];
    RDFNode dPlace = solution.get("death");
    String deathPlace = "No Death Place";
    if (dPlace != null)
        if (dPlace.isResource())
            deathPlace = (dPlace.asResource() + "").split("resource/")[1];
        else if (dPlace.isLiteral())
            deathPlace = (dPlace.asLiteral() + "").split("@")[0];
    RDFNode occupation = solution.get("occup");
    String occup = "No Occupation";
    if (occupation != null)
        if (occupation.isResource())
            occup = !(occupation.asResource() + "").contains("resource") ? "No Occupation" : (occupation.asResource() + "").split("resource/")[1];
        else if (occupation.isLiteral())
            occup = (occupation.asLiteral() + "").split("@")[0];
    RDFNode spOcuup = solution.get("spOccu");
    String spouseOccupation = "No Spouse Occupation";
    if (spOcuup != null)
        if (spOcuup.isResource())
            spouseOccupation = !(spOcuup.asResource() + "").contains("resource") ? "No Spouse Occupation" : (spOcuup.asResource() + "").split("resource/")[1];
        else if (dPlace.isLiteral())
            spouseOccupation = (spOcuup.asLiteral() + "").split("@")[0];
    RDFNode bDate = solution.get("bDate");
    String birthDate = !bDate.isLiteral() ? null : bDate.asLiteral().getValue() + "";
    RDFNode dDate = solution.get("dDate");
    String deathDate = null;
    java.sql.Date sqlDeathDate = null;
    if (dDate == null)
        sqlDeathDate = null;
    else {
        deathDate = !dDate.isLiteral() ? null : dDate.asLiteral().getValue() + "";
        if (deathDate.contains(".") || deathDate.contains("c."))
            sqlDeathDate = null;
        else if (deathDate.split("-").length == 1 && deathDate.matches("[0-9]+") && deathDate.length() <= 4)
            sqlDeathDate = stringToSqlDate(deathDate, new SimpleDateFormat("yyyy"));
        else if (deathDate.matches("[0-9][0-9][0-9][0-9][-][0-9][0-9][-][0-9][0-9]"))
            sqlDeathDate = stringToSqlDate(deathDate, new SimpleDateFormat("yyyy-MM-dd"));
        else if (deathDate.matches("--[0-9][0-9][-][0-9][0-9]"))
            sqlDeathDate = stringToSqlDate(deathDate.substring(2, deathDate.length()), new SimpleDateFormat("MM-dd"));
        int monthNum = 1;
        for (String month : months) {
            if (deathDate.equals(month)) {
                sqlDeathDate = stringToSqlDate(monthNum + "", new SimpleDateFormat("MM"));
                break;
            }
            if (deathDate.startsWith(month)) {
                String parseDeathDate = deathDate.split(" ")[1] + "-" + monthNum;
                sqlDeathDate = stringToSqlDate(parseDeathDate, new SimpleDateFormat("yyyy-MM"));
                break;
            }
            ++monthNum;
        }
    }
    String photoLink = solution.get("photo") == null ? "No Photo" : solution.get("photo") + "";
    java.sql.Date sqlBirthDate = null;
    if (birthDate.contains(".") || birthDate.contains("c."))
        sqlBirthDate = null;
    else if (birthDate.split("-").length == 1 && birthDate.matches("[0-9]+") && birthDate.length() <= 4)
        sqlBirthDate = stringToSqlDate(birthDate, new SimpleDateFormat("yyyy"));
    else if (birthDate.matches("--[0-9][0-9][-][0-9][0-9]"))
        sqlDeathDate = stringToSqlDate(birthDate.substring(2, birthDate.length()), new SimpleDateFormat("MM-dd"));
    else if (birthDate.matches("[0-9][0-9][0-9][0-9][-][0-9][0-9][-][0-9][0-9]"))
        sqlBirthDate = stringToSqlDate(birthDate, new SimpleDateFormat("yyyy-MM-dd"));
    int monthNum = 1;
    for (String month : months) {
        if (birthDate.equals(month)) {
            sqlBirthDate = stringToSqlDate(monthNum + "", new SimpleDateFormat("MM"));
            break;
        }
        if (birthDate.startsWith(month)) {
            String parseBirthDate = birthDate.split(" ")[1] + "-" + monthNum;
            sqlBirthDate = stringToSqlDate(parseBirthDate, new SimpleDateFormat("yyyy-MM"));
            break;
        }
        ++monthNum;
    }
    Object[] inp = new Object[9];
    inp[0] = name;
    inp[1] = birthPlace;
    inp[2] = deathPlace;
    inp[3] = sqlBirthDate;
    inp[4] = sqlDeathDate;
    inp[5] = occup;
    inp[6] = spouseName;
    inp[7] = spouseOccupation;
    inp[8] = photoLink;
    // System.out.println(photoLink);
    connector.runUpdate("INSERT INTO basic_info VALUES(?,?,?,?,?,?,?,?,?)", inp);
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) SimpleDateFormat(java.text.SimpleDateFormat) RDFNode(org.apache.jena.rdf.model.RDFNode) Date(java.sql.Date)

Example 3 with QuerySolution

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

the class ExtractorTest method abstractTest.

@Ignore
@Test
public void abstractTest() {
    String name = "Jessica Zelinka", newName = WordUtils.capitalize(name).replaceAll(" ", "_");
    Extractor extr = new Extractor(newName);
    extr.executeQuery(QueryTypes.ABSTRACT);
    ResultSetRewindable results = extr.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];
    assertEquals(overviewStr, "Jessica Zelinka (born September 3, 1981 in London, Ontario) is a Canadian pentathlete, heptathlete, and 100 m hurdler. Her personal best score is 6599 points for the heptathlon. She was the gold medalist at the 2007 Pan American Games. Zelinka won silver at the 2010 Commonwealth Games and repeated her silver medal at the 2014 Commonwealth Games. At the 2012 Summer Olympics Zelinka finished in 7th overall in both the heptathlon and 100 m hurdles.");
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Extractor(infoeval.main.WikiData.Extractor) ResultSetRewindable(org.apache.jena.query.ResultSetRewindable) RDFNode(org.apache.jena.rdf.model.RDFNode) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 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 5 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)

Aggregations

QuerySolution (org.apache.jena.query.QuerySolution)106 ResultSet (org.apache.jena.query.ResultSet)85 QueryExecution (org.apache.jena.query.QueryExecution)78 ArrayList (java.util.ArrayList)55 Test (org.junit.Test)51 Dataset (org.apache.jena.query.Dataset)45 Resource (org.apache.jena.rdf.model.Resource)43 Query (org.apache.jena.query.Query)26 Literal (org.apache.jena.rdf.model.Literal)21 RDFNode (org.apache.jena.rdf.model.RDFNode)20 Model (org.apache.jena.rdf.model.Model)12 HashMap (java.util.HashMap)7 ResultSetRewindable (org.apache.jena.query.ResultSetRewindable)6 Extractor (infoeval.main.WikiData.Extractor)4 Node (org.apache.jena.graph.Node)4 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)4 IOException (java.io.IOException)3 Date (java.sql.Date)3 SimpleDateFormat (java.text.SimpleDateFormat)3 LEGATO (legato.LEGATO)3