list of dots

User Manual    [Previous]   [Next]   

Namespace Directives

Namespaces allow you to group similar entities to promote cohesion, as well as reduce the possibility of name collision. Sub-namespaces are separated using a period (.).

In the first example, the classes Faculty and Student will be in namespace school.admin. In Java they will be generated into the admin package (directory) within the school package. Similarly, class Building will be in the elevator.structure namespace.

A previously defined namespace for an entity can be redefined using the "--redefine" option. If the "--redefine" option is not used, the namespace will not change and a warning will be issued (example 2).

Entities declared before any namespace or after "namespace default;" will be in the default package. Entities declared after "namespace -;" will not be in the last declared namespace. Instead, they will be in the default package. If declared after a non-default namespace, the namespace of an entity in the default namespace will be redefined (example 3).

There are some cases where an entity should be imported thus cannot be in the default package and will automatically be placed in another package (example 4). However, automatic import code generation in interfaces that extend interfaces in other packages is not supported yet.

Example 1: Sub-namespaces

namespace school.admin;

class Faculty{}
class Student{}

namespace elevator.structure;

class Building
{
  1 -- * Classroom;
}

class Classroom{}
      

Load the above code into UmpleOnline

 

Example 2: Redefining a namespace

// Entities A, B and C are currently in
// namespace m.

namespace m;
class A{}
interface B{}
class C{}


// To redefine the namespace of an
// entity, use the --redefine option.
// B is now in namespace n.

namespace n --redefine;
interface B{}


// If --redefine is not used, a warning will
// be issued when trying to redefine the 
// namespace of an entity

namespace p;
class C{}


// Using namepace -; will deactivate the last
// declared namespace. There will not be an
// attempt to redefine the namespace of interface
// B and a warning will not be issued

namespace -;
interface B{}
      

Load the above code into UmpleOnline

 

Example 3: Default namespace

// Class A will be in the default namespace

class A{}
class B{}


// Class B was in the default namespace
// But now will be in namespace m

namespace m;
class B{}
class C{}
namespace -;


// Because namespace -; was used, namespace m
// is no longer active, and class D will be 
// in the default namespace

class D{}


// Class C is in namespace m, but can
// be placed in the default namespace

namespace default --redefine;
class C{}
      

Load the above code into UmpleOnline

 

Example 4: Automatically redefined namespace

// Classes A is in the 
// default namespace. Class B is in
// namespace m. Both are
// linked to each other via an association,
// therefore, and because they are not all
// in the same namespace, an import text
// should be generated. However, many programming
// languages do not support import from
// the default namespace so the namespace of
// A will become m because B is in m.

class A{}
namespace m;
class B{*--* A;}
namespace -;


// The same issue occurs when an
// entity in a non-default namespace
// extends or implements an entity
// in the default namespace

interface E{}
interface F{}
namespace n;
interface J{isA E;}
class K{isA F;}


// Here an import text will be generated for A in X
// because A is in a different namespace
// This feature is not yet supported for interfaces
// that extend interfaces in other namespaces
// and the import text will not be generated for K in Y

namespace p;
class X{isA A;}
// interface Y{isA E;}
      

Load the above code into UmpleOnline

 

Syntax


// Namespaces divide the code into logical groups. See NamespaceDirectives
namespace- : namespace [namespace] [[namespaceoption]]? ;

namespaceoption : [=option:--redefine]