ISO 10303-21:2016(E)

Annex F
(normative)

ECMAScript binding for the anchor section

F.1 Introduction

The binding described in this annex maps the anchor section of an exchange structure to ECMAScript objects.

NOTE 1      ECMAScript is the term used in ISO/IEC 16262 for the language more commonly known as JavaScript.

NOTE 2      The anchor items may refer to data instances in the body of the exchange structure.

The binding enables the materialization of the data referenced by the anchors as objects that can be processed in an application.

The binding is defined as a set of functions that operate on a context defined by an object called the P21 object.

F.2 Required properties of the P21 object

The P21 object delivered by the functions that read an exchange structure shall have one property for each anchor in the exchange structure. This property shall have the same name as the anchor.

EXAMPLE 1      The following code uses a synchronous read function to read an exchange structure and access an anchor called geometry.

function read_model_geometry()
{
    var model = read_model("example.p21");
    return model.geometry;
} 

NOTE 1     The read_model function is only an example. Implementations may prefer to use asynchronous read functionality to get better performance.

When an application reads an exchange structure it shall determine what anchors are expected from the file_name.name attribute of the header section. If two exchange structures have the same file_name.name then each anchor with the same name shall have the same representation.

NOTE 2     For example, if the filename.name attribute has the value "workingstep.paths" then the exchange structure may be expected to have anchors representing toolpaths.

F.3 Anchor value mappings

Each anchor shall have properties with the following descriptions.

EXAMPLE 2      The ECMAScript expression "model.first.$value = new P21.Integer (10);" is equivalent to the following exchange structure code.

ANCHOR;
<first> = 10;
ENDSEC;

EXAMPLE 3      The two ECMAScript expressions "model.second.$value = new P21.Real (10);" and "model.second.$third = new P21.String ("10");" are equivalent to the following exchange structure code.

ANCHOR;
<second> = 10. {third:'10'};
ENDSEC;

Each anchor value shall be represented as a P21.Wrapper object. Each instance shall be specialized to one of the subtypes of P21.Wrapper listed below. The P21.Wrapper object shall have a method called toP21String() in addition to the toString() and valueOf() methods prescribed by ECMAScript.

The toP21String() method shall return the ECMAScript string value '$' if the instance has not been specialized.

NOTE      In ECMAScript the underlying value of a wrapper is accessed implicitly when necessary using the valueOf() method, and a printable representation is accessed implicitly when necessary using the toString() method.

F.3.1 integer mapping

A property defined by an INTEGER shall be mapped to a P21.Integer object. The valueOf() method shall return the integer literal represented as an ECMAScript number. The toString() method shall return the ECMAScript string representation of the ECMAScript number. The toP21String() method shall return a string representation of the ECMAScript number that meets the requirements defined by this part of ISO 10303 for an integer literal.

NOTE      Multiple examples in this annex assume that a P21 object has been read and assigned to an ECMAScript variable called model.

EXAMPLE      The ECMAScript expression "model.example.$value = new P21.Integer (10);" is equivalent to the following exchange structure code.

ANCHOR;
..
<example> = 10;
..
ENDSEC;

NOTE      There is no integer type in ECMAScript. All numbers have the type number.

F.3.2 real mapping

A property defined by a REAL shall be mapped to a P21.Real object. The valueOf() method shall return the real literal represented as an ECMAScript number. The toString() method shall return the ECMAScript string representation of the ECMAScript number. The toP21String() method shall return a string representation of the ECMAScript number that meets the requirements defined by this part of ISO 10303 for a real literal.

EXAMPLE      The ECMAScript expression "model.example.$value = new P21.Real (10);" is equivalent to the following exchange structure code.

ANCHOR;
..
<example> = 10.;
..
ENDSEC;

NOTE      This part of ISO 10303 requires the representation of real numbers to include a ".".

F.3.3 string mapping

A property defined by a STRING shall be mapped to a P21.String object. The valueOf() method shall return the string literal without the first and last "'" characters. The toString() method shall return the same result as the valueOf() method. The toP21String() method shall return a string that meets the requirements defined by this part of ISO 10303 for a string literal. This string shall include the first and last "'" characters.

EXAMPLE      The ECMAScript expression "model.example.$value = new P21.String ('This is a message');" is equivalent to the following exchange structure code.

ANCHOR;
..
<example> = 'This is a message';
..
ENDSEC;

F.3.4 enumeration mapping

A property defined by an ENUMERATION shall be mapped to a P21.Enumeration object. The valueOf() method shall return the ECMAScript value true if the exchange structure value is ".T.", the ECMAScript value false if the exchange structure value is ".F.", and the enumeration represented as an ECMAScript string with the first and final "." characters omitted otherwise. The toString() method shall return the ECMAScript result of applying the toString() method to the result of the valueOf() method. The toP21String () method shall return the enumeration string defined by this part of ISO 10303.

EXAMPLE 1      The ECMAScript expression "model.example.$value = new P21.Enumeration ('RED');" is equivalent to the following exchange structure code.

ANCHOR;
..
<example> = .RED.;
..
ENDSEC;

EXAMPLE 2      The ECMAScript expression "model.example.$value = new P21.Enumeration (true);" is equivalent to the following exchange structure code.

ANCHOR;
..
<example> = .T.;
..
ENDSEC;

NOTE 1      The EXPRESS BOOLEAN and LOGICAL types represent true and false as ".T." and ".F.". In principle other enumerations can also use these values. If so the mapping may produce unexpected results but no information will be lost.

NOTE 2      The ECMAScript environment implicitly converts null values to false in boolean expressions. This would be an issue for OPTIONAL BOOLEAN values in an SDAI implementation but is the expected behavior for this part of ISO 10303.

F.3.5 binary mapping

A property defined by a BINARY shall be mapped to a P21.Binary object. The valueOf() method shall return the binary literal represented as an ECMAScript string with the first and last quote characters omitted. The toString() method shall produce the same result. The toP21String() method shall produce a string with the first and last quote characters in place.

EXAMPLE      The ECMAScript expression "model.example.$value = new P21.Binary ('0123456789ABCDEF');" is equivalent to the following exchange structure code.

ANCHOR;
..
<example> = "0123456789ABCDEF";
..
ENDSEC;

F.3.6 entity instance name mapping

A property that is defined by a ENTITY_INSTANCE_NAME shall be mapped to a P21.EID object. The valueOf() method shall return an encoding of the entity addressed by the entity instance name or NULL.

NOTE      An anchor defined by an entity should be given an application specific ECMAScript mapping.

The toString() method shall produce the entity instance name without the initial "#" character encoded as an ECMAScript string. The toP21String() method shall produce a string with the encoding described by this part of ISO 10303 that includes the initial "#" character.

EXAMPLE      The ECMAScript expression "model.example.$value = new P21.EID ('20');" is equivalent to the following exchange structure code.

ANCHOR;
..
<example> = #20;
..
ENDSEC;

F.3.7 value instance name mapping

A property defined by an VALUE_INSTANCE_NAME shall be mapped to a P21.VID object. The valueOf() method shall return an encoding for the value addressed by the value instance name.

The toString() method shall produce the value instance name without the initial "@" character encoded as an ECMAScript string. The toP21String() method shall produce a string with the encoding described by this part of ISO 10303 that includes the initial "@" character.

EXAMPLE      The ECMAScript expression "model.example.$value = new P21.VID ('20');" is equivalent to the following exchange structure code.

ANCHOR;
..
<example> = @20;
..
ENDSEC;

F.3.8 constant instance name mapping

A property that is defined by a CONSTANT_ENTITY_NAME shall be mapped to a P21.CIN object. The valueOf() method shall return an encoding of the entity addressed by the entity instance name or NULL.

NOTE      For an anchor defined by a EXPRESS constant the name of the constant is probably more useful than the value.

The toString() method shall produce the entity instance name without the initial "#" character encoded as an ECMAScript string. The toP21String() method shall produce a string with the encoding described by this part of ISO 10303 that includes the initial "#" character.

EXAMPLE      The ECMAScript expression "model.example.$value = new P21.CIN ('INCH');" is equivalent to the following exchange structure code.

ANCHOR;
..
<example> = #INCH;
..
ENDSEC;

F.3.9 constant value name mapping

A property defined by an CONSTANT_VALUE_NAME shall be mapped to a P21.CVN object. The valueOf() method shall return an encoding for the value addressed by the constant value name.

The toString() method shall produce the value instance name without the initial "@" character encoded as an ECMAScript string. The toP21String() method shall produce a string with the encoding described by this part of ISO 10303 that includes the initial "@" character.

EXAMPLE      The ECMAScript expression "model.example.$value = new P21.CVN ('PI');" is equivalent to the following exchange structure code.

ANCHOR;
..
<example> = @PI;
..
ENDSEC;

F.3.10 null value mapping

A property defined by a null value ("$") shall be mapped to an ECMAScript null.

EXAMPLE      The ECMAScript expression "model.example.$value = null;" is equivalent to the following exchange structure code.

ANCHOR;
..
<example> = $;
..
ENDSEC;

F.3.11 anchor item list mapping

A property that is defined by a ANCHOR_ITEM_LIST shall be mapped to a P21.List object. The valueOf() method shall return an ECMAScript array with each member of the list represented using the encoding prescribed for the corresponding valueOf() method in annex F.3. The toString() method shall return a one dimensional ECMAScript array with each member of the list represented using the encoding prescribed for the corresponding toString() method. The toP21String() method shall return a one dimensional ECMAScript array with each member of the list represented using the encoding prescribed for the corresponding toP21String() method.

EXAMPLE      The ECMAScript expression "model.example.$value = new P21.List (new P21.Integer (1), new P21.Integer(2), new P21.Integer(3));" is equivalent to the following exchange structure code.

ANCHOR;
..
<example> = (1, 2, 3);
..
ENDSEC;

F.3.12 URI mapping

A property that is defined by a RESOURCE shall be mapped to a P21.URI object.

If the value addressed by the anchor is an exchange structure then the valueOf() method shall return the anchor addressed by the URI encoded as described in this annex.

If the value addressed by the anchor is not an exchange structure then the valueOf method shall return NULL.

The toString() method shall return the URI represented as an ECMAScript string with the initial "<" and final ">" characters omitted. The toP21String() method shall return the string with the "<" and final ">" characters included.

EXAMPLE      The ECMAScript expression "model.example.$value = new P21.URI ('#wheel');" is equivalent to the following exchange structure code.

ANCHOR;
..
#10 = <#wheel>;
..
ENDSEC;

F.4 Required methods of the P21 object

The P21.Model object shall have the following methods.

F.4.1 uri() method

The uri() method shall return the address of the exchange structure as a P21.URI object.

F.4.2 name() method

The name() method shall return the file_name.name (see clause 8.2.3) of the header section of the exchange structure as a P21.String object.

F.4.3 schema_population() method

The schema_population() method shall return an array of P21.Population objects describing the schema_population of the exchange structure (see clause 8.2.5).

Each P21.Population object shall have the following methods:

F.4.4 set_uri() method

The set_uri() method shall set the address of the exchange structure to the P21.URI object given by the argument.

F.4.5 set_name() method

The set_name() method shall set the file_name.name of the header section of the exchange structure to the P21.String object given by the argument.

NOTE      Applications use the file_name.name to determine the ECMAScript representation of the anchors in an exchange structure .

F.4.6 set_schema_population() method

The set_schema_population() method shall set the schema population to the array of P21.Population objects given by the argument.

Each P21.Population object shall have the following methods:

EXAMPLE      The following lines of ECMAScript code create a new P21.Population object and add it to the current schema population of the exchange structure in model.

pop = new P21.Population;
pop.set_uri (new P21.URI ('http//www.acme.org/beta.stp'));
pop.set_stamp (new date ('4/2/2013'));
pop.set_verification ();
model.set_schema_population (model.schema_population().push (pop));

© ISO 2016 — All rights reserved