From 6d18c51d039478e8ff3ac0f759061e75d23153c8 Mon Sep 17 00:00:00 2001 From: Antonio Garcia-Dominguez Date: Fri, 20 Dec 2024 11:13:08 +0100 Subject: [PATCH] Move filtering by preference to 3-arg getProperty This avoids some of the code duplication we had, as the logic really should have been in the 3-argument getProperty and not the 2-argument one (which is only for convenience and for the _literal suffix). --- .../eclipse/epsilon/emc/rdf/RDFResource.java | 84 +++++++------------ 1 file changed, 31 insertions(+), 53 deletions(-) diff --git a/bundles/org.eclipse.epsilon.emc.rdf/src/org/eclipse/epsilon/emc/rdf/RDFResource.java b/bundles/org.eclipse.epsilon.emc.rdf/src/org/eclipse/epsilon/emc/rdf/RDFResource.java index 267ee02..57a52a6 100644 --- a/bundles/org.eclipse.epsilon.emc.rdf/src/org/eclipse/epsilon/emc/rdf/RDFResource.java +++ b/bundles/org.eclipse.epsilon.emc.rdf/src/org/eclipse/epsilon/emc/rdf/RDFResource.java @@ -36,8 +36,8 @@ public class RDFResource extends RDFModelElement { protected static final String LITERAL_SUFFIX = "_literal"; - - enum LiteralMode { + + public enum LiteralMode { RAW, VALUES_ONLY } @@ -54,47 +54,28 @@ public Resource getResource() { public Collection getProperty(String property, IEolContext context) { final RDFQualifiedName pName = RDFQualifiedName.from(property, this.owningModel::getNamespaceURI); + Collection value = getProperty(pName, context, LiteralMode.VALUES_ONLY); - Collection value = getProperty(pName, context, LiteralMode.RAW); - if (pName.languageTag == null && !value.stream().anyMatch(p -> p instanceof RDFResource)) { - value = filterByPreferredLanguage(value, LiteralMode.RAW); - } - if (!value.isEmpty()) { - return convertLiteralsToValues(value); - } - - if (pName.localName.endsWith(LITERAL_SUFFIX)) { + if (value.isEmpty() && pName.localName.endsWith(LITERAL_SUFFIX)) { final String localNameWithoutSuffix = pName.localName.substring(0, pName.localName.length() - LITERAL_SUFFIX.length()); RDFQualifiedName withoutLiteral = pName.withLocalName(localNameWithoutSuffix); - value = getProperty(withoutLiteral, context, LiteralMode.RAW); - if (pName.languageTag == null && !value.stream().anyMatch(p -> p instanceof RDFResource)) { - value = filterByPreferredLanguage(value, LiteralMode.RAW); - } } return value; } - private Collection convertLiteralsToValues(Collection value) { + protected Collection convertLiteralsToValues(Collection value) { return value.stream() .map(e -> e instanceof RDFLiteral ? ((RDFLiteral)e).getValue() : e) .collect(Collectors.toList()); } - private Collection filterByPreferredLanguage(Collection value, LiteralMode literalMode) { + protected Collection filterByPreferredLanguage(Collection value) { // If no preferred languages are specified, don't do any filtering if (super.getModel().getLanguagePreference().isEmpty()) { - switch (literalMode) { - case RAW: - return value; - case VALUES_ONLY: - return value.stream().map(e -> e instanceof RDFLiteral - ? ((RDFLiteral) e).getValue() : e).collect(Collectors.toList()); - default: - throw new IllegalArgumentException("Unknown literal mode " + literalMode); - } + return value; } // Otherwise, group literals by language tag @@ -111,28 +92,14 @@ private Collection filterByPreferredLanguage(Collection value, L for (String tag : super.getModel().getLanguagePreference()) { if (literalsByTag.containsKey(tag)) { - switch (literalMode) { - case RAW: - return new ArrayList<>(literalsByTag.get(tag)); - case VALUES_ONLY: - return literalsByTag.get(tag).stream().map(l -> - l.getValue()).collect(Collectors.toList()); - } + return new ArrayList<>(literalsByTag.get(tag)); } } // If we don't find any matches in the preferred languages, // fall back to the untagged literals (if any). Collection rawFromUntagged = literalsByTag.get(""); - switch (literalMode) { - case RAW: - return new ArrayList<>(rawFromUntagged); - case VALUES_ONLY: - return rawFromUntagged.stream().map(l -> l.getValue()) - .collect(Collectors.toList()); - default: - throw new IllegalArgumentException("Unknown literal mode " + literalMode); - } + return new ArrayList<>(rawFromUntagged); } public Collection getProperty(RDFQualifiedName pName, IEolContext context, LiteralMode literalMode) { @@ -158,13 +125,14 @@ public Collection getProperty(RDFQualifiedName pName, IEolContext contex }); } + Collection rawValues; if (pName.prefix == null) { // If no prefix was specified, watch out for ambiguity and issue warning in that case ListMultimap values = MultimapBuilder.hashKeys().arrayListValues().build(); while (itStatements.hasNext()) { Statement stmt = itStatements.next(); values.put(stmt.getPredicate().getURI(), - convertToModelObject(stmt.getObject(), literalMode)); + convertToModelObject(stmt.getObject())); } final Set distinctKeys = values.keySet(); @@ -176,15 +144,30 @@ public Collection getProperty(RDFQualifiedName pName, IEolContext contex )); } - return values.values(); + rawValues = values.values(); } else { // Prefix was specified: we don't have to worry about ambiguity final List values = new ArrayList<>(); while (itStatements.hasNext()) { Statement stmt = itStatements.next(); - values.add(convertToModelObject(stmt.getObject(), literalMode)); + values.add(convertToModelObject(stmt.getObject())); } - return values; + rawValues = values; + } + + // Filter by preferred languages if any are set + if (pName.languageTag == null && !rawValues.stream().anyMatch(p -> p instanceof RDFResource)) { + rawValues = filterByPreferredLanguage(rawValues); + } + + // Convert literals to values depending on mode + switch (literalMode) { + case VALUES_ONLY: + return convertLiteralsToValues(rawValues); + case RAW: + return rawValues; + default: + throw new IllegalArgumentException("Unknown literal mode " + literalMode); } } @@ -201,14 +184,9 @@ public String getUri() { return resource.getURI(); } - protected Object convertToModelObject(RDFNode node, LiteralMode lMode) { + protected Object convertToModelObject(RDFNode node) { if (node instanceof Literal) { - switch (lMode) { - case RAW: - return new RDFLiteral((Literal) node, this.owningModel); - case VALUES_ONLY: - return ((Literal) node).getValue(); - } + return new RDFLiteral((Literal) node, this.owningModel); } else if (node instanceof Resource) { return new RDFResource((Resource) node, this.owningModel); }