6.7.4 Checking instance
uniqueness
Name uniqueness
To check that the names of instances of a given object type
are unique, you have three alternatives:
| 1) | for
uniqueness across the whole repository (or at least all elements loaded from all
open projects), use Toggle Uniqueness in the Object Tool Properties list pop-up menu (Section 2.1.4). That is then a feature of that
type’s instances, wherever they are
used. |
| 2) | for
uniqueness among objects directly in a graph, use a Uniqueness Constraint
(Section 2.1.8) for that object type
in the Graph Tool.
|
| 3) | For
other cases, you can make a generator.
|
Uniqueness checks in generators are
often used as part of a __LiveCheck() generator, so the modeler can see any
problems straightaway in the Live Check pane at the bottom of the Diagram
Editor. E.g.
foreach .() where not __Unique()
{ id ' is not unique for ' type newline }That uses
__Unique(), which builds up a cache by type of each id encountered, and responds
'T' for true for a new id (i.e. a cache
miss). You can also change to use something other than the type or id. The first
argument, which defaults to type if empty, gives the key to collect values
under. The second argument, which defaults to id if empty, gives the value to
add to the collection under that key. E.g. if you want to make sure that among
the set of all Variable and VariableRef objects, :Name properties are unique,
you can't just use __Unique(), as that would store the Variables and
VariableRefs under separate keys, so you wouldn't spot a Variable with the same
name as a VariableRef. Instead:
foreach .(Variable | VariableRef)
where not __Unique('Variables', :Name)
{ id ' is not unique' newline}Another improvement is to
note that if there are 5 objects with the same name, this will not report the
first, but will report all 4 subsequent objects of that name, each on their own
line. Better would be one line that reports all 5:
foreach .()
where not __Unique()
unique type '_' id
{ 'Duplicate id for ' type ':'
foreach .() where type=type;1 and id=id;1
{ ' ' id
}
newline
}Note the 3
rd line’s use of
unique type '_' id to only execute the
subloop for the first time we encounter the same type and id as a
duplicate.
Maximum of one instance per graph
To check that a given object type only has one instance per
graph, you would normally use an Occurrence Constraint in the graph type. You
could also use the following generator definition:
Report 'Check XYZ instances'
_translators()
$occurrences='0'
foreach .XYZ { $occurrences++%null }
if $occurrences > '1' num then
'Object type XYZ found '
$occurrences
' times in this graph:'
foreach .XYZ
{ id newline
}
endif
endreportWhen the generator is run on a graph containing
more than one occurrence of XYZ, an output window will report the number and
names of XYZ objects. Each object name can be double-clicked to take the user to
the object. If no duplicates are found, no output window is opened.