Initializing help system before first use

Namespace definition


Type: Programming
Rating: 3 (intermediate)
Description: A namespace denotes a group of identifiers in a program that is distinguished by a common name (prefix). 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. The use of namespaces can help structure data when working with multiple packages.
File(s): mynspkg1.mos (library, compile separately), mynspkg2.mos (library, compile separately), myns_test.mos

mynspkg1.mos
(!******************************************************
   Mosel Example Problems
   ======================

   file mynspkg1.mos 
   `````````````````
   Testing namespaces
   - main model: mynstest.mos -
 
   (c) 2018 Fair Isaac Corporation
       author: S. Heipcke, 15 May 2018
*******************************************************!)

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    ! Index set in different namespace
   vi, ns3~vi, ns3~ns31~vi: integer   ! 3 different entities
 end-declarations

 public declarations
   vp: integer
 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

mynspkg2.mos
(!******************************************************
   Mosel Example Problems
   ======================

   file mynspkg2.mos 
   `````````````````
   Testing namespaces
   - main model: mynstest.mos -
 
   (c) 2018 Fair Isaac Corporation
       author: S. Heipcke, 15 May 2018
*******************************************************!)

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("  vi=", vi)              ! This fails: private symbol of pkg1
   writeln("  vp=", vp)               ! Public symbol of pkg1
   writeln("  Ar(5)=", Ar(5))         ! Contained in 'ns1' (prefix optional)
 end-procedure
end-package


myns_test.mos
(!******************************************************
   Mosel Example Problems
   ======================

   file myns_test.mos 
   ``````````````````
   Testing namespaces

   *** Compile mynspkg1.mos and mynspkg2.mos before running this model ****

   (c) 2018 Fair Isaac Corporation
       author: S. Heipcke, Sep. 2018
*******************************************************!)

model "mynstest"
 uses 'mynspkg1', 'mynspkg2'
! namespace ns1    ! Goes wrong (compilation): ns1 is restricted to pkg1+pkg2
 namespace ns2     ! A new namespace
 nssearch ns3      ! Symbols from 'ns3' can be used without prefix

 declarations
   ns2~vi: integer
   I, ns2~R: range
!   vi: integer    ! Goes wrong at runtime: clash with ns3~vi through nssearch
 end-declarations

 writeln("****Subroutine calls")
 frompkg2(5)                           ! Public routine from package pkg2
 writeln("n3~vi:",vi, " vp:",vp)       ! Display values of n3~vi and vp

! ns1~proc1(10)    ! Goes wrong (compilation): ns1 is restricted to pkg1+pkg2
 proc2(4)          ! This works: public subroutine from pkg1
! proc3(6)         ! Goes wrong (compilation): private subroutine of pkg1

 ! Store and display contents of namespace 'ns3'
 writeln("****Contents of ns3:")
 initializations to "tee:mem:mynsav&"
   ns3
 end-initializations

 ! Initialize entities with matching names from saved namespace
 writeln("****Values read:")
 initializations from "mem:mynsav"
   ns2 as "ns3"
!   I as "ns3~R"
 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)  

end-model

© 2001-2023 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.