All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.uniroma2.art.lime.profiler.impl.ResourceLocationUtils Maven / Gradle / Ivy

There is a newer version: 0.4.7
Show newest version
package it.uniroma2.art.lime.profiler.impl;

import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.rdf4j.common.iteration.Iterations;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.vocabulary.VOID;
import org.eclipse.rdf4j.queryrender.RenderUtils;
import org.eclipse.rdf4j.rio.ntriples.NTriplesUtil;

import it.uniroma2.art.lime.model.repo.LIMERepositoryConnectionWrapper;
import it.uniroma2.art.lime.profiler.ProfilerContext;
import it.uniroma2.art.lime.profiler.ProfilerOptions;

public abstract class ResourceLocationUtils {
	public static Map retrieveDatasetsByUriSpace(ProfilerOptions options,
			LIMERepositoryConnectionWrapper metadataConnection) {

		return Iterations
				.stream(metadataConnection.getStatements(null, VOID.URI_SPACE, null,
						options.isIncludeInferred(), options.getContexts()))
				.collect(Collectors.toMap(s -> s.getObject().stringValue(), s -> s.getSubject(),
						(v1, v2) -> v1));
	}

	public static void appendUriSpaceLogic(ProfilerOptions options, IRI dataGraph, StringBuilder sb,
			LIMERepositoryConnectionWrapper metadataConnection, String varPrefix, String referenceVar) {
		Map datasetsByUriSpace = retrieveDatasetsByUriSpace(options, metadataConnection);

		if (!datasetsByUriSpace.isEmpty() || !options.isDefaultToLocalReference()) {
			sb.append(
				// @formatter:off
				" OPTIONAL {                                                               \n" +
				" 	VALUES(?" + varPrefix + "referenceDataset ?" + varPrefix + "referenceDatasetUriSpaceT) { \n"
				// @formatter:on
			);

			for (Map.Entry entry : datasetsByUriSpace.entrySet()) {
				sb.append("(").append(NTriplesUtil.toNTriplesString(entry.getValue())).append(" ")
						.append(NTriplesUtil.toNTriplesString(
								SimpleValueFactory.getInstance().createLiteral(entry.getKey())))
						.append(")");
			}

			sb.append(
				// @formatter:off
				" 	}                                                                                                             \n" +
				"   FILTER(STRSTARTS(STR(?" + referenceVar + "), ?" + varPrefix + "referenceDatasetUriSpaceT))                    \n" +
				"  }                                                                                                              \n" +
				"  bind(IF(BOUND(?" + varPrefix + "referenceDatasetUriSpaceT), ?" + varPrefix +"referenceDatasetUriSpaceT,        \n" +
				"  			IF(not exists {graph "+ RenderUtils.toSPARQL(dataGraph)+" {?" + referenceVar + " a []}},              \n" +
				"  				REPLACE(STR(?" + referenceVar + "), \"(.+(#|\\\\/)).*\", \"$1\"),                                 \n" +
				"  					?unboundVariable)) as ?" + varPrefix + "referenceDatasetUriSpace)                             \n"
				// @formatter:on
			);
		}
	}

	public static Resource getDatasetOrMintNew(ProfilerContext profilerContext, Map additionalDatasets,
			Resource mainDataset, IRI resourceDataset, String uriSpace) {
		if (resourceDataset != null) {
			return resourceDataset;
		} else {
			if (uriSpace == null) {
				return mainDataset;
			} else {
				Resource newDataset = additionalDatasets.get(uriSpace);

				if (newDataset == null) {
					newDataset = profilerContext.mintDatasetResource();
					additionalDatasets.put(uriSpace, newDataset);
				}

				return newDataset;
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy