MAP transformation in rule issue

  • Platform information:
    • Hardware: _VM/6GB
    • OS: UBUNTU 18.04 LTS
    • Java Runtime Environment: Java™ SE Runtime Environment (build 1.8.0_201-b09)
    • openHAB version: 2.5.0M2
  • Issue of the topic: MAP transformation not working for all entries in MAP file

I use a MAP file for conversion of ID to strings and vice versa. However it is not working for specific entries:

Following extract of my used rule:

        var Boolean EligableWaterSourceFound = false
	var String newWaterSourcePrefix
	var String currentWaterSourcePrefix
	var Integer newWaterSourceID
	newWaterSourceID = Integer::parseInt(String::format("%s",transform("MAP", "sprinkler.map", "WATER_SOURCE_ID_0")   ))
	logInfo("SprinklerRule:SprWaterSourceSelection", " start loop with newWaterSourceID  reset = " + newWaterSourceID)
	gWaterSourcePriority.members.sortBy[ s | ((s.state as DecimalType).intValue) ].forEach[ item |
		// find prefix of this item
		logInfo("SprinklerRule:SprWaterSourceSelection", "LoopThroughSources in PriorityList: processing item: " + item.name)
		// if eligable Watersource already found we do not search further
		if( EligableWaterSourceFound==false)
		{
				
			var splittedActiveItemName = item.name.split("_")
			newWaterSourcePrefix = "" + splittedActiveItemName.get(0)
			logInfo("SprinklerRule:SprWaterSourceSelection", " newWaterSourcePrefix MYTEST" + newWaterSourcePrefix)
			newWaterSourceID = Integer::parseInt(String::format("%s",transform("MAP", "sprinkler.map", "CISTERN")   ))
			//newWaterSourceID = Integer::parseInt(String::format("%s",transform("MAP", "sprinkler.map", newWaterSourcePrefix)   ))
			logInfo("SprinklerRule:SprWaterSourceSelection", " newWaterSourceID " + newWaterSourceID)

The first “transform” is working fine:

newWaterSourceID = Integer::parseInt(String::format("%s",transform("MAP", "sprinkler.map", "WATER_SOURCE_ID_0")   ))

The 2nd transform - (for testing I even replaced the variable used as key with a fixed coded string) does issue an error:

newWaterSourceID = Integer::parseInt(String::format("%s",transform("MAP", "sprinkler.map", "Cistern")   ))
			//newWaterSourceID = Integer::parseInt(String::format("%s",transform("MAP", "sprinkler.map", newWaterSourcePrefix)   ))

Correspnding log entry - with debug for map transformation is turned on in karaf console:

2020-03-24 07:03:26.677 [INFO ] [prinklerRule:SprWaterSourceSelection] - Check Source Eligability: Cistern_Eligable=OFF
2020-03-24 07:03:26.679 [INFO ] [prinklerRule:SprWaterSourceSelection] - Check Source Eligability: WaterShaft_Eligable=OFF
2020-03-24 07:03:26.680 [INFO ] [prinklerRule:SprWaterSourceSelection] - Check Source Eligability: WaterPipe_Eligable=ON
2020-03-24 07:03:26.682 [DEBUG] [ap.internal.MapTransformationService] - Transformation resulted in '0'
2020-03-24 07:03:26.684 [INFO ] [prinklerRule:SprWaterSourceSelection] -  start loop with newWaterSourceID  reset = 0
2020-03-24 07:03:26.689 [INFO ] [prinklerRule:SprWaterSourceSelection] - LoopThroughSources in PriorityList: processing item: WaterPipe_Priority
2020-03-24 07:03:26.692 [INFO ] [prinklerRule:SprWaterSourceSelection] -  newWaterSourcePrefix MYTESTWaterPipe
2020-03-24 07:03:26.694 [WARN ] [rm.AbstractFileTransformationService] - Could not transform 'Cistern' with the file 'sprinkler.map' : Target value not found in map for 'Cistern'

my map file - located in the transform directory:

#  mapping for Sprinkler
WATER_SOURCE_NAME_0=>>keine Wasserquelle<<
WATER_SOURCE_NAME_1=Sickerschacht
WATER_SOURCE_NAME_2=Brunnen
WATER_SOURCE_NAME_3=Wasserleitung
#
# Water Source 0 means no water source
WATER_SOURCE_ID_0=0
WATER_SOURCE_ID_1=1
WATER_SOURCE_ID_2=2
WATER_SOURCE_ID_3=3
#
#
# needs to be consistant with item name
#
WATER_SOURCE_PREFIX_0=_
WATER_SOURCE_PREFIX_1=Cistern_
WATER_SOURCE_PREFIX_2=WaterShaft_
WATER_SOURCE_PREFIX_3=WaterPipe_
#
# needs to be consistant with item name
#
Cistern=1
WaterShaft=2
WaterPipe=3
#

I already experimented with a lot of things:

  • excluding the “_” character
  • changing all uppercase, all lowercase
  • removing all comments in map file
  • removing all key value pairs except 3 entries
    Cistern=1
    WaterShaft=2
    WaterPipe=3
  • changing ID of first map to see if the map transformation itself is working (e.g. see logs when I change the key value from WATER_SOURCE_ID_0 to WATER_SOURCE_ID_1 )
newWaterSourceID = Integer::parseInt(String::format("%s",transform("MAP", "sprinkler.map", "WATER_SOURCE_ID_1")   ))

with corresponding log:

2020-03-24 07:07:39.700 [INFO ] [prinklerRule:SprWaterSourceSelection] - Check Source Eligability: Cistern_Eligable=OFF
2020-03-24 07:07:39.701 [INFO ] [prinklerRule:SprWaterSourceSelection] - Check Source Eligability: WaterShaft_Eligable=OFF
2020-03-24 07:07:39.703 [INFO ] [prinklerRule:SprWaterSourceSelection] - Check Source Eligability: WaterPipe_Eligable=ON
2020-03-24 07:07:39.704 [DEBUG] [ap.internal.MapTransformationService] - Transformation resulted in '1'
2020-03-24 07:07:39.706 [INFO ] [prinklerRule:SprWaterSourceSelection] -  start loop with newWaterSourceID  reset = 1

which indicates that map transformation is working in principle. I also use some other map transformations without issue.

I do not know why the other key-value pairs in the map file do not work as expected and the keys are not found?
Any thoughts what could be the issue here is welcome. any more debugging methods for the map transformation?
Many thanks!

A copy-paste of the map from your post just works for me.

var Integer newWaterSourceID
var String interim
interim = transform("MAP", "sprinkler.map", "WATER_SOURCE_ID_0")
logInfo("test", "stringy " + interim)
newWaterSourceID = Integer::parseInt(String::format("%s",transform("MAP", "sprinkler.map", "WATER_SOURCE_ID_0")   ))
logInfo("test", "numeric " + newWaterSourceID.toString)
interim = transform("MAP", "sprinkler.map", "Cistern")
logInfo("test", "stringy " + interim)
newWaterSourceID = Integer::parseInt(String::format("%s",transform("MAP", "sprinkler.map", "Cistern")   ))
logInfo("test", "numeric " + newWaterSourceID.toString)
2020-03-24 12:40:48.501 [INFO ] [.eclipse.smarthome.model.script.test] - stringy 0
2020-03-24 12:40:48.504 [INFO ] [.eclipse.smarthome.model.script.test] - numeric 0
2020-03-24 12:40:48.507 [INFO ] [.eclipse.smarthome.model.script.test] - stringy 1
2020-03-24 12:40:48.509 [INFO ] [.eclipse.smarthome.model.script.test] - numeric 1

I suggest you’ve got some invisible characters in your map file. Delete it and literally copy-paste from your own post. Make sure your editor saves in UTF-8

Thanks for this immediate testing.
I inspected my file - even with a Hex Editor - no sign to any invisible character.
File was also saved in UTF-8 Format (actually using VS Code) - but also checked with other editors.

However when trying all this I noticed that it worked with a different filename of the .map file (so e.g. sp_test.map was working).
Finally simple renaming my original sprinkler.map to sprink.map made it working as well.
I do not understand such a behaviour… maybe something todo with internal caching in openHab?
But so far issue solved.
Thanks