Java Code Examples for org.apache.jena.rdf.model.Resource#getURI()

The following examples show how to use org.apache.jena.rdf.model.Resource#getURI() . 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: DB2DescribeHandler.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public void describe(Resource r) {
	// Default model.
	DB2Closure.closure(otherModel(r, dataset.getDefaultModel()), false, acc, resources);

	String query = "SELECT ?g { GRAPH ?g { <" + r.getURI() + "> ?p ?o } }";
	QueryExecution qExec = RdfStoreQueryExecutionFactory.create(query,
			dataset);

	ResultSet rs = qExec.execSelect();
	for (; rs.hasNext();) {
		QuerySolution qs = rs.next();
		String gName = qs.getResource("g").getURI(); // mdb for DB2
		Model model = dataset.getNamedModel(gName);
		Resource r2 = otherModel(r, model);
		DB2Closure.closure(r2, false, acc, resources);
	}

	qExec.close();
	
	DB2Closure.closure(r, false, acc, resources);
}
 
Example 2
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 3
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 4
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 6 votes vote down vote up
private TopicIF getType(RDFNode rdfprop, Model model)
  throws JenaException, MalformedURLException {

  Resource subject = (Resource) rdfprop;
  Property prop = model.getProperty(RTM_TYPE);
  NodeIterator it = model.listObjectsOfProperty(subject, prop);
  while (it.hasNext()) {
    Resource obj = (Resource) it.next();
    LocatorIF loc = new URILocator(obj.getURI());
    TopicIF topic = topicmap.getTopicBySubjectIdentifier(loc);
    if (topic == null) {
      topic = builder.makeTopic();
      topic.addSubjectIdentifier(loc);
    }
    return topic;
  }
  return null;
}
 
Example 5
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 6 votes vote down vote up
/**
 * Finds all RTM_IN_SCOPE properties for this property and returns a
 * collection containing the RDF URIs of the values as URILocators.
 */
private Collection getScope(RDFNode rdfprop, Model model)
  throws JenaException, MalformedURLException {

  Resource subject = (Resource) rdfprop;
  Property prop = model.getProperty(RTM_IN_SCOPE);
  NodeIterator it = model.listObjectsOfProperty(subject, prop);
  ArrayList scope = new ArrayList();

  while (it.hasNext()) {
    Object o = it.next();

    if (!(o instanceof Resource))
      throw new RDFMappingException("Scoping topic must be specified by a resource, not by " + o);

    Resource obj = (Resource) o;
    LocatorIF loc = new URILocator(obj.getURI());
    scope.add(loc);
  }

  return scope;
}
 
Example 6
Source File: JenaUtil.java    From shacl with Apache License 2.0 6 votes vote down vote up
private static Node invokeFunction(Resource function, ExprList args, Dataset dataset) {

		if (dataset == null) {
	        dataset = ARQFactory.get().getDataset(ModelFactory.createDefaultModel());
	    }
		
		E_Function expr = new E_Function(function.getURI(), args);
		DatasetGraph dsg = dataset.asDatasetGraph();
		Context cxt = ARQ.getContext().copy();
		cxt.set(ARQConstants.sysCurrentTime, NodeFactoryExtra.nowAsDateTime());
		FunctionEnv env = new ExecutionContext(cxt, dsg.getDefaultGraph(), dsg, null);
		try {
			NodeValue r = expr.eval(BindingRoot.create(), env);
			if(r != null) {
				return r.asNode();
			}
		}
		catch(ExprEvalException ex) {
		}
		return null;
	}
 
Example 7
Source File: SHACLCWriter.java    From shacl with Apache License 2.0 5 votes vote down vote up
private String iri(Resource resource) {
	String qname = resource.getModel().qnameFor(resource.getURI());
	if(qname != null) {
		return qname;
	}
	else {
		return "<" + resource.getURI() + ">";
	}
}
 
Example 8
Source File: SimpleSubClassInferencer.java    From gerbil with GNU Affero General Public License v3.0 5 votes vote down vote up
private void addOrUpdateUri(Resource resource, ClassSet hierarchy, ClassNodeFactory<? extends ClassNode> factory,
        Set<String> alreadySeenUris) {
    String uri = resource.getURI();
    ClassNode node = hierarchy.getNode(uri);
    if (node == null) {
        node = factory.createNode(uri);
        hierarchy.addNode(node);
    } else {
        factory.updateNode(node);
    }
    alreadySeenUris.add(uri);

    StmtIterator iterator = classModel.listStatements(resource, OWL.sameAs, (RDFNode) null);
    Statement stmt;
    while (iterator.hasNext()) {
        stmt = iterator.next();
        uri = stmt.getObject().asResource().getURI();
        hierarchy.addUriToNode(node, uri);
        alreadySeenUris.add(uri);
    }
    iterator = classModel.listStatements(resource, OWL.equivalentClass, (RDFNode) null);
    while (iterator.hasNext()) {
        stmt = iterator.next();
        uri = stmt.getObject().asResource().getURI();
        hierarchy.addUriToNode(node, uri);
        alreadySeenUris.add(uri);
    }
}
 
Example 9
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private LocatorIF getTopicIndicator(Resource subject, String property,
                                    Model model)
  throws JenaException, MalformedURLException {
  Property prop = model.getProperty(property);
  NodeIterator it = model.listObjectsOfProperty(subject, prop);
  while (it.hasNext()) {
    Resource obj = (Resource) it.next();
    if (obj.isAnon())
      continue; // FIXME: is this really ok?
    return new URILocator(obj.getURI());
  }
  return null;
}
 
Example 10
Source File: SPDXChecksum.java    From tools with Apache License 2.0 5 votes vote down vote up
public static String algorithmResourceToString(Resource algorithmResource) throws InvalidSPDXAnalysisException {
	String uri = algorithmResource.getURI();
	if (!algorithmResource.isURIResource()) {
		throw(new InvalidSPDXAnalysisException("Algorithm resource must be a URI"));
	}
	String retval = URI_TO_ALGORITHM.get(uri);
	if (retval == null) {
		throw(new InvalidSPDXAnalysisException("Invalid algorithm resource."));
	}
	return retval;
}
 
Example 11
Source File: SPDXFile.java    From tools with Apache License 2.0 5 votes vote down vote up
public static String fileTypeResourceToString(Resource fileTypeResource) throws InvalidSPDXAnalysisException {
	if (!fileTypeResource.isURIResource()) {
		throw(new InvalidSPDXAnalysisException("File type resource must be a URI."));
	}
	String retval = fileTypeResource.getURI();
	if (retval == null) {
		throw(new InvalidSPDXAnalysisException("Not a recognized file type for an SPDX document."));
	}
	return retval;
}
 
Example 12
Source File: TestManifest.java    From incubator-taverna-language with Apache License 2.0 5 votes vote down vote up
private URI asURI(Resource proxy) {
	if (proxy == null) {
		return null;
	}
	String uri = proxy.getURI();
	if (uri == null) {
		return null;
	}
	return bundle.getRoot().toUri().resolve(uri);
}
 
Example 13
Source File: TestCase.java    From shacl with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(TestCase other) {
	Resource env1 = getResource().getPropertyResourceValue(DASH.testEnvironment);
	Resource env2 = other.getResource().getPropertyResourceValue(DASH.testEnvironment);
	if(env1 != null) {
		String uri1 = env1.getURI();
		if(env2 != null) {
			String uri2 = env2.getURI();
			int c = uri1.compareTo(uri2);
			if(c != 0) {
				return c;
			}
			else {
				Integer m1 = getResource().hasProperty(DASH.testModifiesEnvironment, JenaDatatypes.TRUE) ? 1 : 0;
				Integer m2 = other.getResource().hasProperty(DASH.testModifiesEnvironment, JenaDatatypes.TRUE) ? 1 : 0;
				int m = m1.compareTo(m2);
				if(m != 0) {
					return m;
				}
			}
		}
		else {
			return 1;
		}
	}
	else if(env2 != null) {
		return -1;
	}
	return getResource().getURI().compareTo(other.getResource().getURI());
}
 
Example 14
Source File: RDFLabels.java    From shacl with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the label for a given Resource.
 * @param resource  the Resource to get the label of
 * @return the label (never null)
 */
public String getLabel(Resource resource) {
	if(resource.isURIResource() && resource.getModel() != null) {
		String qname = resource.getModel().qnameFor(resource.getURI());
		if(qname != null) {
			return qname;
		}
		else {
			return "<" + resource.getURI() + ">";
		}
	}
	else if(resource.isAnon() && resource.getModel() != null && resource.hasProperty(RDF.first)) {
		StringBuffer buffer = new StringBuffer("[");
		Iterator<RDFNode> members = resource.as(RDFList.class).iterator();
		while(members.hasNext()) {
			RDFNode member = members.next();
			buffer.append(RDFLabels.get().getNodeLabel(member));
			if(members.hasNext()) {
				buffer.append(", ");
			}
		}
		buffer.append("]");
		return buffer.toString();
	}
	else if(resource.isAnon()) {
		return getBlankNodeLabel(resource);
	}
	else {
		return resource.toString();
	}
}
 
Example 15
Source File: RdfDataManager.java    From rdf2neo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Similarly to {@link #getCyNode(Resource, String, String)}, uses a binding (i.e., row) from a 
 * {@link CyRelationLoadingHandler#getRelationTypesSparql() relation type query} and creates a new {@link CyRelation}
 * with the RDF mapped data.
 */
public CyRelation getCyRelation ( QuerySolution relRow )
{		
	Resource relRes = relRow.get ( "iri" ).asResource ();
	CyRelation cyRelation = new CyRelation ( relRes.getURI () );
	
	cyRelation.setType ( this.getCypherId ( relRow.get ( "type" ), this.getCyRelationTypeIdConverter () ) );

	cyRelation.setFromIri ( relRow.get ( "fromIri" ).asResource ().getURI () );
	cyRelation.setToIri ( relRow.get ( "toIri" ).asResource ().getURI () );
			
	return cyRelation;
}
 
Example 16
Source File: RdfDataManager.java    From rdf2neo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Uses the underlining TDB and mapping queries to create a new {@link CyNode} instance.
 * 
 * @param nodeRes the RDF/Jena resource correspnding to the Cypher node. This provides the ?iri paramter in the queries below.
 * @param labelsSparql the node labels query, which is usually taken from {@link CyNodeLoadingHandler#getLabelsSparql()}.
 * @param propsSparql the node properties query, which is usually taken from {@link CyNodeLoadingHandler#getNodePropsSparql()}.
 */
public CyNode getCyNode ( Resource nodeRes, String labelsSparql, String propsSparql )
{
	ensureOpen ();
	
	QuerySolutionMap params = new QuerySolutionMap ();
	params.add ( "iri", nodeRes );

	CyNode cyNode = new CyNode ( nodeRes.getURI () );
	
	// The node's labels
	if ( labelsSparql != null )
	{
		// If it's omitted, it will get the default label.
		Query qry = SparqlUtils.getCachedQuery ( labelsSparql );
		Function<String, String> labelIdConverter = this.getCyNodeLabelIdConverter ();
		
		boolean wasInTnx = dataSet.isInTransaction ();
		if ( !wasInTnx ) dataSet.begin ( ReadWrite.READ );
		try {
			QueryExecution qx = QueryExecutionFactory.create ( qry, this.getDataSet(), params );
			qx.execSelect ().forEachRemaining ( row ->
				cyNode.addLabel ( this.getCypherId ( row.get ( "label" ), labelIdConverter ) )
			);
		}
		finally {
			if ( !wasInTnx && dataSet.isInTransaction () ) dataSet.end ();
		}
	}
	
	// and the properties
	this.addCypherProps ( cyNode, propsSparql );
	
	return cyNode;
}
 
Example 17
Source File: JenaResource.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Optional<String> asIri() {
  if (this.rdfNode.isResource()) {
    Resource resource = rdfNode.asResource();
    String uri = resource.getURI();
    return uri == null ? Optional.of(resource.getId().getLabelString()) : Optional.of(uri);
  } else {
    return Optional.empty();
  }
}
 
Example 18
Source File: DBkWik.java    From FCA-Map with GNU General Public License v3.0 4 votes vote down vote up
public static final boolean ownAsResource(Resource r) {
  String that_uri = r.getURI();
  return getType(that_uri).equals("resource");
}
 
Example 19
Source File: MappingCell.java    From FCA-Map with GNU General Public License v3.0 4 votes vote down vote up
public MappingCell(Resource resource1, Resource resource2, String relation_text, String confidence_text) {
  this(resource1.getURI(), resource2.getURI(), relation_text, confidence_text);
  m_resource1 = resource1;
  m_resource2 = resource2;
}
 
Example 20
Source File: DBkWik.java    From FCA-Map with GNU General Public License v3.0 4 votes vote down vote up
public static final boolean ownAsTemplate(Resource r) {
  String that_uri = r.getURI();
  return that_uri.startsWith(uri) && that_uri.contains("/resource/Template:");
}