Search in sources :

Example 6 with Resource

use of org.openrdf.model.Resource in project blueprints by tinkerpop.

the class SailTest method testRemoveStatements.

@Test
public void testRemoveStatements() throws Exception {
    SailConnection sc = sail.getConnection();
    try {
        sc.begin();
        URI uriA = sail.getValueFactory().createURI("http://example.org/uriA");
        URI uriB = sail.getValueFactory().createURI("http://example.org/uriB");
        URI uriC = sail.getValueFactory().createURI("http://example.org/uriC");
        Resource[] contexts = { uriA, null };
        int count;
        // Remove from all contexts.
        sc.removeStatements(uriA, null, null);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(0, count);
        sc.addStatement(uriA, uriB, uriC, contexts);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(2, count);
        sc.removeStatements(uriA, null, null);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(0, count);
        // Remove from one partition context.
        sc.removeStatements(uriA, null, null);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(0, count);
        sc.addStatement(uriA, uriB, uriC, contexts);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(2, count);
        Resource[] oneContext = { uriA };
        sc.removeStatements(uriA, null, null, oneContext);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(1, count);
        // Remove from the null context.
        sc.removeStatements(uriA, null, null);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(0, count);
        sc.addStatement(uriA, uriB, uriC, contexts);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(2, count);
        Resource[] nullContext = { null };
        sc.removeStatements(uriA, null, null, nullContext);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(1, count);
        // Remove from more than one context.
        sc.removeStatements(uriA, null, null);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(0, count);
        sc.addStatement(uriA, uriB, uriC, contexts);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false);
        assertEquals(2, count);
        sc.removeStatements(uriA, null, null);
        sc.commit();
        sc.begin();
        count = countStatements(sc, uriA, null, null, false, contexts);
        assertEquals(0, count);
    } finally {
        sc.rollback();
        sc.close();
    }
}
Also used : NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) Resource(org.openrdf.model.Resource) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 7 with Resource

use of org.openrdf.model.Resource in project blueprints by tinkerpop.

the class SailVertex method setProperty.

public void setProperty(final String key, final Object value) {
    if (this.rawVertex instanceof Resource) {
        throw new RuntimeException(URI_BLANK_NODE_PROPERTIES);
    } else {
        boolean update = false;
        final Literal oldLiteral = (Literal) this.rawVertex;
        if (key.equals(SailTokens.DATATYPE)) {
            this.rawVertex = new LiteralImpl(oldLiteral.getLabel(), new URIImpl(this.graph.expandPrefix(value.toString())));
            update = true;
        } else if (key.equals(SailTokens.LANGUAGE)) {
            this.rawVertex = new LiteralImpl(oldLiteral.getLabel(), value.toString());
            update = true;
        }
        if (update) {
            this.updateLiteral(oldLiteral, (Literal) this.rawVertex);
        }
    }
}
Also used : LiteralImpl(org.openrdf.model.impl.LiteralImpl) Literal(org.openrdf.model.Literal) Resource(org.openrdf.model.Resource) URIImpl(org.openrdf.model.impl.URIImpl)

Example 8 with Resource

use of org.openrdf.model.Resource in project blueprints by tinkerpop.

the class SailVertex method removeProperty.

public <T> T removeProperty(final String key) {
    if (this.rawVertex instanceof Resource) {
        throw new RuntimeException(URI_BLANK_NODE_PROPERTIES);
    } else {
        final Literal oldLiteral = (Literal) this.rawVertex;
        if (key.equals(SailTokens.DATATYPE) || key.equals(SailTokens.LANGUAGE)) {
            this.rawVertex = new LiteralImpl(oldLiteral.getLabel());
            this.updateLiteral(oldLiteral, (Literal) this.rawVertex);
        }
        if (key.equals(SailTokens.DATATYPE)) {
            return (T) oldLiteral.getDatatype().toString();
        } else if (key.equals(SailTokens.LANGUAGE)) {
            return (T) oldLiteral.getLanguage();
        }
    }
    return null;
}
Also used : LiteralImpl(org.openrdf.model.impl.LiteralImpl) Literal(org.openrdf.model.Literal) Resource(org.openrdf.model.Resource)

Example 9 with Resource

use of org.openrdf.model.Resource 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 10 with Resource

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

the class SesameYard method findReferences.

@Override
public QueryResultList<String> findReferences(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
    if (parsedQuery == null) {
        throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
    }
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    RepositoryConnection con = null;
    TupleQueryResult results = null;
    try {
        con = repository.getConnection();
        con.begin();
        //execute the query
        int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
        results = executeSparqlFieldQuery(con, query, limit, false);
        //parse the results
        List<String> ids = limit > 0 ? new ArrayList<String>(limit) : new ArrayList<String>();
        while (results.hasNext()) {
            BindingSet result = results.next();
            Value value = result.getValue(query.getRootVariableName());
            if (value instanceof Resource) {
                ids.add(value.stringValue());
            }
        }
        con.commit();
        return new QueryResultListImpl<String>(query, ids, String.class);
    } catch (RepositoryException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } catch (QueryEvaluationException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } finally {
        if (results != null) {
            //close the result if present
            try {
                results.close();
            } catch (QueryEvaluationException ignore) {
            /* ignore */
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            /* ignore */
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) BindingSet(org.openrdf.query.BindingSet) Resource(org.openrdf.model.Resource) RepositoryException(org.openrdf.repository.RepositoryException) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) Value(org.openrdf.model.Value) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) TupleQueryResult(org.openrdf.query.TupleQueryResult)

Aggregations

Resource (org.openrdf.model.Resource)60 Statement (org.openrdf.model.Statement)22 URI (org.openrdf.model.URI)20 Value (org.openrdf.model.Value)17 HashSet (java.util.HashSet)14 Graph (org.openrdf.model.Graph)7 Literal (org.openrdf.model.Literal)6 SailConnection (org.openrdf.sail.SailConnection)5 SailException (org.openrdf.sail.SailException)5 Entry (cbit.vcell.biomodel.meta.registry.Registry.Entry)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)4 Map (java.util.Map)3 Set (java.util.Set)3 Element (org.jdom.Element)3 HashGraph (org.sbpax.impl.HashGraph)3 VCID (cbit.vcell.biomodel.meta.VCID)2 Registry (cbit.vcell.biomodel.meta.registry.Registry)2 Edge (com.tinkerpop.blueprints.Edge)2