From 0fdd2c45ddd2701a59cbfef691afe5b7b15496f5 Mon Sep 17 00:00:00 2001 From: Francis Giraldeau Date: Tue, 25 Mar 2014 12:00:48 -0400 Subject: [PATCH] TMF: Add get parent to state system It is possible to get the children of an attribute. This patch add also required methods to query the parent attributes. * Add getParent() method to Attribute, AttributeTree and StateSystem * Add unit test to check the functionality * Provides the required documentation Change-Id: I635326068c2a298b32952599e09b2426b2e1fbb0 Signed-off-by: Francis Giraldeau Reviewed-on: https://git.eclipse.org/r/24026 Tested-by: Hudson CI Reviewed-by: Alexandre Montplaisir IP-Clean: Alexandre Montplaisir Tested-by: Alexandre Montplaisir Reviewed-by: Genevieve Bastien IP-Clean: Genevieve Bastien Tested-by: Genevieve Bastien --- .../tests/stateprovider/StateSystemTest.java | 21 +++++++++++++++++++ .../tmf/core/statesystem/Attribute.java | 18 ++++++++++++++++ .../tmf/core/statesystem/AttributeTree.java | 16 ++++++++++++++ .../tmf/core/statesystem/StateSystem.java | 5 +++++ .../tmf/core/statesystem/ITmfStateSystem.java | 11 ++++++++++ 5 files changed, 71 insertions(+) diff --git a/org.eclipse.linuxtools.lttng2.kernel.core.tests/src/org/eclipse/linuxtools/lttng2/kernel/core/tests/stateprovider/StateSystemTest.java b/org.eclipse.linuxtools.lttng2.kernel.core.tests/src/org/eclipse/linuxtools/lttng2/kernel/core/tests/stateprovider/StateSystemTest.java index 2f46cc34e4..8a3708a71a 100644 --- a/org.eclipse.linuxtools.lttng2.kernel.core.tests/src/org/eclipse/linuxtools/lttng2/kernel/core/tests/stateprovider/StateSystemTest.java +++ b/org.eclipse.linuxtools.lttng2.kernel.core.tests/src/org/eclipse/linuxtools/lttng2/kernel/core/tests/stateprovider/StateSystemTest.java @@ -422,4 +422,25 @@ public abstract class StateSystemTest { fail(); } } + + @Test + public void testParentAttribute() { + String[] path = { "CPUs/0/Current_thread", + "CPUs/0", + "CPUs" }; + try { + int q = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); + for (int i = 0; i < path.length; i++) { + String name = ssq.getFullAttributePath(q); + assertEquals(path[i], name); + q = ssq.getParentAttributeQuark(q); + } + assertEquals(-1, q); + q = ssq.getParentAttributeQuark(q); + assertEquals(-1, q); + } catch (AttributeNotFoundException e) { + fail(); + } + } + } diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/Attribute.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/Attribute.java index 4cd830dd5c..6f42a3a34b 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/Attribute.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/Attribute.java @@ -129,6 +129,24 @@ public abstract class Attribute { return targetNode.getQuark(); } + /** + * Get the parent attribute of this attribute + * + * @return The parent attribute + */ + public Attribute getParentAttribute() { + return this.parent; + } + + /** + * Get the parent quark of this attribute + * + * @return The quark of the parent attribute + */ + public int getParentAttributeQuark() { + return this.parent.getQuark(); + } + /* The methods how to access children are left to derived classes */ /** diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/AttributeTree.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/AttributeTree.java index 3d897c69e3..958c867dfc 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/AttributeTree.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/AttributeTree.java @@ -357,6 +357,22 @@ public final class AttributeTree { return listOfChildren; } + /** + * Returns the parent quark of the attribute. The root attribute has no + * parent and will return -1 + * + * @param quark + * The quark of the attribute + * @return Quark of the parent attribute or -1 for the root + * attribute + */ + public int getParentAttributeQuark(int quark) { + if (quark == -1) { + return quark; + } + return attributeList.get(quark).getParentAttributeQuark(); + } + private void addSubAttributes(List list, Attribute curAttribute, boolean recursive) { for (Attribute childNode : curAttribute.getSubAttributes()) { diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/StateSystem.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/StateSystem.java index 981467fe23..00871e846f 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/StateSystem.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/StateSystem.java @@ -277,6 +277,11 @@ public class StateSystem implements ITmfStateSystemBuilder { return ret; } + @Override + public int getParentAttributeQuark(int quark) { + return getAttributeTree().getParentAttributeQuark(quark); + } + @Override public List getQuarks(String... pattern) { List quarks = new LinkedList<>(); diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/ITmfStateSystem.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/ITmfStateSystem.java index 4682c15fd4..05f662b3d9 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/ITmfStateSystem.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/ITmfStateSystem.java @@ -248,6 +248,17 @@ public interface ITmfStateSystem { */ String getFullAttributePath(int attributeQuark); + /** + * Returns the parent quark of the attribute. + * + * @param attributeQuark + * The quark of the attribute + * @return Quark of the parent attribute or -1 if root quark or + * no parent. + * @since 3.0 + */ + int getParentAttributeQuark(int attributeQuark); + /** * @name Query methods */ -- 2.34.1