Xcos ecore: fix datatype definition 04/18804/1
Clément DAVID [Fri, 9 Dec 2016 11:35:11 +0000 (12:35 +0100)]
The previous datatype definition was not fully decoded as the Model
enforce int[3] values and we decoded and set them int by int. The new
definition is also clearer from external users (type rows columns).

Change-Id: I96cf427c878fdbae63a73f28ef25dfa28137e15f

scilab/modules/scicos/includes/XMIResource.hxx
scilab/modules/scicos/src/cpp/XMIResource_load.cpp
scilab/modules/scicos/src/cpp/XMIResource_save.cpp
scilab/modules/xcos/model/Xcos.genmodel
scilab/modules/xcos/model/Xcos.xcore [deleted file]
scilab/modules/xcos/model/xcos.ecore

index ed86e7f..488ef69 100644 (file)
@@ -43,7 +43,7 @@ private:
     /*
      * Save helpers
      */
-
+    int writeDatatype(xmlTextWriterPtr writer, const std::vector<int>& datatype);
     int writePoint(xmlTextWriterPtr writer, double x, double y);
     int writeGeometry(xmlTextWriterPtr writer, ScicosID id, kind_t kind);
     int writeAbstractLayer(xmlTextWriterPtr writer, ScicosID id, kind_t kind);
@@ -63,6 +63,7 @@ private:
     int loadStringArray(xmlTextReaderPtr reader, enum object_properties_t property, const model::BaseObject& o);
     int loadEncodedStringArray(xmlTextReaderPtr reader, enum object_properties_t property, const model::BaseObject& o);
     int loadBase64(xmlTextReaderPtr reader, enum object_properties_t property, const model::BaseObject& o);
+    int loadDatatype(xmlTextReaderPtr reader, const model::BaseObject& o);
     int loadPoint(xmlTextReaderPtr reader, const model::BaseObject& o);
     int loadGeometry(xmlTextReaderPtr reader, const model::BaseObject& o);
     int loadAbstractBaseObject(xmlTextReaderPtr reader, const model::BaseObject& o);
@@ -108,6 +109,7 @@ private:
         e_blocktype,
         e_child,
         e_color,
+        e_columns,
         e_connectedSignal,
         e_context,
         e_controlPoint,
@@ -152,6 +154,7 @@ private:
         e_properties,
         e_realtimeScale,
         e_relativeTolerance,
+        e_rows,
         e_rpar,
         e_solver,
         e_sourceBlock,
index 14ef65c..042490d 100644 (file)
@@ -99,6 +99,7 @@ int XMIResource::load(const char* uri)
     constXcosNames[e_blocktype] = xmlTextReaderConstString(reader, BAD_CAST ("blocktype"));
     constXcosNames[e_child] = xmlTextReaderConstString(reader, BAD_CAST ("child"));
     constXcosNames[e_color] = xmlTextReaderConstString(reader, BAD_CAST ("color"));
+    constXcosNames[e_columns] = xmlTextReaderConstString(reader, BAD_CAST ("columns"));
     constXcosNames[e_connectedSignal] = xmlTextReaderConstString(reader, BAD_CAST ("connectedSignal"));
     constXcosNames[e_context] = xmlTextReaderConstString(reader, BAD_CAST ("context"));
     constXcosNames[e_controlPoint] = xmlTextReaderConstString(reader, BAD_CAST ("controlPoint"));
@@ -143,6 +144,7 @@ int XMIResource::load(const char* uri)
     constXcosNames[e_properties] = xmlTextReaderConstString(reader, BAD_CAST ("properties"));
     constXcosNames[e_realtimeScale] = xmlTextReaderConstString(reader, BAD_CAST ("realtimeScale"));
     constXcosNames[e_relativeTolerance] = xmlTextReaderConstString(reader, BAD_CAST ("relativeTolerance"));
+    constXcosNames[e_rows] = xmlTextReaderConstString(reader, BAD_CAST ("rows"));
     constXcosNames[e_rpar] = xmlTextReaderConstString(reader, BAD_CAST ("rpar"));
     constXcosNames[e_solver] = xmlTextReaderConstString(reader, BAD_CAST ("solver"));
     constXcosNames[e_sourceBlock] = xmlTextReaderConstString(reader, BAD_CAST ("sourceBlock"));
@@ -455,6 +457,40 @@ int XMIResource::loadBase64(xmlTextReaderPtr reader, enum object_properties_t pr
     return 1;
 }
 
+int XMIResource::loadDatatype(xmlTextReaderPtr reader, const model::BaseObject& o)
+{
+    assert(o.kind() == PORT);
+
+    std::vector<int> datatype;
+    controller.getObjectProperty(o.id(), o.kind(), DATATYPE, datatype);
+
+    // iterate on attributes
+    for (int rc = xmlTextReaderMoveToFirstAttribute(reader); rc > 0; rc = xmlTextReaderMoveToNextAttribute(reader))
+    {
+        auto found = std::find(constXcosNames.begin(), constXcosNames.end(), xmlTextReaderConstName(reader));
+        enum xcosNames current = static_cast<enum xcosNames>(std::distance(constXcosNames.begin(), found));
+        switch (current)
+        {
+            case e_type:
+                datatype[2]  = to_double(xmlTextReaderConstValue(reader));
+                break;
+            case e_rows:
+                datatype[0]  = to_double(xmlTextReaderConstValue(reader));
+                break;
+            case e_columns:
+                datatype[1]  = to_double(xmlTextReaderConstValue(reader));
+                break;
+            default:
+                // ignore other parameters
+                // TODO: Does other metamodels might be managed there ?
+                break;
+        }
+    }
+
+    controller.setObjectProperty(o.id(), o.kind(), DATATYPE, datatype);
+    return 1;
+}
+
 int XMIResource::loadPoint(xmlTextReaderPtr reader, const model::BaseObject& o)
 {
     assert(o.kind() == LINK);
@@ -757,8 +793,6 @@ int XMIResource::loadPort(xmlTextReaderPtr reader, const model::BaseObject& o)
             case e_sourceBlock:
             {
                 // not lookup needed thanks to the XML hierarchy
-                const model::BaseObject& parent = processed.back();
-                controller.setObjectProperty(o.id(), o.kind(), SOURCE_BLOCK, parent.id());
                 break;
             }
             case e_kind:
@@ -1161,6 +1195,7 @@ int XMIResource::processElement(xmlTextReaderPtr reader)
 
             // decode content
             model::BaseObject child(o, PORT);
+            processed.push_back(child);
             return loadPort(reader, child);
         }
         case e_geometry:
@@ -1242,11 +1277,7 @@ int XMIResource::processElement(xmlTextReaderPtr reader)
             return loadSimulationConfig(reader, processed.back());
         case e_datatype:
             // datatype is a Port property
-            if (!xmlTextReaderIsEmptyElement(reader))
-            {
-                parent = current;
-            }
-            return 1;
+            return loadDatatype(reader, processed.back());
         default:
             sciprint("Unknown \"%s\" element name at line %d\n", name, xmlTextReaderGetParserLineNumber(reader) - 1);
             return -1;
index 8412bed..c7ca3db 100644 (file)
@@ -165,12 +165,47 @@ static int writeBase64(xmlTextWriterPtr writer, const char* name, const std::vec
     return status;
 }
 
+int XMIResource::writeDatatype(xmlTextWriterPtr writer, const std::vector<int>& datatype)
+{
+    int status;
+
+    status = xmlTextWriterStartElement(writer, BAD_CAST("datatype"));
+    if (status == -1)
+    {
+        return status;
+    }
+
+    status = xmlTextWriterWriteAttribute(writer, BAD_CAST("type"), BAD_CAST(to_string(datatype[2]).c_str()));
+    if (status == -1)
+    {
+        return status;
+    }
+
+    status = xmlTextWriterWriteAttribute(writer, BAD_CAST("rows"), BAD_CAST(to_string(datatype[0]).c_str()));
+    if (status == -1)
+    {
+        return status;
+    }
+
+    status = xmlTextWriterWriteAttribute(writer, BAD_CAST("columns"), BAD_CAST(to_string(datatype[1]).c_str()));
+    if (status == -1)
+    {
+        return status;
+    }
+
+    status = xmlTextWriterEndElement(writer);
+    if (status == -1)
+    {
+        return status;
+    }
+
+    return status;
+}
 
 int XMIResource::writePoint(xmlTextWriterPtr writer, double x, double y)
 {
     int status;
 
-
     status = xmlTextWriterStartElement(writer, BAD_CAST("controlPoint"));
     if (status == -1)
     {
@@ -359,7 +394,7 @@ int XMIResource::writeDiagram(xmlTextWriterPtr writer)
     {
         return status;
     }
-    status = xmlTextWriterWriteAttributeNS(writer, BAD_CAST("xsi"), BAD_CAST("schemaLocation"), BAD_CAST("http://www.w3.org/2001/XMLSchema-instance"), BAD_CAST("org.scilab.modules.xcos Xcos.xcore#/EPackage"));
+    status = xmlTextWriterWriteAttributeNS(writer, BAD_CAST("xsi"), BAD_CAST("schemaLocation"), BAD_CAST("http://www.w3.org/2001/XMLSchema-instance"), BAD_CAST("org.scilab.modules.xcos xcos.ecore"));
     if (status == -1)
     {
         return status;
@@ -917,16 +952,9 @@ int XMIResource::writePort(xmlTextWriterPtr writer, enum object_properties_t con
         return status;
     }
 
-    std::vector<double> doubleArrayValue;
-    controller.getObjectProperty(id, BLOCK, DATATYPE, doubleArrayValue);
-    for (double d : doubleArrayValue)
-    {
-        status = xmlTextWriterWriteElement(writer, BAD_CAST("datatype"), BAD_CAST(to_string(d).c_str()));
-        if (status == -1)
-        {
-            return status;
-        }
-    }
+    std::vector<int> intArrayValue;
+    controller.getObjectProperty(id, PORT, DATATYPE, intArrayValue);
+    status = writeDatatype(writer, intArrayValue);
 
     status = xmlTextWriterEndElement(writer);
     if (status == -1)
index 2370b76..a01ecbd 100644 (file)
@@ -2,7 +2,7 @@
 <genmodel:GenModel xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
     xmlns:genmodel="http://www.eclipse.org/emf/2002/GenModel" modelDirectory="/xcos/src" modelPluginID="xcos" runtimeJar="true"
     modelName="Xcos" updateClasspath="false" rootExtendsClass="org.eclipse.emf.ecore.impl.MinimalEObjectImpl$Container"
-    testsDirectory="" booleanFlagsField="eFlags" booleanFlagsReservedBits="8" importerID="org.eclipse.emf.ecore.xcore.importer"
+    testsDirectory="" booleanFlagsField="eFlags" booleanFlagsReservedBits="8" importerID="org.eclipse.emf.importer.ecore"
     containmentProxies="true" complianceLevel="8.0" language="en" usedGenPackages="../../../org.eclipse.emf.ecore/model/Ecore.genmodel#//ecore"
     operationReflection="true" importOrganizing="true" decoration="Live" oSGiCompatible="true">
   <genAnnotations source="http://www.eclipse.org/emf/2002/GenModel/exporter/org.eclipse.xsd.ecore.exporter">
@@ -17,7 +17,7 @@
     </genAnnotations>
     <details key="directoryURI" value="."/>
   </genAnnotations>
-  <foreignModel>Xcos.xcore</foreignModel>
+  <foreignModel>xcos.ecore</foreignModel>
   <modelPluginVariables>org.eclipse.xtext.xbase.lib</modelPluginVariables>
   <modelPluginVariables>org.eclipse.emf.ecore.xcore.lib</modelPluginVariables>
   <genPackages prefix="Xcos" basePackage="org.scilab.modules" disposableProviderFactory="true"
@@ -59,6 +59,7 @@
     </genClasses>
     <genClasses ecoreClass="xcos.ecore#//SimulationConfig">
       <genFeatures createChild="false" ecoreFeature="ecore:EAttribute xcos.ecore#//SimulationConfig/finalTime"/>
+      <genFeatures createChild="false" ecoreFeature="ecore:EAttribute xcos.ecore#//SimulationConfig/absoluteTime"/>
       <genFeatures createChild="false" ecoreFeature="ecore:EAttribute xcos.ecore#//SimulationConfig/absoluteTolerance"/>
       <genFeatures createChild="false" ecoreFeature="ecore:EAttribute xcos.ecore#//SimulationConfig/relativeTolerance"/>
       <genFeatures createChild="false" ecoreFeature="ecore:EAttribute xcos.ecore#//SimulationConfig/timeTolerance"/>
       <genFeatures createChild="false" ecoreFeature="ecore:EAttribute xcos.ecore#//Annotation/style"/>
     </genClasses>
     <genClasses ecoreClass="xcos.ecore#//CompiledRepresentation"/>
+    <genClasses ecoreClass="xcos.ecore#//Datatype">
+      <genFeatures createChild="false" ecoreFeature="ecore:EAttribute xcos.ecore#//Datatype/typ"/>
+      <genFeatures createChild="false" ecoreFeature="ecore:EAttribute xcos.ecore#//Datatype/rows"/>
+      <genFeatures createChild="false" ecoreFeature="ecore:EAttribute xcos.ecore#//Datatype/columns"/>
+    </genClasses>
   </genPackages>
 </genmodel:GenModel>
diff --git a/scilab/modules/xcos/model/Xcos.xcore b/scilab/modules/xcos/model/Xcos.xcore
deleted file mode 100644 (file)
index 14bc452..0000000
+++ /dev/null
@@ -1,176 +0,0 @@
-package org.scilab.modules.xcos
-
-/* Encoded Scilab values */
-class Binary
-{
-       /* RFC 2045 Base64 encoded double[] values (space separated) */
-       String base64
-}
-
-/* A point is a position on the diagram */
-class Point
-{
-       /* X coordinate */
-       double x
-       /* Y coordinate */
-       double y
-}
-
-/* Rectangular coordinates of the object */
-class Geometry
-{
-       /* X coordinate */
-       double x
-       /* Y coordinate */
-       double y
-       /* Height of the object */
-       double height
-       /* Width of the object */
-       double width
-}
-
-/* The kind of a port */
-enum PortKind
-{
-       in, out, ein, eout
-}
-
-/* Shared definition for both a Diagram and a SuperBlock (eg. a Block) */
-abstract class Layer
-{
-       /* Scilab scripts added at current layer */
-       String[] context
-       /* contained objects */
-       contains BaseObject[] child opposite parent
-}
-
-/* Shared definition for an object contained in a Layer */
-abstract class BaseObject {
-       /* Unique IDentifier : usually on the universally unique identifier (UUID) form */
-       id String uid
-       
-       /* The diagram (to ease in memory representation) */
-       refers Diagram[1..1] parentDiagram
-       /* parent Layer to navigate back */
-       container Layer[1..1] parent opposite child
-}
-
-/* A diagram is the top-level object of a model. It contains simulation parameters and the hierarchical data-flow and event-flow graphs. */
-class Diagram extends Layer
-{
-       /* title of a diagram */
-       String title
-       /* Latest saved path */
-       String path
-       
-       /* Simulation settings */
-       contains SimulationConfig properties
-       /* Simulation debug level */
-       int debugLevel
-       
-       /* Version of the diagram */
-       String version
-}
-
-/* Simulation settings */
-class SimulationConfig
-{
-       /* Simulation final time (starting at 0) */
-       double finalTime
-       double absoluteTolerance
-       double relativeTolerance
-       double timeTolerance
-       double deltaT
-       double realtimeScale
-       double solver
-       double deltaH
-}
-
-/*  */
-class Block extends BaseObject, Layer
-{      
-       contains Geometry geometry
-       String description
-       String label
-       String style
-       
-       String interfaceFunction
-       String[] expression
-       contains Binary exprs
-       
-       int[] nzcross
-       int[] nmode
-       contains Binary equations
-       
-       String functionName
-       int functionAPI
-       boolean dependsOnU
-       boolean dependsOnT
-       char blocktype
-       
-       contains Port[] in
-       contains Port[] out
-       contains Port[] ein
-       contains Port[] eout
-       
-       double[] rpar
-       int[] ipar
-       contains Binary opar
-       
-       double[] state
-       double[] dstate
-       contains Binary odstate
-}
-
-class Port
-{      
-       id String uid
-       
-       int[] datatype
-       double firing
-       
-       refers Block sourceBlock
-       PortKind kind
-       boolean implicit
-       refers Link connectedSignal
-       
-       String style
-       String label
-}
-
-class Link extends BaseObject
-{      
-       refers Port sourcePort
-       refers Port destinationPort
-       
-       contains Geometry geometry
-       contains Point[] controlPoint
-       
-       String style
-       String label
-       double lineWidth
-       double lineHeight
-       int color
-}
-
-class Annotation extends BaseObject
-{
-       contains Geometry geometry
-       String description
-       String font
-       String fontSize
-       String style
-}
-
-/****************************************************************************************
- * An executable representation can be exported only if the diagram has been "compiled" *
- ****************************************************************************************/
-/*
- * 
- */
-class CompiledRepresentation
-{
-       
-}
-
index 96571a6..a607e6c 100644 (file)
         <details key="documentation" value=" Simulation final time (starting at 0)"/>
       </eAnnotations>
     </eStructuralFeatures>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="absoluteTime" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EDouble"/>
     <eStructuralFeatures xsi:type="ecore:EAttribute" name="absoluteTolerance" unique="false"
         eType="ecore:EDataType ../../../org.eclipse.emf.ecore/model/Ecore.ecore#//EDouble"/>
     <eStructuralFeatures xsi:type="ecore:EAttribute" name="relativeTolerance" unique="false"
   <eClassifiers xsi:type="ecore:EClass" name="Port">
     <eStructuralFeatures xsi:type="ecore:EAttribute" name="uid" unique="false" eType="ecore:EDataType ../../../org.eclipse.emf.ecore/model/Ecore.ecore#//EString"
         iD="true"/>
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="datatype" unique="false"
-        upperBound="-1" eType="ecore:EDataType ../../../org.eclipse.emf.ecore/model/Ecore.ecore#//EInt"/>
     <eStructuralFeatures xsi:type="ecore:EAttribute" name="firing" unique="false"
         eType="ecore:EDataType ../../../org.eclipse.emf.ecore/model/Ecore.ecore#//EDouble"/>
+    <eStructuralFeatures xsi:type="ecore:EReference" name="datatype" eType="#//Datatype"/>
     <eStructuralFeatures xsi:type="ecore:EReference" name="sourceBlock" eType="#//Block"/>
     <eStructuralFeatures xsi:type="ecore:EAttribute" name="kind" unique="false" eType="#//PortKind"/>
     <eStructuralFeatures xsi:type="ecore:EAttribute" name="implicit" unique="false"
       <details key="documentation" value="***************************************************************************************&#xA;An executable representation can be exported only if the diagram has been &quot;compiled&quot; *&#xA;**************************************************************************************"/>
     </eAnnotations>
   </eClassifiers>
+  <eClassifiers xsi:type="ecore:EClass" name="Datatype">
+    <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
+      <details key="documentation" value="Datatype of any propagated value"/>
+    </eAnnotations>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="type" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt">
+      <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
+        <details key="documentation" value="type identifier : 1 for double, 2 for complex, 3 for int32, 4 for int16, 5 for int8, 6 for uint32, 7 for uint16, 8 for uint8"/>
+      </eAnnotations>
+    </eStructuralFeatures>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="rows" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="columns" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
+  </eClassifiers>
 </ecore:EPackage>