use of org.eclipse.tracecompass.internal.tmf.analysis.xml.core.fsm.model.values.DataDrivenValueConstant in project tracecompass by tracecompass.
the class TmfXmlStateAttributeAndLocationCuTest method testValidStateAttributeCompilation.
/**
* Test the compilation of a valid state attribute strings, except locations
*
* @throws SAXException
* Exception thrown by parser
* @throws IOException
* Exception thrown by parser
* @throws ParserConfigurationException
* Exception thrown by parser
*/
@Test
public void testValidStateAttributeCompilation() throws SAXException, IOException, ParserConfigurationException {
String[] validStrings = { "<stateAttribute type=\"null\" />", "<stateAttribute type=\"constant\" value=\"42\" />", "<stateAttribute type=\"eventField\" value=\"myfield\" />", "<stateAttribute type=\"eventName\" />", "<stateAttribute type=\"eventName\" value=\"ignored\" />", "<stateAttribute type=\"query\" ><stateAttribute type=\"constant\" value=\"queryPath\"/></stateAttribute>", "<stateAttribute type=\"self\" />", "<stateAttribute type=\"pool\" />" };
DataDrivenValue[] generated = { TmfXmlTestUtils.NULL_VALUE, new DataDrivenValueConstant(null, ITmfStateValue.Type.NULL, "42"), new DataDrivenValueEventField(null, ITmfStateValue.Type.NULL, "myfield"), new DataDrivenValueEventName(null), new DataDrivenValueEventName(null), new DataDrivenValueQuery(null, ITmfStateValue.Type.NULL, new DataDrivenStateSystemPath(ImmutableList.of(new DataDrivenValueConstant(null, ITmfStateValue.Type.NULL, "queryPath")), IBaseQuarkProvider.IDENTITY_BASE_QUARK)), new DataDrivenValueSelf(ITmfStateValue.Type.NULL), DataDrivenValuePool.getInstance() };
for (int i = 0; i < validStrings.length; i++) {
String validString = validStrings[i];
DataDrivenValue runtimeObj = generated[i];
Element xmlElement = TmfXmlTestUtils.getXmlElement(TmfXmlStrings.STATE_ATTRIBUTE, validString);
assertNotNull(xmlElement);
List<@NonNull TmfXmlStateValueCu> compileAttribute = TmfXmlStateValueCu.compileAttribute(ANALYSIS_DATA, xmlElement);
assertNotNull(validString, compileAttribute);
assertEquals("Number of attributes", 1, compileAttribute.size());
TmfXmlStateValueCu value = compileAttribute.get(0);
assertEquals("Expected attribute", runtimeObj, value.generate());
}
}
use of org.eclipse.tracecompass.internal.tmf.analysis.xml.core.fsm.model.values.DataDrivenValueConstant in project tracecompass by tracecompass.
the class TmfXmlActionCu method compileSegmentAction.
@Nullable
private static TmfXmlActionCu compileSegmentAction(AnalysisCompilationData analysisData, Element node) {
// Compile the segment type
List<@NonNull Element> segmentType = TmfXmlUtils.getChildElements(node, TmfXmlStrings.SEGMENT_TYPE);
if (segmentType.size() != 1) {
// TODO: Validation message here
// $NON-NLS-1$ //$NON-NLS-2$
Activator.logWarning("Segment action: There should be one and only one " + TmfXmlStrings.SEGMENT_TYPE + " element.");
return null;
}
Element typeElement = Objects.requireNonNull(segmentType.get(0));
String segmentName = typeElement.getAttribute(TmfXmlStrings.SEGMENT_NAME);
TmfXmlStateValueCu segmentTypeValue = null;
if (!segmentName.isEmpty()) {
// Constant name for the segment type
segmentTypeValue = new TmfXmlStateValueCu(() -> new DataDrivenValueConstant(null, ITmfStateValue.Type.NULL, segmentName));
} else {
List<Element> nameElements = TmfXmlUtils.getChildElements(typeElement, TmfXmlStrings.SEGMENT_NAME);
if (nameElements.size() != 1) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("Segment type: You need to either give a segName attribute or define one and only one <segName> element with child <stateValue> to get the type name");
return null;
}
Element segNameElement = Objects.requireNonNull(nameElements.get(0));
List<Element> nameValueElements = TmfXmlUtils.getChildElements(segNameElement, TmfXmlStrings.STATE_VALUE);
if (nameValueElements.size() != 1) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("Segment type: the <segName> element should have one and only one <stateValue> child");
return null;
}
Element segNameValueElement = Objects.requireNonNull(nameValueElements.get(0));
segmentTypeValue = TmfXmlStateValueCu.compileValue(analysisData, segNameValueElement);
}
if (segmentTypeValue == null) {
return null;
}
// Compile the optional time element
TmfXmlStateValueCu beginTimeValue = null;
TmfXmlStateValueCu durationValue = null;
TmfXmlStateValueCu endTimeValue = null;
List<Element> timeElements = TmfXmlUtils.getChildElements(node, TmfXmlStrings.SEGMENT_TIME);
if (timeElements.size() > 1) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logWarning("Segment time: there should be only one <segTime> element");
}
if (timeElements.size() > 0) {
Element segTimeElement = Objects.requireNonNull(timeElements.get(0));
List<Element> beginElements = TmfXmlUtils.getChildElements(segTimeElement, TmfXmlStrings.BEGIN);
if (beginElements.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("Segment time: there should be one <begin> element to describe segment start time");
return null;
}
if (beginElements.size() > 1) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logWarning("Segment time: there should be only one <begin> element");
}
beginTimeValue = TmfXmlStateValueCu.compileValue(analysisData, Objects.requireNonNull(beginElements.get(0)));
if (beginTimeValue == null) {
return null;
}
beginTimeValue = new TmfXmlStateValueCu(new TmfXmlStateValueCu.ValueWrapperGenerator(beginTimeValue, ITmfStateValue.Type.LONG));
// Validate end time or duration
List<Element> durationElements = TmfXmlUtils.getChildElements(segTimeElement, TmfXmlStrings.DURATION);
List<Element> endElements = TmfXmlUtils.getChildElements(segTimeElement, TmfXmlStrings.END);
if (durationElements.isEmpty() && endElements.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("Segment time: there should be either a <duration> or <end> element");
return null;
}
if (!durationElements.isEmpty() && !endElements.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("Segment time: only one of <duration> or <end> shoud be present");
return null;
}
if (durationElements.size() > 1) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logWarning("Segment time: there should be only one <duration> element");
}
if (endElements.size() > 1) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logWarning("Segment time: there should be only one <end> element");
}
if (!durationElements.isEmpty()) {
durationValue = TmfXmlStateValueCu.compileValue(analysisData, Objects.requireNonNull(durationElements.get(0)));
if (durationValue == null) {
return null;
}
durationValue = new TmfXmlStateValueCu(new TmfXmlStateValueCu.ValueWrapperGenerator(durationValue, ITmfStateValue.Type.LONG));
} else {
endTimeValue = TmfXmlStateValueCu.compileValue(analysisData, Objects.requireNonNull(endElements.get(0)));
if (endTimeValue == null) {
return null;
}
endTimeValue = new TmfXmlStateValueCu(new TmfXmlStateValueCu.ValueWrapperGenerator(endTimeValue, ITmfStateValue.Type.LONG));
}
}
// Compile the segment fields
List<Element> contentElements = TmfXmlUtils.getChildElements(node, TmfXmlStrings.SEGMENT_CONTENT);
Map<String, TmfXmlStateValueCu> fieldMap = new HashMap<>();
for (Element contentEl : contentElements) {
List<Element> fieldElements = TmfXmlUtils.getChildElements(contentEl, TmfXmlStrings.SEGMENT_FIELD);
for (Element fieldEl : fieldElements) {
String name = fieldEl.getAttribute(TmfXmlStrings.NAME);
if (name.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("Segment field does not have a name");
return null;
}
if (fieldMap.containsKey(name)) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("Redefinition of field " + name);
return null;
}
TmfXmlStateValueCu fieldCu = TmfXmlStateValueCu.compileSegmentField(analysisData, fieldEl);
if (fieldCu == null) {
return null;
}
fieldMap.put(name, fieldCu);
}
}
return new TmfXmlActionSegmentCu(segmentTypeValue, beginTimeValue, durationValue, endTimeValue, fieldMap);
}
use of org.eclipse.tracecompass.internal.tmf.analysis.xml.core.fsm.model.values.DataDrivenValueConstant in project tracecompass by tracecompass.
the class TmfXmlStateValueCu method compileAttribute.
/**
* Compile a stateAttribute XML element. It returns a list since some
* attributes may link to location values that are replaced by the
* corresponding attribute values.
*
* @param analysisData
* The analysis data already compiled
* @param valueEl
* The XML element to compile
* @return A list of value compilation units this element compiles to, or
* <code>null</code> if there was a compilation error
*/
@Nullable
public static List<TmfXmlStateValueCu> compileAttribute(AnalysisCompilationData analysisData, Element valueEl) {
String type = valueEl.getAttribute(TmfXmlStrings.TYPE);
Type forcedType = ITmfStateValue.Type.NULL;
switch(type) {
case TmfXmlStrings.TYPE_CONSTANT:
{
String name = getValueString(analysisData, valueEl);
if (name == null || name.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("The value of a constant attribute should not be empty");
return null;
}
if (name.equals(CURRENT_SCENARIO)) {
return Collections.singletonList(CURRENT_SCENARIO_QUARK);
}
TmfXmlStateValueCu tmfXmlStateValueCu = new TmfXmlStateValueCu(() -> new DataDrivenValueConstant(null, forcedType, name));
return Collections.singletonList(tmfXmlStateValueCu);
}
case TmfXmlStrings.EVENT_FIELD:
{
String name = getValueString(analysisData, valueEl);
if (name == null || name.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("The value of an event field attribute should not be null");
return null;
}
return Collections.singletonList(new TmfXmlStateValueCu(() -> new DataDrivenValueEventField(null, forcedType, name)));
}
case TmfXmlStrings.TYPE_LOCATION:
{
String name = getValueString(analysisData, valueEl);
if (name == null || name.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("The value of a location attribute should not be null");
return null;
}
TmfXmlLocationCu location = analysisData.getLocation(name);
if (location == null) {
// TODO: Validation message here
// $NON-NLS-1$ //$NON-NLS-2$
Activator.logError("Location " + name + " does not exist");
return null;
}
return location.getValues();
}
case TmfXmlStrings.TYPE_QUERY:
{
List<Element> childElements = TmfXmlUtils.getChildElements(valueEl, TmfXmlStrings.STATE_ATTRIBUTE);
if (childElements.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("A query state attribute should have children attributes");
return null;
}
TmfXmlStateSystemPathCu path = TmfXmlStateSystemPathCu.compile(analysisData, childElements);
if (path == null) {
return null;
}
return Collections.singletonList(new TmfXmlStateValueCu(new StateValueQueryGenerator(path, null, forcedType)));
}
case TmfXmlStrings.TYPE_EVENT_NAME:
return Collections.singletonList(new TmfXmlStateValueCu(() -> new DataDrivenValueEventName(null)));
case TmfXmlStrings.TYPE_NULL:
return Collections.singletonList(new TmfXmlStateValueCu(() -> new DataDrivenValueConstant(null, forcedType, null)));
case TmfXmlStrings.TYPE_SELF:
return Collections.singletonList(new TmfXmlStateValueCu(() -> new DataDrivenValueSelf(forcedType)));
case TmfXmlStrings.TYPE_POOL:
return Collections.singletonList(new TmfXmlStateValueCu(() -> DataDrivenValuePool.getInstance()));
default:
// $NON-NLS-1$
Activator.logError("Compiling state value: The XML element is not of the right type " + type);
}
return null;
}
use of org.eclipse.tracecompass.internal.tmf.analysis.xml.core.fsm.model.values.DataDrivenValueConstant in project tracecompass by tracecompass.
the class TmfXmlStateValueCu method compileSegmentField.
/**
* Compile a segment field element into a state value
*
* @param analysisData
* The analysis data already compiled
* @param element
* The XML element to compile
* @return The state value compilation unit
*/
@Nullable
public static TmfXmlStateValueCu compileSegmentField(AnalysisCompilationData analysisData, Element element) {
String typeStr = element.getAttribute(TmfXmlStrings.TYPE);
String name = element.getAttribute(TmfXmlStrings.NAME);
if (typeStr.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("Segment field: missing 'type' attribute for field " + name);
return null;
}
ITmfStateValue.Type type = TmfXmlUtils.getTmfStateValueByName(typeStr);
if (type == null) {
// TODO: Validation message here
Activator.logError(// $NON-NLS-1$
"The given type name \"" + typeStr + // $NON-NLS-1$
"\" does not correspond to any ITmfStateValue.Type");
return null;
}
String constantValue = getValueString(analysisData, element);
List<Element> childElements = TmfXmlUtils.getChildElements(element, TmfXmlStrings.STATE_VALUE);
if (type.equals(Type.NULL)) {
if (constantValue != null && !constantValue.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logWarning("Segment field: type is null, the constant value will be ignored");
}
if (!childElements.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logWarning("Segment field: type is null, the <stateValue> element will be ignored");
}
return new TmfXmlStateValueCu(() -> new DataDrivenValueConstant(null, type, null));
}
if (constantValue == null || constantValue.isEmpty()) {
// No constant field, look at the state value element
if (childElements.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("Segment field: there should be either a 'value' attribute or <stateValue> element for type " + typeStr);
return null;
}
if (childElements.size() > 1) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logWarning("Segment field: there should be only one <stateValue> element under the field");
}
TmfXmlStateValueCu valueCu = compileValue(analysisData, Objects.requireNonNull(childElements.get(0)));
if (valueCu == null) {
return null;
}
return new TmfXmlStateValueCu(new ValueWrapperGenerator(valueCu, type));
}
// Create a constant state value with forced type
if (!childElements.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logWarning("Segment field: type is null, the <stateValue> element will be ignored");
}
Object value = convertValueToType(constantValue, type);
if (value == null) {
return null;
}
return new TmfXmlStateValueCu(() -> new DataDrivenValueConstant(null, type, value));
}
use of org.eclipse.tracecompass.internal.tmf.analysis.xml.core.fsm.model.values.DataDrivenValueConstant in project tracecompass by tracecompass.
the class TmfXmlStateValueCu method compileValue.
/**
* Compile a stateValue XML element
*
* @param analysisData
* The analysis data already compiled
* @param valueEl
* The XML element to compile
* @return The value compilation unit or <code>null</code> if there were
* compilation errors
*/
@Nullable
public static TmfXmlStateValueCu compileValue(AnalysisCompilationData analysisData, Element valueEl) {
String type = valueEl.getAttribute(TmfXmlStrings.TYPE);
/*
* Stack peek will have a separate treatment here, as the state value
* may have sub attributes
*
* FIXME: This case should not be supported this way, have a special
* type for stack peek and remove the peek stack action..
*/
String stack = valueEl.getAttribute(TmfXmlStrings.ATTRIBUTE_STACK);
StackAction stackAction = DataDrivenActionStateChange.StackAction.getTypeFromString(stack);
if (stackAction == StackAction.PEEK) {
type = TmfXmlStrings.STACK_PEEK;
}
// Verify mapping group data
String mapGroupAttrib = valueEl.getAttribute(TmfXmlStrings.MAPPING_GROUP);
TmfXmlMappingGroupCu mappingGroup = analysisData.getMappingGroup(mapGroupAttrib);
// Make sure the mapping group exists
if (!mapGroupAttrib.isEmpty() && mappingGroup == null) {
// TODO: Validation message here
// $NON-NLS-1$ //$NON-NLS-2$
Activator.logError("The mapping group " + mapGroupAttrib + " does not exist in this analysis");
return null;
}
String mappingGroupId = (mapGroupAttrib.isEmpty() ? null : mapGroupAttrib);
/*
* Forced type allows to convert the value to a certain type : For
* example, a process's TID in an event field may arrive with a LONG
* format but we want to store the data in an INT
*/
String forcedTypeName = valueEl.getAttribute(TmfXmlStrings.FORCED_TYPE);
ITmfStateValue.Type forcedType = forcedTypeName.isEmpty() ? ITmfStateValue.Type.NULL : TmfXmlUtils.getTmfStateValueByName(forcedTypeName);
if (forcedType == null) {
// TODO: Validation message here
Activator.logError(// $NON-NLS-1$
"The given type name \"" + forcedType + // $NON-NLS-1$
"\" does not correspond to any ITmfStateValue.Type");
return null;
}
switch(type) {
case TmfXmlStrings.TYPE_INT:
{
if (mappingGroup != null) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logWarning("state value is type int but a mappingGroup is specified");
}
String value = getValueString(analysisData, valueEl);
if (value == null) {
return null;
}
Object intValue = convertValueToType(value, Type.INTEGER);
if (intValue == null) {
return null;
}
return new TmfXmlStateValueCu(() -> new DataDrivenValueConstant(mappingGroupId, forcedType, intValue));
}
case TmfXmlStrings.TYPE_LONG:
{
if (mappingGroup != null) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logWarning("state value is type long but a mappingGroup is specified");
}
String value = getValueString(analysisData, valueEl);
if (value == null) {
return null;
}
Object longValue = convertValueToType(value, Type.LONG);
if (longValue == null) {
return null;
}
return new TmfXmlStateValueCu(() -> new DataDrivenValueConstant(mappingGroupId, forcedType, longValue));
}
case TmfXmlStrings.TYPE_STRING:
{
String value = getValueString(analysisData, valueEl);
return new TmfXmlStateValueCu(() -> new DataDrivenValueConstant(mappingGroupId, forcedType, value));
}
case TmfXmlStrings.TYPE_NULL:
{
if (mappingGroup != null) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logWarning("state value is type null but a mappingGroup is specified");
}
return new TmfXmlStateValueCu(() -> new DataDrivenValueConstant(mappingGroupId, forcedType, null));
}
case TmfXmlStrings.EVENT_FIELD:
{
String name = getValueString(analysisData, valueEl);
if (name == null || name.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("The value of an event field attribute should not be null");
return null;
}
return new TmfXmlStateValueCu(() -> new DataDrivenValueEventField(mappingGroupId, forcedType, name));
}
case TmfXmlStrings.TYPE_EVENT_NAME:
return new TmfXmlStateValueCu(() -> new DataDrivenValueEventName(mappingGroupId));
case TmfXmlStrings.TYPE_DELETE:
{
if (mappingGroup != null) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logWarning("state value is type delete but a mappingGroup is specified");
}
return new TmfXmlStateValueCu(() -> new DataDrivenValueConstant(mappingGroupId, forcedType, null));
}
case TmfXmlStrings.TYPE_QUERY:
{
List<Element> childElements = TmfXmlUtils.getChildElements(valueEl, TmfXmlStrings.STATE_ATTRIBUTE);
if (childElements.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("A query state value should have children attributes");
return null;
}
TmfXmlStateSystemPathCu path = TmfXmlStateSystemPathCu.compile(analysisData, childElements);
if (path == null) {
return null;
}
return new TmfXmlStateValueCu(new StateValueQueryGenerator(path, mappingGroupId, forcedType));
}
case TmfXmlStrings.TYPE_SCRIPT:
{
List<Element> childElements = TmfXmlUtils.getChildElements(valueEl, TmfXmlStrings.STATE_VALUE);
Map<String, TmfXmlStateValueCu> values = new HashMap<>();
for (Element subAttributeNode : childElements) {
// state values in a script should have an ID
String valueId = subAttributeNode.getAttribute(TmfXmlStrings.ID);
TmfXmlStateValueCu subAttrib = TmfXmlStateValueCu.compileValue(analysisData, subAttributeNode);
if (subAttrib == null) {
return null;
}
values.put(valueId, subAttrib);
}
String script = getValueString(analysisData, valueEl);
if (script == null) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logError("The script resolves to null");
return null;
}
String scriptEngine = valueEl.getAttribute(TmfXmlStrings.SCRIPT_ENGINE);
if (scriptEngine.isEmpty()) {
scriptEngine = DataDrivenValueScript.DEFAULT_SCRIPT_ENGINE;
}
return new TmfXmlStateValueCu(new StateValueScriptGenerator(values, script, scriptEngine, mappingGroupId, forcedType));
}
case TmfXmlStrings.STACK_PEEK:
{
// A stack peek is like a query at the top of the stack
List<Element> childElements = TmfXmlUtils.getChildElements(valueEl, TmfXmlStrings.STATE_ATTRIBUTE);
if (childElements.isEmpty()) {
// TODO: Validation message here
// $NON-NLS-1$
Activator.logWarning("Compiling state value: Stack peek should have children state attributes");
}
TmfXmlStateSystemPathCu path = TmfXmlStateSystemPathCu.compile(analysisData, childElements);
if (path == null) {
return null;
}
return new TmfXmlStateValueCu(new StateValueStackPeekGenerator(path, mappingGroupId, forcedType));
}
default:
// $NON-NLS-1$
Activator.logError("Compiling state value: The XML element is not of the right type " + type);
}
return null;
}
Aggregations