Search in sources :

Example 96 with PrefixMapping

use of org.apache.jena.shared.PrefixMapping in project webofneeds by researchstudio-sat.

the class CommentUnrestrictedFacet method connectFromNeed.

@Override
public void connectFromNeed(Connection con, Model content, WonMessage wonMessage) throws NoSuchNeedException, IllegalMessageForNeedStateException, ConnectionAlreadyExistsException {
    super.connectFromNeed(con, content, wonMessage);
    /* send a connect back */
    try {
        // TODO: use new system
        // needFacingConnectionClient.open(con, content, null);
        Need need = needRepository.findOneByNeedURI(con.getNeedURI());
        Model needContent = need.getDatatsetHolder().getDataset().getDefaultModel();
        PrefixMapping prefixMapping = PrefixMapping.Factory.create();
        // prefixMapping.setNsPrefix(SIOC.getURI(),"sioc");
        needContent.withDefaultMappings(prefixMapping);
        needContent.setNsPrefix("sioc", SIOC.getURI());
        Resource post = needContent.createResource(con.getNeedURI().toString(), SIOC.POST);
        Resource reply = needContent.createResource(con.getRemoteNeedURI().toString(), SIOC.POST);
        needContent.add(needContent.createStatement(needContent.getResource(con.getNeedURI().toString()), SIOC.HAS_REPLY, needContent.getResource(con.getRemoteNeedURI().toString())));
        // add WON node link
        logger.debug("linked data:" + RdfUtils.toString(needContent));
        need.getDatatsetHolder().getDataset().setDefaultModel(needContent);
        needRepository.save(need);
    // } catch (NoSuchConnectionException e) {
    // e.printStackTrace();
    // } catch (IllegalMessageForConnectionStateException e) {
    // e.printStackTrace();
    } catch (Exception e) {
        // To change body of catch statement use File | Settings | File Templates.
        e.printStackTrace();
    }
/* when connected change linked data*/
}
Also used : Need(won.protocol.model.Need) PrefixMapping(org.apache.jena.shared.PrefixMapping) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) ConnectionAlreadyExistsException(won.protocol.exception.ConnectionAlreadyExistsException) IllegalMessageForNeedStateException(won.protocol.exception.IllegalMessageForNeedStateException) NoSuchNeedException(won.protocol.exception.NoSuchNeedException)

Example 97 with PrefixMapping

use of org.apache.jena.shared.PrefixMapping in project zeppelin by apache.

the class JenaInterpreter method query.

@Override
public InterpreterResult query(String query) {
    LOGGER.info("SPARQL: Run Query '" + query + "' against " + serviceEndpoint);
    try {
        queryExecution = QueryExecutionFactory.sparqlService(serviceEndpoint, query);
        PrefixMapping prefixMapping = queryExecution.getQuery().getPrefixMapping();
        // execute query and get Results
        ResultSet results = queryExecution.execSelect();
        // transform ResultSet to TSV-String
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ResultSetFormatter.outputAsTSV(outputStream, results);
        String tsv = new String(outputStream.toByteArray());
        if (replaceURIs) {
            LOGGER.info("SPARQL: Replacing URIs");
            tsv = replaceURIs(tsv, prefixMapping);
        }
        if (removeDatatypes) {
            LOGGER.info("SPARQL: Removing datatypes");
            tsv = removeDatatypes(tsv);
        }
        return new InterpreterResult(InterpreterResult.Code.SUCCESS, InterpreterResult.Type.TABLE, tsv);
    } catch (QueryParseException e) {
        LOGGER.error(e.toString());
        return new InterpreterResult(InterpreterResult.Code.ERROR, "Error: " + e.getMessage());
    } catch (QueryExceptionHTTP e) {
        LOGGER.error(e.toString());
        int responseCode = e.getResponseCode();
        if (responseCode == HttpStatus.SC_UNAUTHORIZED) {
            return new InterpreterResult(InterpreterResult.Code.ERROR, "Unauthorized.");
        } else if (responseCode == HttpStatus.SC_NOT_FOUND) {
            return new InterpreterResult(InterpreterResult.Code.ERROR, "Endpoint not found, please check endpoint in the configuration.");
        } else {
            return new InterpreterResult(InterpreterResult.Code.ERROR, "Error: " + e.getMessage());
        }
    } catch (ARQException e) {
        return new InterpreterResult(InterpreterResult.Code.INCOMPLETE, "Query cancelled.");
    }
}
Also used : PrefixMapping(org.apache.jena.shared.PrefixMapping) ARQException(org.apache.jena.sparql.ARQException) ResultSet(org.apache.jena.query.ResultSet) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) QueryExceptionHTTP(org.apache.jena.sparql.engine.http.QueryExceptionHTTP) QueryParseException(org.apache.jena.query.QueryParseException)

Example 98 with PrefixMapping

use of org.apache.jena.shared.PrefixMapping in project jena by apache.

the class ParseHandlerResolver method declItem.

@Override
protected void declItem(ItemList list, Item item) {
    if (list != declList)
        // Deeper
        return;
    // Prefix - deeper than one.
    boolean isBase = list.get(0).isSymbol(baseTag);
    boolean isPrefix = list.get(0).isSymbol(prefixTag);
    // Old state has already been saved.
    if (isBase) {
        if (!item.isNode())
            throwException("(base ...): not an RDF node for the base.", item);
        if (!item.getNode().isURI())
            throwException("(base ...): not an IRI for the base.", item);
        String newBase = item.getNode().getURI();
        if (baseURI != null)
            baseURI = baseURI.resolve(newBase);
        else
            baseURI = IRIs.resolveIRI(newBase);
        // Remember first base seen
        if (topBase == null)
            topBase = newBase;
        return;
    }
    if (isPrefix) {
        PrefixMapping newPrefixes = new PrefixMappingImpl();
        PrefixMapping itemMappings = BuilderPrefixMapping.build(item);
        // Add exising, overwrite with new
        newPrefixes.setNsPrefixes(prefixes);
        newPrefixes.setNsPrefixes(itemMappings);
        prefixes = newPrefixes;
        // Remember first prefix mapping seen.
        if (topMap == null)
            topMap = itemMappings;
        return;
    }
    throwException("Inconsistent: " + list.shortString(), list);
}
Also used : BuilderPrefixMapping(org.apache.jena.sparql.sse.builders.BuilderPrefixMapping) PrefixMapping(org.apache.jena.shared.PrefixMapping) PrefixMappingImpl(org.apache.jena.shared.impl.PrefixMappingImpl)

Example 99 with PrefixMapping

use of org.apache.jena.shared.PrefixMapping in project jena by apache.

the class WriterBasePrefix method printPrefix.

private static boolean printPrefix(IndentedWriter iWriter, Prologue prologue) {
    PrefixMapping prefixMapping = prologue.getPrefixMapping();
    if (prefixMapping != null) {
        Map<String, String> m = prefixMapping.getNsPrefixMap();
        if (!m.isEmpty()) {
            int s = iWriter.getCol();
            WriterLib.start(iWriter, Tags.tagPrefix, NoNL);
            WriterLib.start(iWriter);
            // Indent to this col.
            int len = iWriter.getCurrentOffset();
            iWriter.incIndent(len);
            Iterator<String> iter = m.keySet().iterator();
            boolean first = true;
            for (; iter.hasNext(); ) {
                if (!first)
                    iWriter.println();
                first = false;
                String prefix = iter.next();
                String uri = prefixMapping.getNsPrefixURI(prefix);
                // Base relative URI = but not prefix mappings!
                uri = FmtUtils.stringForURI(uri, prologue.getBaseURI());
                WriterLib.start(iWriter);
                iWriter.print(prefix);
                iWriter.print(": ");
                iWriter.print(uri);
                WriterLib.finish(iWriter);
            }
            iWriter.decIndent(len);
            WriterLib.finish(iWriter);
            iWriter.ensureStartOfLine();
            return true;
        }
    }
    return false;
}
Also used : PrefixMapping(org.apache.jena.shared.PrefixMapping)

Example 100 with PrefixMapping

use of org.apache.jena.shared.PrefixMapping in project jena by apache.

the class AbstractTestGraphOverDatasetGraph method graphDSG_prefixes_3.

@Test
public void graphDSG_prefixes_3() {
    Graph g = makeDefaultGraph(baseDSG);
    PrefixMapping pmap = g.getPrefixMapping();
    assertNotNull(pmap);
}
Also used : PrefixMapping(org.apache.jena.shared.PrefixMapping) Graph(org.apache.jena.graph.Graph) Test(org.junit.Test)

Aggregations

PrefixMapping (org.apache.jena.shared.PrefixMapping)149 Test (org.junit.Test)47 PrefixMappingImpl (org.apache.jena.shared.impl.PrefixMappingImpl)29 Graph (org.apache.jena.graph.Graph)22 BaseTest (org.apache.jena.atlas.junit.BaseTest)11 Triple (org.apache.jena.graph.Triple)9 Resource (org.apache.jena.rdf.model.Resource)9 Path (org.apache.jena.sparql.path.Path)8 ConfigTest (org.apache.jena.tdb.ConfigTest)8 DatasetPrefixesTDB (org.apache.jena.tdb.store.DatasetPrefixesTDB)8 ContractTest (org.xenei.junit.contract.ContractTest)8 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)7 ArrayList (java.util.ArrayList)5 Node (org.apache.jena.graph.Node)5 Query (org.apache.jena.query.Query)5 Model (org.apache.jena.rdf.model.Model)5 PrefixMappingSDB (org.apache.jena.sdb.graph.PrefixMappingSDB)5 URI (java.net.URI)4 HashMap (java.util.HashMap)4 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)4