com.hp.hpl.jena.rdf.model.ResIterator Java Examples

The following examples show how to use com.hp.hpl.jena.rdf.model.ResIterator. 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: WP2RDFXMLWriter.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
protected void writeRDFStatements(Model model, PrintWriter writer) {
	ResIterator rIter = model.listSubjects();
	@SuppressWarnings("unused")
	Resource nextR = null;
	while (rIter.hasNext())
		writeRDFStatements(model, rIter.nextResource(), writer);
}
 
Example #2
Source File: DirectoryServlet.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
	D2RServer server = D2RServer.fromServletContext(getServletContext());
	server.checkMappingFileChanged();
	if (request.getPathInfo() == null) {
		response.sendError(404);
		return;
	}
	String classMapName = request.getPathInfo().substring(1);
	int limit = server.getConfig().getLimitPerClassMap();
	Model resourceList = server.getMapping().getResourceCollection(classMapName).getInventoryModel(limit);
	if (resourceList == null) {
		response.sendError(404, "Sorry, class map '" + classMapName + "' not found.");
		return;
	}
	Map<String,String> resources = new TreeMap<String,String>();
	ResIterator subjects = resourceList.listSubjects();
	while (subjects.hasNext()) {
		Resource resource = subjects.nextResource();
		if (!resource.isURIResource()) {
			continue;
		}
		String uri = resource.getURI();
		Statement labelStmt = PageServlet.getBestLabel(resource);
		String label = (labelStmt == null) ? resource.getURI() : labelStmt.getString();
		resources.put(uri, label);
	}
	Map<String,String> classMapLinks = new TreeMap<String,String>();
	for (String name: server.getMapping().getResourceCollectionNames()) {
		classMapLinks.put(name, server.baseURI() + "directory/" + name);
	}
	VelocityWrapper velocity = new VelocityWrapper(this, request, response);
	Context context = velocity.getContext();
	context.put("rdf_link", server.baseURI() + "all/" + classMapName);
	context.put("classmap", classMapName);
	context.put("classmap_links", classMapLinks);
	context.put("resources", resources);
	context.put("limit_per_class_map", server.getConfig().getLimitPerClassMap());
	velocity.mergeTemplateXHTML("directory_page.vm");
}
 
Example #3
Source File: DatasetDescriptionServlet.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
private static Set<Resource> generatePartitions(Model m, Resource type,
		Property p) {
	Set<Resource> partitions = new HashSet<Resource>();
	ResIterator classIt = m.listResourcesWithProperty(RDF.type, type);
	while (classIt.hasNext()) {
		Resource classMap = classIt.next();
		StmtIterator pIt = classMap.listProperties(p);
		while (pIt.hasNext()) {
			partitions.add((Resource) pIt.next().getObject());
		}
	}
	return partitions;
}
 
Example #4
Source File: ConfigLoader.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
protected Resource findServerResource() {
	ResIterator it = this.model.listSubjectsWithProperty(RDF.type,
			D2RConfig.Server);
	if (!it.hasNext()) {
		return null;
	}
	return it.nextResource();
}
 
Example #5
Source File: ConfigLoader.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
protected Resource findDatabaseResource() {
	ResIterator it = this.model.listSubjectsWithProperty(RDF.type,
			D2RQ.Database);
	if (!it.hasNext()) {
		return null;
	}
	return it.nextResource();
}
 
Example #6
Source File: R2RMLReader.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
public Set<Resource> listResourcesWith(Property... properties) {
	Set<Resource> result = new HashSet<Resource>();
	for (Property property: properties) {
		ResIterator it = model.listResourcesWithProperty(property);
		while (it.hasNext()) {
			result.add(it.next());
		}
	}
	return result;
}
 
Example #7
Source File: R2RMLReader.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
private void checkForSpuriousTypes() {
	// This doesn't catch spurious subclass triples, e.g., rr:SubjectMap,
	// rr:R2RMLView.
	for (ComponentType type: ComponentType.values()) {
		ResIterator it = model.listResourcesWithProperty(RDF.type, type.asResource());
		while (it.hasNext()) {
			Resource r = it.next();
			if (mapping.getMappingComponent(r, type) == null) {
				report.report(Problem.SPURIOUS_TYPE, r, RDF.type, type.asResource());
			}
		}
	}
	remainingTriples.removeAll(null, RDF.type, (RDFNode) null);
}
 
Example #8
Source File: Entity.java    From xcurator with Apache License 2.0 5 votes vote down vote up
public static Set<Resource> getAllEntitiesOfType(Model model, String type) {
    Set<Resource> ret = new HashSet<Resource>();

    ResIterator list = model.listSubjectsWithProperty(RDF.type, model.createResource(type));
    while (list.hasNext()) {
        Resource entity = list.next();
        ret.add(entity);
    }

    return ret;
}
 
Example #9
Source File: RdfGeneratorTest2.java    From xcurator with Apache License 2.0 5 votes vote down vote up
/**
 * Run the RDF generator pipeline for clinical trial data Before running
 * this, run the Mapping Discovery Test first to generate the mapping file
 * for clinical trials.
 *
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
@Test
public void test_generateRdfs_clinical_trials() throws SAXException, IOException, ParserConfigurationException {
    // Setup deserializer
    mappingDeserialization = new XmlBasedMappingDeserialization(
            new FileInputStream("output/clinicaltrials-mapping.xml"), parser);

    Document dataDocument = parser.parse(RdfGeneratorTest2.class.getResourceAsStream(
            "/clinicaltrials/data/content.xml"), 10);
    rdfGenerator = new RdfGenerator(new DataDocument(dataDocument), new XmlBasedMapping());

    // Add steps
    rdfGenerator.addStep(mappingDeserialization);
    rdfGenerator.addStep(rdfGeneration);

    // Generate
    rdfGenerator.generateRdfs();

    // Verify
    Model model = TDBFactory.createModel(testTdbDir);
    Assert.assertFalse("No RDF was generated. TDB directory: " + testTdbDir, model.isEmpty());

    ResIterator iter = model.listResourcesWithProperty(RDF.type);
    while (iter.hasNext()) {
        Resource resource = iter.nextResource();
        System.out.println(resource.getLocalName());
        StmtIterator iterStm = resource.listProperties();
        while (iterStm.hasNext()) {
            System.out.println(iterStm.nextStatement().toString());
        }
    }
}
 
Example #10
Source File: RdfGeneratorTest.java    From xcurator with Apache License 2.0 5 votes vote down vote up
/**
 * Run the RDF generator pipeline for clinical trial data Before running
 * this, run the Mapping Discovery Test first to generate the mapping file
 * for clinical trials.
 *
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
@Test
public void test_generateRdfs_clinical_trials() throws SAXException, IOException, ParserConfigurationException {
    // Setup deserializer
    mappingDeserialization = new XmlBasedMappingDeserialization(
            new FileInputStream("output/clinicaltrials-mapping.xml"), parser);

    Document dataDocument = parser.parse(RdfGeneratorTest.class.getResourceAsStream(
            "/clinicaltrials/data/content.xml"), 10);
    rdfGenerator = new RdfGenerator(new DataDocument(dataDocument), new XmlBasedMapping());

    // Add steps
    rdfGenerator.addStep(mappingDeserialization);
    rdfGenerator.addStep(rdfGeneration);

    // Generate
    rdfGenerator.generateRdfs();

    // Verify
    Model model = TDBFactory.createModel(testTdbDir);
    Assert.assertFalse("No RDF was generated. TDB directory: " + testTdbDir, model.isEmpty());

    ResIterator iter = model.listResourcesWithProperty(RDF.type);
    while (iter.hasNext()) {
        Resource resource = iter.nextResource();
        System.out.println(resource.getLocalName());
        StmtIterator iterStm = resource.listProperties();
        while (iterStm.hasNext()) {
            System.out.println(iterStm.nextStatement().toString());
        }
    }
}
 
Example #11
Source File: ConfigurationBean.java    From LodView with MIT License 5 votes vote down vote up
private Map<String, String> populateColorPairMatcher() {
	Map<String, String> result = new HashMap<String, String>();
	ResIterator iter = confModel.listSubjectsWithProperty(confModel.createProperty(confModel.getNsPrefixURI("conf"), "hasColorPair"));
	while (iter.hasNext()) {
		Resource res = iter.next();
		NodeIterator values = confModel.listObjectsOfProperty(res, confModel.createProperty(confModel.getNsPrefixURI("conf"), "hasColorPair"));
		while (values.hasNext()) {
			RDFNode node = values.next();
			result.put(res.toString(), node.toString());
			break;
		}
	}
	return result;
}
 
Example #12
Source File: RdfGeneratorTest2.java    From xcurator with Apache License 2.0 4 votes vote down vote up
@Test
// Run test_discoverMapping_multiple_XBRLs to generate the mapping file before running
// this test.
public void test_generateRdfs_multiple_XBRLs() throws SAXException, IOException, ParserConfigurationException {
    // Setup deserializer
    mappingDeserialization = new XmlBasedMappingDeserialization(
            new FileInputStream("output/xbrl-mapping.xml"), parser);

    Document fb2013 = parser.parse(BasicEntityDiscoveryTest.class.getResourceAsStream(
            "/secxbrls/data/fb-20131231.xml"), -1);

    Document msft2013 = parser.parse(BasicEntityDiscoveryTest.class.getResourceAsStream(
            "/secxbrls/data/msft-20130630.xml"), -1);

    Document goog2013 = parser.parse(BasicEntityDiscoveryTest.class.getResourceAsStream(
            "/secxbrls/data/goog-20131231.xml"), -1);

    rdfGenerator = new RdfGenerator(new XmlBasedMapping());

    // Add document and steps
    rdfGenerator.addDataDocument(new DataDocument(fb2013, "http://example.org/resource/fb-20131231"))
            .addDataDocument(new DataDocument(msft2013, "http://example.org/resource/msft-20130630"))
            .addDataDocument(new DataDocument(goog2013, "http://example.org/resource/goog-20131231"))
            .addStep(mappingDeserialization)
            .addStep(rdfGeneration);

    // Generate
    rdfGenerator.generateRdfs();

    // Verify
    Model model = TDBFactory.createModel(testTdbDir);
    Assert.assertFalse("No RDF was generated. TDB directory: " + testTdbDir, model.isEmpty());

    ResIterator iter = model.listResourcesWithProperty(RDF.type);
    while (iter.hasNext()) {
        Resource resource = iter.nextResource();
        System.out.println(resource.getLocalName());
        StmtIterator iterStm = resource.listProperties();
        while (iterStm.hasNext()) {
            System.out.println(iterStm.nextStatement().toString());
        }
    }

}
 
Example #13
Source File: RdfGeneratorTest.java    From xcurator with Apache License 2.0 4 votes vote down vote up
@Test
// Run test_discoverMapping_multiple_XBRLs to generate the mapping file before running
// this test.
public void test_generateRdfs_multiple_XBRLs() throws SAXException, IOException, ParserConfigurationException {
    // Setup deserializer
    mappingDeserialization = new XmlBasedMappingDeserialization(
            new FileInputStream("output/xbrl-mapping.xml"), parser);

    Document fb2013 = parser.parse(BasicEntityDiscoveryTest.class.getResourceAsStream(
            "/secxbrls/data/fb-20131231.xml"), -1);

    Document msft2013 = parser.parse(BasicEntityDiscoveryTest.class.getResourceAsStream(
            "/secxbrls/data/msft-20130630.xml"), -1);

    Document goog2013 = parser.parse(BasicEntityDiscoveryTest.class.getResourceAsStream(
            "/secxbrls/data/goog-20131231.xml"), -1);

    rdfGenerator = new RdfGenerator(new XmlBasedMapping());

    // Add document and steps
    rdfGenerator.addDataDocument(new DataDocument(fb2013, "http://example.org/resource/fb-20131231"))
            .addDataDocument(new DataDocument(msft2013, "http://example.org/resource/msft-20130630"))
            .addDataDocument(new DataDocument(goog2013, "http://example.org/resource/goog-20131231"))
            .addStep(mappingDeserialization)
            .addStep(rdfGeneration);

    // Generate
    rdfGenerator.generateRdfs();

    // Verify
    Model model = TDBFactory.createModel(testTdbDir);
    Assert.assertFalse("No RDF was generated. TDB directory: " + testTdbDir, model.isEmpty());

    ResIterator iter = model.listResourcesWithProperty(RDF.type);
    while (iter.hasNext()) {
        Resource resource = iter.nextResource();
        System.out.println(resource.getLocalName());
        StmtIterator iterStm = resource.listProperties();
        while (iterStm.hasNext()) {
            System.out.println(iterStm.nextStatement().toString());
        }
    }

}
 
Example #14
Source File: Parser.java    From r2rml-parser with Apache License 2.0 4 votes vote down vote up
public LinkedList<LogicalTableView> findLogicalTableViews() {
	LinkedList<LogicalTableView> results = new LinkedList<LogicalTableView>();
	Property sqlQuery = mapModel.getProperty(rrNs + "sqlQuery");
	ResIterator iter = mapModel.listSubjectsWithProperty(sqlQuery);
	while (iter.hasNext()) {
	    Resource r = iter.nextResource();
	    NodeIterator iter2 = mapModel.listObjectsOfProperty(r, sqlQuery);
	    while (iter2.hasNext()) { //should only have one
	    	RDFNode rn = iter2.next();
	    	LogicalTableView logicalTableView = new LogicalTableView();
	    	logicalTableView.setUri(r.getURI());
	    	String query = rn.asLiteral().toString();
	    	//Windows standard line separator is "\r\n"--a carriage-return followed by a linefeed. The regex below matches any number of either of the two characters
	    	query = query.replaceAll("[\r\n]+", " ");
	    	if (query.indexOf(';') != -1) query = query.replace(';', ' ');
	    	
	    	log.info("Testing query: <" + r.getURI() + "> with value: " + query);
	    	db.testQuery(query);
	    	
	    	SelectQuery selectQuery = new SelectQuery(query, properties);

	    	//if the dump is incremental, add an order by to ensure results are retrieved in the same order
	    	if (properties.containsKey("jena.jena.storeOutputModelUsingTdb")
	    		&& properties.getProperty("jena.jena.storeOutputModelUsingTdb").contains("false")
	    		&& properties.containsKey("default.incremental")
	    		&& properties.getProperty("default.incremental").contains("true")) {
	    		String q = selectQuery.getQuery();
	    		if (q.toLowerCase().indexOf("order by") == -1) {
	    			String fieldName = selectQuery.getFields().get(0).getName();
	    			int as = fieldName.toLowerCase().indexOf(" as ");
	    			if (as > -1) {
	    				fieldName = fieldName.substring(0, as);
		    			q += " order by " + fieldName;
	    				selectQuery.setQuery(q);
	    				log.info("Added ORDER BY to query that now is: " + q);
	    			}
	    		}
	    	}
	    	
	    	logicalTableView.setSelectQuery(selectQuery);
	    	results.add(logicalTableView);
	    }
	}
	return results;
}