Search in sources :

Example 6 with RDFNode

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

the class QuerySolutionMap method addAll.

/** Add all of the solutions from one QuerySolution into this QuerySolutionMap */
public void addAll(QuerySolution other) {
    Iterator<String> iter = other.varNames();
    for (; iter.hasNext(); ) {
        String vName = iter.next();
        RDFNode rdfNode = other.get(vName);
        map.put(vName, rdfNode);
    }
}
Also used : RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 7 with RDFNode

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

the class TestSyntaxTransform method testQueryModel.

private void testQueryModel(String input, String output, String varStr, String valStr) {
    Query q1 = QueryFactory.create(PREFIX + input);
    Query qExpected = QueryFactory.create(PREFIX + output);
    Map<String, RDFNode> map = new HashMap<>();
    Node n = SSE.parseNode(valStr);
    RDFNode x = ModelUtils.convertGraphNodeToRDFNode(n);
    map.put(varStr, x);
    Query qTrans = QueryTransformOps.transformQuery(q1, map);
    assertEquals(qExpected, qTrans);
}
Also used : Query(org.apache.jena.query.Query) HashMap(java.util.HashMap) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 8 with RDFNode

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

the class JenaPreparedStatement method setObject.

@Override
public void setObject(int parameterIndex, Object value) throws SQLException {
    if (value == null)
        throw new SQLException("Setting a null value is not permitted");
    if (value instanceof Node) {
        this.setParameter(parameterIndex, (Node) value);
    } else if (value instanceof RDFNode) {
        this.setParameter(parameterIndex, ((RDFNode) value).asNode());
    } else if (value instanceof String) {
        this.setParameter(parameterIndex, NodeFactory.createLiteral((String) value));
    } else if (value instanceof Boolean) {
        this.setParameter(parameterIndex, NodeFactory.createLiteral(Boolean.toString((Boolean) value), XSDDatatype.XSDboolean));
    } else if (value instanceof Long) {
        this.setParameter(parameterIndex, NodeFactoryExtra.intToNode((Long) value));
    } else if (value instanceof Integer) {
        this.setParameter(parameterIndex, NodeFactoryExtra.intToNode((Integer) value));
    } else if (value instanceof Short) {
        this.setParameter(parameterIndex, NodeFactory.createLiteral(Short.toString((Short) value), XSDDatatype.XSDshort));
    } else if (value instanceof Byte) {
        this.setParameter(parameterIndex, NodeFactory.createLiteral(Byte.toString((Byte) value), XSDDatatype.XSDbyte));
    } else if (value instanceof BigDecimal) {
        this.setParameter(parameterIndex, NodeFactory.createLiteral(((BigDecimal) value).toPlainString(), XSDDatatype.XSDdecimal));
    } else if (value instanceof Float) {
        this.setParameter(parameterIndex, NodeFactoryExtra.floatToNode((Float) value));
    } else if (value instanceof Double) {
        this.setParameter(parameterIndex, NodeFactoryExtra.doubleToNode((Double) value));
    } else if (value instanceof Date) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(((Date) value).getTime());
        this.setParameter(parameterIndex, NodeFactoryExtra.dateTimeToNode(c));
    } else if (value instanceof Time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(((Time) value).getTime());
        this.setParameter(parameterIndex, NodeFactoryExtra.timeToNode(c));
    } else if (value instanceof Calendar) {
        this.setParameter(parameterIndex, NodeFactoryExtra.dateTimeToNode((Calendar) value));
    } else if (value instanceof URL) {
        this.setParameter(parameterIndex, NodeFactory.createURI(value.toString()));
    } else if (value instanceof URI) {
        this.setParameter(parameterIndex, NodeFactory.createURI(value.toString()));
    } else if (value instanceof IRI) {
        this.setParameter(parameterIndex, NodeFactory.createURI(value.toString()));
    } else {
        throw new SQLException("setObject() received a value that could not be converted to a RDF node for use in a SPARQL query");
    }
}
Also used : IRI(org.apache.jena.iri.IRI) SQLException(java.sql.SQLException) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) Calendar(java.util.Calendar) Time(java.sql.Time) ParameterizedSparqlString(org.apache.jena.query.ParameterizedSparqlString) URI(java.net.URI) BigDecimal(java.math.BigDecimal) Date(java.sql.Date) URL(java.net.URL) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 9 with RDFNode

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

the class TextIndexLuceneAssembler method open.

/*
    <#index> a :TextIndexLucene ;
        #text:directory "mem" ;
        #text:directory "DIR" ;
        text:directory <file:DIR> ;
        text:entityMap <#endMap> ;
        .
    */
@SuppressWarnings("resource")
@Override
public TextIndex open(Assembler a, Resource root, Mode mode) {
    try {
        if (!GraphUtils.exactlyOneProperty(root, pDirectory))
            throw new TextIndexException("No 'text:directory' property on " + root);
        Directory directory;
        RDFNode n = root.getProperty(pDirectory).getObject();
        if (n.isLiteral()) {
            String literalValue = n.asLiteral().getLexicalForm();
            if (literalValue.equals("mem")) {
                directory = new RAMDirectory();
            } else {
                File dir = new File(literalValue);
                directory = FSDirectory.open(dir.toPath());
            }
        } else {
            Resource x = n.asResource();
            String path = IRILib.IRIToFilename(x.getURI());
            File dir = new File(path);
            directory = FSDirectory.open(dir.toPath());
        }
        Analyzer analyzer = null;
        Statement analyzerStatement = root.getProperty(pAnalyzer);
        if (null != analyzerStatement) {
            RDFNode aNode = analyzerStatement.getObject();
            if (!aNode.isResource()) {
                throw new TextIndexException("Text analyzer property is not a resource : " + aNode);
            }
            Resource analyzerResource = (Resource) aNode;
            analyzer = (Analyzer) a.open(analyzerResource);
        }
        Analyzer queryAnalyzer = null;
        Statement queryAnalyzerStatement = root.getProperty(pQueryAnalyzer);
        if (null != queryAnalyzerStatement) {
            RDFNode qaNode = queryAnalyzerStatement.getObject();
            if (!qaNode.isResource()) {
                throw new TextIndexException("Text query analyzer property is not a resource : " + qaNode);
            }
            Resource analyzerResource = (Resource) qaNode;
            queryAnalyzer = (Analyzer) a.open(analyzerResource);
        }
        String queryParser = null;
        Statement queryParserStatement = root.getProperty(pQueryParser);
        if (null != queryParserStatement) {
            RDFNode qpNode = queryParserStatement.getObject();
            if (!qpNode.isResource()) {
                throw new TextIndexException("Text query parser property is not a resource : " + qpNode);
            }
            Resource parserResource = (Resource) qpNode;
            queryParser = parserResource.getLocalName();
        }
        boolean isMultilingualSupport = false;
        Statement mlSupportStatement = root.getProperty(pMultilingualSupport);
        if (null != mlSupportStatement) {
            RDFNode mlsNode = mlSupportStatement.getObject();
            if (!mlsNode.isLiteral()) {
                throw new TextIndexException("text:multilingualSupport property must be a string : " + mlsNode);
            }
            isMultilingualSupport = mlsNode.asLiteral().getBoolean();
        }
        boolean storeValues = false;
        Statement storeValuesStatement = root.getProperty(pStoreValues);
        if (null != storeValuesStatement) {
            RDFNode svNode = storeValuesStatement.getObject();
            if (!svNode.isLiteral()) {
                throw new TextIndexException("text:storeValues property must be a string : " + svNode);
            }
            storeValues = svNode.asLiteral().getBoolean();
        }
        Resource r = GraphUtils.getResourceValue(root, pEntityMap);
        EntityDefinition docDef = (EntityDefinition) a.open(r);
        TextIndexConfig config = new TextIndexConfig(docDef);
        config.setAnalyzer(analyzer);
        config.setQueryAnalyzer(queryAnalyzer);
        config.setQueryParser(queryParser);
        config.setMultilingualSupport(isMultilingualSupport);
        config.setValueStored(storeValues);
        return TextDatasetFactory.createLuceneIndex(directory, config);
    } catch (IOException e) {
        IO.exception(e);
        return null;
    }
}
Also used : Statement(org.apache.jena.rdf.model.Statement) Resource(org.apache.jena.rdf.model.Resource) IOException(java.io.IOException) Analyzer(org.apache.lucene.analysis.Analyzer) RAMDirectory(org.apache.lucene.store.RAMDirectory) File(java.io.File) RDFNode(org.apache.jena.rdf.model.RDFNode) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 10 with RDFNode

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

the class ConfigurableAnalyzerAssembler method toFilterList.

private List<String> toFilterList(Resource list) {
    List<String> result = new ArrayList<>();
    Resource current = list;
    while (current != null && !current.equals(RDF.nil)) {
        Statement stmt = current.getProperty(RDF.first);
        if (stmt == null) {
            throw new TextIndexException("filter list not well formed");
        }
        RDFNode node = stmt.getObject();
        if (!node.isResource()) {
            throw new TextIndexException("filter is not a resource : " + node);
        }
        result.add(node.asResource().getLocalName());
        stmt = current.getProperty(RDF.rest);
        if (stmt == null) {
            throw new TextIndexException("filter list not terminated by rdf:nil");
        }
        node = stmt.getObject();
        if (!node.isResource()) {
            throw new TextIndexException("filter list node is not a resource : " + node);
        }
        current = node.asResource();
    }
    return result;
}
Also used : TextIndexException(org.apache.jena.query.text.TextIndexException) Statement(org.apache.jena.rdf.model.Statement) ArrayList(java.util.ArrayList) Resource(org.apache.jena.rdf.model.Resource) 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