Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (core): GEXF! #826

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/concepts/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ Enola 🕵🏾‍♀️ visualizes (TBD) the relationships of its _Entities_ usi
* [yEd](https://www.yworks.com) (yFiles) #commercial #sdk #freemium
* [Graphviz](https://graphviz.org) #available
* [Gephi](https://gephi.org), with [Gephi Lite](https://gephi.org/gephi-lite/) which uses [SigmaJS](https://www.sigmajs.org) on [Graphology](https://graphology.github.io) for JS #[FOSDEM](https://github.com/vorburger/vorburger.ch-Notes/blob/develop/conferences/FOSDEM-2024.md) #dynamicGraph #web #active #planned #ToDo
* [D3js.org](https://d3js.org) also has [Graphs](https://observablehq.com/@d3/force-directed-graph-component?collection=@d3/charts) (and [d3rdf](https://github.com/Rathachai/d3rdf))
* [vis.js](https://visjs.org)
* [D3js.org](https://d3js.org), and [Observable Plot](https://observablehq.com/plot/) has #[graph](https://observablehq.com/@d3/force-directed-graph-component?collection=@d3/charts) (and [d3rdf](https://github.com/Rathachai/d3rdf)) #[timeline](https://observablehq.com/@observablehq/plot-civilizations-timeline?intent=fork)
* [vis.js](https://visjs.org) #opensource #graph #timeline #web
* [Cytoscape](https://cytoscape.org) #opensource #graph #desktop #[plugins](https://apps.cytoscape.org/)
* [Cytoscape.js](https://js.cytoscape.org/) #graph #library #web
* [Mermaid](https://mermaid.js.org) #opensource
* [Mermaid](https://mermaid.js.org) #graph #[timeline](https://mermaid.js.org/syntax/timeline.html) #opensource
* [PlantUML](https://plantuml.com) #opensource
* [GraphStream](https://graphstream-project.org) #opensource #dynamicGraph #java #swing #desktop #inactive
* [Vega Lite](https://vega.github.io/vega-lite/) & [Vega](https://vega.github.io/vega/) #opensource
Expand Down
41 changes: 41 additions & 0 deletions java/dev/enola/common/time/TimesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.common.time;

import static com.google.common.truth.Truth.assertThat;

import org.junit.Test;

import java.time.Instant;
import java.time.temporal.ChronoUnit;

public class TimesTest {

@Test
public void beginningOfTimeInstantToString() {
var instant = Instant.MIN.plus(1, ChronoUnit.DAYS);
var text = "-1000000000-01-02T00:00:00Z";
assertThat(instant.toString()).isEqualTo(text);
}

@Test
public void endOfTimeWithInstantParse() {
// NB: The + prefix is mandatory if it's not 4 YYYY digits
Instant.parse("+300000-12-30T23:59:59Z");
}
}
3 changes: 3 additions & 0 deletions java/dev/enola/core/rosetta/Rosetta.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import dev.enola.core.meta.proto.EntityKinds;
import dev.enola.core.proto.Entity;
import dev.enola.rdf.RdfResourceConverter;
import dev.enola.thing.gen.gexf.GexfGenerator;
import dev.enola.thing.gen.gexf.GexfResourceConverter;
import dev.enola.thing.gen.graphviz.GraphvizGenerator;
import dev.enola.thing.gen.graphviz.GraphvizResourceConverter;
import dev.enola.thing.metadata.ThingMetadataProvider;
Expand Down Expand Up @@ -94,6 +96,7 @@ public Rosetta(ResourceProvider rp) {
messageResourceConverter,
new YamlJsonResourceConverter(),
new GraphvizResourceConverter(new GraphvizGenerator(tmp)),
new GexfResourceConverter(new GexfGenerator(tmp)),
new CharResourceConverter()));
}

Expand Down
19 changes: 11 additions & 8 deletions java/dev/enola/core/rosetta/RosettaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import dev.enola.model.Datatypes;
import dev.enola.rdf.RdfMediaTypes;
import dev.enola.thing.Thing;
import dev.enola.thing.gen.gexf.GexfMediaType;
import dev.enola.thing.gen.graphviz.GraphvizMediaType;
import dev.enola.thing.impl.ImmutableThing;
import dev.enola.thing.io.ThingMediaTypes;
Expand Down Expand Up @@ -186,10 +187,8 @@ public void testYamlToTurtle() throws Exception {
}

@Test
public void testGraphviz() throws Exception {
var in = rp.get("classpath:/graphviz.ttl");
var out = new MemoryResource(GraphvizMediaType.GV);

public void testGraphvizAndGexf() throws Exception {
var in = rp.get("classpath:/graph.ttl");
try (var ctx = TLC.open()) {
// This tests that StackedThingProvider in GraphvizGenerator works;
// if it did not "shadow", then we would have an empty Salutation.
Expand All @@ -203,10 +202,14 @@ public void testGraphviz() throws Exception {
var namespaceConverter = new NamespaceConverterWithRepository(namespaceRepo);
ctx.push(NamespaceConverter.class, namespaceConverter);

rosetta.convertInto(in, out);
}
var gexf = new MemoryResource(GexfMediaType.GEXF);
rosetta.convertInto(in, gexf);
assertThat(gexf)
.hasCharsEqualTo(rp.get("classpath:/graph.expected.gexf.xml?charset=UTF-8"));

var expectedOut = rp.get("classpath:/graphviz.expected.gv");
assertThat(out).hasCharsEqualTo(expectedOut);
var gv = new MemoryResource(GraphvizMediaType.GV);
rosetta.convertInto(in, gv);
assertThat(gv).hasCharsEqualTo(rp.get("classpath:/graph.expected.gv"));
}
}
}
1 change: 1 addition & 0 deletions java/dev/enola/datatype/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ java_library(
),
visibility = ["//:__subpackages__"],
deps = [
"//java/dev/enola/common",
"//java/dev/enola/common/context",
"//java/dev/enola/common/convert",
"//java/dev/enola/data",
Expand Down
2 changes: 2 additions & 0 deletions java/dev/enola/datatype/Datatype.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ default ObjectToStringBiConverter<Object> stringConverterFromObject() {
Optional<Class<T>> javaType();

// TODO Optional<TypeToken<T>> javaTypeToken();

DatatypeBuilder<T> child();
}
56 changes: 56 additions & 0 deletions java/dev/enola/datatype/DatatypeBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.datatype;

import static java.util.Objects.requireNonNull;

import dev.enola.common.Builder;
import dev.enola.common.convert.ObjectToStringBiConverter;

import org.jspecify.annotations.Nullable;

import java.util.Optional;
import java.util.regex.Pattern;

public class DatatypeBuilder<T> implements Builder<Datatype<T>> {

private String iri;
private final @Nullable Pattern pattern;
private final ObjectToStringBiConverter<T> stringConverter;
private final @Nullable Class<T> javaType;

DatatypeBuilder(Datatype<T> clone) {
this.iri = clone.iri();
this.pattern = clone.pattern().orElse(null);
this.stringConverter = clone.stringConverter();
this.javaType = clone.javaType().orElse(null);
}

public DatatypeBuilder<T> iri(String iri) {
this.iri = requireNonNull(iri);
return this;
}

// TODO setters for (all?) other methods of Datatype

@Override
public Datatype<T> build() {
return new ImmutableDatatype<T>(
iri, Optional.ofNullable(pattern), stringConverter, Optional.ofNullable(javaType));
}
}
5 changes: 5 additions & 0 deletions java/dev/enola/datatype/ImmutableDatatype.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public ImmutableDatatype(
String pattern) {
this(iri, stringConverter, javaType, Pattern.compile(pattern));
}

@Override
public DatatypeBuilder<T> child() {
return new DatatypeBuilder<>(this);
}
}
10 changes: 9 additions & 1 deletion java/dev/enola/model/enola/Datatypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@
import dev.enola.datatype.ImmutableDatatype;

import java.nio.file.attribute.FileTime;
import java.time.Instant;

/** Enola's built-in core datatypes. */
// TODO Doc: <p>See https://enola.dev/datatypes --- once we put something on
// https://docs.enola.dev/models/datatypes and have a working HTTP redirector.
public final class Datatypes {

public static final Datatype<Instant> TIMESTAMP =
dev.enola.model.xsd.Datatypes.DATE_TIME
.child()
.iri("https://enola.dev/Timestamp")
.build();

public static final Datatype<UnsignedLong> UNSIGNED_LONG =
new ImmutableDatatype<>(
"https://enola.dev/UnsignedLong",
Expand All @@ -50,7 +57,8 @@ public final class Datatypes {
// Replace use of Base64.getEncoder().encodeToString() in MessageToThingConverter#b64()

// Beware: The order here matters very much, for DatatypeRepository#match()
public static final Iterable<Datatype<?>> ALL = ImmutableList.of(UNSIGNED_LONG, FILE_TIME);
public static final Iterable<Datatype<?>> ALL =
ImmutableList.of(UNSIGNED_LONG, TIMESTAMP, FILE_TIME);

private Datatypes() {}
}
8 changes: 1 addition & 7 deletions java/dev/enola/model/schemaorg/Datatypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@
import com.google.common.collect.ImmutableList;

import dev.enola.datatype.Datatype;
import dev.enola.datatype.ImmutableDatatype;

import java.time.LocalDate;

/** Datatypes for <a href="https://schema.org/">Schema.org</a>. */
public final class Datatypes {

// TODO How-to copy/paste even less from dev.enola.model.xsd.Datatypes.DATE?
public static final Datatype<LocalDate> DATE =
new ImmutableDatatype<>(
"https://schema.org/Date",
dev.enola.model.xsd.Datatypes.DATE.stringConverter(),
LocalDate.class,
dev.enola.model.xsd.Datatypes.DATE.pattern().get());
dev.enola.model.xsd.Datatypes.DATE.child().iri("https://schema.org/Date").build();

// Beware: The order here may matter very much, for DatatypeRepository#match()
public static final Iterable<Datatype<?>> ALL = ImmutableList.of(DATE);
Expand Down
3 changes: 3 additions & 0 deletions java/dev/enola/thing/PredicatesObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public interface PredicatesObjects {
// TODO Reconsider if this method is really required? Why not just #properties().keySet()?
Set<String> predicateIRIs();

// TODO These is*() methods could be replaced with a Visitor - but how-to for nested Structs?!
// Look into e.g. how EMF does Visitors...

default boolean isLink(String predicateIRI) {
var value = get(predicateIRI);
return value instanceof URI || value instanceof Link;
Expand Down
1 change: 1 addition & 0 deletions java/dev/enola/thing/gen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ java_library(
"//java/dev/enola/common/convert",
"//java/dev/enola/common/function",
"//java/dev/enola/common/io",
"//java/dev/enola/common/time",
"//java/dev/enola/common/tree",
"//java/dev/enola/data",
"//java/dev/enola/datatype",
Expand Down
40 changes: 40 additions & 0 deletions java/dev/enola/thing/gen/ThingsIntoAppendableConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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;

import dev.enola.common.convert.ConversionException;
import dev.enola.common.convert.ConverterIntoAppendable;
import dev.enola.common.io.metadata.Metadata;
import dev.enola.common.io.resource.WritableResource;
import dev.enola.thing.Thing;

import java.io.IOException;

public interface ThingsIntoAppendableConverter extends ConverterIntoAppendable<Iterable<Thing>> {

default void convertIntoOrThrow(Iterable<Thing> things, WritableResource into)
throws ConversionException, IOException {
try (var out = into.charSink().openBufferedStream()) {
convertIntoOrThrow(things, out);
}
}

default String label(Metadata metadata) {
return metadata.emoji() + metadata.label();
}
}
Loading