tmf.xml: Add mapping group to XML description
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / internal / tmf / analysis / xml / core / model / TmfXmlStateValue.java
index 35a2d0143c2ee82c2b8acceea0ba6288da01a11b..2f0360a7bb740d9b1b11852e78b4785b60fb770d 100644 (file)
 package org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model;
 
 import java.util.List;
+import java.util.Set;
 
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator;
 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
+import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.pattern.stateprovider.XmlPatternStateProvider;
 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
+import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.XmlStateProvider;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
@@ -55,6 +59,8 @@ public abstract class TmfXmlStateValue implements ITmfXmlStateValue {
 
     /* Whether this state value is an increment of the previous value */
     private final boolean fIncrement;
+    /* Whether to update the current attribute or create a new state */
+    private final boolean fUpdate;
     /* Stack value */
     private final ValueTypeStack fStackType;
     /* Forced value type */
@@ -62,6 +68,8 @@ public abstract class TmfXmlStateValue implements ITmfXmlStateValue {
 
     private final IXmlStateSystemContainer fContainer;
 
+    private final String fMappingGroup;
+
     /**
      * Different behaviors of an attribute that is to be stacked
      */
@@ -121,6 +129,9 @@ public abstract class TmfXmlStateValue implements ITmfXmlStateValue {
         /* Check if there is an increment for the value */
         fIncrement = Boolean.parseBoolean(node.getAttribute(TmfXmlStrings.INCREMENT));
 
+        /* Check if this value is an update of the ongoing state */
+        fUpdate = Boolean.parseBoolean(node.getAttribute(TmfXmlStrings.UPDATE));
+
         /* Process the XML Element state value */
         fStateValue = initializeStateValue(modelFactory, node);
 
@@ -151,6 +162,8 @@ public abstract class TmfXmlStateValue implements ITmfXmlStateValue {
          */
         String stack = node.getAttribute(TmfXmlStrings.ATTRIBUTE_STACK);
         fStackType = ValueTypeStack.getTypeFromString(stack);
+
+        fMappingGroup = node.getAttribute(TmfXmlStrings.MAPPING_GROUP);
     }
 
     /**
@@ -193,6 +206,16 @@ public abstract class TmfXmlStateValue implements ITmfXmlStateValue {
         return fIncrement;
     }
 
+    /**
+     * Return whether this value should replace the current value of the
+     * attribute or if a new state should be created.
+     *
+     * @return <code>true</code> if the value is to replace the current one
+     */
+    protected boolean isUpdate() {
+        return fUpdate;
+    }
+
     /**
      * Get the stack type of this attribute. If the attribute is to be pushed or
      * popped to a stack. The behavior of the stack attribute will depend on the
@@ -216,7 +239,31 @@ public abstract class TmfXmlStateValue implements ITmfXmlStateValue {
 
     @Override
     public ITmfStateValue getValue(@Nullable ITmfEvent event, @Nullable TmfXmlScenarioInfo scenarioInfo) throws AttributeNotFoundException {
-        return fStateValue.getValue(event, scenarioInfo);
+        return getMappedValue(event, scenarioInfo, fStateValue.getValue(event, scenarioInfo));
+    }
+
+    private ITmfStateValue getMappedValue(@Nullable ITmfEvent event, @Nullable TmfXmlScenarioInfo scenarioInfo, ITmfStateValue value) {
+        try {
+            Set<TmfXmlMapEntry> group = null;
+            if (fContainer instanceof XmlPatternStateProvider) {
+                group = ((XmlPatternStateProvider) fContainer).getMappingGroup(fMappingGroup);
+            } else if (fContainer instanceof XmlStateProvider) {
+                group = ((XmlStateProvider) fContainer).getMappingGroup(fMappingGroup);
+            }
+            if (group != null) {
+                for (TmfXmlMapEntry entry : group) {
+                    if (entry.getKey().getValue(event, scenarioInfo).equals(value)) {
+                        return entry.getValue().getValue(event, scenarioInfo);
+                    }
+                }
+            }
+            return value;
+        } catch (AttributeNotFoundException e) {
+            Activator.logError("Unable to map the state value"); //$NON-NLS-1$
+            // FIXME maybe we should return the raw state value instead of a
+            // null state value
+            return TmfStateValue.nullValue();
+        }
     }
 
     /**
@@ -382,7 +429,8 @@ public abstract class TmfXmlStateValue implements ITmfXmlStateValue {
             quark = attribute.getAttributeQuark(event, quark, scenarioInfo);
             /* the query is not valid, we stop the state change */
             if (quark == IXmlStateSystemContainer.ERROR_QUARK) {
-                throw new AttributeNotFoundException("Not found XML attribute " + attribute); //$NON-NLS-1$
+                Activator.logError("Not found XML attribute " + attribute); //$NON-NLS-1$
+                return;
             }
         }
 
This page took 0.025292 seconds and 5 git commands to generate.