Skip to content
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
24 changes: 19 additions & 5 deletions core/src/main/java/com/github/jsonldjava/core/JsonLdApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ else if (JsonLdConsts.REVERSE.equals(expandedProperty)) {
else if (frameExpansion && (JsonLdConsts.EXPLICIT.equals(expandedProperty)
|| JsonLdConsts.DEFAULT.equals(expandedProperty)
|| JsonLdConsts.EMBED.equals(expandedProperty)
|| JsonLdConsts.REQUIRE_ALL.equals(expandedProperty)
|| JsonLdConsts.EMBED_CHILDREN.equals(expandedProperty)
|| JsonLdConsts.OMIT_DEFAULT.equals(expandedProperty))) {
expandedValue = expand(activeCtx, expandedProperty, value);
Expand Down Expand Up @@ -994,7 +995,7 @@ else if (result.containsKey(JsonLdConsts.SET)
result = null;
}
// 12.2)
else if (result != null && result.containsKey(JsonLdConsts.ID)
else if (result != null && !frameExpansion && result.containsKey(JsonLdConsts.ID)
&& result.size() == 1) {
result = null;
}
Expand Down Expand Up @@ -1287,11 +1288,13 @@ private class FramingContext {
public boolean omitDefault;
public Map<String, EmbedNode> uniqueEmbeds;
public LinkedList<String> subjectStack;
public boolean requireAll;

public FramingContext() {
embed = Embed.LAST;
explicit = false;
omitDefault = false;
requireAll = false;
uniqueEmbeds = new HashMap<>();
subjectStack = new LinkedList<>();
}
Expand All @@ -1307,6 +1310,9 @@ public FramingContext(JsonLdOptions opts) {
if (opts.getOmitDefault() != null) {
this.omitDefault = opts.getOmitDefault();
}
if (opts.getRequireAll() != null) {
this.requireAll = opts.getRequireAll();
}
}
}

Expand Down Expand Up @@ -1385,14 +1391,16 @@ private void frame(FramingContext state, Map<String, Object> nodes, Map<String,
// TODO: handle @requireAll
Embed embed = getFrameEmbed(frame, state.embed);
final Boolean explicitOn = getFrameFlag(frame, JsonLdConsts.EXPLICIT, state.explicit);
final Boolean requireAll = getFrameFlag(frame, JsonLdConsts.REQUIRE_ALL, state.requireAll);
final Map<String, Object> flags = newMap();
flags.put(JsonLdConsts.EXPLICIT, explicitOn);
flags.put(JsonLdConsts.EMBED, embed);
flags.put(JsonLdConsts.REQUIRE_ALL, requireAll);

// 3.
// Create a list of matched subjects by filtering subjects against frame
// using the Frame Matching algorithm with state, subjects, frame, and requireAll.
final Map<String, Object> matches = filterNodes(state, nodes, frame);
final Map<String, Object> matches = filterNodes(state, nodes, frame, requireAll);
final List<String> ids = new ArrayList<String>(matches.keySet());
Collections.sort(ids);

Expand Down Expand Up @@ -1671,19 +1679,19 @@ private static void removeDependents(Map<String, EmbedNode> embeds, String id) {
}

private Map<String, Object> filterNodes(FramingContext state, Map<String, Object> nodes,
Map<String, Object> frame) throws JsonLdError {
Map<String, Object> frame, boolean requireAll) throws JsonLdError {
final Map<String, Object> rval = newMap();
for (final String id : nodes.keySet()) {
final Map<String, Object> element = (Map<String, Object>) nodes.get(id);
if (element != null && filterNode(state, element, frame)) {
if (element != null && filterNode(state, element, frame, requireAll)) {
rval.put(id, element);
}
}
return rval;
}

private boolean filterNode(FramingContext state, Map<String, Object> node,
Map<String, Object> frame) throws JsonLdError {
Map<String, Object> frame, boolean requireAll) throws JsonLdError {
final Object types = frame.get(JsonLdConsts.TYPE);
final Object frameIds = frame.get(JsonLdConsts.ID);
// https://json-ld.org/spec/latest/json-ld-framing/#frame-matching
Expand Down Expand Up @@ -1712,8 +1720,14 @@ else if (!(frameIds instanceof List)) {
}
}
}
return false;
}
// 2. Node matches if frame has no non-keyword properties.TODO
// 3. If requireAll is true, node matches if all non-keyword properties
// (property) in frame match any of the following conditions. Or, if
// requireAll is false, if any of the non-keyword properties (property)
// in frame match any of the following conditions. For the values of each
// property from frame in node:
// 3.1 If property is @type:
if (types != null) {
if (!(types instanceof List)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public final class JsonLdConsts {
public static final String BLANK_NODE_PREFIX = "_:";
public static final String VOCAB = "@vocab";
public static final String BASE = "@base";
public static final String REQUIRE_ALL = "@requireAll";

public enum Embed { ALWAYS, NEVER, LAST, LINK; }
}
10 changes: 10 additions & 0 deletions core/src/main/java/com/github/jsonldjava/core/JsonLdOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public JsonLdOptions(String base) {
private Boolean explicit = null;
private Boolean omitDefault = null;
private Boolean pruneBlankNodeIdentifiers = true;
private Boolean requireAll = false;

// RDF conversion options :
// http://www.w3.org/TR/json-ld-api/#serialize-rdf-as-json-ld-algorithm
Expand Down Expand Up @@ -138,6 +139,14 @@ public void setPruneBlankNodeIdentifiers(Boolean pruneBlankNodeIdentifiers) {
this.pruneBlankNodeIdentifiers = pruneBlankNodeIdentifiers;
}

public Boolean getRequireAll() {
return this.requireAll;
}

public void setRequireAll(Boolean requireAll) {
this.requireAll = requireAll;
}

public Boolean getCompactArrays() {
return compactArrays;
}
Expand Down Expand Up @@ -207,4 +216,5 @@ public void setDocumentLoader(DocumentLoader documentLoader) {
public String format = null;
public Boolean useNamespaces = false;
public String outputForm = null;

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static boolean isKeyword(Object key) {
|| "@graph".equals(key) || "@id".equals(key) || "@index".equals(key)
|| "@language".equals(key) || "@list".equals(key) || "@omitDefault".equals(key)
|| "@reverse".equals(key) || "@preserve".equals(key) || "@set".equals(key)
|| "@type".equals(key) || "@value".equals(key) || "@vocab".equals(key);
|| "@type".equals(key) || "@value".equals(key) || "@vocab".equals(key) || "@requireAll".equals(key);
}

public static Boolean deepCompare(Object v1, Object v2, Boolean listOrderMatters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,52 @@ public void testFrame0004() throws IOException, JsonLdError {
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0004-out.jsonld"));
assertEquals(out, frame2);
}

@Test
public void testFrame0005() throws IOException, JsonLdError {
final Object frame = JsonUtils
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0005-frame.jsonld"));
final Object in = JsonUtils
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0005-in.jsonld"));

JsonLdOptions opts = new JsonLdOptions();
opts.setCompactArrays(true);
final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts);

final Object out = JsonUtils
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0005-out.jsonld"));
assertEquals(out, frame2);
}

@Test
public void testFrame0006() throws IOException, JsonLdError {
final Object frame = JsonUtils
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0006-frame.jsonld"));
final Object in = JsonUtils
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0006-in.jsonld"));

JsonLdOptions opts = new JsonLdOptions();
opts.setCompactArrays(true);
final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts);

final Object out = JsonUtils
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0006-out.jsonld"));
assertEquals(out, frame2);
}

@Test
public void testFrame0007() throws IOException, JsonLdError {
final Object frame = JsonUtils
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0007-frame.jsonld"));
final Object in = JsonUtils
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0007-in.jsonld"));

JsonLdOptions opts = new JsonLdOptions();
opts.setCompactArrays(true);
final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts);

final Object out = JsonUtils
.fromInputStream(getClass().getResourceAsStream("/custom/frame-0007-out.jsonld"));
assertEquals(out, frame2);
}
}
4 changes: 4 additions & 0 deletions core/src/test/resources/custom/frame-0005-frame.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"@context": {},
"@type": "http://www.myresource/uuidtype"
}
19 changes: 19 additions & 0 deletions core/src/test/resources/custom/frame-0005-in.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"@context": {
"rdfs": "http://www.w3.org/2000/01/rdf-schema#"
},
"@id": "http://www.myresource/uuid",
"@type": "http://www.myresource/uuidtype",
"http://www.myresource.com/ontology/1.0#talksAbout": {
"@list": [
{
"@id": "http://rdf.freebase.com/ns/m.018w8",
"rdfs:label": [
{
"@value": "Basketball",
"@language": "en"
}
]
}
] }
}
15 changes: 15 additions & 0 deletions core/src/test/resources/custom/frame-0005-out.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"@graph" : [ {
"@id" : "http://www.myresource/uuid",
"@type" : "http://www.myresource/uuidtype",
"http://www.myresource.com/ontology/1.0#talksAbout" : {
"@list" : [ {
"@id" : "http://rdf.freebase.com/ns/m.018w8",
"http://www.w3.org/2000/01/rdf-schema#label" : {
"@language" : "en",
"@value" : "Basketball"
}
} ]
}
} ]
}
1 change: 1 addition & 0 deletions core/src/test/resources/custom/frame-0006-frame.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
11 changes: 11 additions & 0 deletions core/src/test/resources/custom/frame-0006-in.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[ {
"@id" : "http://example.com/canvas-1",
"@type" : "http://example.com"
}, {
"@id" : "http://example.com/element",
"http://example.com" : {
"@list" : [ {
"@id" : "http://example.com/canvas-1"
} ]
}
} ]
14 changes: 14 additions & 0 deletions core/src/test/resources/custom/frame-0006-out.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"@graph" : [ {
"@id" : "http://example.com/canvas-1",
"@type" : "http://example.com"
}, {
"@id" : "http://example.com/element",
"http://example.com" : {
"@list" : [ {
"@id" : "http://example.com/canvas-1",
"@type" : "http://example.com"
} ]
}
} ]
}
12 changes: 12 additions & 0 deletions core/src/test/resources/custom/frame-0007-frame.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"@context": {
"dc": "http://purl.org/dc/elements/1.1/",
"ex": "http://example.org/vocab#"
},
"@type": "ex:Library",
"ex:contains": {
"@explicit":true,
"dc:title":{"@default":"Title missing"},
"dc:creator":{}
}
}
24 changes: 24 additions & 0 deletions core/src/test/resources/custom/frame-0007-in.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"@context": {
"dc": "http://purl.org/dc/elements/1.1/",
"ex": "http://example.org/vocab#"
},
"@graph": [
{
"@id": "http://example.org/library",
"@type": "ex:Library",
"ex:contains": [{"@id":"http://example.org/library/the-republic#introduction"},{"@id":"http://example.org/library/the-republic"}]
},
{
"@id": "http://example.org/library/the-republic",
"@type": "ex:Book",
"dc:creator": "Plato",
"dc:title": "The Republic"
},
{
"@id": "http://example.org/library/the-republic#introduction",
"@type": "ex:Book",
"dc:creator": "Plato"
}
]
}
26 changes: 26 additions & 0 deletions core/src/test/resources/custom/frame-0007-out.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"@context": {
"dc": "http://purl.org/dc/elements/1.1/",
"ex": "http://example.org/vocab#"
},
"@graph": [
{
"@id": "http://example.org/library",
"@type": "ex:Library",
"ex:contains": [
{
"@id": "http://example.org/library/the-republic#introduction",
"@type": "ex:Book",
"dc:creator": "Plato",
"dc:title": "Title missing"
},
{
"@id": "http://example.org/library/the-republic",
"@type": "ex:Book",
"dc:creator": "Plato",
"dc:title": "The Republic"
}
]
}
]
}
14 changes: 3 additions & 11 deletions core/src/test/resources/json-ld.org/frame-0022-frame.jsonld
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
{
"@context": {
"dc": "http://purl.org/dc/elements/1.1/",
"ex": "http://example.org/vocab#"
},
"@type": "ex:Library",
"ex:contains": {
"@explicit":true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My impression was that this test file was intended to be a custom test. However, it seems to have mistakenly landed in core/src/test/resources/json-ld.org/ instead of core/src/test/resources/custom. The PR which included it was #168 by @hmottestad for background reference.

Could you move it to core/src/test/resources/custom as part of this PR?

"dc:title":{"@default":"Title missing"},
"dc:creator":{}
}
}
"@context": {"ex": "http://example.org/"},
"@id": "ex:Sub1"
}
32 changes: 9 additions & 23 deletions core/src/test/resources/json-ld.org/frame-0022-in.jsonld
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
{
"@context": {
"dc": "http://purl.org/dc/elements/1.1/",
"ex": "http://example.org/vocab#"
},
"@graph": [
{
"@id": "http://example.org/library",
"@type": "ex:Library",
"ex:contains": [{"@id":"http://example.org/library/the-republic#introduction"},{"@id":"http://example.org/library/the-republic"}]
},
{
"@id": "http://example.org/library/the-republic",
"@type": "ex:Book",
"dc:creator": "Plato",
"dc:title": "The Republic"
},
{
"@id": "http://example.org/library/the-republic#introduction",
"@type": "ex:Book",
"dc:creator": "Plato"
}
]
}
"@context": {"ex": "http://example.org/"},
"@graph": [{
"@id": "ex:Sub1",
"@type": "ex:Type1"
}, {
"@id": "ex:Sub2",
"@type": "ex:Type2"
}]
}
Loading