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

The following examples show how to use org.apache.jena.rdf.model.Model#listStatements() . 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: GtRDFReader.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
protected void performReading() {
    final Model model = RDFDataMgr.loadModel(inputFilePath);
    final StmtIterator iter = model.listStatements();
    while (iter.hasNext()) {
        Statement stmt = iter.nextStatement();

        final String pred = stmt.getPredicate().toString();
        if (!(pred.contains("sameAs"))) {
            continue;
        }

        final String sub = stmt.getSubject().toString();
        final String obj = stmt.getObject().toString();

        // add a new edge for every pair of duplicate entities
        int entityId1 = urlToEntityId1.get(sub);
        int entityId2 = urlToEntityId1.get(obj) + datasetLimit;

        duplicatesGraph.addEdge(entityId1, entityId2);
    }
}
 
Example 2
Source File: Exporter.java    From fcrepo-import-export with Apache License 2.0 6 votes vote down vote up
private Set<URI> filterInboundReferences(final URI uri, final Model model) {
    final String withSlash = withSlash(uri).toString();
    final String withoutSlash = withoutSlash(uri).toString();
    final Set<URI> inboundMembers = new HashSet<>();
    final List<Statement> removeList = new ArrayList<>();
    for (final StmtIterator inbound = model.listStatements(); inbound.hasNext(); ) {
        final Statement s = inbound.next();
        final String subject = s.getSubject().toString();
        if (!subject.equals(withSlash) && !subject.equals(withoutSlash)) {
            removeList.add(s);
            logger.trace("Filtering inbound reference: {}", s);
            inboundMembers.add(URI.create(subject));
        }
    }

    model.remove(removeList);
    return inboundMembers;
}
 
Example 3
Source File: Exporter.java    From fcrepo-import-export with Apache License 2.0 6 votes vote down vote up
/**
 * Filter out the binary resource references from the model
 * @param uri the URI for the resource
 * @param model the RDF Model of the resource
 * @return the RDF model with no binary references
 * @throws FcrepoOperationFailedException
 * @throws IOException
 */
private void filterBinaryReferences(final URI uri, final Model model) throws IOException,
        FcrepoOperationFailedException {

    final List<Statement> removeList = new ArrayList<>();
    for (final StmtIterator it = model.listStatements(); it.hasNext();) {
        final Statement s = it.nextStatement();

        final RDFNode obj = s.getObject();
        if (obj.isResource() && obj.toString().startsWith(repositoryRoot.toString())
                && !s.getPredicate().toString().equals(REPOSITORY_NAMESPACE + "hasTransactionProvider")) {
            try (final FcrepoResponse resp = client().head(URI.create(obj.toString())).disableRedirects()
                    .perform()) {
                checkValidResponse(resp, URI.create(obj.toString()), config.getUsername());
                final List<URI> linkHeaders = resp.getLinkHeaders("type");
                if (linkHeaders.contains(binaryURI)) {
                    removeList.add(s);
                }
            }
        }
    }

    model.remove(removeList);
}
 
Example 4
Source File: RDFIntroSpector.java    From ontopia with Apache License 2.0 6 votes vote down vote up
private static void parseN3(GrabMappingsHandler handler, String infileurl) {
  Model model = ModelFactory.createDefaultModel();
  model.read(infileurl, "N3");

  AResourceImpl sub = new AResourceImpl();
  AResourceImpl pred = new AResourceImpl();
  AResourceImpl objres = new AResourceImpl();
  ALiteralImpl objlit = new ALiteralImpl();
  StmtIterator it = model.listStatements();
  while (it.hasNext()) {
    Statement stmt = it.nextStatement();
    RDFNode object = stmt.getObject();
    sub.setResource(stmt.getSubject());
    pred.setResource(stmt.getPredicate());
    
    if (object instanceof Literal) {
      objlit.setLiteral((Literal) object);
      handler.statement(sub, pred, objlit);
    } else {
      objres.setResource((Resource) object);
      handler.statement(sub, pred, objres);
    }
  }
}
 
Example 5
Source File: Importer.java    From fcrepo-import-export with Apache License 2.0 6 votes vote down vote up
/**
 * Removes statements from the provided model that affect triples that need not be (and indeed
 * cannot be) modified directly through PUT, POST or PATCH requests to fedora.
 *
 * Certain triples included in a resource from fedora cannot be explicitly stored, but because
 * they're derived from other content that *can* be stored will still appear identical when the
 * other RDF and content is ingested.  Examples include those properties that reflect innate
 * characteristics of binary resources like file size and message digest,  Or triples that
 * represent characteristics of rdf resources like the number of children, whether it has
 * versions and some of the types.
 *
 * @param model the RDF statements about an exported resource
 * @return the provided model updated to omit statements that may not be updated directly through
 *         the fedora API
 * @throws IOException
 * @throws FcrepoOperationFailedException
 */
private Model sanitize(final Model model) throws IOException, FcrepoOperationFailedException {
    final List<Statement> remove = new ArrayList<>();
    for (final StmtIterator it = model.listStatements(); it.hasNext(); ) {
        final Statement s = it.nextStatement();

        if ((s.getPredicate().getNameSpace().equals(REPOSITORY_NAMESPACE) && !relaxedPredicate(s.getPredicate()))
                || s.getSubject().getURI().endsWith("fcr:export?format=jcr/xml")
                || s.getSubject().getURI().equals(REPOSITORY_NAMESPACE + "jcr/xml")
                || s.getPredicate().equals(DESCRIBEDBY)
                || s.getPredicate().equals(CONTAINS)
                || s.getPredicate().equals(HAS_MESSAGE_DIGEST)
                || (s.getPredicate().equals(RDF_TYPE) && forbiddenType(s.getResource()))) {
            remove.add(s);
        } else if (s.getObject().isResource()) {
            // make sure that referenced repository objects exist
            final String obj = s.getResource().toString();
            if (obj.startsWith(repositoryRoot.toString())) {
                ensureExists(URI.create(obj));
            }
        }
    }
    return model.remove(remove);
}
 
Example 6
Source File: JenaUtil.java    From shacl with Apache License 2.0 6 votes vote down vote up
/**
 * Gets all instances of a given class and its subclasses.
 * @param cls  the class to get the instances of
 * @return the instances
 */
public static Set<Resource> getAllInstances(Resource cls) {
	JenaUtil.setGraphReadOptimization(true);
	try {
		Model model = cls.getModel();
		Set<Resource> classes = getAllSubClasses(cls);
		classes.add(cls);
		Set<Resource> results = new HashSet<>();
		for(Resource subClass : classes) {
			StmtIterator it = model.listStatements(null, RDF.type, subClass);
			while (it.hasNext()) {
				results.add(it.next().getSubject());
			}
		}
		return results;
	}
	finally {
		JenaUtil.setGraphReadOptimization(false);
	}
}
 
Example 7
Source File: AbstractDataGenerator.java    From IGUANA with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Will send a {@link org.apache.jena.rdf.model.Model} 
 */
public void sendData(Model m) throws Exception {
	int sendedSize=0;
	//split Model into small parts
	StmtIterator sti = m.listStatements();
	while(sti.hasNext()){
		Model send = ModelFactory.createDefaultModel();
		send.setNsPrefixes(m.getNsPrefixMap());
		while(sendedSize < maxSize){
			Statement stmt = sti.next();
			send.add(stmt);
			sendedSize+=1;
		}
		sendDataSnippet(RabbitMQUtils.getData(send));
	}
	
}
 
Example 8
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 9
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 10
Source File: ClassHierarchyLoader.java    From gerbil with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void copyClassHierarchy(Model readModel, Model model, Set<Resource> classes) {
    StmtIterator stmtIterator = readModel.listStatements();
    Statement s;
    while (stmtIterator.hasNext()) {
        s = stmtIterator.next();
        if (classes.contains(s.getSubject()) && ALLOWED_PROPERTIES.contains(s.getPredicate())
                && (!s.getObject().isAnon())) {
            model.add(s);
        }
    }
}
 
Example 11
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private void buildMappings(Model model) throws MalformedURLException {
  mappings = new HashMap();
  Property mapsTo = model.createProperty(RTM_MAPSTO);
  StmtIterator it = model.listStatements(null, mapsTo, (RDFNode) null);
  while (it.hasNext()) {
    Statement stmt = (Statement) it.next();
    StatementHandler mapper = getMapper(stmt.getSubject(), stmt.getObject(), model);
    mappings.put(stmt.getSubject().getURI(), mapper);
  }
  it.close();
}
 
Example 12
Source File: RdfDocumentGraphConsumerTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testDocumentGraphRdfithRelationsAsLinks()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException,
        URISyntaxException {

  processJCas(
      RdfDocumentGraphConsumer.PARAM_QUERY_ENDPOINT,
      "http://localhost:" + port + "/ds/query",
      RdfDocumentGraphConsumer.PARAM_UPDATE_ENDPOINT,
      "http://localhost:" + port + "/ds/update",
      RdfDocumentGraphConsumer.PARAM_STORE_ENDPOINT,
      "http://localhost:" + port + "/ds/data",
      RdfDocumentGraphConsumer.PARAM_OUTPUT_RELATIONS_AS_LINKS,
      true);

  Model expected =
      RDFDataMgr.loadModel(EXPECTED_DOCUMENT_RELATION_AS_LINKS_FILE.toURI().toString());
  Model model = ds.getDefaultModel();
  Resource resource =
      expected.getResource(
          "http://baleen.dstl.gov.uk/8b408a0c7163fdfff06ced3e80d7d2b3acd9db900905c4783c28295b8c996165");
  resource.removeProperties(); // Get rid of the timestamp

  StmtIterator listStatements = expected.listStatements();
  while (listStatements.hasNext()) {
    Statement statement = listStatements.next();
    assertTrue("Missing statement " + statement.toString(), model.contains(statement));
  }
  assertTrue(model.containsAll(expected));
}
 
Example 13
Source File: RdfDocumentGraphConsumerTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testDocumentGraphRdf()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException,
        URISyntaxException {

  processJCas(
      RdfDocumentGraphConsumer.PARAM_QUERY_ENDPOINT,
      "http://localhost:" + port + "/ds/query",
      RdfDocumentGraphConsumer.PARAM_UPDATE_ENDPOINT,
      "http://localhost:" + port + "/ds/update",
      RdfDocumentGraphConsumer.PARAM_STORE_ENDPOINT,
      "http://localhost:" + port + "/ds/data");

  Model expected = RDFDataMgr.loadModel(EXPECTED_DOCUMENT_FILE.toURI().toString());
  Model model = ds.getDefaultModel();
  Resource resource =
      expected.getResource(
          "http://baleen.dstl.gov.uk/8b408a0c7163fdfff06ced3e80d7d2b3acd9db900905c4783c28295b8c996165");
  resource.removeProperties(); // Get rid of the timestamp

  StmtIterator listStatements = expected.listStatements();
  while (listStatements.hasNext()) {
    Statement statement = listStatements.next();
    assertTrue("Missing statement " + statement.toString(), model.contains(statement));
  }
  assertTrue(model.containsAll(expected));
}
 
Example 14
Source File: RdfEntityGraphConsumerTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testEntityGraphRdf()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException,
        URISyntaxException {

  processJCas(
      RdfEntityGraphConsumer.PARAM_QUERY_ENDPOINT,
      "http://localhost:3330/ds/query",
      RdfEntityGraphConsumer.PARAM_UPDATE_ENDPOINT,
      "http://localhost:3330/ds/update",
      RdfEntityGraphConsumer.PARAM_STORE_ENDPOINT,
      "http://localhost:3330/ds/data");

  Model expected = RDFDataMgr.loadModel(EXPECTED_DOCUMENT_FILE.toURI().toString());
  Model model = ds.getDefaultModel();
  Resource resource =
      expected.getResource(
          "http://baleen.dstl.gov.uk/8b408a0c7163fdfff06ced3e80d7d2b3acd9db900905c4783c28295b8c996165");
  resource.removeProperties(); // Get rid of the timestamp

  StmtIterator listStatements = expected.listStatements();
  while (listStatements.hasNext()) {
    Statement statement = listStatements.next();
    assertTrue("Missing statement " + statement.toString(), model.contains(statement));
  }
  assertTrue(model.containsAll(expected));
}
 
Example 15
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 16
Source File: LinkedDataFragmentBase.java    From Server.Java with MIT License 5 votes vote down vote up
/**
 * This implementation uses {@link #addControls(Model)}, which should be
 * overridden in subclasses (instead of overriding this method). 
 * @return 
 */
@Override
public StmtIterator getControls()
{
    final Model output = ModelFactory.createDefaultModel();
    addControls( output );
    return output.listStatements();
}
 
Example 17
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
private static void listAllProperties(Resource subject, Property predicate, Set<Property> reached,
		List<Statement> results) {
	reached.add(predicate);
	StmtIterator sit;
	Model model;
	if (subject != null) {
		sit = subject.listProperties(predicate);
		model = subject.getModel();
	}
	else {
		model = predicate.getModel();
		sit = model.listStatements(null, predicate, (RDFNode)null);
	}
	while (sit.hasNext()) {
		results.add(sit.next());
	}

	// Iterate into direct subproperties
	StmtIterator it = model.listStatements(null, RDFS.subPropertyOf, predicate);
	while (it.hasNext()) {
		Statement sps = it.next();
		if (!reached.contains(sps.getSubject())) {
			Property subProperty = asProperty(sps.getSubject());
			listAllProperties(subject, subProperty, reached, results);
		}
	}
}
 
Example 18
Source File: LinkedDataFragmentBase.java    From Server.Java with MIT License 5 votes vote down vote up
/**
 * This implementation uses {@link #addMetadata(Model)}, which should be
 * overridden in subclasses (instead of overriding this method). 
 * @return 
 */
@Override
public StmtIterator getMetadata()
{
    final Model output = ModelFactory.createDefaultModel();
    addMetadata( output );
    return output.listStatements();
}
 
Example 19
Source File: Prov.java    From SPADE with GNU General Public License v3.0 4 votes vote down vote up
private boolean loadAnnotationsFromRDFs(Map<String, String> nsPrefixToFileMap){

		for(String nsprefix : nsPrefixToFileMap.keySet()){

			String rdfFile = nsPrefixToFileMap.get(nsprefix);

			Model model = null;
			try{
				model = FileManager.get().loadModel(rdfFile);

				StmtIterator stmtIterator = model.listStatements();

				while(stmtIterator.hasNext()){
					Statement statement = stmtIterator.nextStatement();

					if(statement.getPredicate().getLocalName().equals("type") &&
							statement.getPredicate().getNameSpace().contains("http://www.w3.org/1999/02/22-rdf-syntax-ns") &&
							(statement.getObject().asResource().getLocalName().equals("Property") &&
									statement.getObject().asResource().getNameSpace().contains("http://www.w3.org/2000/01/rdf-schema") ||
									statement.getObject().asResource().getLocalName().equals("Property") &&
											statement.getObject().asResource().getNameSpace().contains("http://www.w3.org/1999/02/22-rdf-syntax-ns"))){
						if(!(statement.getSubject().getLocalName() == null || statement.getSubject().getNameSpace() == null)){
							Set<String> nsSet = null;
							if((nsSet = annotationToNamespaceMap.get(statement.getSubject().getLocalName())) == null){
								nsSet = new HashSet<String>();
								annotationToNamespaceMap.put(statement.getSubject().getLocalName(), nsSet);
							}
							nsSet.add(nsprefix);
							namespacePrefixToURIMap.put(nsprefix, statement.getSubject().getNameSpace());
						}
					}
				}

				model.close();
			}catch(Exception exception){
				logger.log(Level.SEVERE, "Failed to read file '"+rdfFile+"'", exception);
				return false;
			}

		}

		for(String annotation : annotationToNamespaceMap.keySet()){
			if(annotationToNamespaceMap.get(annotation).size() > 1){
				List<String> filepaths = new ArrayList<String>();
				for(String nsPrefix : annotationToNamespaceMap.get(annotation)){
					filepaths.add(nsPrefixToFileMap.get(nsPrefix));
				}
				logger.log(Level.WARNING, "Files " + filepaths + " all have the property with name '"+annotation+"'");
			}
		}

		return true;

	}
 
Example 20
Source File: Exporter.java    From fcrepo-import-export with Apache License 2.0 4 votes vote down vote up
/**
 * Initiates export of versions for the given resource if it is a versioned resourced
 * 
 * @param uri resource uri
 * @throws FcrepoOperationFailedException
 * @throws IOException
 */
private void exportVersions(final URI uri) throws FcrepoOperationFailedException, IOException {
    // Do not check for versions if disabled, already exporting a version, or the repo root
    if (!config.includeVersions() || uri.equals(repositoryRoot)) {
        return;
    }

    // Resolve the timemap endpoint for this resource
    final URI timemapURI;
    try (FcrepoResponse response = client().head(uri).disableRedirects().perform()) {
        checkValidResponse(response, uri, config.getUsername());
        if (response.getLinkHeaders("type").stream()
            .filter(x -> x.toString().equals(MEMENTO.getURI()))
            .count() > 0) {
            logger.trace("Resource {} is a memento and therefore not versioned:  ", uri);
            return;
        } else if (response.getLinkHeaders("type").stream()
            .filter(x -> x.toString().equals(TIMEMAP.getURI()))
            .count() > 0) {
            logger.trace("Resource {} is a timemap and therefore not versioned:  ", uri);
            return;
        }

        timemapURI = response.getLinkHeaders("timemap").stream().findFirst().orElse(null);
        if (timemapURI == null) {
            logger.trace("Resource {} is not versioned:  no rel=\"timemap\" Link header present on {}", uri);
            return;
        }
    }

    export(timemapURI);

    try (FcrepoResponse response = client().get(timemapURI).accept(config.getRdfLanguage()).perform()) {
       // Verify that timemapURI can be accessed, which will fail if the resource is not versioned
        checkValidResponse(response, timemapURI, config.getUsername());
        // Extract uris of mementos for export
        final Model model = createDefaultModel().read(response.getBody(), null, config.getRdfLanguage());
        final StmtIterator versionsIt = model.listStatements();
        while (versionsIt.hasNext()) {
            final Statement versionSt = versionsIt.next();
            if (versionSt.getPredicate().equals(CONTAINS)) {
                final Resource versionResc = versionSt.getResource();
                exportLogger.info("Exporting version: {}", versionResc.getURI());
                logger.info("Exporting version {} for {}", versionResc.getURI(), uri);
                export(URI.create(versionResc.getURI()));
            }
        }
    }
}