structs in uniface

66
| STRUCTS IN UNIFACE Daniel Iseli Uniface CSS

Upload: uniface

Post on 06-May-2015

919 views

Category:

Technology


17 download

DESCRIPTION

Structs are key to the architecture of how we are building the Uniface 10 IDE, so we will share details on best practices learnt from the Uniface 10 development The presentation topics: • The purpose of structs • Using structs • Best practices

TRANSCRIPT

Page 1: Structs in Uniface

| STRUCTS IN UNIFACEDaniel IseliUniface CSS

Page 2: Structs in Uniface

Agenda

• The purpose of structs• Using structs• Best practices

Page 3: Structs in Uniface

The purpose of structs

Page 4: Structs in Uniface

The purpose of structs

What is a struct?

Tree-like data structure Manipulate complex data dynamically

Transform complex data: XML data Uniface component data JSON (coming soon)

Page 5: Structs in Uniface

The purpose of structs

What is a struct?

• Collection of data objects / nodes

• Organized hierarchically under single root node

• Struct member: node that has a parent node

• Nested Struct: member that is Struct

Page 6: Structs in Uniface

The purpose of structs

What is a struct?

• A Struct only exists:

• In memory• As long as referenced by struct variable /

parameter

Page 7: Structs in Uniface

1

Demo

Page 8: Structs in Uniface

The purpose of structs

Structs and Members

• Struct consists of:• One top node• 0-n sub-nodes (members)

• Each member can hold:• A scalar data value (e.g. number, string, or date)• Or another (nested) Struct

Page 9: Structs in Uniface

The purpose of structs

Structs and Members

• Struct / Struct member can have a name• Is case-sensitive• Can be an empty string

• Members can have the same name.

• Members are sequentially ordered

Page 10: Structs in Uniface

The purpose of structs

Struct Leaves

• Struct member that has:• No value• Or only single scalar value

• Adding member to leaf:• Changes to nested Struct• Value of former leaf held in nameless Struct

(scalar Struct)

Page 11: Structs in Uniface

The purpose of structs

Struct Example

Page 12: Structs in Uniface

The purpose of structs

Struct Variables

• Does not contain value• Contains 0-n references to nodes• Can contain multiple references to different nodes• Several variables can refer to one Struct node.

Page 13: Structs in Uniface

The purpose of structs

Struct Variables

Page 14: Structs in Uniface

The purpose of structs

Struct Parameters

• Structs can be passed as parameters:• By reference• By value

• Default behavior:• By reference: partner operations and entries• By value: public operations

Page 15: Structs in Uniface

The purpose of structs

Struct Parameters

• Behavior can be changed during declaration

• Limitation of by reference:• Only works with locals instances• Passing to remote instance will throw error

Page 16: Structs in Uniface

vBookStruct->chapter

The purpose of structs

Struct Access Operators

• De-reference operator: -><name>

• Collection of members with specified name

Page 17: Structs in Uniface

The purpose of structs

Struct Access Operators

vBookStruct->chapter

Page 18: Structs in Uniface

vBookStruct->*vBookStruct->*

The purpose of structs

Struct Access Operators

• De-reference operator: ->*

• Special case: returns collection to all members

Page 19: Structs in Uniface

vBookStruct->*vBookStruct->c*

The purpose of structs

Struct Access Operators

• De-reference operator: ->*

• Special case: returns collection to all members

Page 20: Structs in Uniface

The purpose of structs

Struct Access Operators

vBookStruct->*

Page 21: Structs in Uniface

vBookStruct->chapter{2}

The purpose of structs

Struct Access Operators

• Struct index operator: {index}

• Reference to single member based on index position in collection of references

Page 22: Structs in Uniface

The purpose of structs

Struct Access Operators

vBookStruct->chapter{2}

Page 23: Structs in Uniface

The purpose of structs

Proc for Manipulating Structs

• Struct data type• $newstruct• componentToStruct• structToComponent• xmlToStruct• structToXml

• $equalStructRefs

Page 24: Structs in Uniface

The purpose of structs

Struct Functions

• $dbgString• $dbgStringPlain

• $collSize• $memberCount

• $isLeaf• $isScalar• $istags

Page 25: Structs in Uniface

The purpose of structs

Struct Functions

• $index

• $name• $parent• $scalar• $tags

Page 26: Structs in Uniface

The purpose of structs

Setting Struct Functions

• Most functions only return information

• Functions that can change data:• $index• $parent• $tags

Page 27: Structs in Uniface

The purpose of structs

Struct Annotations

• Descriptive data elements• Known as tags

• Contain information to:• Correctly convert data to / from specific formats• Interpret data correctly

Page 28: Structs in Uniface

The purpose of structs

Struct Annotations

• Each node has special child• Node accessed using $tags

• No specific position• Not counted / treated as members

Page 29: Structs in Uniface

The purpose of structs

Struct Annotations for Data Conversion

• Specific for complex data format

• componentToStruct / xmlToStruct:• Set annotations

• structToComponent / structToXml:• Use annotations for conversion process

Page 30: Structs in Uniface

The purpose of structs

Struct Annotations for Data Conversion<?xml version='1.0' ?><!DOCTYPE UNIFACE PUBLIC "UNIFACE.DTD" "UNIFACE.DTD"><UNIFACE release="9.6" xmlengine="2.0"><TABLE>

[] [$tags] [xmlVersion] = 1.0 [xmlClass] = document [UNIFACE] [$tags] [xmlClass] = element [release] = "9.6" [$tags] [xmlClass] = attribute [xmlengine] = "2.0" [$tags] [xmlClass] = attribute [TABLE] [$tags] [xmlClass] = element

xmlToStruct

Page 31: Structs in Uniface

The purpose of structs

Struct Annotations for XMLTag Values

xmlClass documentelementattribute…

xmlNamespaceURI Value of the namespace string

xmlNamespaceAlias Value of the alias string

xmlVersion Value of the version attribute in the <?xml … ?> declaration

xmlEncoding Value of the encoding attribute in the <?xml … ?> declaration

xmlStandAlone Value of the standalone attribute in the <?xml … ?> declaration

xmlDataType Data type

xmlTypeNamespace Namespace of URI of a schema-defined data type

Page 32: Structs in Uniface

The purpose of structs

Struct Annotations for XMLTag Values

xmlTypeCategory simple complex

xmlAttrMode #IMPLIED#REQUIRED#FIXED

xmlPublicID Formal Public Id used to identify the DTD

xmlSystemID URI for the file containing the DTD

xmlNotation Name of notation declaration

Page 33: Structs in Uniface

The purpose of structs

Creating Structs (Explicitly)

• $newstruct

variablesstruct vStruct

endvariables

vStruct = $newstruct

Page 34: Structs in Uniface

The purpose of structs

variablesstruct vStruct

endvariables

vStruct = "My Book"

Creating Structs (Implicitly)

• Assigning value to a struct variable / parameter

Page 35: Structs in Uniface

The purpose of structs

variablesstruct vStruct

endvariables

vStruct->$name = "My Book"

Creating Structs (Implicitly)

• Assigning value to:• $name• $parent• $scalar• $tags

Page 36: Structs in Uniface

The purpose of structs

variablesstruct vStruct

endvariables

xmlToStruct vStruct, "Book.xml"

Creating Structs (Implicitly)

• Proc conversion instruction:• xmlToStruct• componentToStruct

Page 37: Structs in Uniface

The purpose of structs

Deleting Structs

• No Proc command• Deleted when:

• not referenced by struct variable/parameter• not owned by another Struct

variablesstruct vStruct

endvariables

vStruct = ""

Page 38: Structs in Uniface

The purpose of structs

Deleting Structs

vStruct1 = ""

Page 39: Structs in Uniface

The purpose of structs

Deleting Structs

vStruct1 = ""

Page 40: Structs in Uniface

The purpose of structs

Deleting Structs

vStruct1 = ""

Page 41: Structs in Uniface

Using structs

Page 42: Structs in Uniface

Using structsTransforming Complex Data Using Structs

struct data type enables dynamic transformations of complex data. For example:• XML from web services into component data structure• Component data into a nested XML data• Merge data from different components• Extract information from any type of complex data

Page 43: Structs in Uniface

Using structsStructs for XML Data

Basic Struct that represents XML data:

1. Top-level Struct for document

2. Named node with element name

3. Named with attribute name

4. Named leaf with element name

5. Named Struct with application name

Page 44: Structs in Uniface

Demo<?xml version="1.0"?><CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD></CATALOG>

<?xml version="1.0"?><CATALOG> <CD TITLE="Empire Burlesque"> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <YEAR>1985</YEAR> <PRICES> <PRICE CURRENCY="USD">10.90</PRICE> <PRICE CURRENCY="EUR">10.00</PRICE> <PRICE CURRENCY="GBP">8.50</PRICE> </PRICES> </CD></CATALOG>

Transform XML -> XML

Page 45: Structs in Uniface

2

Demo

Page 46: Structs in Uniface

Using structsStructs for Uniface Component Data

Struct that represents Uniface component data

1. Top-level Struct withcomponent name

2. Named nodes for entities

3. Fixed named node OCC

4. Named leaf with field name

5. Named nodes for inner entites

Page 47: Structs in Uniface

Demo

Transform CMP -> CMP

Page 48: Structs in Uniface

3

Demo

Page 49: Structs in Uniface

Using structsMusic City Web Service Example

• Calling Music City Web Service

• Returned XML Data converted to Struct• Struct transformed to match component structure• Struct loaded into component

Page 50: Structs in Uniface

<?xml version="1.0" encoding="UTF-8"?><ufnc:parameter xmlns:ufnc="urn:uniface:applic:complexparameter"> <ns0:artists xmlns:ns0="urn:uniface:applic:musiccityplex"> <ns0:artist> <ns0:artist_id>333</ns0:artist_id> <ns0:gid>703c7062-cc25-47e0-919e-ef740b88c725</ns0:gid> <ns0:name>Chris von Sneidern</ns0:name> <ns0:begin_date_year>0000</ns0:begin_date_year> <ns0:begin_date_month>00</ns0:begin_date_month> <ns0:begin_date_day>00</ns0:begin_date_day> <ns0:end_date_year>0</ns0:end_date_year> <ns0:end_date_month>0</ns0:end_date_month> <ns0:end_date_day>0</ns0:end_date_day> <ns0:type>Person</ns0:type> <ns0:gender/> <ns0:country> <ns0:code>US</ns0:code> <ns0:name>United States</ns0:name> </ns0:country> <ns0:comment/> <ns0:ipi_code/> <ns0:last_updated>2011-12-11T08:39:58.00</ns0:last_updated> <ns0:artist_credit_name> <ns0:position>0</ns0:position> <ns0:join_phrase>\\N</ns0:join_phrase> <ns0:artist_credit> <ns0:artist_count>1</ns0:artist_count> <ns0:ref_count>122</ns0:ref_count> <ns0:created>2012-02-14T14:17:01.00</ns0:created> <ns0:release_group> <ns0:type>Album</ns0:type> <ns0:comment/> <ns0:name>Sight &amp; … </ns0:release_group>

Using structsMusic City Web Service Example

Transform XML -> CMP

Page 51: Structs in Uniface

4

Demo

Page 52: Structs in Uniface

Using structsxmlToStruct vs. xmlToStruct/schema

• xmlToStruct:• Uniface data type of all elements/attributes is string.

• xmlToStruct/schema:• Elements/attributes with predefined data types

included as scalar Structs of matching Uniface data types.

• Values are converted to Uniface format.

Page 53: Structs in Uniface

Using structsXML Schemas

• What is a schema?

• XML Schema is an XML-based alternative to DTD.• An XML schema describes the structure of an XML

document.• The XML Schema language is also referred to as XML

Schema Definition (XSD).

Page 54: Structs in Uniface

<!– parameter.xsd --><?xml version="1.0" encoding="UTF-8"?><xsd:schema elementFormDefault="qualified" targetNamespace="urn:uniface:applic:complexparameter" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ufnc="urn:uniface:applic:complexparameter"> <xsd:element name="parameter" type="ufnc:parameter" /> <xsd:complexType name="parameter"> <xsd:sequence> <xsd:any minOccurs="0" /> </xsd:sequence> </xsd:complexType></xsd:schema>

<!– musiccityplex.xsd --><?xml version="1.0" encoding="utf-8"?><xsd:schema elementFormDefault="qualified" targetNamespace="urn:uniface:applic:musiccityplex" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:mc="urn:uniface:applic:musiccityplex"> <xsd:element name="artists" type="mc:artists"/> <xsd:complexType name="artists"> <xsd:sequence> <xsd:element name="artist" type="mc:artist“ minOccurs="0" maxOccurs="unbounded" /> </xsd:sequence> </xsd:complexType> <xsd:complexType name="artist"> <xsd:sequence> <xsd:element name="artist_id" type="xsd:int"/> <xsd:element name="gid" type="xsd:string"/> <xsd:element name="name" type="xsd:string"/> <xsd:element name="begin_date_year" type="xsd:short" …/> … <xsd:element name="country" type="mc:country" …/> … <xsd:element name="last_updated" type="xsd:dateTime"/> … </xsd:sequence> </xsd:complexType>

Using structsMusic City Web Service Example (2)

• XSD Samples:

Page 55: Structs in Uniface

; Create Schema ListvSchemaList = ""vItems = ""putitem/id vItems, "NAMESPACE", %\ "urn:uniface:applic:complexparameter"putitem/id vItems, "LOCATION", "parameter.xsd"putitem vSchemaList, -1, vItemsvItems = ""putitem/id vItems, "NAMESPACE", %\ "urn:uniface:applic:musiccityplex"putitem/id vItems, "LOCATION", "musiccityplex.xsd"putitem vSchemaList, -1, vItems; xmltostruct/schemaxmltostruct/schema $Struct$, vXmlData, vSchemaList

Using structsMusic City Web Service Example (2)

• How to use:

Page 56: Structs in Uniface

5

Demo

Page 57: Structs in Uniface

Need more info?Go to:

Page 58: Structs in Uniface

Best practices

Page 59: Structs in Uniface

Best practicesStruct usage in the IDE10

Page 60: Structs in Uniface

Best practicesStruct usage in the IDE10

Controller ViewView

View

Repository

Structured data exchange

managed using Structs

Page 61: Structs in Uniface

Best practicesStruct usage in the IDE10

Some direct spin offs: More efficient use of Memory Performance improvements Consistency improvement Documentation improvements

A lot of improvements available in release 9.6.02

Page 62: Structs in Uniface

; Do

vStruct2 = vStruct->Node11->Node21vStruct2->Node31 = "A"vStruct2->Node32 = "B"vStruct2->Node33 = "C"vStruct2->Node33 = "D"

; Don’t

vStruct->Node11->Node21->Node31 = "A"vStruct->Node11->Node21->Node32 = "B"vStruct->Node11->Node21->Node33 = "C"vStruct->Node11->Node21->Node33 = "D"

Best practicesWhat to avoid?

Minimize loops through the Struct Minimize amount of Proc code in the deepest

branches

Page 63: Structs in Uniface

; Do

vStruct2 = vStruct->Node11->Node21vStruct2->Node31 = "A"vStruct2->Node32 = "B"vStruct2->Node33 = "C"vStruct2->Node33 = "D"

Best practicesWhat to avoid?

Minimize loops through the Struct Minimize amount of Proc code in the deepest

branches

Page 64: Structs in Uniface

; Do

N = vStruct->$collSizewhile (I <= N); ...

; Don’t

while (I <= vStruct->$collSize); ...

Best practicesWhat to avoid?

Use by reference wherever possible, instead of by value

Minimize usage of $collsize and $membercount

Adress Struct members with ->*{index} instead of -><name>{index} where possible

Page 65: Structs in Uniface

; Do

N = vStruct->$collSizewhile (I <= N); ...

Best practicesWhat to avoid?

Use by reference wherever possible, instead of by value

Minimize usage of $collsize and $membercount

Adress Struct members with ->*{index} instead of -><name>{index} where possible

Page 66: Structs in Uniface

Summary:

• Structs are used for manipulating complex data

• Structs can be used for transforming data sent to and received from web services

• Structs will play an increasingly important role in Uniface (e.g. Uniface 10)