Search in sources :

Example 31 with Literal

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

the class TestDatasetWithLuceneStoredLiterals method testLiteralValue.

@Test
public void testLiteralValue() {
    // test basic capturing of the literal value in a variable
    final String turtle = PF_DATA;
    String queryString = StrUtils.strjoinNL(QUERY_PROLOG, "SELECT ?s ?literal", "WHERE {", "    (?s ?score ?literal) text:query ('text') .", "}");
    Set<String> expectedURIs = new HashSet<>();
    expectedURIs.addAll(Arrays.asList(R_S1));
    Map<String, Literal> literals = doTestSearchWithLiterals(turtle, queryString, expectedURIs);
    Literal value = literals.get(R_S1);
    assertNotNull(value);
    assertEquals(NodeFactory.createLiteral("text"), value.asNode());
}
Also used : Literal(org.apache.jena.rdf.model.Literal) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 32 with Literal

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

the class TestDatasetWithLuceneStoredLiterals method testLiteralValueNonDefaultField.

@Test
public void testLiteralValueNonDefaultField() {
    // test basic capturing of the literal value in a variable
    final String testName = "testLiteralValueNonDefaultField";
    final String turtle = StrUtils.strjoinNL(TURTLE_PROLOG, "<" + RESOURCE_BASE + testName + ">", "  rdfs:comment 'a text comment'", ".");
    String queryString = StrUtils.strjoinNL(QUERY_PROLOG, "SELECT ?s ?literal", "WHERE {", "    (?s ?score ?literal) text:query (rdfs:comment 'text') .", "}");
    Set<String> expectedURIs = new HashSet<>();
    expectedURIs.addAll(Arrays.asList(RESOURCE_BASE + testName));
    Map<String, Literal> literals = doTestSearchWithLiterals(turtle, queryString, expectedURIs);
    Literal value = literals.get(RESOURCE_BASE + testName);
    assertNotNull(value);
    assertEquals(NodeFactory.createLiteral("a text comment"), value.asNode());
}
Also used : Literal(org.apache.jena.rdf.model.Literal) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 33 with Literal

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

the class FusekiBuilder method buildDataAccessPoint.

/** Build a DataAccessPoint, including DataService at Resource svc */
public static DataAccessPoint buildDataAccessPoint(Resource svc, DatasetDescriptionRegistry dsDescMap) {
    RDFNode n = FusekiLib.getOne(svc, "fu:name");
    if (!n.isLiteral())
        throw new FusekiConfigException("Not a literal for access point name: " + FmtUtils.stringForRDFNode(n));
    Literal object = n.asLiteral();
    if (object.getDatatype() != null && !object.getDatatype().equals(XSDDatatype.XSDstring))
        Fuseki.configLog.error(format("Service name '%s' is not a string", FmtUtils.stringForRDFNode(object)));
    String name = object.getLexicalForm();
    name = DataAccessPoint.canonical(name);
    DataService dataService = FusekiBuilder.buildDataService(svc, dsDescMap);
    DataAccessPoint dataAccess = new DataAccessPoint(name, dataService);
    return dataAccess;
}
Also used : FusekiConfigException(org.apache.jena.fuseki.FusekiConfigException) Literal(org.apache.jena.rdf.model.Literal) DataAccessPoint(org.apache.jena.fuseki.server.DataAccessPoint) RDFNode(org.apache.jena.rdf.model.RDFNode) DataService(org.apache.jena.fuseki.server.DataService)

Example 34 with Literal

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

the class RDFOutput method asRDF.

public Resource asRDF(Model model, ResultSet resultSet, boolean includeRowIndex) {
    Resource results = model.createResource();
    // This always goes in.
    results.addProperty(RDF.type, ResultSetGraphVocab.ResultSet);
    for (String vName : resultSet.getResultVars()) results.addProperty(ResultSetGraphVocab.resultVariable, vName);
    int count = 0;
    for (; resultSet.hasNext(); ) {
        count++;
        QuerySolution rBind = resultSet.nextSolution();
        Resource thisSolution = model.createResource();
        if (includeTypeProperties)
            thisSolution.addProperty(RDF.type, ResultSetGraphVocab.ResultSolution);
        results.addProperty(ResultSetGraphVocab.solution, thisSolution);
        if (includeRowIndex) {
            // This can lead to equivalent result sets having different graphs
            // Best used if and only if query was completely sorted. 
            Literal x = model.createTypedLiteral(count + "", XSDDatatype.XSDinteger);
            thisSolution.addLiteral(ResultSetGraphVocab.index, x);
        }
        Iterator<String> iter = getAllVars() ? rBind.varNames() : resultSet.getResultVars().iterator();
        for (; iter.hasNext(); ) {
            Resource thisBinding = model.createResource();
            String rVar = iter.next();
            RDFNode n = rBind.get(rVar);
            if (n == null)
                continue;
            //                }
            if (includeTypeProperties)
                thisBinding.addProperty(RDF.type, ResultSetGraphVocab.ResultBinding);
            thisBinding.addProperty(ResultSetGraphVocab.variable, rVar);
            thisBinding.addProperty(ResultSetGraphVocab.value, n);
            thisSolution.addProperty(ResultSetGraphVocab.binding, thisBinding);
        }
    }
    results.addProperty(ResultSetGraphVocab.size, model.createTypedLiteral(count));
    addPrefixes(model);
    return results;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Literal(org.apache.jena.rdf.model.Literal) Resource(org.apache.jena.rdf.model.Resource) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 35 with Literal

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

the class AbstractContainerMethods method testFillingContainer.

public void testFillingContainer() {
    final Container c = createContainer();
    final String lang = "fr";
    final Literal tvLiteral = model.createLiteral("test 12 string 2");
    // Resource tvResObj = model.createResource( new ResTestObjF() );
    c.add(AbstractModelTestBase.tvBoolean);
    Assert.assertTrue(c.contains(AbstractModelTestBase.tvBoolean));
    c.add(AbstractModelTestBase.tvByte);
    Assert.assertTrue(c.contains(AbstractModelTestBase.tvByte));
    c.add(AbstractModelTestBase.tvShort);
    Assert.assertTrue(c.contains(AbstractModelTestBase.tvShort));
    c.add(AbstractModelTestBase.tvInt);
    Assert.assertTrue(c.contains(AbstractModelTestBase.tvInt));
    c.add(AbstractModelTestBase.tvLong);
    Assert.assertTrue(c.contains(AbstractModelTestBase.tvLong));
    c.add(AbstractModelTestBase.tvChar);
    Assert.assertTrue(c.contains(AbstractModelTestBase.tvChar));
    c.add(AbstractModelTestBase.tvFloat);
    Assert.assertTrue(c.contains(AbstractModelTestBase.tvFloat));
    c.add(AbstractModelTestBase.tvString);
    Assert.assertTrue(c.contains(AbstractModelTestBase.tvString));
    c.add(AbstractModelTestBase.tvString, lang);
    Assert.assertTrue(c.contains(AbstractModelTestBase.tvString, lang));
    c.add(tvLiteral);
    Assert.assertTrue(c.contains(tvLiteral));
    // c.add( tvResObj ); assertTrue( c.contains( tvResObj ) );
    c.add(AbstractModelTestBase.tvLitObj);
    Assert.assertTrue(c.contains(AbstractModelTestBase.tvLitObj));
    Assert.assertEquals(11, c.size());
}
Also used : Container(org.apache.jena.rdf.model.Container) Literal(org.apache.jena.rdf.model.Literal)

Aggregations

Literal (org.apache.jena.rdf.model.Literal)42 Resource (org.apache.jena.rdf.model.Resource)17 Test (org.junit.Test)15 Model (org.apache.jena.rdf.model.Model)12 Property (org.apache.jena.rdf.model.Property)9 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)7 RDFNode (org.apache.jena.rdf.model.RDFNode)7 Node (org.apache.jena.graph.Node)6 HashSet (java.util.HashSet)4 QuerySolution (org.apache.jena.query.QuerySolution)4 Store (org.apache.jena.sdb.Store)3 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)2 SecuredLiteral (org.apache.jena.permissions.model.SecuredLiteral)2 Dataset (org.apache.jena.query.Dataset)2 Query (org.apache.jena.query.Query)2 QueryExecution (org.apache.jena.query.QueryExecution)2 ResultSet (org.apache.jena.query.ResultSet)2