From 19061004038be5a09fe5c34e3919027e31422eb6 Mon Sep 17 00:00:00 2001 From: Michael Vorburger Date: Sun, 30 Jun 2024 12:54:46 +0200 Subject: [PATCH] feat (core): Introduce ThingIO --- java/dev/enola/cli/CommandWithModel.java | 3 ++ java/dev/enola/thing/io/Loader.java | 3 ++ .../thing/io/ResourceIntoThingConverters.java | 4 ++ java/dev/enola/thing/io/ThingIO.java | 44 +++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 java/dev/enola/thing/io/ThingIO.java diff --git a/java/dev/enola/cli/CommandWithModel.java b/java/dev/enola/cli/CommandWithModel.java index 6af3706e4..9c2185f53 100644 --- a/java/dev/enola/cli/CommandWithModel.java +++ b/java/dev/enola/cli/CommandWithModel.java @@ -87,6 +87,9 @@ public final void run() throws Exception { // TODO Move elsewhere for continuous ("shell") mode, as this is "expensive". ServiceProvider grpc = null; if (group.load != null) { + // TODO Use (new) ThingIO / ResourceIntoThingConverters infra here! (With TLC.open(), + // similar to how it's done in RdfResourceIntoThingConverterTest; but needs more + // thought.) // TODO Replace DatatypeRepository with store itself, once a Datatype is a Thing DatatypeRepository dtr = new DatatypeRepositoryBuilder().build(); ThingMemoryRepositoryROBuilder store = new ThingMemoryRepositoryROBuilder(); diff --git a/java/dev/enola/thing/io/Loader.java b/java/dev/enola/thing/io/Loader.java index c91307aa6..a35d25390 100644 --- a/java/dev/enola/thing/io/Loader.java +++ b/java/dev/enola/thing/io/Loader.java @@ -33,6 +33,8 @@ public class Loader implements ConverterInto, Store> { + // TODO This must simply use the (new) ResourceIntoThingConverters ! + // TODO Do this multi-threaded, in parallel... private static final Logger LOG = LoggerFactory.getLogger(Loader.class); @@ -59,6 +61,7 @@ private void load(ReadableResource resource, Store store) { things.get() .forEach( thingBuilder -> { + // TODO Move this into ResourceIntoThingConverters thingBuilder.set(KIRI.E.ORIGIN, resource.uri()); var thing = thingBuilder.build(); store.merge(thing); diff --git a/java/dev/enola/thing/io/ResourceIntoThingConverters.java b/java/dev/enola/thing/io/ResourceIntoThingConverters.java index 9ec25f514..db792d4f7 100644 --- a/java/dev/enola/thing/io/ResourceIntoThingConverters.java +++ b/java/dev/enola/thing/io/ResourceIntoThingConverters.java @@ -32,6 +32,10 @@ public class ResourceIntoThingConverters implements Converter>> { + // TODO Reconsider use of generics here -VS- in ThingIO + + // TODO Support a more "streaming" API... + private final ImmutableList> converters; public ResourceIntoThingConverters(Iterable> converters) { diff --git a/java/dev/enola/thing/io/ThingIO.java b/java/dev/enola/thing/io/ThingIO.java new file mode 100644 index 000000000..11ff527ac --- /dev/null +++ b/java/dev/enola/thing/io/ThingIO.java @@ -0,0 +1,44 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright 2024 The Enola Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dev.enola.thing.io; + +import dev.enola.common.io.resource.ResourceProvider; +import dev.enola.thing.Thing; + +import java.net.URI; + +public class ThingIO { + + // TODO Support a more "streaming" API... + + private final ResourceProvider rp = null; // TODO NOT new ResourceProviders(); + + private final ResourceIntoThingConverters converter = + new ResourceIntoThingConverters<>(); + + public Iterable read(URI uri) { + return read(uri, Thing.class); + } + + public Iterable read(URI uri, Class klass) { + var resource = rp.getReadableResource(uri); + return null; // TODO converter.convert(resource); + } + + public void write(Iterable things, URI uri) {} +}