Skip to content

Commit

Permalink
feat (core): Graph Commons JSON Converter
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Oct 25, 2024
1 parent 7b1afb0 commit 73d8e5e
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 3 deletions.
1 change: 1 addition & 0 deletions java/dev/enola/thing/gen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ java_library(
"@maven//:com_google_auto_service_auto_service_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_errorprone_error_prone_type_annotations",
"@maven//:com_google_code_gson_gson",
"@maven//:com_google_guava_guava",
"@maven//:org_jspecify_jspecify",
"@maven//:org_slf4j_slf4j_api",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2024 The Enola <https://enola.dev> 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.gen.graphcommons;

import com.google.common.io.CharStreams;
import com.google.gson.FormattingStyle;
import com.google.gson.Strictness;
import com.google.gson.stream.JsonWriter;

import dev.enola.common.context.TLC;
import dev.enola.common.convert.ConversionException;
import dev.enola.thing.Thing;
import dev.enola.thing.gen.ThingsIntoAppendableConverter;
import dev.enola.thing.repo.StackedThingProvider;
import dev.enola.thing.repo.ThingProvider;

import java.io.IOException;

/** Generator of JSON Format used by <a href="https://graphcommons.com/>Graph Commons</a>. */
public class GraphCommonsJsonGenerator implements ThingsIntoAppendableConverter {

@Override
public boolean convertInto(Iterable<Thing> from, Appendable out)
throws ConversionException, IOException {
var writer = CharStreams.asWriter(out);
var jsonWriter = new JsonWriter(writer);
jsonWriter.setStrictness(Strictness.STRICT);
jsonWriter.setFormattingStyle(FormattingStyle.PRETTY); // TODO COMPACT, if ?pretty
jsonWriter.setSerializeNulls(false);
jsonWriter.setHtmlSafe(true); // TODO ?
try (var ctx = TLC.open()) {
ctx.push(ThingProvider.class, new StackedThingProvider(from));
out.append("{\n \"nodes\": [\n");
for (Thing thing : from) printThingNode(thing, jsonWriter);
out.append(" ],\n \"edges\": [\n");
for (Thing thing : from) printThingEdges(thing, jsonWriter);
out.append(" ],\n \"nodeTypes\": [\n");
// TODO printThingNodeTypes()
out.append(" ],\n \"edgeTypes\": [\n");
// TODO printThingEdgeTypes()
out.append(" ],\n");
out.append(" \"name\": \"Enola.dev\"\n");
out.append('}');
}
jsonWriter.flush();
writer.close();
return true;
}

private void printThingNode(Thing thing, JsonWriter out) throws IOException {
out.beginObject();
out.name("id").value(thing.iri());
out.endObject();
}

private void printThingEdges(Thing thing, JsonWriter out) {}
}
20 changes: 20 additions & 0 deletions java/dev/enola/thing/gen/graphcommons/GraphCommonsMediaType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2024 The Enola <https://enola.dev> 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.gen.graphcommons;

public class GraphCommonsMediaType {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2024 The Enola <https://enola.dev> 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.gen.graphcommons;

import dev.enola.common.io.resource.ReadableResource;
import dev.enola.common.io.resource.WritableResource;
import dev.enola.common.io.resource.convert.CatchingResourceConverter;

public class GraphCommonsResourceConverter implements CatchingResourceConverter {

@Override
public boolean convertIntoThrows(ReadableResource from, WritableResource into)
throws Exception {
return false;
}
}
6 changes: 3 additions & 3 deletions java/dev/enola/thing/gen/graphviz/GraphvizGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,21 @@ public boolean convertInto(Iterable<Thing> from, Appendable out)
ctx.push(ThingProvider.class, new StackedThingProvider(from));
for (Thing thing : from) {
thingIRIs.add(thing.iri());
printFullThing(thing, out, thingIRIs, linkIRIs);
printThing(thing, out, thingIRIs, linkIRIs);
}
// Remove links to all things which were processed after we processed them
linkIRIs.removeAll(thingIRIs);
// linkIRIs now contains things which were linked to but that have no properties
for (String orphanIRI : linkIRIs) {
var orphanThing = new OnlyIRIThing(orphanIRI);
printFullThing(orphanThing, out, thingIRIs, linkIRIs);
printThing(orphanThing, out, thingIRIs, linkIRIs);
}
}
out.append("}\n");
return true;
}

private void printFullThing(
private void printThing(
Thing thing, Appendable out, Set<String> thingIRIs, Set<String> linkIRIs)
throws IOException {
boolean full = TLC.optional(Flags.FULL).orElse(false);
Expand Down

0 comments on commit 73d8e5e

Please sign in to comment.