Java Code Examples for com.hp.hpl.jena.rdf.model.Model#listStatements()

The following examples show how to use com.hp.hpl.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: VocabularySummarizer.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
public Collection<Resource> getUndefinedResources(Model model) {
	Set<Resource> result = new HashSet<Resource>();
	StmtIterator it = model.listStatements();
	while (it.hasNext()) {
		Statement stmt = it.nextStatement();
		if (stmt.getSubject().isURIResource()
				&& stmt.getSubject().getURI().startsWith(namespace)
				&& !resources.contains(stmt.getSubject())) {
			result.add(stmt.getSubject());
		}
		if (stmt.getPredicate().equals(RDF.type)) continue;
		if (stmt.getObject().isURIResource()
				&& stmt.getResource().getURI().startsWith(namespace)
				&& !resources.contains(stmt.getResource())) {
			result.add(stmt.getResource());
		}
	}
	return result;
}
 
Example 2
Source File: SparqlExtractor.java    From wandora with GNU General Public License v3.0 6 votes vote down vote up
public void RDF2TopicMap(Model model, TopicMap map) {
    // list the statements in the Model
    StmtIterator iter = model.listStatements();
    Statement stmt = null;
    int counter = 0;

    while (iter.hasNext() && !forceStop()) {
        try {
            stmt = iter.nextStatement();  // get next statement
            handleStatement(stmt, map);
        }
        catch(Exception e) {
            log(e);
        }
        counter++;
        setProgress(counter);
        if(counter % 100 == 0) hlog("RDF statements processed: " + counter);

    }
    log("Total RDF statements processed: " + counter);
}
 
Example 3
Source File: TrigUtil.java    From EventCoreference with Apache License 2.0 6 votes vote down vote up
static public ArrayList<String> getAllEntityEvents (Dataset dataset, String entity) {
    ArrayList<String> events = new ArrayList<String>();
    Iterator<String> it = dataset.listNames();
    while (it.hasNext()) {
        String name = it.next();
        if (!name.equals(instanceGraph) && (!name.equals(provenanceGraph))) {
            Model namedModel = dataset.getNamedModel(name);
            StmtIterator siter = namedModel.listStatements();
            while (siter.hasNext()) {
                Statement s = siter.nextStatement();
                String object = getObjectValue(s).toLowerCase();
                if (object.indexOf(entity.toLowerCase()) > -1) {
                    String subject = s.getSubject().getURI();
                    if (!events.contains(subject)) {
                        events.add(subject);
                    }
                }
            }
        }
    }
    return events;
}
 
Example 4
Source File: JenaSesameUtils.java    From anno4j with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the Jena Model to a Sesame Graph
 * @param theModel the model to convert
 * @return the set of statements in the Jena model saved in a sesame Graph
 */
public static Graph asSesameGraph(Model theModel) {
    Graph aGraph = new GraphImpl();

    StmtIterator sIter = theModel.listStatements();
    while (sIter.hasNext()) {
        aGraph.add(asSesameStatement(sIter.nextStatement()));
    }

    sIter.close();

    return aGraph;
}
 
Example 5
Source File: WP2RDFXMLWriter.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
protected void writeRDFStatements(Model model, Resource subject,
		PrintWriter writer) {
	StmtIterator sIter = model
			.listStatements(subject, null, (RDFNode) null);
	writeDescriptionHeader(subject, writer);
	while (sIter.hasNext()) {
		Statement nextSt = sIter.nextStatement();
		if (nextSt.getObject().toString().startsWith("null^^")) {
			sIter.nextStatement();
			continue;
		}
		writePredicate(nextSt, writer);
	}
	writeDescriptionTrailer(subject, writer);
}
 
Example 6
Source File: VocabularySummarizer.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
public Collection<Resource> getUndefinedClasses(Model model) {
	Set<Resource> result = new HashSet<Resource>();
	StmtIterator it = model.listStatements(null, RDF.type, (RDFNode) null);
	while (it.hasNext()) {
		Statement stmt = it.nextStatement();
		if (stmt.getObject().isURIResource()
				&& stmt.getResource().getURI().startsWith(namespace)
				&& !classes.contains(stmt.getObject())) {
			result.add(stmt.getResource());
		}
	}
	return result;
}
 
Example 7
Source File: VocabularySummarizer.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
public Collection<Property> getUndefinedProperties(Model model) {
	Set<Property> result = new HashSet<Property>();
	StmtIterator it = model.listStatements();
	while (it.hasNext()) {
		Statement stmt = it.nextStatement();
		if (stmt.getPredicate().getURI().startsWith(namespace)
				&& !properties.contains(stmt.getPredicate())) {
			result.add(stmt.getPredicate());
		}
	}
	return result;
}
 
Example 8
Source File: VocabularySummarizer.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
public boolean usesVocabulary(Model model) {
	StmtIterator it = model.listStatements();
	while (it.hasNext()) {
		Statement stmt = it.nextStatement();
		if (stmt.getPredicate().getURI().startsWith(namespace)) {
			return true;
		}
		if (stmt.getPredicate().equals(RDF.type) && stmt.getResource().getURI().startsWith(namespace)) {
			return true;
		}
	}
	return false;
}
 
Example 9
Source File: VocabularySummarizer.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
public Model triplesInvolvingVocabulary(Model model) {
	Model result = ModelFactory.createDefaultModel();
	result.getNsPrefixMap().putAll(model.getNsPrefixMap());
	StmtIterator it = model.listStatements();
	while (it.hasNext()) {
		Statement stmt = it.next();
		if (properties.contains(stmt.getPredicate())
				|| (stmt.getPredicate().equals(RDF.type) && classes.contains(stmt.getObject()))) {
			result.add(stmt);
		}
	}
	return result;
}
 
Example 10
Source File: PageServlet.java    From GeoTriples with Apache License 2.0 5 votes vote down vote up
private Collection<Property> collectProperties(Model m, Resource r) {
	Collection<Property> result = new TreeSet<Property>();
	StmtIterator it = r.listProperties();
	while (it.hasNext()) {
		result.add(new Property(it.nextStatement(), false));
	}
	it = m.listStatements(null, null, r);
	while (it.hasNext()) {
		result.add(new Property(it.nextStatement(), true));
	}
	return result;
}
 
Example 11
Source File: Course.java    From neo4jena with Apache License 2.0 5 votes vote down vote up
public static void write(GraphDatabaseService njgraph) {
	InputStream in = FileManager.get().open( inputFileName );
	if (in == null) {
           throw new IllegalArgumentException( "File: " + inputFileName + " not found");
       }
       
	Model model = ModelFactory.createDefaultModel();
       model.read(in,"","TTL");
       double triples = model.size();
       log.info("Model loaded with " +  triples + " triples");
       System.out.println("Model loaded with " +  triples + " triples");
       Map<String, String> prefixMap = model.getNsPrefixMap();
      // System.out.println("Prefix Mapping: " + prefixMap);
       
	NeoGraph graph = new NeoGraph(njgraph);
	graph.getPrefixMapping().setNsPrefixes(prefixMap);
	graph.startBulkLoad();
	log.info("Connection created");
	Model njmodel = ModelFactory.createModelForGraph(graph);
	log.info("NeoGraph Model initiated");
	System.out.println("NeoGraph Model initiated");
	
	//log.info(njmodel.add(model));
	//njmodel.add(model);
	StmtIterator iterator = model.listStatements();
	StopWatch watch = new StopWatch();
	int count = 0;
	while(iterator.hasNext()){
		njmodel.add(iterator.next());
		count++;
	}
	System.out.println("Total triples loaded are:"+ count);
	graph.stopBulkLoad();
	//log.info("Storing completed (ms): " + watch.stop());
	System.out.println("Storing completed (ms): " + watch.stop());
}
 
Example 12
Source File: WriteStatementsKnowledgeStore.java    From EventCoreference with Apache License 2.0 5 votes vote down vote up
static public void main (String[] args) {
    Dataset dataset  = null;
    String address = "http://145.100.57.176:50053/";
    ArrayList<org.openrdf.model.Statement> statements = new ArrayList<org.openrdf.model.Statement>();
            Iterator<String> it = dataset.listNames();
            while (it.hasNext()) {
                String name = it.next();

                Model namedModel = dataset.getNamedModel(name);
                StmtIterator siter = namedModel.listStatements();
                while (siter.hasNext()) {
                    com.hp.hpl.jena.rdf.model.Statement s = siter.nextStatement();
                    org.openrdf.model.Statement statement =castJenaOpenRdf(s, name);
                    if (statement!=null) {
                        statements.add(statement);
                    }
                }
            }
            if (DEBUG) {
                try {
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    RDFDataMgr.write(os, dataset, RDFFormat.TRIG_PRETTY);
                    String rdfString = new String(os.toByteArray(), "UTF-8");
                    System.out.println("rdfString = " + rdfString);
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
           // System.out.println("address = " + address);
    WriteStatementsKnowledgeStore.storeTriples(statements, address);
}
 
Example 13
Source File: GetEventStats.java    From EventCoreference with Apache License 2.0 5 votes vote down vote up
static ArrayList<String> getAllEsoEvents (Dataset dataset, ArrayList<String> esoTypes) {
    ArrayList<String> events = new ArrayList<String>();
    Iterator<String> it = dataset.listNames();
    while (it.hasNext()) {
        String name = it.next();
        if (name.equals(instanceGraph)) {
            Model namedModel = dataset.getNamedModel(name);
            StmtIterator siter = namedModel.listStatements();
            while (siter.hasNext()) {
                Statement s = siter.nextStatement();
                //  System.out.println("s.toString() = " + s.toString());
                if (s.getPredicate().toString().endsWith("#type")) {
                    for (int i = 0; i < esoTypes.size(); i++) {
                        String esoType = esoTypes.get(i);
                        if (s.getObject().toString().endsWith(esoType)) {
                            String subject = s.getSubject().getURI();
                            if (!events.contains(subject)) {
                                events.add(subject);
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
    return events;
}
 
Example 14
Source File: SelectEntityTrig.java    From EventCoreference with Apache License 2.0 4 votes vote down vote up
static public void main (String[] args) {

        String trigfolderPath = "";
        trigfolderPath = "/Users/piek/Desktop/tweede-kamer/events";
        String entity = "";
        String date = "";
        entity = "lippens";
        for (int i = 0; i < args.length; i++) {
            String arg = args[i];
            if (arg.equals("--trig-folder") && args.length>(i+1)) {
                trigfolderPath = args[i+1];
            }
            else if (arg.equals("--entity") && args.length>(i+1)) {
                entity = args[i+1];
            }
            else if (arg.equals("--event-date") && args.length>(i+1)) {
                date = args[i+1];
            }
        }
        if (entity.isEmpty()) {
            System.out.println("Entity is empty");
            return;
        }
        File trigfolder = new File(trigfolderPath);
        String trigEntityPath = trigfolder.getParent()+"/"+entity;
        File entityTrigFolder = new File (trigEntityPath);
        if (!entityTrigFolder.exists()) {
            entityTrigFolder.mkdir();
        }
        if (!entityTrigFolder.exists()) {
            System.out.println("Could not create entity trig folder");
            return;
        }
        dataset = TDBFactory.createDataset();
        ArrayList<File> trigFiles = Util.makeRecursiveFileList(trigfolder, ".trig");
        System.out.println(trigfolder.getName() + " trigFiles.size() = " + trigFiles.size());
        int cnt = 1;
        for (int i = 0; i < trigFiles.size(); i++) {
            File file = trigFiles.get(i);
            if (!file.getParentFile().getName().startsWith(date)) {
                continue;
            }
            if (i%500==0) {
                System.out.println("i = " + i);
               // if (i>1000) break;
            }
            ArrayList<String> events = new ArrayList<String>();
            dataset = RDFDataMgr.loadDataset(file.getAbsolutePath());
            Model namedModel = dataset.getNamedModel(TrigUtil.instanceGraph);
            StmtIterator siter = namedModel.listStatements();
            while (siter.hasNext()) {
                Statement s = siter.nextStatement();
                String subject = s.getSubject().getURI().toLowerCase();
                if (subject.indexOf(entity.toLowerCase())>-1) {
                    String trigName = trigEntityPath+"/"+cnt+"_"+file.getName();
                    File trigCopy = new File(trigName);
                    copyFile(file, trigCopy);
                    cnt++;
                    break;
                }
            }
            dataset = null;
        }
    }