Namespaces
A namespace is a group of identifiers in a program that is distinguished by a common name (prefix). When working with mutiple packages it can be helpful to introduce namespaces in order to structure the data and to determine which model entities are accessible to other (all or preselected) packages or models.
A fully qualified entity name in Mosel is of the form
nspc~ident
where nspc is a namespace name and ident an identifier in the namespace. Namespaces and their access are specified via specific compiler directives at the start of the model or package. The package example mynspkg1 below defines three namespaces ('ns1', 'ns3', and 'ns3∼ns31'), two of which are restricted to a namespace group that comprises a second package mynspkg2, and the namespace 'ns3' is visible to all packages and models. The package further states via the nssearch directive that any unqualified entity names employed in the package should be searched for in the namespace 'ns1', meaning that the names belonging to this namespace can be used without the namespace prefix ns1∼.
package mynspkg1 namespace ns1, ns3, ns3~ns31 ! This package defines 3 namespaces: nsgroup ns1: "mynspkg2" ! * ns1 + ns3~ns31 restricted to pkg2 nsgroup ns3~ns31: "mynspkg2" ! * ns3 is visible to all nssearch ns1 ! 'ns1' can be used without prefix declarations ns3~R = 1..10 ns1~Ar: array(ns3~R) of integer ! Array with index set in another namespace vi, ns3~vi, ns3~ns31~vi: integer ! 3 different entities end-declarations public declarations vp: integer ! This entity is visible to all end-declarations procedure ns1~proc1(val:integer) ! Subroutine in a namespace ns3~vi:=val; ns3~ns31~vi:=2*val; vi:=val; vp:=val Ar(5):=val ! No prefix: 'ns1' is in search list writeln(" In ns1~proc1: ", vi) end-procedure public procedure proc2(val:integer) ! Public subroutine writeln(" In proc2: ", val) end-procedure procedure proc3(val:integer) ! Private subroutine writeln(" In proc3: ", val) end-procedure end-package
The package mynspkg1 shows some examples of entity and subroutine defintions for the three cases: private (vi, proc3), in a namespace (ns3∼R, ns1∼Ar, ns3∼vi, ns3∼ns31∼vi, ns1∼proc1), and public (vp, proc2).
The second package mynspkg2 that uses functionality from mynspkg1 needs to state which namespaces are used, either via a namespace or a nssearch directive.
package mynspkg2 uses 'mynspkg1' namespace ns3~ns31 ! Namespace used in this package nssearch ns1 ! 'ns1' can be used without prefix public procedure frompkg2(val: integer) proc1(val) ! Procedure in namespace 'ns1' writeln(" frompkg2:",ns3~ns31~vi) ! Namespace 'ns3~ns31' is not searched writeln(" vp=", vp) ! Public symbol of pkg1 writeln(" Ar(5)=", Ar(5)) ! Contained in 'ns1' (prefix optional) end-procedure end-package
Any model or further package using the previous two packages can access the namespace 'ns3' and also define new namespaces of its own, but it is not allowed to access the other two namespaces that are restricted to this group of packages.
model "mynstest" uses 'mynspkg1', 'mynspkg2' namespace ns2 ! A new namespace nssearch ns3 ! Symbols from 'ns3' can be used without prefix frompkg2(5) ! Public routine from package mynspkg1 writeln("n3~vi:", vi, " vp:", vp) ! Display values of n3~vi and vp proc2(4) ! Public subroutine from mynspkg1 end-model
An interesting feature of namespaces is that an entire namespace can be saved via initializations to simply by indicating its name and the stored information can subsequently be used to initialize entities in some other namespace with matching names and types.
declarations ns2~vi: integer I, ns2~R: range end-declarations ! Store contents of namespace 'ns3' initializations to "mem:mynsav" ns3 end-initializations ! Initialize entities with matching names from the saved namespace initializations from "mem:mynsav" ns2 as "ns3" end-initializations writeln("ns2~vi:", ns2~vi) ! Has received the value of ns3~vi ! Read an individual entity from the saved namespace initializations from "mem:mynsav" I as "ns3~R" end-initializations writeln("I:", I)
© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.