Search in sources :

Example 1 with ResIterator

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

the class TestListSubjectsEtc method testListSubjectsNoRemove.

public void testListSubjectsNoRemove() {
    final Model m = ModelHelper.modelWithStatements(this, "a P b; b Q c; c R a");
    final ResIterator it = m.listSubjects();
    it.next();
    try {
        it.remove();
        Assert.fail("listSubjects should not support .remove()");
    } catch (final UnsupportedOperationException e) {
        JenaTestBase.pass();
    }
}
Also used : ResIterator(org.apache.jena.rdf.model.ResIterator) Model(org.apache.jena.rdf.model.Model)

Example 2 with ResIterator

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

the class GeoSPARQLOperations method applyDefaultGeometry.

/**
 * Apply hasDefaultGeometry for every Feature with a single hasGeometry
 * property.
 *
 * @param model
 */
public static final void applyDefaultGeometry(Model model) {
    ResIterator featureIt = model.listResourcesWithProperty(Geo.HAS_GEOMETRY_PROP);
    while (featureIt.hasNext()) {
        Resource feature = featureIt.nextResource();
        if (!feature.hasProperty(Geo.HAS_DEFAULT_GEOMETRY_PROP)) {
            List<Statement> statement = feature.listProperties(Geo.HAS_GEOMETRY_PROP).toList();
            if (statement.size() == 1) {
                try {
                    Resource geometry = statement.get(0).getResource();
                    feature.addProperty(Geo.HAS_DEFAULT_GEOMETRY_PROP, geometry);
                } catch (Exception ex) {
                    LOGGER.error("Error creating default geometry: {}", ex.getMessage());
                }
            }
        }
    }
}
Also used : ResIterator(org.apache.jena.rdf.model.ResIterator) Statement(org.apache.jena.rdf.model.Statement) Resource(org.apache.jena.rdf.model.Resource) TransformException(org.opengis.referencing.operation.TransformException) DatatypeFormatException(org.apache.jena.datatypes.DatatypeFormatException) FactoryException(org.opengis.util.FactoryException) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) IOException(java.io.IOException)

Example 3 with ResIterator

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

the class GeoSPARQLOperations method convertGeoPredicates.

/**
 * Convert Geo Predicates (Lat/Lon) in Model to WKT Geometry Literal.<br>
 * Option to remove Lat and Lon predicates after combining.
 *
 * @param model
 * @param isRemoveGeoPredicates
 * @return Converted model.
 */
public static final Model convertGeoPredicates(Model model, boolean isRemoveGeoPredicates) {
    Model outputModel = ModelFactory.createDefaultModel();
    outputModel.add(model);
    outputModel.setNsPrefixes(model.getNsPrefixMap());
    if (outputModel.containsResource(SpatialExtension.GEO_LAT_PROP)) {
        ResIterator resIt = outputModel.listSubjectsWithProperty(SpatialExtension.GEO_LAT_PROP);
        while (resIt.hasNext()) {
            Resource feature = resIt.nextResource();
            if (feature.hasProperty(SpatialExtension.GEO_LON_PROP) && feature.hasProperty(SpatialExtension.GEO_LAT_PROP)) {
                // Create a GeometryLiteral from Lat/Lon
                Literal lat = feature.getProperty(SpatialExtension.GEO_LAT_PROP).getLiteral();
                Literal lon = feature.getProperty(SpatialExtension.GEO_LON_PROP).getLiteral();
                try {
                    Literal latLonPoint = ConvertLatLon.toLiteral(lat.getFloat(), lon.getFloat());
                    Resource geometry = createGeometry(feature);
                    // Add Geometry to Feature and GeometryLiteral to Geometry.
                    outputModel.add(feature, Geo.HAS_GEOMETRY_PROP, geometry);
                    outputModel.add(geometry, Geo.HAS_SERIALIZATION_PROP, latLonPoint);
                    outputModel.add(geometry, Geo.AS_WKT_PROP, latLonPoint);
                } catch (DatatypeFormatException ex) {
                    LOGGER.error("Feature: {} has geo lat/lon out of bounds. Lat: {}, Lon: {}", feature, lat, lon);
                }
            }
        }
        if (isRemoveGeoPredicates) {
            outputModel.removeAll(null, SpatialExtension.GEO_LAT_PROP, null);
            outputModel.removeAll(null, SpatialExtension.GEO_LON_PROP, null);
        }
    }
    return outputModel;
}
Also used : ResIterator(org.apache.jena.rdf.model.ResIterator) DatatypeFormatException(org.apache.jena.datatypes.DatatypeFormatException) Literal(org.apache.jena.rdf.model.Literal) InfModel(org.apache.jena.rdf.model.InfModel) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource)

Example 4 with ResIterator

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

the class SecurityExample method main.

/**
 * @param args
 */
public static void main(String[] args) {
    String[] names = { "alice", "bob", "chuck", "darla" };
    RDFNode msgType = ResourceFactory.createResource("http://example.com/msg");
    Property pTo = ResourceFactory.createProperty("http://example.com/to");
    Property pFrom = ResourceFactory.createProperty("http://example.com/from");
    Property pSubj = ResourceFactory.createProperty("http://example.com/subj");
    Model model = ModelFactory.createDefaultModel();
    URL url = SecurityExample.class.getClassLoader().getResource("org/apache/jena/security/example/example.ttl");
    model.read(url.toExternalForm());
    ResIterator ri = model.listSubjectsWithProperty(RDF.type, msgType);
    System.out.println("All the messages");
    while (ri.hasNext()) {
        Resource msg = ri.next();
        Statement to = msg.getProperty(pTo);
        Statement from = msg.getProperty(pFrom);
        Statement subj = msg.getProperty(pSubj);
        System.out.println(String.format("%s to: %s  from: %s  subj: %s", msg, to.getObject(), from.getObject(), subj.getObject()));
    }
    System.out.println();
    ExampleEvaluator evaluator = new ExampleEvaluator(model);
    model = Factory.getInstance(evaluator, "http://example.com/SecuredModel", model);
    for (String userName : names) {
        evaluator.setPrincipal(userName);
        System.out.println("Messages " + userName + " can manipulate");
        ri = model.listSubjectsWithProperty(RDF.type, msgType);
        while (ri.hasNext()) {
            Resource msg = ri.next();
            Statement to = msg.getProperty(pTo);
            Statement from = msg.getProperty(pFrom);
            Statement subj = msg.getProperty(pSubj);
            System.out.println(String.format("%s to: %s  from: %s  subj: %s", msg, to.getObject(), from.getObject(), subj.getObject()));
        }
        ri.close();
        for (String name : names) {
            System.out.println(String.format("%s messages to %s", model.listSubjectsWithProperty(pTo, name).toList().size(), name));
        }
        System.out.println();
    }
}
Also used : ResIterator(org.apache.jena.rdf.model.ResIterator) Statement(org.apache.jena.rdf.model.Statement) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode) URL(java.net.URL)

Example 5 with ResIterator

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

the class TriGBlankNodeOutputTests method checkTuples.

@Override
protected void checkTuples(File f, long expected) {
    super.checkTuples(f, expected);
    Model m = RDFDataMgr.loadModel("file://" + f.getAbsolutePath(), this.getRdfLanguage());
    ResIterator iter = m.listSubjects();
    Set<Node> subjects = new HashSet<Node>();
    while (iter.hasNext()) {
        Resource res = iter.next();
        Assert.assertTrue(res.isAnon());
        subjects.add(res.asNode());
    }
    // Should only be one subject unless the data was empty in which case
    // there will be zero subjects
    Assert.assertEquals(expected == 0 ? 0 : 1, subjects.size());
}
Also used : ResIterator(org.apache.jena.rdf.model.ResIterator) Node(org.apache.jena.graph.Node) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) HashSet(java.util.HashSet)

Aggregations

ResIterator (org.apache.jena.rdf.model.ResIterator)12 Resource (org.apache.jena.rdf.model.Resource)10 Model (org.apache.jena.rdf.model.Model)7 HashSet (java.util.HashSet)3 DatatypeFormatException (org.apache.jena.datatypes.DatatypeFormatException)3 MismatchedDimensionException (org.opengis.geometry.MismatchedDimensionException)3 TransformException (org.opengis.referencing.operation.TransformException)3 FactoryException (org.opengis.util.FactoryException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Node (org.apache.jena.graph.Node)2 InfModel (org.apache.jena.rdf.model.InfModel)2 Literal (org.apache.jena.rdf.model.Literal)2 Property (org.apache.jena.rdf.model.Property)2 RDFNode (org.apache.jena.rdf.model.RDFNode)2 Statement (org.apache.jena.rdf.model.Statement)2 URL (java.net.URL)1 Set (java.util.Set)1 RdfResource (nl.knaw.huygens.timbuctoo.rml.rdfshim.RdfResource)1 MappingDocumentBuilder (nl.knaw.huygens.timbuctoo.rml.rmldata.builders.MappingDocumentBuilder)1