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();
}
}
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());
}
}
}
}
}
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;
}
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();
}
}
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());
}
Aggregations