Skip to content

Experiment for improved context control #246

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
33 changes: 33 additions & 0 deletions src/main/java/edu/kit/datamanager/ro_crate/RoCrate.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,45 @@ public void setUntrackedFiles(Collection<File> files) {
this.untrackedFiles = files;
}

private boolean isKeyUnused(String key, ObjectNode node) {
Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> entry = fields.next();
String fieldName = entry.getKey();
if (fieldName.startsWith(key)) {
return false;
}
JsonNode value = entry.getValue();
if (value.isObject()) {
if (!isKeyUnused(key, (ObjectNode) value)) {
return false;
}
} else if (value.isArray()) {
for (JsonNode element : value) {
if (element.isObject() && !isKeyUnused(key, (ObjectNode) element)) {
return false;
}
}
}
}
return true;
}

public boolean cleanupContext() {
// TODO we are missing the relations between the pairs and the URL, in order to check if a URL can be removed.
var contextUrls = this.metadataContext.getUrls();
var contextAllKeys = this.metadataContext.getKeys();
var contextExplicitKeys = this.metadataContext.getExplicitKeys();
}

@Override
@Deprecated(forRemoval = true)
public void deleteValuePairFromContext(String key) {
this.metadataContext.deleteValuePairFromContext(key);
}

@Override
@Deprecated(forRemoval = true)
public void deleteUrlFromContext(String key) {
this.metadataContext.deleteUrlFromContext(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,25 @@ public interface CrateMetadataContext {
*/
Set<String> getKeys();

/**
* Get an immutable collection of the keys in the metadata context that are
* explicitly set, not indirectly using a URL.
* @return the explicitly set keys in the metadata context
*/
Set<String> getExplicitKeys();

/**
* Get an immutable map of the context.
* @return an immutable map containing the context key-value pairs
*/
Map<String, String> getPairs();

/**
* Get an immutable collection of the urls in the metadata context.
* @return the urls in the metadata context
*/
Set<String> getUrls();

void deleteValuePairFromContext(String key);

void deleteUrlFromContext(String url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ public Set<String> getKeys() {
return Set.copyOf(merged);
}

@Override
public Set<String> getExplicitKeys() {
return Set.copyOf(this.other.keySet());
}

@Override
public Map<String, String> getPairs() {
Map<String, String> merged = new HashMap<>();
Expand All @@ -222,6 +227,11 @@ public Map<String, String> getPairs() {
return Map.copyOf(merged);
}

@Override
public Set<String> getUrls() {
return Set.copyOf(this.urls);
}


@Override
public void deleteValuePairFromContext(String key) {
Expand Down
Loading