|
| 1 | +package io.github.linkedfactory.core.komma; |
| 2 | + |
| 3 | +import io.github.linkedfactory.core.kvin.DelegatingKvin; |
| 4 | +import io.github.linkedfactory.core.kvin.Kvin; |
| 5 | +import io.github.linkedfactory.core.rdf4j.kvin.KvinSail; |
| 6 | +import net.enilink.composition.annotations.Iri; |
| 7 | +import net.enilink.komma.core.URI; |
| 8 | +import net.enilink.komma.core.URIs; |
| 9 | +import net.enilink.komma.model.MODELS; |
| 10 | +import net.enilink.komma.model.rdf4j.PersistentModelSetSupport; |
| 11 | +import org.eclipse.core.runtime.FileLocator; |
| 12 | +import org.eclipse.rdf4j.repository.Repository; |
| 13 | +import org.eclipse.rdf4j.repository.RepositoryException; |
| 14 | +import org.eclipse.rdf4j.repository.sail.SailRepository; |
| 15 | +import org.eclipse.rdf4j.sail.NotifyingSail; |
| 16 | +import org.eclipse.rdf4j.sail.Sail; |
| 17 | +import org.eclipse.rdf4j.sail.inferencer.fc.SchemaCachingRDFSInferencer; |
| 18 | +import org.eclipse.rdf4j.sail.nativerdf.NativeStore; |
| 19 | +import org.osgi.framework.BundleContext; |
| 20 | +import org.osgi.framework.FrameworkUtil; |
| 21 | +import org.osgi.framework.ServiceReference; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.net.URL; |
| 25 | +import java.util.function.Supplier; |
| 26 | + |
| 27 | +@Iri(MODELS.NAMESPACE + "KvinPersistentModelSet") |
| 28 | +public abstract class KvinPersistentModelSet extends PersistentModelSetSupport { |
| 29 | + static BundleContext bundleContext = FrameworkUtil.getBundle(KvinPersistentModelSet.class).getBundleContext(); |
| 30 | + static Kvin kvin; |
| 31 | + |
| 32 | + public Repository createRepository() throws RepositoryException { |
| 33 | + URI repo = getRepository(); |
| 34 | + if (repo.scheme() == "workspace") { |
| 35 | + try { |
| 36 | + String instanceFilter = "(type=osgi.instance.area)"; |
| 37 | + BundleContext context = FrameworkUtil.getBundle(PersistentModelSetSupport.class).getBundleContext(); |
| 38 | + ServiceReference<?>[] refs = context |
| 39 | + .getServiceReferences("org.eclipse.osgi.service.datalocation.Location", instanceFilter); |
| 40 | + if (refs.length > 0) { |
| 41 | + Object location = context.getService(refs[0]); |
| 42 | + URL loc = (URL) location.getClass().getMethod("getURL").invoke(location); |
| 43 | + URI workspace = URIs.createURI(FileLocator.resolve(loc).toString()); |
| 44 | + if (workspace.lastSegment() == "") { |
| 45 | + workspace = workspace.trimSegments(1); |
| 46 | + } |
| 47 | + repo = workspace.appendSegments(repo.segments()); |
| 48 | + } |
| 49 | + } catch (Exception e) { |
| 50 | + throw new RepositoryException(e); |
| 51 | + } |
| 52 | + } else { |
| 53 | + throw new RepositoryException("Location service for workspace scheme not found"); |
| 54 | + } |
| 55 | + |
| 56 | + NotifyingSail store = new NativeStore(new File(repo.toFileString())); |
| 57 | + if (! Boolean.FALSE.equals(getInference())) { |
| 58 | + store = new SchemaCachingRDFSInferencer(store); |
| 59 | + } |
| 60 | + Supplier<Kvin> kvinSupplier = () -> { |
| 61 | + if (kvin != null) { |
| 62 | + return kvin; |
| 63 | + } else { |
| 64 | + return bundleContext.getService(bundleContext.getServiceReference(Kvin.class)); |
| 65 | + } |
| 66 | + }; |
| 67 | + Sail kvinSail = new KvinSail(new DelegatingKvin(kvinSupplier), store); |
| 68 | + SailRepository repository = new SailRepository(kvinSail); |
| 69 | + repository.init(); |
| 70 | + addBasicKnowledge(repository); |
| 71 | + return repository; |
| 72 | + } |
| 73 | + |
| 74 | + public static void setKvin(Kvin kvin) { |
| 75 | + KvinPersistentModelSet.kvin = kvin; |
| 76 | + } |
| 77 | +} |
0 commit comments