Definition of types
In Section Initialization of records from file we have seen the example arcs.mos that defines a record to represent arcs of a network. If we wish to use this data structure in different models we may move its definition into a package 'arcpkg' to avoid having to repeat it in every model.
Such a package may look as follows (file arcpkg.mos):
package arcpkg public declarations arc = public record ! Arcs: Source,Sink: string ! Source and sink of arc Cost: real ! Cost coefficient end-record end-declarations end-package
which is used thus from the model file:
model "Arcs2"
uses "arcpkg"
declarations
NODES: set of string ! Set of nodes
ARC: array(ARCSET:range) of arc ! Arcs
end-declarations
initializations from 'arcs.dat'
ARC
end-initializations
! Calculate the set of nodes
NODES:=union(a in ARCSET) {ARC(a).Source, ARC(a).Sink}
writeln(NODES)
writeln("Average arc cost: ", sum(a in ARCSET) ARC(a).Cost / getsize(ARCSET) )
end-model At this place, the use of the keyword public may call for some explanation. Here and also in the example `myconstants' the whole declarations block is preceded by the public marker, indicating that all objects declared in the block are public (i.e., usable outside of the package definition file). If only some declarations are public and others in the same block are private to the package, the public marker needs to preceed the name of every object within the declarations that is to become public instead of marking the entire block as public.
The second occurrence of the public marker in the definition of package `arcpkg' is immediately in front of the keyword record, meaning that all fields of the record are public. Again, it is possible to select which fields are accessible from external files (for example, you may wish to reserve some fields for special flags or calculations within your package) by moving the keyword public from the record definition in front of every field name that is to be marked as public.
A definition of package `arcpkg' equivalent to the one printed above therefore is the following.
package arcpkg2 declarations public arc = record ! Arcs: public Source,Sink: string ! Source and sink of arc public Cost: real ! Cost coefficient end-record end-declarations end-package
