Search in sources :

Example 1 with RDFNode

use of org.apache.jena.rdf.model.RDFNode 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 RDFNode

use of org.apache.jena.rdf.model.RDFNode 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 RDFNode

use of org.apache.jena.rdf.model.RDFNode 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 RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class CmdDescAssembler method open.

/* This SPARQL query will process arguments 
PREFIX acmd:     <http://jena.hpl.hp.com/2007/sdb#>
PREFIX rdf:      <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX list:    <http://jena.apache.org/ARQ/list#>

SELECT ?name ?value
{ ?x rdf:type acmd:Cmd ;
     acmd:args ?args .

  { ?args list:member [ acmd:name ?name  ; acmd:value  ?value ] }
UNION
  { ?args list:member ?e .
    OPTIONAL { ?e acmd:name ?name }
    FILTER (!bound(?name)) .
    ?e acmd:value ?value .
  }
UNION
  { ?args list:member ?value . FILTER isLiteral(?value) }
}     
     */
@Override
public Object open(Assembler a, Resource root, Mode mode) {
    CmdDesc cd = new CmdDesc();
    String main = GraphUtils.getStringValue(root, AssemblerVocab.pMain);
    if (main == null)
        main = GraphUtils.getStringValue(root, AssemblerVocab.pClassname);
    cd.setCmd(main);
    Resource x = GraphUtils.getResourceValue(root, AssemblerVocab.pArgs);
    if (x != null) {
        for (; !x.equals(RDF.nil); ) {
            RDFNode e = x.getRequiredProperty(RDF.first).getObject();
            // Move to next list item
            x = x.getRequiredProperty(RDF.rest).getResource();
            // Either : a literal or a named pair.
            if (e.isLiteral()) {
                cd.addPosn(((Literal) e).getString());
                continue;
            }
            Resource entry = (Resource) e;
            String name = GraphUtils.getStringValue(entry, AssemblerVocab.pArgName);
            String value = GraphUtils.getStringValue(entry, AssemblerVocab.pArgValue);
            if (value == null)
                throw new CommandAssemblerException(entry, "Strange entry: " + entry);
            if (name != null)
                cd.addNamedArg(name, value);
            else
                cd.addPosn(value);
        }
    }
    return cd;
}
Also used : Resource(org.apache.jena.rdf.model.Resource) CommandAssemblerException(org.apache.jena.sdb.assembler.CommandAssemblerException) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 5 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class FusekiLib method getOne.

public static RDFNode getOne(Resource svc, String property) {
    ResultSet rs = FusekiLib.query("SELECT * { ?svc " + property + " ?x}", svc.getModel(), "svc", svc);
    if (!rs.hasNext())
        throw new FusekiConfigException("No property '" + property + "' for service " + FusekiLib.nodeLabel(svc));
    RDFNode x = rs.next().get("x");
    if (rs.hasNext())
        throw new FusekiConfigException("Multiple properties '" + property + "' for service " + FusekiLib.nodeLabel(svc));
    return x;
}
Also used : RDFNode(org.apache.jena.rdf.model.RDFNode)

Aggregations

RDFNode (org.apache.jena.rdf.model.RDFNode)145 Resource (org.apache.jena.rdf.model.Resource)51 Model (org.apache.jena.rdf.model.Model)30 Statement (org.apache.jena.rdf.model.Statement)27 Test (org.junit.Test)26 ArrayList (java.util.ArrayList)22 Node (org.apache.jena.graph.Node)20 QuerySolution (org.apache.jena.query.QuerySolution)20 TextIndexException (org.apache.jena.query.text.TextIndexException)17 Property (org.apache.jena.rdf.model.Property)17 Literal (org.apache.jena.rdf.model.Literal)15 SecuredRDFNode (org.apache.jena.permissions.model.SecuredRDFNode)13 Dataset (org.apache.jena.query.Dataset)13 Triple (org.apache.jena.graph.Triple)12 QueryExecution (org.apache.jena.query.QueryExecution)10 StmtIterator (org.apache.jena.rdf.model.StmtIterator)9 Query (org.apache.jena.query.Query)8 NodeIterator (org.apache.jena.rdf.model.NodeIterator)8 RDFList (org.apache.jena.rdf.model.RDFList)8 AccessDeniedException (org.apache.jena.shared.AccessDeniedException)7