Search in sources :

Example 16 with Statement

use of org.openrdf.model.Statement in project backstage by zepheira.

the class Database method exportRDFa.

public String exportRDFa(int limit, String title) {
    try {
        RepositoryConnection con = getRepository().getConnection();
        RepositoryResult<Statement> result = con.getStatements(null, null, null, false);
        String out = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><style>span {padding:4px}</style><title>" + title + "</title></head><body>";
        int itemCount = 0;
        Resource prevS = null;
        URI prevP = null;
        Value prevO = null;
        while (result.hasNext() && itemCount <= limit) {
            Statement st = result.next();
            String currLabel = null;
            Resource s = st.getSubject();
            String sStr = s.toString();
            // strip "http://127.0.0.1/" prefix from subj/pred
            if (sStr.startsWith("http://127.0.0.1/")) {
                sStr = sStr.substring(17);
            }
            URI p = st.getPredicate();
            String pStr = p.toString();
            if (pStr.startsWith("http://127.0.0.1/")) {
                pStr = pStr.substring(17);
            }
            Value o = st.getObject();
            String oStr = o.stringValue();
            // Determine if our object and subjects are URI
            boolean objectIsURI = false;
            try {
                oStr = (new java.net.URL(oStr).toString());
                objectIsURI = true;
            } catch (Exception e) {
                objectIsURI = false;
            }
            boolean subjectIsURI = false;
            try {
                sStr = (new java.net.URL(sStr).toString());
                subjectIsURI = true;
            } catch (Exception e) {
                subjectIsURI = false;
            }
            // FIXME. Use heuristic to determine if property is an image type. See above re fix.
            boolean objectIsImage = false;
            if (oStr.endsWith("png") || oStr.endsWith("jpg") || oStr.endsWith("gif") || oStr.endsWith("svg") || oStr.endsWith("jpeg")) {
                objectIsImage = true;
            }
            // arrives before we need it.
            if (pStr.equals("http://www.w3.org/2000/01/rdf-schema#label")) {
                currLabel = oStr;
            }
            // group by subject, count as an item
            if (s != prevS) {
                itemCount++;
                if (itemCount > limit)
                    break;
                if (subjectIsURI) {
                    out += "<p about=\"" + sStr + "\"" + ">\n";
                } else {
                    out += "<p about=\"#" + sStr + "\"" + ">\n";
                }
            }
            // skip externally irrelevant triples or those we handle elsewhere
            if (!pStr.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type") && !pStr.equals("http://simile.mit.edu/2006/11/exhibit#id") && !pStr.equals("http://www.w3.org/2000/01/rdf-schema#label")) {
                if (objectIsURI) {
                    // if we don't have a label, use subject
                    if (currLabel == null) {
                        currLabel = sStr;
                    }
                    if (objectIsImage) {
                        out += "<img property=\"" + pStr + "\" src=\"" + oStr + "\" alt=\"" + currLabel + "\" />";
                    } else {
                        out += "<a property=\"" + pStr + "\" href=\"" + oStr + "\">" + currLabel + "</a>";
                    }
                } else {
                    out += "<span property=\"" + pStr + "\">" + oStr + "</span>\n";
                }
            }
            prevS = s;
            prevP = p;
            prevO = o;
        }
        return out + "</body></html>";
    } catch (OpenRDFException e) {
        return "<html><head><title=\"Error\"></head><body>" + e.toString() + "</body></html>";
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) OpenRDFException(org.openrdf.OpenRDFException) URI(org.openrdf.model.URI) SailException(org.openrdf.sail.SailException) OpenRDFException(org.openrdf.OpenRDFException) Value(org.openrdf.model.Value)

Example 17 with Statement

use of org.openrdf.model.Statement in project stanbol by apache.

the class AbstractSesameBackend method listObjectsInternal.

protected Collection<Value> listObjectsInternal(RepositoryConnection connection, Resource subject, org.openrdf.model.URI property, boolean includeInferred, Resource... context) throws RepositoryException {
    ValueFactory valueFactory = connection.getValueFactory();
    Set<Value> result = new HashSet<Value>();
    RepositoryResult<Statement> qResult = connection.getStatements(merge(subject, connection.getValueFactory()), merge(property, connection.getValueFactory()), null, includeInferred, context);
    try {
        while (qResult.hasNext()) {
            result.add(qResult.next().getObject());
        }
    } finally {
        qResult.close();
    }
    return result;
}
Also used : Statement(org.openrdf.model.Statement) Value(org.openrdf.model.Value) ValueFactory(org.openrdf.model.ValueFactory) HashSet(java.util.HashSet)

Example 18 with Statement

use of org.openrdf.model.Statement in project stanbol by apache.

the class RdfIndexingSource method extractRepresentation.

/**
     * Extracts all {@link Statement}s part of the Representation. If
     * {@link #followBNodeState} this is called recursively for {@link Statement}s
     * where the value is an {@link BNode}.
     */
protected void extractRepresentation(RepositoryConnection con, Model model, Resource node, Set<BNode> visited) throws RepositoryException {
    //we need all the outgoing relations and also want to follow bNodes until
    //the next UriRef. However we are not interested in incoming relations!
    RepositoryResult<Statement> outgoing = con.getStatements(node, null, null, includeInferred, contexts);
    Statement statement;
    Set<BNode> bnodes = followBNodeState ? new HashSet<BNode>() : null;
    while (outgoing.hasNext()) {
        statement = outgoing.next();
        model.add(statement);
        if (followBNodeState) {
            Value object = statement.getObject();
            if (object instanceof BNode && !visited.contains(object)) {
                bnodes.add((BNode) object);
            }
        }
    //else do not follow values beeing BNodes
    }
    outgoing.close();
    if (followBNodeState) {
        for (BNode bnode : bnodes) {
            visited.add(bnode);
            //TODO: recursive calls could cause stackoverflows with wired graphs
            extractRepresentation(con, model, bnode, visited);
        }
    }
}
Also used : BNode(org.openrdf.model.BNode) Statement(org.openrdf.model.Statement) Value(org.openrdf.model.Value)

Example 19 with Statement

use of org.openrdf.model.Statement in project stanbol by apache.

the class SesameYard method extractRepresentation.

/**
     * Recursive Method internally doing all the work for 
     * {@link #createRepresentationGraph(UriRef, TripleCollection)}
     * @param con the repository connection to read the data from
     * @param model The model to add the statements retrieved
     * @param node the current node. Changes in recursive calls as it follows
     * @param visited holding all the visited BNodes to avoid cycles. Other nodes 
     * need not be added because this implementation would not follow it anyway
     * outgoing relations if the object is a {@link BNode} instance.
     * @throws RepositoryException 
     */
private void extractRepresentation(RepositoryConnection con, Model model, Resource node, Set<BNode> visited) throws RepositoryException {
    //we need all the outgoing relations and also want to follow bNodes until
    //the next UriRef. However we are not interested in incoming relations!
    RepositoryResult<Statement> outgoing = con.getStatements(node, null, null, includeInferred, contexts);
    Statement statement;
    Set<BNode> bnodes = new HashSet<BNode>();
    while (outgoing.hasNext()) {
        statement = outgoing.next();
        model.add(statement);
        Value object = statement.getObject();
        if (object instanceof BNode && !visited.contains(object)) {
            bnodes.add((BNode) object);
        }
    }
    outgoing.close();
    for (BNode bnode : bnodes) {
        visited.add(bnode);
        //TODO: recursive calls could cause stackoverflows with wired graphs
        extractRepresentation(con, model, bnode, visited);
    }
}
Also used : BNode(org.openrdf.model.BNode) Statement(org.openrdf.model.Statement) Value(org.openrdf.model.Value) HashSet(java.util.HashSet)

Example 20 with Statement

use of org.openrdf.model.Statement in project stanbol by apache.

the class RdfRepresentation method removeAllNaturalText.

@Override
public void removeAllNaturalText(String field, String... languages) throws IllegalArgumentException {
    if (field == null) {
        throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
    } else if (field.isEmpty()) {
        throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
    }
    ValueTypeFilter<Literal> vtf = new ValueTypeFilter<Literal>(languages);
    Iterator<Statement> statements = model.filter(subject, sesameFactory.createURI(field), null).iterator();
    while (statements.hasNext()) {
        Statement statement = statements.next();
        if (vtf.evaluate(statement.getObject())) {
            statements.remove();
        }
    }
}
Also used : Statement(org.openrdf.model.Statement) Literal(org.openrdf.model.Literal)

Aggregations

Statement (org.openrdf.model.Statement)67 Resource (org.openrdf.model.Resource)22 Value (org.openrdf.model.Value)22 URI (org.openrdf.model.URI)21 HashSet (java.util.HashSet)16 SailException (org.openrdf.sail.SailException)10 Collection (java.util.Collection)9 Literal (org.openrdf.model.Literal)9 Graph (org.openrdf.model.Graph)7 Edge (com.tinkerpop.blueprints.Edge)6 Test (org.junit.Test)6 Vertex (com.tinkerpop.blueprints.Vertex)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 PrintWriter (java.io.PrintWriter)3 Set (java.util.Set)3 BNode (org.openrdf.model.BNode)3 ValueFactory (org.openrdf.model.ValueFactory)3 RDFHandlerException (org.openrdf.rio.RDFHandlerException)3 RdfXmlSerializer (org.qi4j.library.rdf.serializer.RdfXmlSerializer)3