-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (core): Graph Commons JSON Converter
- Loading branch information
Showing
19 changed files
with
344 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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.gexf; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import static dev.enola.common.context.testlib.SingletonRule.$; | ||
|
||
import dev.enola.common.context.testlib.SingletonRule; | ||
import dev.enola.common.io.mediatype.MediaTypeProviders; | ||
import dev.enola.common.io.resource.ClasspathResource; | ||
import dev.enola.common.xml.XmlMediaType; | ||
|
||
import org.junit.Ignore; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public class GexfMediaTypeTest { | ||
|
||
@Rule | ||
public SingletonRule r = $(MediaTypeProviders.set(new GexfMediaType(), new XmlMediaType())); | ||
|
||
@Test | ||
@Ignore // TODO Figure out why this doesn't work (but only IFF XmlMediaType is present) | ||
public void gexfMediaType() { | ||
var r = new ClasspathResource.Provider().get("classpath:/graph.expected.gexf"); | ||
assertThat(r.mediaType()).isEqualTo(GexfMediaType.GEXF); | ||
} | ||
|
||
@Test | ||
public void TODO_empty() {} | ||
} |
73 changes: 73 additions & 0 deletions
73
java/dev/enola/thing/gen/graphcommons/GraphCommonsJsonGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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 static com.google.gson.FormattingStyle.PRETTY; | ||
import static com.google.gson.Strictness.STRICT; | ||
|
||
import com.google.common.io.CharStreams; | ||
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(STRICT); | ||
jsonWriter.setFormattingStyle(PRETTY); // TODO FormattingStyle.COMPACT, if !pretty | ||
jsonWriter.setSerializeNulls(false); | ||
jsonWriter.setHtmlSafe(true); // TODO ? | ||
jsonWriter.beginObject(); | ||
try (var ctx = TLC.open()) { | ||
ctx.push(ThingProvider.class, new StackedThingProvider(from)); | ||
jsonWriter.name("nodes").beginArray(); | ||
for (Thing thing : from) printThingNode(thing, jsonWriter); | ||
jsonWriter.endArray().name("edges").beginArray(); | ||
for (Thing thing : from) printThingEdges(thing, jsonWriter); | ||
jsonWriter.endArray().name("nodeTypes").beginArray(); | ||
// TODO printThingNodeTypes() | ||
jsonWriter.endArray().name("edgeTypes").beginArray(); | ||
// TODO printThingEdgeTypes() | ||
jsonWriter.endArray().name("name").value("Enola.dev"); | ||
} | ||
jsonWriter.endObject(); | ||
jsonWriter.flush(); | ||
writer.close(); | ||
return true; | ||
} | ||
|
||
private void printThingNode(Thing thing, JsonWriter jsonWriter) throws IOException { | ||
jsonWriter.beginObject(); | ||
jsonWriter.name("id").value(thing.iri()); | ||
jsonWriter.endObject(); | ||
} | ||
|
||
private void printThingEdges(Thing thing, JsonWriter jsonWriter) {} | ||
} |
Oops, something went wrong.