Search in sources :

Example 36 with RDFNode

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

the class QueryExecUtils method getExactlyOne.

/**
     * Execute, expecting the result to be one row, one column. Return that one
     * RDFNode or throw an exception
     */
public static RDFNode getExactlyOne(QueryExecution qExec, String varname) {
    try {
        ResultSet rs = qExec.execSelect();
        if (!rs.hasNext())
            throw new ARQException("Not found: var ?" + varname);
        QuerySolution qs = rs.nextSolution();
        RDFNode r = qs.get(varname);
        if (rs.hasNext())
            throw new ARQException("More than one: var ?" + varname);
        return r;
    } finally {
        qExec.close();
    }
}
Also used : ARQException(org.apache.jena.sparql.ARQException) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 37 with RDFNode

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

the class QueryExecUtils method getOne.

/**
     * Execute, expecting the result to be one row, one column. Return that one
     * RDFNode or null Throw excpetion if more than one.
     */
public static RDFNode getOne(QueryExecution qExec, String varname) {
    try {
        ResultSet rs = qExec.execSelect();
        if (!rs.hasNext())
            return null;
        QuerySolution qs = rs.nextSolution();
        RDFNode r = qs.get(varname);
        if (rs.hasNext()) {
            QuerySolution qs2 = rs.next();
            RDFNode r2 = qs2.get(varname);
            if (rs.hasNext())
                throw new ARQException("More than one: var ?" + varname + " -> " + r + ", " + r2 + ", ...");
            else
                throw new ARQException("Found two matches: var ?" + varname + " -> " + r + ", " + r2);
        }
        return r;
    } finally {
        qExec.close();
    }
}
Also used : ARQException(org.apache.jena.sparql.ARQException) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 38 with RDFNode

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

the class SecuredRDFNodeImpl method as.

@SuppressWarnings("unchecked")
@Override
public <T extends RDFNode> T as(final Class<T> view) throws ReadDeniedException, AuthenticationRequiredException, SecuredUnsupportedPolymorphismException {
    checkRead();
    // see if the base Item can as
    T baseAs = holder.getBaseItem().as(view);
    if (view.equals(SecuredRDFNodeImpl.class) || view.equals(RDFNode.class)) {
        return (T) this;
    }
    final Method m = getConstructor(view);
    if (m == null) {
        throw new SecuredUnsupportedPolymorphismException(this, view);
    }
    try {
        return (T) m.invoke(null, securedModel, baseAs);
    } catch (final UnsupportedPolymorphismException e) {
        throw new SecuredUnsupportedPolymorphismException(this, view);
    } catch (final IllegalArgumentException e) {
        throw new RuntimeException(e);
    } catch (final IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (final InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}
Also used : SecuredUnsupportedPolymorphismException(org.apache.jena.permissions.model.SecuredUnsupportedPolymorphismException) UnsupportedPolymorphismException(org.apache.jena.enhanced.UnsupportedPolymorphismException) SecuredUnsupportedPolymorphismException(org.apache.jena.permissions.model.SecuredUnsupportedPolymorphismException) Method(java.lang.reflect.Method) SecuredRDFNode(org.apache.jena.permissions.model.SecuredRDFNode) RDFNode(org.apache.jena.rdf.model.RDFNode) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 39 with RDFNode

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

the class SpatialIndexLuceneAssembler method open.

/*
    <#index> a :SpatialIndexLucene ;
        #spatial:directory "mem" ;
        spatial:directory <file:DIR> ;
        spatial:definition <#definition> ;
        .
    */
@SuppressWarnings("resource")
@Override
public SpatialIndex open(Assembler a, Resource root, Mode mode) {
    try {
        if (!GraphUtils.exactlyOneProperty(root, pDirectory))
            throw new SpatialIndexException("No 'spatial:directory' property on " + root);
        Directory directory;
        RDFNode n = root.getProperty(pDirectory).getObject();
        if (n.isLiteral()) {
            if (!"mem".equals(n.asLiteral().getLexicalForm()))
                throw new SpatialIndexException("No 'spatial:directory' property on " + root + " is a literal and not \"mem\"");
            directory = new RAMDirectory();
        } else {
            Resource x = n.asResource();
            String path = IRILib.IRIToFilename(x.getURI());
            File dir = new File(path);
            directory = FSDirectory.open(dir.toPath());
        }
        Resource r = GraphUtils.getResourceValue(root, pDefinition);
        EntityDefinition docDef = (EntityDefinition) a.open(r);
        return SpatialDatasetFactory.createLuceneIndex(directory, docDef);
    } catch (IOException e) {
        IO.exception(e);
        return null;
    }
}
Also used : EntityDefinition(org.apache.jena.query.spatial.EntityDefinition) Resource(org.apache.jena.rdf.model.Resource) SpatialIndexException(org.apache.jena.query.spatial.SpatialIndexException) IOException(java.io.IOException) File(java.io.File) RDFNode(org.apache.jena.rdf.model.RDFNode) RAMDirectory(org.apache.lucene.store.RAMDirectory) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) SpatialVocab.pDirectory(org.apache.jena.query.spatial.assembler.SpatialVocab.pDirectory)

Example 40 with RDFNode

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

the class AbstractTestModel method testCreateLiteralFromNode.

public void testCreateLiteralFromNode() {
    RDFNode S = model.getRDFNode(NodeCreateUtils.create("42"));
    assertInstanceOf(Literal.class, S);
    assertEquals("42", ((Literal) S).getLexicalForm());
}
Also used : RDFNode(org.apache.jena.rdf.model.RDFNode)

Aggregations

RDFNode (org.apache.jena.rdf.model.RDFNode)48 Resource (org.apache.jena.rdf.model.Resource)20 QuerySolution (org.apache.jena.query.QuerySolution)9 Model (org.apache.jena.rdf.model.Model)9 Node (org.apache.jena.graph.Node)8 Literal (org.apache.jena.rdf.model.Literal)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 Dataset (org.apache.jena.query.Dataset)5 Property (org.apache.jena.rdf.model.Property)5 Date (java.sql.Date)4 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)4 TextIndexException (org.apache.jena.query.text.TextIndexException)4 Extractor (infoeval.main.WikiData.Extractor)3 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)3 ResultSetRewindable (org.apache.jena.query.ResultSetRewindable)3 Statement (org.apache.jena.rdf.model.Statement)3 ARQException (org.apache.jena.sparql.ARQException)3 UpdateRequest (org.apache.jena.update.UpdateRequest)3 File (java.io.File)2