This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use name spaces in knowledge graph (#32)
* feat: small improvements * feat: use name spaces in knowledge graph * fix: broken test * feat: blue bike popup improved * feat: small improvements and fixes
- Loading branch information
Showing
41 changed files
with
746 additions
and
416 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
2 changes: 1 addition & 1 deletion
2
.../be/informatievlaanderen/vsds/demonstrator/triple/application/services/TripleService.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
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
22 changes: 0 additions & 22 deletions
22
...a/be/informatievlaanderen/vsds/demonstrator/triple/domain/entities/MemberDescription.java
This file was deleted.
Oops, something went wrong.
6 changes: 4 additions & 2 deletions
6
...e/informatievlaanderen/vsds/demonstrator/triple/domain/repositories/TripleRepository.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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package be.informatievlaanderen.vsds.demonstrator.triple.domain.repositories; | ||
|
||
import be.informatievlaanderen.vsds.demonstrator.triple.domain.entities.MemberDescription; | ||
import be.informatievlaanderen.vsds.demonstrator.triple.domain.valueobjects.Triple; | ||
|
||
import java.util.List; | ||
|
||
public interface TripleRepository { | ||
MemberDescription getById(String id); | ||
List<Triple> getById(String id); | ||
} |
117 changes: 117 additions & 0 deletions
117
...java/be/informatievlaanderen/vsds/demonstrator/triple/domain/services/TriplesFactory.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,117 @@ | ||
package be.informatievlaanderen.vsds.demonstrator.triple.domain.services; | ||
|
||
import be.informatievlaanderen.vsds.demonstrator.triple.domain.valueobjects.Node; | ||
import be.informatievlaanderen.vsds.demonstrator.triple.domain.valueobjects.Triple; | ||
import org.eclipse.rdf4j.model.IRI; | ||
import org.eclipse.rdf4j.model.Model; | ||
import org.eclipse.rdf4j.model.Statement; | ||
import org.eclipse.rdf4j.model.Value; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
public class TriplesFactory { | ||
private static final Map<String, String> COMMON_NAMESPACES = Map.ofEntries( | ||
Map.entry("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"), | ||
Map.entry("rdfs", "http://www.w3.org/2000/01/rdf-schema#"), | ||
Map.entry("owl", "http://www.w3.org/2002/07/owl#"), | ||
Map.entry("xsd", "http://www.w3.org/2001/XMLSchema#"), | ||
Map.entry("dct", "http://purl.org/dc/terms/"), | ||
Map.entry("adms", "http://www.w3.org/ns/adms#"), | ||
Map.entry("prov", "http://www.w3.org/ns/prov#"), | ||
Map.entry("purl", "http://purl.org/dc/elements/1.1/"), | ||
Map.entry("foaf", "http://xmlns.com/foaf/0.1/"), | ||
Map.entry("org", "http://www.w3.org/ns/org#"), | ||
Map.entry("legal", "http://www.w3.org/ns/legal#"), | ||
Map.entry("m8g", "http://data.europa.eu/m8g/"), | ||
Map.entry("locn", "http://www.w3.org/ns/locn#"), | ||
Map.entry("ldes", "http://w3id.org/ldes#"), | ||
Map.entry("tree", "https://w3id.org/tree#"), | ||
Map.entry("sh", "http://www.w3.org/ns/shacl#"), | ||
Map.entry("skos", "http://www.w3.org/2004/02/skos/core#"), | ||
Map.entry("schema", "http://schema.org/") | ||
); | ||
private static final String VALID_PREFIX_REGEX = "^[a-zA-Z_][\\w.-]*$"; // NCName regex | ||
private final Stream<Statement> statementStream; | ||
private final Map<String, String> namespaces; | ||
|
||
private TriplesFactory(@NotNull Model model) { | ||
this.statementStream = model.stream(); | ||
namespaces = new HashMap<>(COMMON_NAMESPACES); | ||
|
||
} | ||
|
||
/** | ||
* Assigns the namespaces to this factory | ||
* | ||
* @param namespaces Map with namespace prefix as key and the according name as value | ||
* @return the factory whereon this method is called | ||
*/ | ||
public TriplesFactory withNamespaces(@NotNull Map<String, String> namespaces) { | ||
this.namespaces.putAll(namespaces); | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Initialize an instance of TriplesFactory with the specified Model | ||
* | ||
* @param model Model with which the initialization needs to be done | ||
* @return new instance of TriplesFactory | ||
*/ | ||
public static TriplesFactory fromModel(@NotNull Model model) { | ||
return new TriplesFactory(model); | ||
} | ||
|
||
/** | ||
* Converts all the statements of the model in this factory to a list of triples | ||
* | ||
* @return the list of triples | ||
*/ | ||
public List<Triple> getTriples() { | ||
return statementStream | ||
.map(statement -> new Triple(valueToNode(statement.getSubject()), valueToNode(statement.getPredicate()), valueToNode(statement.getObject()))) | ||
.toList(); | ||
} | ||
|
||
private Node valueToNode(@NotNull Value value) { | ||
if (value.isIRI()) { | ||
String name = ((IRI) value).getNamespace(); | ||
return getNamespace(name).or(() -> createNamespace(name)) | ||
.map(namespace -> new Node(value.stringValue(), namespace)) | ||
.orElseGet(() -> new Node(value.stringValue())); | ||
} | ||
return new Node(value.stringValue()); | ||
} | ||
|
||
private Optional<Map.Entry<String, String>> getNamespace(@NotNull String name) { | ||
return namespaces.entrySet().stream().filter(entry -> entry.getValue().equals(name)).findFirst(); | ||
} | ||
|
||
private Optional<Map.Entry<String, String>> createNamespace(@NotNull String name) { | ||
Optional<String> prefixOptional = name.contains("#") | ||
? createNamespacePrefix(name) | ||
: createNonNamespacePrefix(name); | ||
|
||
return prefixOptional | ||
.filter(prefix -> prefix.matches(VALID_PREFIX_REGEX)) | ||
.map(prefix -> Map.entry(prefix, name)); | ||
} | ||
|
||
private Optional<String> createNamespacePrefix(@NotNull String name) { | ||
int lastIndexOfHashtag = name.lastIndexOf('#'); | ||
int lastIndexOfSlash = name.lastIndexOf('/'); | ||
return Optional.of(name.substring(lastIndexOfSlash + 1, lastIndexOfHashtag)); | ||
|
||
} | ||
|
||
private Optional<String> createNonNamespacePrefix(@NotNull String name) { | ||
String[] nameSplit = name.split("/"); | ||
int index = nameSplit.length - 1; | ||
return Optional.ofNullable(nameSplit[index]); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
.../main/java/be/informatievlaanderen/vsds/demonstrator/triple/domain/valueobjects/Node.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,50 @@ | ||
package be.informatievlaanderen.vsds.demonstrator.triple.domain.valueobjects; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class Node { | ||
private final String value; | ||
private Map.Entry<String, String> namespace; | ||
|
||
public Node(@NotNull String value) { | ||
this.value = value; | ||
} | ||
|
||
public Node(@NotNull String value, @Nullable Map.Entry<String, String> namespace) { | ||
this.value = value; | ||
this.namespace = namespace; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public String getPrefixedValue() { | ||
if (namespace != null) { | ||
return value.replace(namespace.getValue(), "%s:".formatted(namespace.getKey())); | ||
} | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Node node = (Node) o; | ||
|
||
if (!value.equals(node.value)) return false; | ||
return Objects.equals(namespace, node.namespace); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = value.hashCode(); | ||
result = 31 * result + (namespace != null ? namespace.hashCode() : 0); | ||
return result; | ||
} | ||
} |
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
Oops, something went wrong.