Data Model > Extending the UNICOM Intelligence Data Model > Working with the Alias Map component > Examples of configuring the Generator > 2: Avoiding duplicates
 
2: Avoiding duplicates
Using a dictionary
This example uses a dictionary to keep track of aliases already created and uses the IMapper object to handle this.
VBScript example
Private Sub Example_2a()
Dim MyConfiguration As MRALIASMAPLib.IGeneratorConfiguration
Dim MyGenerator As New MRALIASMAPLib.Generator
Dim MyMapper As New MRALIASMAPLib.Mapper
Dim MyAlias As MRALIASMAPLib.Alias

Set MyConfiguration = MyGenerator.Configuration
'Clear the default configuration
MyConfiguration.Clear
'Define the maximum number of characters
MyConfiguration.MaxLength = 6
'Remove everything not an uppercase English letter
MyConfiguration.Rules.AddNew "[^A-Z]+"
Set MyMapper.Generator = MyGenerator
MyMapper.MaxAttempts = 4

Set MyAlias = MyMapper.CreateAlias("Data Collection Development Library")
Debug.Print MyAlias.Name
' This lines gives a run-time error because it creates a duplicate alias
Set MyAlias = MyMapper.CreateAlias("Data Definition Language")
End Sub
mrScript example
Sub Example_2a()
Dim MyConfiguration, MyGenerator, MyMapper, MyAlias
Set MyGenerator = CreateObject("MRALIASMAP.Generator")
Set MyMapper = CreateObject("MRALIASMAP.Mapper")
Set MyAlias = CreateObject("MRALIASMAP.Alias")

Set MyConfiguration = MyGenerator.Configuration

'Clear the default configuration
MyConfiguration.Clear()
'Define the maximum number of characters
MyConfiguration.MaxLength = 6
'Remove everything not an uppercase English letter
MyConfiguration.Rules.AddNew("[^A-Z]+")
Set MyMapper.Generator = MyGenerator
MyMapper.MaxAttempts = 4

Set MyAlias = MyMapper.CreateAlias("Data Collection Development Library")
Debug.Log(MyAlias.Name)
' This lines gives a run-time error because it creates a duplicate alias
Set MyAlias = MyMapper.CreateAlias("Data Definition Language")
End Sub
In this example, we get a run-time error when we attempt to create the second alias, because it is identical to the previous one, and the dictionary (which is handled by the mapper) does not allow duplicate aliases. If we had not set MaxAttempts, this example would loop infinitely, because the generator has no mechanism for changing the second alias to make it unique. But with MaxAttempts set to 4, we get an error after the fourth unsuccessful attempt.
Introducing a counter
Now we will use a counter so that the second alias is unique. We will use the dollar sign ($) to add the counter at the end of each text. In regular expressions, the dollar sign matches the end of an input string.
VBScript example
Private Sub Example_2b()
Dim MyConfiguration As MRALIASMAPLib.IGeneratorConfiguration
Dim MyGenerator As New MRALIASMAPLib.Generator
Dim MyMapper As New MRALIASMAPLib.Mapper
Dim MyAlias As MRALIASMAPLib.Alias
Dim MyRule As MRALIASMAPLib.Rule
Set MyConfiguration = MyGenerator.Configuration
'Clear the default configuration
MyConfiguration.Clear
MyConfiguration.MaxLength = 6
MyConfiguration.Rules.AddNew "[^A-Z]+"
'The $ sign matches the end of the text
Set MyRule = MyConfiguration.Rules.AddNew("$")
'Adds the first counter at the end of the text
'MyRule.Replacements.AddOperator ropAutoCounter, 0
'0 is default, so we just write
MyRule.Replacements.AddOperator ropAutoCounter
'The counter defaults to counting digitally 1, 2, ...9, 10, 11, ...
MyConfiguration.AutoCounters.AddNew
Set MyMapper.Generator = MyGenerator
MyMapper.MaxAttempts = 4

Set MyAlias = MyMapper.CreateAlias("Data Collection Development Library")
' Print the alias, which is "DDL"
Debug.Print MyAlias.Name
Set MyAlias = MyMapper.CreateAlias("Data Definition Language")
' Print the alias, which is "D1D1L1"
Debug.Print MyAlias.Name
End Sub
mrScript example
Sub Example_2b()
Dim MyConfiguration, MyGenerator, MyMapper, MyAlias, MyRule
Set MyGenerator = CreateObject("MRALIASMAP.Generator")
Set MyMapper = CreateObject("MRALIASMAP.Mapper")
Set MyAlias = CreateObject("MRALIASMAP.Alias")
Set MyRule = CreateObject("MRALIASMAP.Rule")
Set MyConfiguration = MyGenerator.Configuration
'Clear the default configuration
MyConfiguration.Clear()
MyConfiguration.MaxLength = 6
MyConfiguration.Rules.AddNew("[^A-Z]+")
'The $ sign matches the end of the text
Set MyRule = MyConfiguration.Rules.AddNew("$")
'Adds the first counter at the end of the text
'MyRule.Replacements.AddOperator(2 '!ropAutoCounter!'), 0
'0 is default, so we just write
MyRule.Replacements.AddOperator(2 '!ropAutoCounter!')
'The counter defaults to counting digitally 1, 2, ...9, 10, 11, ...
MyConfiguration.AutoCounters.AddNew()
Set MyMapper.Generator = MyGenerator
MyMapper.MaxAttempts = 4
Set MyAlias = MyMapper.CreateAlias("Data Collection Development Library")
' Print the alias, which is "DDL"
Debug.Log( MyAlias.Name)
Set MyAlias = MyMapper.CreateAlias("Data Definition Language")
' Print the alias, which is "D1D1L1"
Debug.Log( MyAlias.Name)
End Sub
Note that the second alias is in the form D1D1L1 and not DDL1 as we might have expected. This is because the counter has been inserted every time the text is passed to the second rule, and this happens each time an upper case letter is encountered.
Moving the counter
If we want the counter to be inserted once only, so the alias is in the form DDL1, we must change the order of the rules:
VBScript example
Private Sub Example_2c()
Dim MyConfiguration As MRALIASMAPLib.IGeneratorConfiguration
Dim MyGenerator As New MRALIASMAPLib.Generator
Dim MyMapper As New MRALIASMAPLib.Mapper
Dim MyAlias As MRALIASMAPLib.Alias
Dim MyRule As MRALIASMAPLib.Rule

Set MyConfiguration = MyGenerator.Configuration

MyConfiguration.Clear
MyConfiguration.MaxLength = 6
Set MyRule = MyConfiguration.Rules.AddNew("$")
MyRule.Replacements.AddOperator ropAutoCounter
MyConfiguration.AutoCounters.AddNew
MyConfiguration.Rules.AddNew "[^A-Z]+"
Set MyMapper.Generator = MyGenerator
MyMapper.MaxAttempts = 4

Set MyAlias = MyMapper.CreateAlias("Data Collection Development Library")
' Print the alias, which is "DDL"
Debug.Print MyAlias.Name
Set MyAlias = MyMapper.CreateAlias("Data Definition Language")
' Print the alias, which is "DDL1"
Debug.Print MyAlias.Name
Set MyAlias = MyMapper.CreateAlias("Data Definition Language")
' Print the alias, which is "DDL2"
Debug.Print MyAlias.Name
Set MyAlias = MyMapper.CreateAlias("Data Definition Language")
' Print the alias, which is "DDL3"
Debug.Print MyAlias.Name
End Sub
mrScript example
Sub Example_2c()
Dim MyConfiguration, MyGenerator, MyMapper, MyAlias, MyRule
Set MyGenerator = CreateObject("MRALIASMAP.Generator")
Set MyMapper = CreateObject("MRALIASMAP.Mapper")
Set MyAlias = CreateObject("MRALIASMAP.Alias")
Set MyRule = CreateObject("MRALIASMAP.Rule")

Set MyConfiguration = MyGenerator.Configuration

MyConfiguration.Clear()
MyConfiguration.MaxLength = 6
Set MyRule = MyConfiguration.Rules.AddNew("$")
MyRule.Replacements.AddOperator(2 '!ropAutoCounter!')
MyConfiguration.AutoCounters.AddNew()
MyConfiguration.Rules.AddNew("[^A-Z]+")
Set MyMapper.Generator = MyGenerator
MyMapper.MaxAttempts = 4

Set MyAlias = MyMapper.CreateAlias("Data Collection Development Library")
' Print the alias, which is "DDL"
Debug.Log( MyAlias.Name)
Set MyAlias = MyMapper.CreateAlias("Data Definition Language")
' Print the alias, which is "DDL1"
Debug.Log( MyAlias.Name)
Set MyAlias = MyMapper.CreateAlias("Data Definition Language")
' Print the alias, which is "DDL2"
Debug.Log( MyAlias.Name)
Set MyAlias = MyMapper.CreateAlias("Data Definition Language")
' Print the alias, which is "DDL3"
Debug.Log( MyAlias.Name)
End Sub
See also
Examples of configuring the Generator