Search in sources :

Example 46 with PrefixMapping

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

the class TestModelAssembler method testGetsPrefixMappings.

public void testGetsPrefixMappings() {
    Assembler a = new FakeModelAssembler();
    PrefixMapping wanted = PrefixMapping.Factory.create().setNsPrefix("my", "urn:secret:42/").setNsPrefix("your", "urn:public:17#");
    Resource root = resourceInModel("x rdf:type ja:DefaultModel; x ja:prefixMapping p1; x ja:prefixMapping p2" + "; p1 rdf:type ja:PrefixMapping; p1 ja:prefix 'my'; p1 ja:namespace 'urn:secret:42/'" + "; p2 rdf:type ja:PrefixMapping; p2 ja:prefix 'your'; p2 ja:namespace 'urn:public:17#'");
    Model m = (Model) a.open(Assembler.prefixMapping, root);
    assertSamePrefixMapping(wanted, m);
}
Also used : PrefixMapping(org.apache.jena.shared.PrefixMapping) Resource(org.apache.jena.rdf.model.Resource) Model(org.apache.jena.rdf.model.Model) ModelAssembler(org.apache.jena.assembler.assemblers.ModelAssembler) Assembler(org.apache.jena.assembler.Assembler) ContentAssembler(org.apache.jena.assembler.assemblers.ContentAssembler)

Example 47 with PrefixMapping

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

the class PrefixMappingUtils method splitMethod.

// -------------------------------------------
/**
 * Assume that prefixes:localName are in the normal places (after/ or #).
 * i.e. node.getNameSpace() ; makes sense.
 * @param m
 */
private static Set<String> splitMethod(Model m) {
    /* Method:
         *
         * For each URI, split in in the usual place, after "/" or "#" for http URIs, and
         * after the last ":" for URNs, then see if that is a declared prefix.
         *
         * Exit early if every prefix is accounted for.
         */
    PrefixMapping prefixMapping = m;
    // Map prefix -> URI.
    Map<String, String> pmap = prefixMapping.getNsPrefixMap();
    // All URIs used as prefixes in the prefix mapping.
    Set<String> prefixURIs = new HashSet<>(pmap.values());
    // Prefix URIs used.
    Set<String> inUsePrefixURIs = new HashSet<>();
    Iterator<Triple> iter = m.getGraph().find(null, null, null);
    while (iter.hasNext()) {
        Triple triple = iter.next();
        processBySplit(prefixURIs, inUsePrefixURIs, triple.getSubject());
        processBySplit(prefixURIs, inUsePrefixURIs, triple.getPredicate());
        processBySplit(prefixURIs, inUsePrefixURIs, triple.getObject());
        if (inUsePrefixURIs.size() == prefixURIs.size())
            // Fast exit.
            break;
    }
    return inUsePrefixURIs;
}
Also used : Triple(org.apache.jena.graph.Triple) PrefixMapping(org.apache.jena.shared.PrefixMapping)

Example 48 with PrefixMapping

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

the class TestPrefixMappingAssembler method testIncludesSingleMapping.

public void testIncludesSingleMapping() {
    PrefixMapping wanted = PrefixMapping.Factory.create().setNsPrefix("pre", "some:prefix/");
    Assembler a = new PrefixMappingAssembler();
    Resource root = resourceInModel("root rdf:type ja:PrefixMapping; root ja:includes pm" + "; pm rdf:type ja:PrefixMapping; pm ja:prefix 'pre'; pm ja:namespace 'some:prefix/'");
    PrefixMapping pm = (PrefixMapping) a.open(root);
    assertSamePrefixMapping(wanted, pm);
}
Also used : PrefixMapping(org.apache.jena.shared.PrefixMapping) PrefixMappingAssembler(org.apache.jena.assembler.assemblers.PrefixMappingAssembler) Resource(org.apache.jena.rdf.model.Resource) PrefixMappingAssembler(org.apache.jena.assembler.assemblers.PrefixMappingAssembler)

Example 49 with PrefixMapping

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

the class TestPrefixMappingUtils method prefixesN.

@Test
public void prefixesN() {
    // All combinations.
    // No "@prefix xsd: <"+XSD.getURI()+"> ." so not in output.
    String data = StrUtils.strjoinNL("@prefix : <http://example/> .", "@prefix ex: <http://example/ex#> .", "@prefix notinuse: <http://example/whatever/> .", "@prefix indirect: <urn:foo:> .", "@prefix indirectx: <urn:ex:> .", "@prefix ns: <http://host/ns> .", "@prefix ns1: <http://host/ns1> .", "@prefix ns2: <http://host/nspace> .", "", ":s1 :p :x1 .", ":s1 ex:p :x1 .", "<urn:foo:bar> :p 1 . ", "<urn:ex:a:b> :p 2 . ", "<urn:ex:verybad#.> :p 1 . ", "ns:x ns1:p 'ns1' . ", "<http://examp/abberev> indirect:p 'foo' . ");
    Graph graph = create(data);
    PrefixMapping pmap = PrefixMappingUtils.calcInUsePrefixMapping(graph);
    PrefixMapping pmapExpected = new PrefixMappingImpl();
    pmapExpected.setNsPrefix("", "http://example/");
    pmapExpected.setNsPrefix("ex", "http://example/ex#");
    pmapExpected.setNsPrefix("indirect", "urn:foo:");
    pmapExpected.setNsPrefix("ns", "http://host/ns");
    pmapExpected.setNsPrefix("ns1", "http://host/ns1");
    pmapExpected.setNsPrefix("indirectx", "urn:ex:");
    // print("Expected:", pmapExpected) ;
    // print("Got:", pmap) ;
    Assert.assertTrue(sameMapping(pmapExpected, pmap));
    Assert.assertTrue(pmap.getNsPrefixURI("notinuse") == null);
}
Also used : PrefixMapping(org.apache.jena.shared.PrefixMapping) Graph(org.apache.jena.graph.Graph) PrefixMappingImpl(org.apache.jena.shared.impl.PrefixMappingImpl) Test(org.junit.Test)

Example 50 with PrefixMapping

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

the class TestPrefixMappingUtils method prefixes4.

@Test
public void prefixes4() {
    // No prefixes.
    String data = StrUtils.strjoinNL("<http://other/s1> <http://example/p> 123 .");
    Graph graph1 = create(data);
    PrefixMapping pmap = PrefixMappingUtils.calcInUsePrefixMapping(graph1);
    Assert.assertEquals(0, size(pmap));
    PrefixMapping pmapExpected = new PrefixMappingImpl();
    Assert.assertTrue(sameMapping(pmapExpected, pmap));
}
Also used : PrefixMapping(org.apache.jena.shared.PrefixMapping) Graph(org.apache.jena.graph.Graph) PrefixMappingImpl(org.apache.jena.shared.impl.PrefixMappingImpl) 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