Java Code Examples for org.eclipse.rdf4j.model.IRI#getLocalName()

The following examples show how to use org.eclipse.rdf4j.model.IRI#getLocalName() . 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: TripleStoreRDF4J.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
private static String createStatements(RepositoryConnection cnx, String objNs, String objType,
    PropertyBag statement,
    Resource context) {
    IRI resource;
    if (objType.equals(rdfDescriptionClass())) {
        resource = cnx.getValueFactory().createIRI("urn:uuid:" + UUID.randomUUID().toString());
    } else {
        resource = cnx.getValueFactory().createIRI(cnx.getNamespace("data"),
            "_" + UUID.randomUUID().toString());
    }
    IRI parentPredicate = RDF.TYPE;
    IRI parentObject = cnx.getValueFactory().createIRI(objNs + objType);
    Statement parentSt = cnx.getValueFactory().createStatement(resource, parentPredicate, parentObject);
    cnx.add(parentSt, context);
    // add rest of statements for subject
    createStatements(cnx, objNs, objType, statement, context,  resource);
    return resource.getLocalName();
}
 
Example 2
Source File: PowsyblWriter.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
private void writeResource(Value obj) throws IOException {
    Resource objRes = (Resource) obj;

    if (objRes instanceof BNode) {
        BNode bNode = (BNode) objRes;
        writeAttribute(RDF.NAMESPACE, "nodeID", getValidNodeId(bNode));
    } else {
        IRI uri = (IRI) objRes;
        String value = uri.toString();
        String prefix = namespaceTable.get(uri.getNamespace());
        // FIXME review the use of hard-coded literal "data" for CGMES
        if (prefix != null && prefix.equals("data")) {
            value = "#" + uri.getLocalName();
        }
        writeAttribute(RDF.NAMESPACE, "resource", value);
    }

    writeEndOfEmptyTag();
}
 
Example 3
Source File: PeriodicQueryUtil.java    From rya with Apache License 2.0 6 votes vote down vote up
private static TimeUnit getTimeUnit(ValueConstant val) {
    Preconditions.checkArgument(val.getValue() instanceof IRI);
    IRI uri = (IRI) val.getValue();
    Preconditions.checkArgument(uri.getNamespace().equals(temporalNameSpace));

    switch (uri.getLocalName()) {
    case "days":
        return TimeUnit.DAYS;
    case "hours":
        return TimeUnit.HOURS;
    case "minutes":
        return TimeUnit.MINUTES;
    default:
        throw new IllegalArgumentException("Invalid time unit for Periodic Function.");
    }
}
 
Example 4
Source File: SmartUriAdapter.java    From rya with Apache License 2.0 6 votes vote down vote up
private static IRI createTypePropertiesUri(final ImmutableMap<RyaIRI, ImmutableMap<RyaIRI, Property>> typeProperties) throws SmartUriException {
    final List<NameValuePair> nameValuePairs = new ArrayList<>();
    for (final Entry<RyaIRI, ImmutableMap<RyaIRI, Property>> typeProperty : typeProperties.entrySet()) {
        final RyaIRI type = typeProperty.getKey();
        final Map<RyaIRI, Property> propertyMap = typeProperty.getValue();
        final IRI typeUri = createIndividualTypeWithPropertiesUri(type, propertyMap);
        final String keyString = type.getDataType().getLocalName();
        final String valueString = typeUri.getLocalName();
        nameValuePairs.add(new BasicNameValuePair(keyString, valueString));
    }

    final URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.addParameters(nameValuePairs);

    String uriString;
    try {
        final java.net.URI uri = uriBuilder.build();
        final String queryString = uri.getRawSchemeSpecificPart();
        uriString = "urn:test" + queryString;
    } catch (final URISyntaxException e) {
        throw new SmartUriException("Unable to create type properties for the Smart URI", e);
    }

    return VF.createIRI(uriString);
}
 
Example 5
Source File: SpinWellKnownVars.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public String getName(IRI IRI) {
	String name = uriToString.get(IRI);
	if (name == null && SPIN.NAMESPACE.equals(IRI.getNamespace()) && IRI.getLocalName().startsWith("_arg")) {
		String lname = IRI.getLocalName();
		try {
			Integer.parseInt(lname.substring("_arg".length()));
			name = lname.substring(1);
		} catch (NumberFormatException nfe) {
			// ignore - not a well-known argN variable
		}
	}
	return name;
}
 
Example 6
Source File: SpinWellKnownVars.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public String getName(IRI IRI) {
	String name = uriToString.get(IRI);
	if (name == null && SPIN.NAMESPACE.equals(IRI.getNamespace()) && IRI.getLocalName().startsWith("_arg")) {
		String lname = IRI.getLocalName();
		try {
			Integer.parseInt(lname.substring("_arg".length()));
			name = lname.substring(1);
		} catch (NumberFormatException nfe) {
			// ignore - not a well-known argN variable
		}
	}
	return name;
}
 
Example 7
Source File: MemURITest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void compareURIs(String uri) throws Exception {
	IRI uriImpl = SimpleValueFactory.getInstance().createIRI(uri);
	MemIRI memURI = new MemIRI(this, uriImpl.getNamespace(), uriImpl.getLocalName());

	assertEquals("MemURI not equal to URIImpl for: " + uri, uriImpl, memURI);
	assertEquals("MemURI has different hash code than URIImpl for: " + uri, uriImpl.hashCode(), memURI.hashCode());
}
 
Example 8
Source File: PowsyblWriter.java    From powsybl-core with Mozilla Public License 2.0 4 votes vote down vote up
private void writeNewSubject(Resource subj, Value obj, String ctxt) throws IOException {
    flushPendingStatements();

    String objString = obj.toString();
    int objSplitIdx = XMLUtil.findURISplitIndex(objString);
    if (objSplitIdx == -1) {
        throw new RDFHandlerException("Unable to create XML namespace-qualified name for predicate: " + objString);
    }
    String objNamespace = objString.substring(0, objSplitIdx);
    String objLocalName = objString.substring(objSplitIdx);

    // Write new subject:
    writeNewLine();
    writeStartOfStartTag(objNamespace, objLocalName);

    // FIXME This is hard-coded logic for processing CGMES data
    IRI uri = (IRI) subj;
    String attName = "ID";
    String value = uri.toString();
    String prefix = namespaceTable.get(uri.getNamespace());
    if (uri.getNamespace().equals("urn:uuid:")) {
        if (objLocalName.equals("FullModel")) {
            attName = "about";
        } else {
            value = "_" + uri.getLocalName();
        }
    }
    if (prefix != null && prefix.equals("data")) {
        if (ctxt.contains("_SSH_") || (ctxt.contains("_DY_") && objLocalName.equals("EnergyConsumer"))
                || (ctxt.contains("_TP_") && !objLocalName.equals("TopologicalNode"))) {
            attName = "about";
            value = "#" + uri.getLocalName();
        } else {
            value = uri.getLocalName();
        }
    }

    writeAttribute(RDF.NAMESPACE, attName, value);
    writeEndOfStartTag();
    writeNewLine();
    lastWrittenSubject = subj;
    lastObjNamespace = objNamespace;
    lastObjLocalName = objLocalName;
}
 
Example 9
Source File: PropertyRegister.java    From Wikidata-Toolkit with Apache License 2.0 4 votes vote down vote up
/**
 * Fetches type information for all known properties from the given SPARQL endpoint, and adds it to the register.
 * The SPARQL endpoint must support the wikibase:propertyType predicate.
 *
 * @param endpoint URI of the SPARQL service to use, for example "https://query.wikidata.org/sparql"
 */
public void fetchUsingSPARQL(URI endpoint) {
	try {
		// this query is written without assuming any PREFIXES like wd: or wdt: to ensure it is as portable
		// as possible (the PropertyRegister might be used with private Wikibase instances and SPARQL endpoints
		// that don't have the same PREFIXES defined as the Wikidata Query Service)
		final String query = "SELECT ?prop ?type ?uri WHERE { " +
				"<" + this.siteUri + this.uriPatternPropertyId + "> <http://wikiba.se/ontology#directClaim> ?uriDirect . " +
				"?prop <http://wikiba.se/ontology#propertyType> ?type . " +
				"OPTIONAL { ?prop ?uriDirect ?uri } " +
				"}";
		final String queryString = "query=" + query + "&format=json";
		final URL queryUrl = new URI(
				endpoint.getScheme(), endpoint.getUserInfo(), endpoint.getHost(), endpoint.getPort(),
				endpoint.getPath(), queryString, null
		).toURL();

		final HttpURLConnection connection = (HttpURLConnection) queryUrl.openConnection();
		connection.setRequestMethod("GET");
		connection.setRequestProperty("User-Agent", "Wikidata-Toolkit PropertyRegister");

		final ObjectMapper mapper = new ObjectMapper();
		JsonNode root = mapper.readTree(connection.getInputStream());
		JsonNode bindings = root.path("results").path("bindings");

		final ValueFactory valueFactory = SimpleValueFactory.getInstance();
		int count = 0;
		int countPatterns = 0;
		for (JsonNode binding : bindings) {
			final IRI property = valueFactory.createIRI(binding.path("prop").path("value").asText());
			final IRI propType = valueFactory.createIRI(binding.path("type").path("value").asText());

			final PropertyIdValue propId = new PropertyIdValueImpl(property.getLocalName(), this.siteUri);
			setPropertyType(propId, propType.toString());
			count += 1;

			if (binding.has("uri")) {
				countPatterns += 1;
				this.uriPatterns.put(propId.getId(), binding.path("uri").path("value").asText());
			}
		}

		logger.info("Fetched type information for " + count + " properties (" +
				countPatterns + " with URI patterns) using SPARQL.");
	} catch(IOException|URISyntaxException e) {
		logger.error("Error when trying to fetch property data using SPARQL: "
				+ e.toString());
	}
}