Java Code Examples for org.apache.jena.rdf.model.StmtIterator#nextStatement()

The following examples show how to use org.apache.jena.rdf.model.StmtIterator#nextStatement() . 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: AdditionalPropertyMatcherImpl.java    From FCA-Map with GNU General Public License v3.0 6 votes vote down vote up
private <T extends Resource> void addContext(Set<T> properties,
                                             int from_id,
                                             Map<Resource, Set<MappingCell>> m,
                                             Map<ResourceWrapper<T>, Set<SubjectObject>> context) {
  for (final T p : properties) {

    StmtIterator it = p.getModel().listStatements(
          new SimpleSelector(null, null, (RDFNode) null) {
            @Override
            public boolean selects(Statement s) {
              return s.getPredicate().getURI().equals(p.getURI()) && s.getObject().isResource();
            }
          }
        );

    while (it.hasNext()) {
      Statement stmt = it.nextStatement();

      Resource subject = stmt.getSubject();
      Resource object  = stmt.getObject().asResource();

      ResourceWrapper<T> rw = new ResourceWrapper<T>(p, from_id);
      addContextFromSO(context, rw, m, subject, object);
    }
  }
}
 
Example 2
Source File: ReasoningOntology.java    From Heracles with GNU General Public License v3.0 6 votes vote down vote up
public HashSet<String> test(String literal){
		
		StmtIterator iter = ontology.listStatements(new SimpleSelector(null, ontology.getProperty("lex"),ontology.createLiteral(literal)));
		
		
		HashSet<String> ontoConcepts=new HashSet<String>();
//		System.out.println(literal);
		while (iter.hasNext()) {
			
			Statement stmt = iter.nextStatement();
			Resource  subject   = stmt.getSubject();     // get the subject
//			StmtIterator iter2 = ontology.listStatements(new SimpleSelector(
//					subject, 
//					ontology.getProperty("http://www.w3.org/2000/01/rdf-schema#subClassOf"),
//					ontology.getResource(NS+"#Mention")));
//			if (iter2.hasNext())
				ontoConcepts.add(subject.getURI());
			
//			System.out.println(subject.toString());
		}
		return ontoConcepts;
	}
 
Example 3
Source File: ReasoningOntology.java    From Heracles with GNU General Public License v3.0 6 votes vote down vote up
public HashMap<String, String> lexToURI(){
		StmtIterator iter = ontology.listStatements(new SimpleSelector(null, ontology.getProperty(NS+"#lex"),(Literal)null));
		
		
		HashMap<String, String> ontoConcepts=new HashMap<String,String>();
		
		while (iter.hasNext()) {
			
			Statement stmt = iter.nextStatement();
			Resource  subject   = stmt.getSubject();     // get the subject
			RDFNode lex = stmt.getObject();
			StmtIterator iter2 = ontology.listStatements(new SimpleSelector(
					subject, 
					ontology.getProperty("http://www.w3.org/2000/01/rdf-schema#subClassOf"),
					ontology.getResource(this.URI_Mention)));
			if (iter2.hasNext()){
				ontoConcepts.put(lex.toString(), subject.getURI());
			} else {
				System.out.println("No subclass of Mention: "+subject.toString());
				
			}
//			
		}
		return ontoConcepts;
	}
 
Example 4
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 5
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 6
Source File: DB2Closure.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
private static void closureNoTest(Resource r, Model closureBlob,
		Collection<Resource> visited, ClosureTest test, Collection<String> resources) {
	visited.add(r);
	String gid = ((DB2Graph) r.getModel().getGraph()).getGraphID();
	String key = null;
	if (r.isAnon()) {
		key = gid + ":" + r.getId();
	} else {
		key = gid + ":" + r.getURI();
	}
	if (resources.contains(key)) {
		return;
	}
	resources.add(key);

	StmtIterator sIter = r.listProperties();

	for (; sIter.hasNext();) {
		Statement stmt = sIter.nextStatement();
		closure(stmt, closureBlob, visited, test, resources);
	}
}
 
Example 7
Source File: ReasoningOntology.java    From Heracles with GNU General Public License v3.0 5 votes vote down vote up
public HashSet<String> getLexicalizedConcepts(String superclassURI, String annotationType, String lemma){
		Literal literal;
		if (lemma == null){
			literal = null;
		} else {
			literal = ontology.createLiteral(lemma);
		}
		StmtIterator iter = ontology.listStatements(new SimpleSelector(null, ontology.getProperty(annotationType),literal));
		
		
		HashSet<String> ontoConcepts=new HashSet<String>();
//		System.out.println(literal);
		while (iter.hasNext()) {
			
			Statement stmt = iter.nextStatement();
			Resource  subject   = stmt.getSubject();     // get the subject
			StmtIterator iter2 = ontology.listStatements(new SimpleSelector(
					subject, 
					ontology.getProperty("http://www.w3.org/2000/01/rdf-schema#subClassOf"),
//					ontology.getProperty("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
					ontology.getResource(superclassURI)));
			if (iter2.hasNext())
				ontoConcepts.add(subject.getURI());
			
//			System.out.println(subject.toString());
		}
		return ontoConcepts;
	}
 
Example 8
Source File: ReasoningOntology.java    From Heracles with GNU General Public License v3.0 5 votes vote down vote up
public HashSet<String> getObjects(String subjectURI, String predicateURI){
	StmtIterator iter = ontology.listStatements(new SimpleSelector(ontology.getResource(subjectURI), ontology.getProperty(predicateURI),(RDFNode)null));
	HashSet<String> targetTypes = new HashSet<String>();
	while (iter.hasNext()) {
		Statement stmt      = iter.nextStatement();  // get next statement
		RDFNode object    = stmt.getObject();      // get the object
		if (object.isResource()){
			targetTypes.add(object.asResource().getURI());
		} else if (object.isLiteral()){
			targetTypes.add(object.asLiteral().getLexicalForm());
		}
	}
	targetTypes.remove(null);
	return targetTypes;
}
 
Example 9
Source File: ReasoningOntology.java    From Heracles with GNU General Public License v3.0 5 votes vote down vote up
public HashSet<String> getSubjects(String objectURI, String predicateURI){
	StmtIterator iter = ontology.listStatements(new SimpleSelector(null,ontology.getProperty(predicateURI),ontology.getResource(objectURI)));
	HashSet<String> targetTypes = new HashSet<String>();
	while (iter.hasNext()) {
		Statement stmt      = iter.nextStatement();  // get next statement
		Resource subject = stmt.getSubject();
		targetTypes.add(subject.asResource().getURI());
	}
	targetTypes.remove(null);
	return targetTypes;
}
 
Example 10
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;

	}