Java Code Examples for org.apache.jena.rdf.model.Model#getProperty()

The following examples show how to use org.apache.jena.rdf.model.Model#getProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JenaTransformationRepairPosLength.java    From trainbenchmark with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void activate(final Collection<JenaPosLengthMatch> matches) throws IOException {
	final Model model = driver.getModel();
	final Property lengthProperty = model.getProperty(BASE_PREFIX + LENGTH);

	for (final JenaPosLengthMatch match : matches) {
		final Resource segment = match.getSegment();
		final int length = match.getLength().getInt();
		final int newLength = -length + 1;

		final Selector selector = new SimpleSelector(segment, lengthProperty, (RDFNode) null);
		final StmtIterator statementsToRemove = model.listStatements(selector);
		if (statementsToRemove.hasNext()) {
			final Statement oldStatement = statementsToRemove.next();
			model.remove(oldStatement);
		}
		
		final Statement newStatement = model.createLiteralStatement(segment, lengthProperty, newLength);
		model.add(newStatement);
	}
}
 
Example 2
Source File: JenaTransformationRepairSwitchMonitored.java    From trainbenchmark with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void activate(final Collection<JenaSwitchMonitoredMatch> matches) throws Exception {
	final Model model = driver.getModel();
	final Property sensorEdge = model.getProperty(BASE_PREFIX + MONITORED_BY);
	final Resource sensorType = model.getResource(BASE_PREFIX + SENSOR);

	for (final JenaSwitchMonitoredMatch match : matches) {
		final Resource sw = match.getSw();
		final Long newVertexId = driver.generateNewVertexId();
		final Resource sensor = model.createResource(BASE_PREFIX + ID_PREFIX + newVertexId);

		model.add(model.createStatement(sw, sensorEdge, sensor));
		model.add(model.createStatement(sensor, RDF.type, sensorType));
	}

}
 
Example 3
Source File: JenaTransformationInjectPosLength.java    From trainbenchmark with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void activate(final Collection<JenaPosLengthInjectMatch> matches) throws IOException {
	final Model model = driver.getModel();
	final Property lengthProperty = model.getProperty(BASE_PREFIX + LENGTH);

	for (final JenaPosLengthInjectMatch match : matches) {
		final Resource segment = match.getSegment();
		final Selector selector = new SimpleSelector(segment, lengthProperty, (RDFNode) null);
		final StmtIterator oldStatements = model.listStatements(selector);
		if (!oldStatements.hasNext()) {
			continue;

		}
		final Statement oldStatement = oldStatements.next();
		model.remove(oldStatement);
		final Statement newStatement = model.createLiteralStatement(segment, lengthProperty, 0);
		model.add(newStatement);
	}
}
 
Example 4
Source File: LicenseException.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Searches the model for a exception with the ID
 * @param model
 * @param id
 * @return Node containing the exception or Null if none found
 */
public static Node findException(Model model, String id) {
	Property idProperty = model.createProperty(SpdxRdfConstants.SPDX_NAMESPACE, 
			SpdxRdfConstants.PROP_LICENSE_EXCEPTION_ID);
	Property typeProperty = model.getProperty(SpdxRdfConstants.RDF_NAMESPACE, 
			SpdxRdfConstants.RDF_PROP_TYPE);
	Property exceptionTypeProperty = model.getProperty(SpdxRdfConstants.SPDX_NAMESPACE,
			SpdxRdfConstants.CLASS_SPDX_LICENSE_EXCEPTION);
	Triple m = Triple.createMatch(null, idProperty.asNode(), null);
	ExtendedIterator<Triple> tripleIter = model.getGraph().find(m);	
	while (tripleIter.hasNext()) {
		Triple t = tripleIter.next();
		if (t.getObject().toString(false).equals(id)) {
			Triple typeMatch = Triple.createMatch(t.getSubject(), typeProperty.asNode(), exceptionTypeProperty.asNode());
			ExtendedIterator<Triple> typeTripleIter = model.getGraph().find(typeMatch);
			if (typeTripleIter.hasNext()) {
				return t.getSubject();
			}
		}
	}
	return null;
}
 
Example 5
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 6 votes vote down vote up
/**
 * Finds all RTM_IN_SCOPE properties for this property and returns a
 * collection containing the RDF URIs of the values as URILocators.
 */
private Collection getScope(RDFNode rdfprop, Model model)
  throws JenaException, MalformedURLException {

  Resource subject = (Resource) rdfprop;
  Property prop = model.getProperty(RTM_IN_SCOPE);
  NodeIterator it = model.listObjectsOfProperty(subject, prop);
  ArrayList scope = new ArrayList();

  while (it.hasNext()) {
    Object o = it.next();

    if (!(o instanceof Resource))
      throw new RDFMappingException("Scoping topic must be specified by a resource, not by " + o);

    Resource obj = (Resource) o;
    LocatorIF loc = new URILocator(obj.getURI());
    scope.add(loc);
  }

  return scope;
}
 
Example 6
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 6 votes vote down vote up
private TopicIF getType(RDFNode rdfprop, Model model)
  throws JenaException, MalformedURLException {

  Resource subject = (Resource) rdfprop;
  Property prop = model.getProperty(RTM_TYPE);
  NodeIterator it = model.listObjectsOfProperty(subject, prop);
  while (it.hasNext()) {
    Resource obj = (Resource) it.next();
    LocatorIF loc = new URILocator(obj.getURI());
    TopicIF topic = topicmap.getTopicBySubjectIdentifier(loc);
    if (topic == null) {
      topic = builder.makeTopic();
      topic.addSubjectIdentifier(loc);
    }
    return topic;
  }
  return null;
}
 
Example 7
Source File: PathEvaluator.java    From shacl with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a PathEvaluator for an arbitrary SPARQL path (except single forward properties).
 * @param path  the path
 * @param shapesModel  the shapes Model
 */
public PathEvaluator(Path path, Model shapesModel) {
	this.jenaPath = path;
	isInverse = jenaPath instanceof P_Inverse && ((P_Inverse)jenaPath).getSubPath() instanceof P_Link;
	if(isInverse) {
		P_Link link = (P_Link) ((P_Inverse)jenaPath).getSubPath();
		predicate = shapesModel.getProperty(link.getNode().getURI());
	}
}
 
Example 8
Source File: JenaTransformationRepairConnectedSegments.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void activate(final Collection<JenaConnectedSegmentsMatch> matches) throws IOException {
	final Model model = driver.getModel();
	final Property connectsToProperty = model.getProperty(BASE_PREFIX + ModelConstants.LENGTH);

	for (final JenaConnectedSegmentsMatch match : matches) {
		final Resource segment2 = match.getSegment2();

		// delete segment2 by removing all (segment2, _, _) and (_, _, segment2) triples
		final Collection<Statement> statementsToRemove = new ArrayList<>();

		final Selector selectorOutgoingEdges = new SimpleSelector(segment2, null, (RDFNode) null);
		final StmtIterator statementsOutgoingEdges = model.listStatements(selectorOutgoingEdges);
		while (statementsOutgoingEdges.hasNext()) {
			statementsToRemove.add(statementsOutgoingEdges.next());
		}
		final Selector selectorIncomingEdges = new SimpleSelector(null, null, segment2);
		final StmtIterator statementsIncomingEdges = model.listStatements(selectorIncomingEdges);
		while (statementsIncomingEdges.hasNext()) {
			statementsToRemove.add(statementsIncomingEdges.next());
		}
		for (final Statement statement : statementsToRemove) {
			model.remove(statement);
		}

		// insert (segment1)-[:connectsTo]->(segment3) edge
		model.add(model.createStatement(match.getSegment1(), connectsToProperty, match.getSegment3()));
	}
}
 
Example 9
Source File: JenaTransformationRepairSwitchSet.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void activate(final Collection<JenaSwitchSetMatch> matches) throws IOException {
	final Model model = driver.getModel();
	final Property currentPositionProperty = model.getProperty(BASE_PREFIX + CURRENTPOSITION);

	for (final JenaSwitchSetMatch match : matches) {
		final Resource sw = match.getSw();

		model.remove(sw, currentPositionProperty, match.getCurrentPosition());
		model.add(sw, currentPositionProperty, match.getPosition());
	}
}
 
Example 10
Source File: JenaTransformationRepairRouteSensor.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void activate(final Collection<JenaRouteSensorMatch> matches) throws IOException {
	final Model model = driver.getModel();

	final Property requires = model.getProperty(BASE_PREFIX + REQUIRES);
	for (final JenaRouteSensorMatch match : matches) {
		final Resource route = match.getRoute();
		final Resource sensor = match.getSensor();

		model.add(model.createStatement(route, requires, sensor));
	}
}
 
Example 11
Source File: JenaTransformationRepairSemaphoreNeighbor.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void activate(final Collection<JenaSemaphoreNeighborMatch> matches) throws IOException {
	final Model model = driver.getModel();

	final Property entry = model.getProperty(BASE_PREFIX + ENTRY);
	for (final JenaSemaphoreNeighborMatch match : matches) {
		final Resource route2 = match.getRoute2();
		final Resource semaphore = match.getSemaphore();

		model.add(model.createStatement(route2, entry, semaphore));
	}
}
 
Example 12
Source File: JenaTransformationInjectSwitchSet.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void activate(final Collection<JenaSwitchSetInjectMatch> matches) {
	final Model model = driver.getModel();
	final Property currentPositionProperty = model.getProperty(BASE_PREFIX + CURRENTPOSITION);

	for (final JenaSwitchSetInjectMatch match : matches) {
		final Resource sw = match.getSw();
		
		final Selector selector = new SimpleSelector(sw, currentPositionProperty, (RDFNode) null);
		final StmtIterator statementsToRemove = model.listStatements(selector);
		if (!statementsToRemove.hasNext()) {
			continue;

		}

		// delete old statement
		final Statement oldStatement = statementsToRemove.next();
		model.remove(oldStatement);

		// get next enum value
		final Resource currentPositionResource = oldStatement.getObject().asResource();
		final String currentPositionRDFString = currentPositionResource.getLocalName();
		final String currentPositionString = RdfHelper.removePrefix(Position.class, currentPositionRDFString);
		final Position currentPosition = Position.valueOf(currentPositionString);
		final Position newCurrentPosition = Position.values()[(currentPosition.ordinal() + 1) % Position.values().length];
		final String newCurrentPositionString = RdfHelper.addEnumPrefix(newCurrentPosition);
		final Resource newCurrentPositionResource = model.createResource(BASE_PREFIX + newCurrentPositionString);

		// set new value
		final Statement newStatement = model.createLiteralStatement(sw, currentPositionProperty, newCurrentPositionResource);
		model.add(newStatement);

	}
}
 
Example 13
Source File: JenaTransformationInjectConnectedSegments.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void activate(final Collection<JenaConnectedSegmentsInjectMatch> matches) throws Exception {
	final Model model = driver.getModel();

	final Property length = model.getProperty(BASE_PREFIX + ModelConstants.LENGTH);
	final Property connectsTo = model.getProperty(BASE_PREFIX + ModelConstants.CONNECTS_TO);
	final Property monitoredBy = model.getProperty(BASE_PREFIX + ModelConstants.MONITORED_BY);
	final Property segmentType = model.getProperty(BASE_PREFIX + ModelConstants.SEGMENT);

	for (final JenaConnectedSegmentsInjectMatch match : matches) {
		// create (segment2) node
		final Long newVertexId = driver.generateNewVertexId();
		final Resource segment2 = model.createResource(BASE_PREFIX + ID_PREFIX + newVertexId);
		model.add(model.createStatement(segment2, RDF.type, segmentType));
		model.add(model.createLiteralStatement(segment2, length, TrainBenchmarkConstants.DEFAULT_SEGMENT_LENGTH));

		// (segment1)-[:connectsTo]->(segment2)
		model.add(match.getSegment1(), connectsTo, segment2);
		// (segment2)-[:connectsTo]->(segment3)
		model.add(segment2, connectsTo, match.getSegment3());
		// (segment2)-[:monitoredBy]->(sensor)
		model.add(segment2, monitoredBy, match.getSensor());

		// remove (segment1)-[:connectsTo]->(segment3)
		model.remove(match.getSegment1(), connectsTo, match.getSegment3());
	}
}
 
Example 14
Source File: JenaTransformationInjectSemaphoreNeighbor.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void activate(final Collection<JenaSemaphoreNeighborInjectMatch> matches) throws IOException {
	final Model model = driver.getModel();
	final Property entry = model.getProperty(RdfConstants.BASE_PREFIX + ModelConstants.ENTRY);
	for (JenaSemaphoreNeighborInjectMatch match : matches) {
		model.remove(match.getRoute(), entry, match.getSemaphore());
	}
}
 
Example 15
Source File: JenaTransformationInjectRouteSensor.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void activate(final Collection<JenaRouteSensorInjectMatch> routeSensorInjectMatchsMatches) throws IOException {
	final Model model = driver.getModel();
	final Property requires = model.getProperty(BASE_PREFIX + ModelConstants.REQUIRES);
	
	for (final JenaRouteSensorInjectMatch rsim : routeSensorInjectMatchsMatches) {
		model.remove(rsim.getRoute(), requires, rsim.getSensor()); 
	}

}
 
Example 16
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private LocatorIF getTopicIndicator(Resource subject, String property,
                                    Model model)
  throws JenaException, MalformedURLException {
  Property prop = model.getProperty(property);
  NodeIterator it = model.listObjectsOfProperty(subject, prop);
  while (it.hasNext()) {
    Resource obj = (Resource) it.next();
    if (obj.isAnon())
      continue; // FIXME: is this really ok?
    return new URILocator(obj.getURI());
  }
  return null;
}