Examples of configuring the Generator
To run these examples, you need to add SPSS MR AliasMap Type Library to the project references.
See also
For a Visual C++ example of configuring the generator, see
Using the Alias Map component.
1: Simple example
The following examples shows how the text Data Collection Development Library can be compressed to DDL. The examples do not use a counter to handle duplicates. So, for example,Data Definition Language would also map to DDL.
VBScript example
Private Sub Example_1()
Dim MyConfiguration As MRALIASMAPLib.IGeneratorConfiguration
Dim MyGenerator As New MRALIASMAPLib.Generator
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]+"
MyGenerator.Text = "Data Collection Development Library"
'Print the output, which is "DDL"
Debug.Print MyGenerator.Alias
End Sub
mrScript example
Sub Example_1()
Dim MyConfiguration, MyGenerator
Set MyGenerator = CreateObject("MRALIASMAP.Generator")
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]+")
MyGenerator.Text = "Data Collection Development Library"
'Print the output, which is "DDL"
Debug.Log(MyGenerator.Alias)
End Sub
In this example there is only one rule, created in the line MyConfiguration.Rules.AddNew ("[^A-Z]+"). [^A-Z]+ is a regular expression, which matches any character that is not an uppercase English letter. The rule replaces characters matching this pattern with the result of the ReplacementCollection. However, the ReplacementCollection is empty (because we did not define it), so the rule effectively removes everything except the uppercase letters and passes them to the next rule. However, the uppercase letters are left unchanged because there are no more rules.
See also
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
The second alias is in the form D1D1L1 and not DDL1 as you 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 you want the counter to be inserted only once, so the alias is in the form DDL1, 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