org.apache.jena.rdf.model.ResIterator Java Examples

The following examples show how to use org.apache.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: ModelWrapper.java    From FCA-Map with GNU General Public License v3.0 6 votes vote down vote up
private void acquireInstances() {
  // XXX: remove instances of DBkWik:Image and SKOS:Concept, Template. Under consideration.
  ResIterator it = null;

  if (m_inferred) {
    it = m_inferred_model.listSubjects();
  } else {
    it = m_raw_model.listSubjects();
  }

  while (it.hasNext()) {
    Resource r = it.nextResource();
    if (DBkWik.ownAsResource(r) && !DBkWik.ownAsTemplate(r) &&
        !r.hasProperty(RDF.type, SKOS.Concept) &&
        !r.hasProperty(RDF.type, DBkWik.Image)) {
      m_instances.add(r);
    }
  }
}
 
Example #2
Source File: ClassHierarchyLoader.java    From gerbil with GNU Affero General Public License v3.0 6 votes vote down vote up
protected Set<Resource> getClasses(Model readModel) {
    ResIterator iterator = readModel.listSubjectsWithProperty(RDF.type, RDFS.Class);
    Resource r;
    Set<Resource> classes = new HashSet<Resource>();
    while (iterator.hasNext()) {
        r = iterator.next();
        if (!r.isAnon()) {
            classes.add(r);
        }
    }
    iterator = readModel.listSubjectsWithProperty(RDF.type, OWL.Class);
    while (iterator.hasNext()) {
        r = iterator.next();
        if (!r.isAnon()) {
            classes.add(r);
        }
    }
    return classes;
}
 
Example #3
Source File: SkolemizingModelProvider.java    From Processor with Apache License 2.0 6 votes vote down vote up
@Override
public Model process(Model model)
{
    if (getRequest().getMethod().equalsIgnoreCase("POST"))
    {
        ResIterator it = model.listSubjects();
        try
        {
            while (it.hasNext())
            {
                Resource resource = it.next();
                process(resource);
            }
        }
        finally
        {
            it.close();
        }
    
        return skolemize(getOntology(), getUriInfo().getBaseUriBuilder(), getUriInfo().getAbsolutePathBuilder(),
                super.process(model));
    }
    
    return super.process(model);
}
 
Example #4
Source File: SkolemizingDatasetProvider.java    From Processor with Apache License 2.0 6 votes vote down vote up
public Model process(Model model)
    {
//        if (getRequest().getMethod().equalsIgnoreCase("POST"))
//        {
            ResIterator it = model.listSubjects();
            try
            {
                while (it.hasNext())
                {
                    Resource resource = it.next();
                    process(resource);
                }
            }
            finally
            {
                it.close();
            }
        
            return skolemize(getOntology(), getUriInfo().getBaseUriBuilder(), getUriInfo().getAbsolutePathBuilder(), model);
//        }
//        
//        return model;
    }
 
Example #5
Source File: SolidContactsExport.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
private List<VCard> parseAddressBook(Resource selfResource, SolidUtilities utilities)
    throws IOException {

  String peopleUri = selfResource.getProperty(NAME_EMAIL_INDEX_PROPERTY).getResource().getURI();
  Model peopleModel = utilities.getModel(peopleUri);
  List<VCard> vcards = new ArrayList<>();
  ResIterator subjects = peopleModel.listSubjects();
  while (subjects.hasNext()) {
    Resource subject = subjects.nextResource();
    Model personModel = utilities.getModel(subject.getURI());
    Resource personResource = SolidUtilities.getResource(subject.getURI(), personModel);
    if (personResource == null) {
      throw new IllegalStateException(subject.getURI() + " not found in " + subject.toString());
    }
    vcards.add(parsePerson(personResource));
  }
  return vcards;
}
 
Example #6
Source File: JenaBasedReader.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
public RmlMappingDocument fromRdf(Model data, Function<RdfResource, Optional<DataSource>> dataSourceFactory) {
  ResIterator tripleMaps = data.listSubjectsWithProperty(data.createProperty(NS_RR + "subjectMap"));
  MappingDocumentBuilder resultBuilder = rmlMappingDocument();
  try {
    while (tripleMaps.hasNext()) {
      Resource resource = tripleMaps.nextResource();
      buildTripleMap(JenaResource.fromModel(data, resource), resultBuilder.withTripleMap(resource.getURI()));
    }
  } finally {
    tripleMaps.close();
  }
  return resultBuilder.build(dataSourceFactory);
}
 
Example #7
Source File: ModelWrapper.java    From FCA-Map with GNU General Public License v3.0 5 votes vote down vote up
private Set<Resource> acquireResources(ResIterator it) {
  Set<Resource> resources = new HashSet<>();
  while (it.hasNext()) {
    Resource r = it.nextResource();
    resources.add(r);
  }
  return resources;
}
 
Example #8
Source File: VerifyEarlReporter.java    From teamengine with Apache License 2.0 5 votes vote down vote up
@Test
public void initializeModel() {
    String testSubject = "http://www.example.org/test/subject";
    Map<String, String> params = new HashMap<String, String>();
    params.put("iut", testSubject);
    params.put("uuid", UUID.randomUUID().toString());
    when(xmlSuite.getAllParameters()).thenReturn(params);
    EarlReporter iut = new EarlReporter();
    Model model = iut.initializeModel(suite);
    assertNotNull(model);
    ResIterator itr = model.listSubjectsWithProperty(RDF.type, EARL.TestSubject);
    assertEquals("Unexpected URI for earl:TestSubject", testSubject, itr.next().getURI());
}
 
Example #9
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private void doConversion(Model model) throws JenaException {
  StatementHandler totm = new ToTMStatementHandler();
  AResourceWrapper subjw = new AResourceWrapper();
  AResourceWrapper propw = new AResourceWrapper();
  AResourceWrapper objtw = new AResourceWrapper();
  ALiteralWrapper litlw = new ALiteralWrapper();

  ResIterator it = model.listSubjects();
  while (it.hasNext()) {
    Resource subject = (Resource) it.next();

    StmtIterator it2 = subject.listProperties(); // get all statements
    while (it2.hasNext()) {
      Statement stmt = (Statement) it2.next();

      subjw.resource = stmt.getSubject();
      propw.resource = stmt.getPredicate();

      RDFNode obj = stmt.getObject();
      if (obj instanceof Resource) {
        objtw.resource = (Resource) obj;
        totm.statement(subjw, propw, objtw);
      } else {
        litlw.literal = (Literal) obj;
        totm.statement(subjw, propw, litlw);
      }
    }
  }
}
 
Example #10
Source File: ConstraintViolationExceptionMapper.java    From Processor with Apache License 2.0 5 votes vote down vote up
@Override
public Response toResponse(ConstraintViolationException ex)
{
    Resource exception = toResource(ex, Response.Status.BAD_REQUEST,
        ResourceFactory.createResource("http://www.w3.org/2011/http-statusCodes#BadRequest"));
    ex.getModel().add(exception.getModel());
    
    SPINConstraints.addConstraintViolationsRDF(ex.getConstraintViolations(), ex.getModel(), true);
    ResIterator it = ex.getModel().listSubjectsWithProperty(RDF.type, SPIN.ConstraintViolation);
    try
    {
        while (it.hasNext())
        {
            Resource violation = it.next();
            // connect Response to ConstraintViolations
            ex.getModel().add(exception, ResourceFactory.createProperty("http://www.w3.org/ns/prov#wasDerivedFrom"), violation);
        }
    }
    finally
    {
        it.close();
    }
    
    return getResponseBuilder(DatasetFactory.create(ex.getModel())).
            status(Response.Status.BAD_REQUEST).
            build();
}
 
Example #11
Source File: Skolemizer.java    From Processor with Apache License 2.0 5 votes vote down vote up
public Model build(Model model)
{
    if (model == null) throw new IllegalArgumentException("Model cannot be null");

    Map<Resource, String> resourceURIMap = new HashMap<>();
    ResIterator resIt = model.listSubjects();
    try
    {
        while (resIt.hasNext())
        {
            Resource resource = resIt.next();
            if (resource.isAnon())
            {
                URI uri = build(resource);
                if (uri != null) resourceURIMap.put(resource, uri.toString());
            }
        }
    }
    finally
    {
        resIt.close();
    }
    
    Iterator<Map.Entry<Resource, String>> entryIt = resourceURIMap.entrySet().iterator();
    while (entryIt.hasNext())
    {
        Map.Entry<Resource, String> entry = entryIt.next();
        ResourceUtils.renameResource(entry.getKey(), entry.getValue());
    }

    return model;
}
 
Example #12
Source File: ModelContainer.java    From hypergraphql with Apache License 2.0 5 votes vote down vote up
List<RDFNode> getSubjectsOfObjectProperty(String predicateURI, String objectURI) {

        ResIterator iterator = this.model.listSubjectsWithProperty(getPropertyFromUri(predicateURI), getResourceFromUri(objectURI));
        List<RDFNode> nodeList = new ArrayList<>();
        iterator.forEachRemaining(nodeList::add);
        return nodeList;
    }
 
Example #13
Source File: Service.java    From hypergraphql with Apache License 2.0 5 votes vote down vote up
private Set<String> findRootIdentifiers(Model model, TypeConfig targetName) {

        Set<String> identifiers = new HashSet<>();
        Model currentmodel = ModelFactory.createDefaultModel();
        Resource res = currentmodel.createResource(targetName.getId());
        Property property = currentmodel.createProperty(RDF_TYPE);

        ResIterator iterator = model.listResourcesWithProperty(property, res);

        while (iterator.hasNext()) {
            identifiers.add(iterator.nextResource().toString());
        }
        return identifiers;
    }
 
Example #14
Source File: ModelContainer.java    From hypergraphql with Apache License 2.0 5 votes vote down vote up
List<RDFNode> getSubjectsOfObjectProperty(String predicateURI, String objectURI) {

        ResIterator iterator = this.model.listSubjectsWithProperty(getPropertyFromUri(predicateURI), getResourceFromUri(objectURI));
        List<RDFNode> nodeList = new ArrayList<>();
        iterator.forEachRemaining(nodeList::add);
        return nodeList;
    }
 
Example #15
Source File: Service.java    From hypergraphql with Apache License 2.0 5 votes vote down vote up
private Set<String> findRootIdentifiers(Model model, TypeConfig targetName) {

        Set<String> identifiers = new HashSet<>();
        Model currentmodel = ModelFactory.createDefaultModel();
        Resource res = currentmodel.createResource(targetName.getId());
        Property property = currentmodel.createProperty(RDF_TYPE);

        ResIterator iterator = model.listResourcesWithProperty(property, res);

        while (iterator.hasNext()) {
            identifiers.add(iterator.nextResource().toString());
        }
        return identifiers;
    }
 
Example #16
Source File: OntModelWrapper.java    From FCA-Map with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Acquire the categories
 */
private void acquireCategories() {
  for (ResIterator it = m_ontology.listResourcesWithProperty(RDF.type, SKOS.Concept); it.hasNext(); ) {
    Resource c = it.nextResource();
    m_categories.add(c);
  }
}
 
Example #17
Source File: ModelStorage.java    From FCA-Map with GNU General Public License v3.0 5 votes vote down vote up
private void acquireCategories() {
  for (ResIterator it = ontModel.listResourcesWithProperty(RDF.type, SKOS.Concept); it.hasNext(); ) {
    Resource r = it.nextResource();
    if (isIgnoredCategory(r)) continue;
    categories.add(r);
  }
}
 
Example #18
Source File: ModelWrapper.java    From FCA-Map with GNU General Public License v3.0 5 votes vote down vote up
private void acquireClasses() {
  ResIterator it = null;

  if (m_inferred) {
    it = m_inferred_model.listResourcesWithProperty(RDF.type, OWL.Class);
  } else {
    it = m_raw_model.listResourcesWithProperty(RDF.type, OWL.Class);
  }

  m_classes = acquireResources(it);
}
 
Example #19
Source File: ModelWrapper.java    From FCA-Map with GNU General Public License v3.0 5 votes vote down vote up
private void acquireProperties() {
  ResIterator it = null;

  if (m_inferred) {
    it = m_inferred_model.listResourcesWithProperty(RDF.type, RDF.Property);
  } else {
    it = m_raw_model.listResourcesWithProperty(RDF.type, RDF.Property);
  }

  m_properties = acquireResources(it);
}
 
Example #20
Source File: Skolemizer.java    From Processor with Apache License 2.0 4 votes vote down vote up
public SortedSet<ClassPrecedence> match(Ontology ontology, Resource resource, Property property, int level)
{
    if (ontology == null) throw new IllegalArgumentException("Ontology cannot be null");
    if (resource == null) throw new IllegalArgumentException("Resource cannot be null");
    if (property == null) throw new IllegalArgumentException("Property cannot be null");

    SortedSet<ClassPrecedence> matchedClasses = new TreeSet<>();
    ResIterator it = ontology.getOntModel().listResourcesWithProperty(LDT.path);
    try
    {
        while (it.hasNext())
        {
            Resource ontClassRes = it.next();
            OntClass ontClass = ontology.getOntModel().getOntResource(ontClassRes).asClass();
            // only match templates defined in this ontology - maybe reverse loops?
            if (ontClass.getIsDefinedBy() != null && ontClass.getIsDefinedBy().equals(ontology) &&
                    resource.hasProperty(property, ontClass))
            {
                ClassPrecedence precedence = new ClassPrecedence(ontClass, level * -1);
                if (log.isTraceEnabled()) log.trace("Resource {} matched OntClass {}", resource, ontClass);
                matchedClasses.add(precedence);
            } 
        }
    }
    finally
    {
        it.close();
    }

    ExtendedIterator<OntResource> imports = ontology.listImports();
    try
    {
        while (imports.hasNext())
        {
            OntResource importRes = imports.next();
            if (importRes.canAs(Ontology.class))
            {
                Ontology importedOntology = importRes.asOntology();
                // traverse imports recursively
                Set<ClassPrecedence> matchedImportClasses = match(importedOntology, resource, property, level + 1);
                matchedClasses.addAll(matchedImportClasses);
            }
        }
    }
    finally
    {
        imports.close();
    }
    
    return matchedClasses;
}
 
Example #21
Source File: WorkflowBundleParser.java    From incubator-taverna-language with Apache License 2.0 4 votes vote down vote up
/**
 * Workaround for TAVERNA-71 to find annotations in WorkflowBundle
 * <p>
 * FIXME: The annotation links should instead be stored in the 
 * {@link Manifest} using taverna-robundle - see TAVERNA-71
 * 
 * @param wb
 * @throws IOException
 */
private void parseAnnotations(final WorkflowBundle wb) throws IOException {
	if (! wb.getAnnotations().isEmpty()) {
		// Assume already parsed
		return;
	}
	URITools uriTools = new URITools();		
	for (ResourceEntry resource : wb.getResources().listResources("annotation").values()) {
		Lang lang = RDFLanguages.contentTypeToLang(resource.getMediaType());
		if (lang == null) {
			// Not a semantic annotation
			continue;
		}
		//System.out.println(resource.getPath());
		//System.out.println(resource.getMediaType());
		Annotation ann = new Annotation();
		// Hackish way to generate a name from the annotation filename
		// as these typically are UUIDs
		String name = resource.getPath().replace("annotation/", "").replaceAll("\\..*", ""); // strip extension
		ann.setName(name);
		ann.setParent(wb);
		
		String path = resource.getPath();
		ann.setBody(URI.create("/" + path));		
		URI base = wb.getGlobalBaseURI().resolve(path);
		Model model = ModelFactory.createDefaultModel();			
		InputStream inputStream = resource.getUcfPackage().getResourceAsInputStream(path);
		if (inputStream == null) {
			// Not found!
			continue;
		}
		RDFDataMgr.read(model, inputStream, 
				base.toASCIIString(), lang);
		ResIterator subjs = model.listSubjects();			
		while (subjs.hasNext()) { 
			Resource r = subjs.next();
			//System.out.println(r);
			WorkflowBean b = uriTools.resolveUri(URI.create(r.getURI()), wb);
			//System.out.println(b);
			if (b != null) {
				ann.setTarget(b);
			}
			break;
		}
	}
}