Search in sources :

Example 36 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project blueprints by tinkerpop.

the class PropertyGraphSailTest method testRDFDump.

@Test
public void testRDFDump() throws Exception {
    Repository repo = new SailRepository(sail);
    RepositoryConnection rc = repo.getConnection();
    try {
        RDFWriter w = Rio.createWriter(RDFFormat.TURTLE, System.out);
        rc.export(w);
    } finally {
        rc.close();
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) SailRepository(org.openrdf.repository.sail.SailRepository) Repository(org.openrdf.repository.Repository) SailRepository(org.openrdf.repository.sail.SailRepository) RDFWriter(org.openrdf.rio.RDFWriter) Test(org.junit.Test)

Example 37 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection 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 38 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project gocd by gocd.

the class SesameGraphTest method checkWhenAddStatementWithResourceObjectExplodesItThrowsAShineRuntimeException.

@Test(expected = ShineRuntimeException.class)
public void checkWhenAddStatementWithResourceObjectExplodesItThrowsAShineRuntimeException() throws RepositoryException {
    RDFProperty property = new RDFProperty("http://www.example.com/ontology#foo");
    RepositoryConnection badConnection = mock(RepositoryConnection.class);
    doThrow(new RepositoryException("")).when(badConnection).add((org.openrdf.model.Resource) any(), (org.openrdf.model.URI) any(), (org.openrdf.model.Value) any());
    org.openrdf.model.ValueFactory stubValueFactory = mock(org.openrdf.model.ValueFactory.class);
    when(badConnection.getValueFactory()).thenReturn(stubValueFactory);
    SesameURIReference stubSubject = mock(SesameURIReference.class);
    when(stubSubject.getSesameNativeResource()).thenReturn(null);
    URIReference stubPredicate = mock(URIReference.class);
    when(stubPredicate.getURIText()).thenReturn("");
    SesameGraph badGraph = new SesameGraph(badConnection, null);
    badGraph.addStatement(stubSubject, property, (Integer) null);
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) RepositoryException(org.openrdf.repository.RepositoryException) Test(org.junit.Test)

Example 39 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project gocd by gocd.

the class SesameGraphTest method checkWhenAddNamespaceExplodesItThrowsAShineRuntimeException.

@Test(expected = ShineRuntimeException.class)
public void checkWhenAddNamespaceExplodesItThrowsAShineRuntimeException() throws Exception {
    RepositoryConnection badConnection = mock(RepositoryConnection.class);
    doThrow(new RepositoryException("")).when(badConnection).setNamespace("foo", "http://example.org/");
    SesameGraph badGraph = new SesameGraph(badConnection, null);
    badGraph.addNamespace(new Namespace("foo", "http://example.org/"));
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) RepositoryException(org.openrdf.repository.RepositoryException) Test(org.junit.Test)

Example 40 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project gocd by gocd.

the class SesameGraphTest method checkGetNamespaceByPrefixExplodesItThrowsAShineRuntimeException.

@Test(expected = ShineRuntimeException.class)
public void checkGetNamespaceByPrefixExplodesItThrowsAShineRuntimeException() throws RepositoryException {
    RepositoryConnection badConnection = mock(RepositoryConnection.class);
    when(badConnection.getNamespace("ex")).thenThrow(new RepositoryException(""));
    SesameGraph badGraph = new SesameGraph(badConnection, null);
    badGraph.getNamespaceByPrefix("ex");
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) RepositoryException(org.openrdf.repository.RepositoryException) Test(org.junit.Test)

Aggregations

RepositoryConnection (org.openrdf.repository.RepositoryConnection)71 TupleQuery (org.openrdf.query.TupleQuery)27 URI (org.openrdf.model.URI)24 RepositoryException (org.openrdf.repository.RepositoryException)20 Test (org.junit.Test)15 Statement (org.openrdf.model.Statement)11 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)10 Literal (org.openrdf.model.Literal)9 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)6 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)6 StatementImpl (org.openrdf.model.impl.StatementImpl)6 Update (org.openrdf.query.Update)6 InputStream (java.io.InputStream)5 Value (org.openrdf.model.Value)5 Repository (org.openrdf.repository.Repository)5 SailRepository (org.openrdf.repository.sail.SailRepository)5 RyaSailRepository (org.apache.rya.rdftriplestore.RyaSailRepository)4 Resource (org.openrdf.model.Resource)4 InputStreamReader (java.io.InputStreamReader)3 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)3