Type

Type

new Type(parameters)

Description:
  • Implements a type on a structured hierarchical value.

    See the introduction for a general presentation.

    Type declaration

    • A typical declaration of a type "SomeType" and of name "mytype" with dedicated parameters is given by one of the following two constructs:
      • Here the myType is a static variable in order to be registered during the whole program execution.
    static symboling::SomeType myType("{name: myType ...}");
    
    • Here myType is allocated and remains allocated during the whole program execution, and deleted at program shutdown.
    Type::addType(new SomeType("{name: myType ...}"));
    
    • Predefined parameterized types built on (String|Numeric|Modal|Enum|List|Set|Record)Type can also be defined as data structure, e.g., for a RecordType:
    Type::addType("{type: record name: myType ...}");
    

    Predefined standard types

    • "value": that matches any predefined value, without any further structure.
    • "modal": that corresponds to the basic implementation of the ModalType.
    • "string": that corresponds to the basic implementation of the StringType.

    Implementation of a derived types

    A type defines

    • getValue(value, false): A syntactic projection from any value to a value that can be manipulated in a neighborhood of this type region.

    • getValue(value, true): A semantic projection from a syntactically correct value onto a semantically correct value within the state space region.

    • getCheckMessage(): A human readable report of what was missing in order the value to be syntactically or semantically correct, during the last getValue() call.

    • getDistance(value_1, value_2): A distance between two semantically correct values.

    • getPath(value_1, value_2): A geodesic, i.e. a path of minimal distance, between two semantically correct values.

    • compare(value_1, value_2): A comparison mechanism between two values of a given type.

    • A typical implementation of a derived type of name "mytype" is given by the following construct.

      • Here the justDone(), setDistance() and getDistance() constructs allow the implementation to always have the distance well defined before calculating the path.
      • Here the clearCheckMessage() and addCheckMessage(value, message_a_la_printf, ...), constructs allow to reset and append a standard error message, optionally with additional parameters using the printf syntax.
    #include "Type.hpp"
    namespace symboling {
      class MyType : public Type {
      public:
        MyType() : Type("{name: "MyType", parameter: value,}") {}
    
        virtual wjson::Value getValue(JSON value, bool semantically_else_syntactically = true) const {
          startCheckMessage(); // Opens the check message mechanism
          wjson::Value result = value;
          ../..
          if(result_to_be_modified) {
            ../.. // Modifies the result accordingly
            addCheckMessage(value, "explanation", ...); // Adds a human readable message about the required modification using a à-la printf format
          }
          stopCheckMessage(); // Closes the check message mechanism
          return result;
        }
    
        virtual double getDistance(JSON lhs, JSON rhs) const {
          // Cache mechanism, returns the distance without recalculating it
          if (Type::justDone(lhs, rhs)) return Type::getDistance(lhs, rhs);
          // Otherwise, calculates the distance
          double distance = NAN;
          ../..
          Type::setDistance(lhs, rhs, distance); // Registers the distance for a next call.
          return distance;
        }
    
        virtual wjson::Value getPath(JSON lhs, JSON rhs) const {
          // Cache mechanism, calculates the distance 1st if not yet done
          if (!Type::justDone(lhs, rhs)) MyType::getDistance(lhs, rhs);
          // Then, calculates the path
          ../..
          return path;
        }
    
        virtual int compare(JSON lhs, JSON rhs) const {
          // Implements the semi-order comparison between two values
        }
    
        virtual JSON getBound(unsigned int index) const {
          // Implements the dynamic calculation of the type bound of a given index
          // By contract, in the constructor:
          //  - The protected variable `bounds_count` must be calculated once, at the object construction.
          //  - The protected variable `bounds` allows to store the list of bound values, if not too many.
          //    - In that case the getBound() does not need to be overwritten.
        }
    
        virtual String asString() {
          static std::string string;
          string = Type::asString();
          // Adds complementary information related to MyType parameters and state
          ../..
          return string;
        }
      } myType;
    }
    

    ValueType specification

    Syntactic projection - Returns the value itself, no projection at this level of specification.
    Semantic projection - Returns the value itself, no projection at this level of specification.
    Distance value - Returns NAN no distance defined at this level of specification.
    Geodesic path - The geodesic is assumed to be atomic, i.e. of length 1 if value are equal and 2 if different.
    Comparison - Returns NAN no comparison possible at this level of specification.
    Bounds - The empty value is the unique bound a this level of specification.
Parameters:
Name Type Description
parameters JSON | String

The type parameters.

  • By contract, the type name is a mandatory parameter, default is "value".
    • Note: Since each name has a different type it used to define hash, less and equal function to index type in containers, it is possible to define std::unordered_map<symboling::Type, T> or std::map<symboling::Type, T>.

Methods

getValue(value, semantically_else_syntacticallyopt) → {Value}

Description:
  • Parses a string in order to decode its elements as a record item.

    • The input string is parsed as a record, when inputing a string of a suitable format.
    • The implementation uses the aidesys::regexReplace() method to:
      • Extract values from the input string using capturing parenthesis of the regex.
      • Substitute the captured value in a wJSON syntax representation of the data structure, As illustrated by this example:
    static symboling::RecordType dateStandardFormat("{"
    "name: date "
    " parseInput: '([0-9]*)-([0-9]*)-([0-9]*)' "
    " parseOutput: '{year: $1 month: $2 day: $3}' }");
    

    which corresponds to recommended international standard format for dates.

Parameters:
Name Type Attributes Default Description
value String

The input value.

semantically_else_syntactically bool <optional>
true
  • If true projects the value onto a semantically valid value with respect to this type.
  • If false projects the value onto a syntactically valid value with respect to this type.
Returns:

The projection of the value onto the a value of this type:

  • If the value is already of this type it is unchanged.
  • If the value can be mapped on a value of this type, then it is returned.
  • Otherwise a default "undefined" value or an empty value is returned.
Type
Value

getParameters() → {JSON}

Description:
  • Returns the type parameters.

Returns:

The type parameters.

Type
JSON

getValue(value, semantically_else_syntacticallyopt) → {Value}

Description:
  • Projects any value on the closest value of this type.

Parameters:
Name Type Attributes Default Description
value JSON | String

The input value.

semantically_else_syntactically bool <optional>
true
  • If true projects the value onto a semantically valid value with respect to this type.
  • If false projects the value onto a syntactically valid value with respect to this type.
Returns:

The projection of the value onto the a value of this type:

  • If the value is already of this type it is unchanged.
  • If the value can be mapped on a value of this type, then it is returned.
  • Otherwise a default "undefined" value or an empty value is returned.
Type
Value

getCheckMessage(valueopt) → {string}

Description:
  • Returns the getValue call check message.

Parameters:
Name Type Attributes Description
value Value <optional>

If specified returns the getValue call check message for this value, otherwise return the last getValue() evaluation check message.

Returns:

A human readable report of what was missing in order the value to be syntactically or semantically correct, during the last getValue call.

  • Empty, i.e. equal to "" if the value fits this type,
  • Otherwise, a human readable multi-line string with explicit errors of the form:
    • // The '$copy-of-the-value' is not of type '$type-name': $explanation
Type
string

getDistance(lhs, rhs) → {double}

Description:
  • Returns the distance between two values of this type.

Parameters:
Name Type Description
lhs Value

The left hand-size value.

rhs Value

The right hand-size value.

Returns:

The positive value of the distance or:

  • 0 if the distance itself is undefined, i.e., all values are indistinguishable. This is the default behavior.
  • DBL_MAX, for infinity, if values are not comparable or unrelated, thus at infinite distance.
Type
double

getPath(lhs, rhs) → {Array.<string>}

Description:
  • Returns a geodesic, i.e. a path of minimal distance between two values of this type.

    • By contract, a cache mechanism is to be used, to properly calculates the distance before calculating the path, see the use ofjustDone(), setDistance() and getDistance(), exemplified above.
Parameters:
Name Type Description
lhs Value

The left hand-size value.

rhs Value

The right hand-size value.

Returns:

The list of intermediate values between left and right hand-size values.

  • If lhs = rhs returns a path of length 1.
  • If lhs and rhs differs only by an atomic change returns a path of length 2. This is the default behavior.
  • Usually, several geodesics correspond to the minimal distance and the method is expected to return the ``1st one ´´ in a sense that is to make explicit for each data type.
Type
Array.<string>

compare(lhs, rhs) → {double}

Description:
  • Returns a semi-order comparison between two values of this type.

Parameters:
Name Type Description
lhs Value

The left hand-size value.

rhs Value

The right hand-size value.

Returns:

The comparison value:

  • <0 if lhs compares less than rhs.
  • >0 if lhs compares more than rhs.
  • 0 if both value are too close to be comparable.
  • NAN if both value can not be compared, this is the default.
Type
double

getBound(index) → {Value}

Description:
  • Returns the one value bound for this data type.

    • The uint getBoundsCount() function returns the precise maximal number of bounds for this type.
      • If the bounds count overflows (above 2^64) then it is set to 0, so that no bounds are available.
Parameters:
Name Type Description
index uint

The bound index.

Returns:

The bound value or the empty undefined value if out of bound.

Type
Value

getBounds(bounds_count, resetopt)

Description:
  • Returns a list of value bounds for this data type.

Parameters:
Name Type Attributes Default Description
bounds_count uint

The maximal number of bounds. If bounds_count = 0 returns all bounds of the type.

reset bool <optional>
false
  • If true, redraw the random bound selection.
  • If false, returns the last bounds calculation result, if with the same bounds_count.
Returns:

Returns a list of at most bounds_count bounds, randomly selected if bounds_count < getBoundsCount().

(static) getType(name) → {Type}

Description:
  • Returns a type of the given name.

Parameters:
Name Type Description
name String

The type name.

Returns:

The corresponding type if any, else the generic type of name "value".

Type
Type

(static) getTypeNames() → {Value}

Description:
  • Returns the list of all type names.

Returns:

The list of all type names.

Type
Value

(static) getTypeParameters() → {Value}

Description:
  • Returns the list of all type parameters.

Returns:

The list of all type parameters.

Type
Value

(static) addType(type)

Description:
  • Adds a new type from deriving som etype with dedicated parameters. A typical construct is of the form:

    Type::addType(new SomeType("{name: myType ...}"));
    
Parameters:
Name Type Description
type Type

The pointer of the type to add.

  • The object is deleted on program shutdown.
  • The type·s can be defined as
    • A JSON data structure, e.g. of the form {type: record name: myType ...}, including as a string, for type parametrizing (String|Numeric|Modal|Enum|List|Set|Record)Type.
    • A JSON list of data structures, e.g. of the form [{type: record name: myType ...}, including as a string.