Search in sources :

Example 96 with Resource

use of org.apache.jena.rdf.model.Resource 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 97 with Resource

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

the class AllAccept method testWithContent.

public void testWithContent() throws IOException {
    File f = FileUtils.tempFileName("assembler-acceptance-", ".n3");
    Model data = model("a P b; b Q c");
    try (FileOutputStream fs = new FileOutputStream(f)) {
        data.write(fs, "N3");
    }
    Resource root = resourceInModel("x rdf:type ja:MemoryModel; x ja:content y; y ja:externalContent file:" + f.getAbsolutePath());
    Model m = Assembler.general.openModel(root);
    assertIsoModels(data, m);
}
Also used : FileOutputStream(java.io.FileOutputStream) Model(org.apache.jena.rdf.model.Model) InfModel(org.apache.jena.rdf.model.InfModel) Resource(org.apache.jena.rdf.model.Resource) File(java.io.File)

Example 98 with Resource

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

the class AllAccept method testUnadornedInferenceModel.

public void testUnadornedInferenceModel() {
    Resource root = resourceInModel("x ja:reasoner R; R rdf:type ja:ReasonerFactory");
    Model m = Assembler.general.openModel(root);
    assertInstanceOf(InfModel.class, m);
    InfModel inf = (InfModel) m;
    assertIsoModels(empty, inf.getRawModel());
    assertInstanceOf(GenericRuleReasoner.class, inf.getReasoner());
}
Also used : Resource(org.apache.jena.rdf.model.Resource) Model(org.apache.jena.rdf.model.Model) InfModel(org.apache.jena.rdf.model.InfModel) InfModel(org.apache.jena.rdf.model.InfModel)

Example 99 with Resource

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

the class TestAssemblerGroup method testClassesLoadedBeforeAddingTypes.

public void testClassesLoadedBeforeAddingTypes() {
    String className = ImplementsSPOO.class.getName();
    Resource root = resourceInModel("_root rdf:type ja:MemoryModel; _x ja:loadClass '" + className + "'");
    ExpandingAssemblerGroup g = new AssemblerGroup.ExpandingAssemblerGroup();
    g.implementWith(resource("ja:MemoryModel"), mockAssembler);
    g.open(root);
    assertEquals(resourceSet("SPOO ja:MemoryModel"), g.implementsTypes());
}
Also used : ExpandingAssemblerGroup(org.apache.jena.assembler.assemblers.AssemblerGroup.ExpandingAssemblerGroup) Resource(org.apache.jena.rdf.model.Resource)

Example 100 with Resource

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

the class TestAssemblerGroup method testLoadsClasses.

public void testLoadsClasses() {
    AssemblerGroup a = AssemblerGroup.create();
    a.implementWith(resource("T"), new MockAssembler());
    Resource root = resourceInModel("x rdf:type T; _c ja:loadClass '" + TestAssemblerGroup.class.getName() + "$Trivial'");
    // In case already loaded.
    loaded = false;
    assertFalse("something has pre-loaded Trivial, so we can't test if it gets loaded", loaded);
    assertEquals("mockmockmock", a.open(root));
    assertTrue("the assembler group did not obey the ja:loadClass directive", loaded);
}
Also used : Resource(org.apache.jena.rdf.model.Resource) ExpandingAssemblerGroup(org.apache.jena.assembler.assemblers.AssemblerGroup.ExpandingAssemblerGroup) AssemblerGroup(org.apache.jena.assembler.assemblers.AssemblerGroup)

Aggregations

Resource (org.apache.jena.rdf.model.Resource)178 Model (org.apache.jena.rdf.model.Model)86 Test (org.junit.Test)55 Property (org.apache.jena.rdf.model.Property)34 RDFNode (org.apache.jena.rdf.model.RDFNode)24 Dataset (org.apache.jena.query.Dataset)20 Literal (org.apache.jena.rdf.model.Literal)17 BaseTest (org.apache.jena.atlas.junit.BaseTest)16 Node (org.apache.jena.graph.Node)16 Statement (org.apache.jena.rdf.model.Statement)14 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)13 StringReader (java.io.StringReader)9 InfModel (org.apache.jena.rdf.model.InfModel)9 Reader (java.io.Reader)8 ArrayList (java.util.ArrayList)8 JsonString (org.apache.jena.atlas.json.JsonString)8 Triple (org.apache.jena.graph.Triple)8 JsonLDWriteContext (org.apache.jena.riot.JsonLDWriteContext)6 PrefixMapping (org.apache.jena.shared.PrefixMapping)6 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)5