From 4e883f18872389d2cfd2957d4ba4b73e21347b93 Mon Sep 17 00:00:00 2001 From: Steve Hickman Date: Mon, 22 Jan 2024 13:50:42 -0800 Subject: [PATCH] minimalQualifiedName --- .../src/com/epistimis/uddl/UddlQNP.java | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/com.epistimis.uddl/src/com/epistimis/uddl/UddlQNP.java b/com.epistimis.uddl/src/com/epistimis/uddl/UddlQNP.java index fe63993..3305493 100644 --- a/com.epistimis.uddl/src/com/epistimis/uddl/UddlQNP.java +++ b/com.epistimis.uddl/src/com/epistimis/uddl/UddlQNP.java @@ -2,6 +2,7 @@ import org.eclipse.emf.ecore.EObject; import org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProvider; +import org.eclipse.xtext.naming.IQualifiedNameConverter; import org.eclipse.xtext.naming.QualifiedName; //import org.eclipse.xtext.xbase.scoping.XbaseQualifiedNameProvider; @@ -21,9 +22,14 @@ import com.epistimis.uddl.uddl.PlatformQueryComposition; import com.epistimis.uddl.uddl.PlatformStruct; import com.epistimis.uddl.uddl.PlatformStructMember; +import com.google.inject.Inject; public class UddlQNP extends DefaultDeclarativeQualifiedNameProvider { // XbaseQualifiedNameProvider + // Because the base class one is private + @Inject + protected IQualifiedNameConverter converter = new IQualifiedNameConverter.DefaultImpl(); + /** * Determine the QualifiedName of obj relative to ctx * @param obj @@ -49,7 +55,40 @@ public QualifiedName relativeQualifiedName return oName; } } - + + /** + * Return the QN of the object, potentially shortened by the specified prefix + * @param obj the object whose QN is to be returned. If the prefix matches the entire + * name, then return only the last segment. If the prefix ends with a wildcard, + * then match all prior segments. + * @param qnPrefix a prefix that may match part of the name of this object + * @return A Qualified name + */ + public QualifiedName minimalQualifiedName(EObject obj, String qnPrefix) { + QualifiedName oName = getFullyQualifiedName(obj); + String prefix2use = qnPrefix; + if (qnPrefix.lastIndexOf('*') == qnPrefix.length()-1) { + prefix2use = qnPrefix.substring(0,qnPrefix.length()-2); + } + QualifiedName testQN = converter.toQualifiedName(prefix2use); + + int ndx = 0; + for (String seg: testQN.getSegments()) { + // If there is a match failure at any point, return the entire oName + if (!oName.getSegment(ndx).equals(seg)) { + return oName; + } + ndx+=1; + } + // If we get here, we matched all the testQN segments. + int testSegCnt = testQN.getSegmentCount(); + if (oName.getSegmentCount() == testSegCnt) { + testSegCnt--; // reduce count by 1 so we return the last segment + } + return oName.skipFirst(testSegCnt); + + } + /** * Do these two QNs have a sequence of matching segments? * @param aqn The first QualifiedName