@prefix adms: <http://www.w3.org/ns/adms#> .
@prefix dbo: <https://dbpedia.org/ontology/> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix locn: <http://www.w3.org/ns/locn#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix schema: <https://schema.org/> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix legal: <http://www.w3.org/ns/legal#> .
@prefix m8g: <http://data.europa.eu/m8g/> .

######## D2RML NAMESPACES ###########

@prefix dr: <http://islab.ntua.gr/ns/d2rml#> .
@prefix drop: <http://islab.ntua.gr/ns/d2rml-op#> .
@prefix dris: <http://islab.ntua.gr/ns/d2rml-is#> .

######## STIRDATA TMP NAMESPACES ###########

@prefix nuts-item: <http://data.europa.eu/nuts/code/> .
@prefix lau-item: <https://w3id.org/stirdata/resource/lau/item/> .

@prefix sd-voc: <https://w3id.org/stirdata/vocabulary/> .
@prefix sd-voc-adminlevel: <https://w3id.org/stirdata/resource/adminUnitLevel/> .

@prefix sd-agency-fi: <http://fi.data.stirdata.eu/resource/agency/> .
@prefix sd-organization-fi: <http://fi.data.stirdata.eu/resource/organization/> .
@prefix sd-unit-fi: <http://fi.data.stirdata.eu/resource/unit/> .
@prefix sd-site-fi: <http://fi.data.stirdata.eu/resource/site/> .
@prefix sd-address-fi: <http://fi.data.stirdata.eu/resource/address/> .
@prefix sd-identifier-fi: <http://fi.data.stirdata.eu/resource/identifier/> .

@prefix sd-companytype-item-fi: <http://fi.data.stirdata.eu/resource/companyType/item/> .
@prefix sd-companystatus-item-fi: <http://fi.data.stirdata.eu/resource/companyStatus/item/> .

@prefix sd-nace-item-fi:    <http://fi.data.stirdata.eu/resource/nace/tol2008/item/> .

######## EXECUTION ORDER ###########

<#Order>
   a dr:D2RMLSpecification ;
   dr:logicalDatasets ( 
                        <#PageMapping>
                        <#OrganizationsMapping> 
                        <#NutsMapping>
                      ) .
#   dr:parameter [
#      drop:name "FULL_PRH_DATA_CSV_URI" ;
#   ] .
   
######## DATA SOURCES ###########

<#PageSource>
   a dris:HTTPSource ;
   dris:uri "https://www.avoindata.fi/data/dataset/yritykset" .
   
<#CountriesSource>
   a dris:HTTPSource ;
   dris:cacheable true ;
   dris:uri "https://stirdata-semantic.ails.ece.ntua.gr/api/content/index/countries/search?text={@@TEXT@@}&keys=label,alt-label&type=match-phrase&fields=label-en,nal,alpha2" .
   
<#PostCodeSource>
   a dris:HTTPSource ;
   dris:cacheable true ;
   dris:uri "https://apps.islab.ntua.gr/geo/api/postcode?country-code={@@COUNTRY@@}&postcode={@@POSTCODE@@}" .
   
<#NutsSource>    
   a dris:HTTPSource ;
   dris:cacheable true ;
   dris:uri "https://stirdata-semantic.ails.ece.ntua.gr/api/content/index/nuts/search?text={@@TEXT@@}&keys=notation&type=match&fields=label" .

<#BussinessRegistryOpenDataSource>
   a dris:HTTPSource .
#   dris:uri "{@@NO_URL_ENCODE@@FULL_PRH_DATA_CSV_URI@@}" .
#   a dris:FileSource ;
#   dris:path "d:/data/stirdata/data/fi/full_prh_data.csv" .

######## TRANSFORMATIONS ###########

<#CountryTransformation>
   dr:logicalArray [ 
      a dr:JSONItemArray ;
      dr:source <#CountriesSource> ;
	  dr:iterator "$";
      dr:limit 1;
   ] .
   
<#PostCodeTransformation>
   dr:logicalArray [ 
      a dr:JSONItemArray ;
      dr:source <#PostCodeSource> ;
	  dr:iterator "$";
	  dr:limit 1;
   ] . 

<#NutsTransformation>
   dr:logicalArray [ 
      a dr:JSONItemArray ;
      dr:source <#NutsSource> ;
	  dr:iterator "$";
	  dr:limit 1;
   ] . 

######## AUXILIARY SOURCE ###########

<#AdminLevelModel>
    a dris:AuxiliaryModel .
	
######## MAIN DATASETS AND MAPPINGS ###########

<#PageMapping>  
   dr:logicalArray [ 
      a dr:RegExItemArray ;
      dr:source <#PageSource> ;
      dr:iterator "(?s)(^.*$)" ;
   ] ;

   dr:definedColumn [
     dr:name "LINK" ;
     dr:function drop:extractMatch ; 
     dr:selector dr:lastElement ;
     dr:parameterBinding [ 
	    dr:parameter "input" ;
        rr:column "##1"  ;
     ] ;
     dr:parameterBinding [  
	    dr:parameter "regex" ;
		rr:constant "<a href=\"(https://www.avoindata.fi/data/dataset/06ce8cf9-8ccc-420b-b558-6cad81c9495a/resource/.*?/download/full_prh_data.csv)\" target=\"_blank\"" ;
     ] ;    
   ] ;

   dr:triplesMap [
      dr:logicalOutput dris:CurrentD2RMLDocument ;
	  
	  rr:subject <#BussinessRegistryOpenDataSource> ;
	  
	  rr:predicateObjectMap [
	     rr:predicate dris:uri ;
		 rr:objectMap [
		    rr:template "{LINK}" ;
			rr:termType rr:Literal ;
		 ] ;
	  ] ;
   ] .
   
<#OrganizationsMapping>  
   rr:logicalTable [ 
      a dr:CSVTable;
      dr:source <#BussinessRegistryOpenDataSource> ;
	  dr:headerRecord true;
	  dr:delimiter ";";
	  dr:quoteCharacter "\"";	  
#      dr:offset 0 ;
#	  dr:limit 10 ;
   ] ;
   
   dr:definedColumns ( 
   [ 
      dr:name "POSTAL_POST_CODE" ;    #postal_post_code can have value 'None', mostly for foreign addresses. In this case postal_address ends with the name county in capitals. May be also missing, '00000' means address unknown
      dr:function drop:extractMatch ; 
      dr:parameterBinding [ 
	     dr:parameter "input" ;
         rr:column "postal_post_code"  ;
      ] ;
      dr:parameterBinding [
	     dr:parameter "regex" ;
         rr:constant "^([0-9]{5})?$"  ;
      ] ; 
   ]   
   [ 
      dr:name "POSTAL_COUNTRY" ; # try to extract postal country when postal_post_code is 'None'
      dr:function drop:extractMatch ; 
      dr:parameterBinding [ 
	     dr:parameter "input" ;
         rr:column "postal_address"  ;
		 dr:condition [
		    rr:column "postal_post_code"  ;
			drop:eq "None"
		 ]
      ] ;
      dr:parameterBinding [
	     dr:parameter "regex" ;
         rr:constant "^.* ([A-Z ]{4,})$"  ;
      ] ; 
   ]
   [ 
      dr:name "POSTAL_ADDRESS" ;    
      dr:function drop:extractMatch ; 
      dr:parameterBinding [ 
	     dr:parameter "input" ;
         rr:column "postal_address"  ;
      ] ;
      dr:parameterBinding [
	     dr:parameter "regex" ;
         #rr:constant "^([^\\d]+?)\\s+([/0-9\\-]+?[a-z]?)(?:\\s+([A-Z]+?))?(?:\\s+([0-9]+))?$"  ;
		 rr:constant "^([^\\d]+?)\\s+([/0-9\\-\\s]+?[a-z]?)(?:\\s+([A-z\\.]+?))?(?:\\s+([0-9]+\\s?[A-z]?))?$" ;
      ] ; 
   ]   
   [ 
      dr:name "STREET_POST_CODE" ;    #street_post_code can have value 'None', mostly for foreign addresses. In this case posta_address ends with the name county in capitals. May be also missing, '00000' means address unknown
      dr:function drop:extractMatch ; 
      dr:parameterBinding [ 
	     dr:parameter "input" ;
         rr:column "street_post_code"  ;
      ] ;
      dr:parameterBinding [
	     dr:parameter "regex" ;
         rr:constant "^([0-9]{5})?$"  ;
      ] ; 
   ]   
   [ 
      dr:name "STREET_COUNTRY" ; # try to extract postal country when postal_post_code is 'None'
      dr:function drop:extractMatch ; 
      dr:parameterBinding [ 
	     dr:parameter "input" ;
         rr:column "street_address"  ;
		 dr:condition [
		    rr:column "street_post_code"  ;
			drop:eq "None"
		 ]
      ] ;
      dr:parameterBinding [
	     dr:parameter "regex" ;
         rr:constant "^.* ([A-Z ]{4,})$"  ;
      ] ; 
   ]
   [ 
      dr:name "STREET_ADDRESS" ;    
      dr:function drop:extractMatch ; 
      dr:parameterBinding [ 
	     dr:parameter "input" ;
         rr:column "street_address"  ;
      ] ;
      dr:parameterBinding [
	     dr:parameter "regex" ;
         #rr:constant "^([^\\d]+?)\\s+([/0-9\\-]+?[a-z]?)(?:\\s+([A-Z]+?))?(?:\\s+([0-9]+))?$"  ;
		 rr:constant "^([^\\d]+?)\\s+([/0-9\\-\\s]+?[a-z]?)(?:\\s+([A-z\\.]+?))?(?:\\s+([0-9]+\\s?[A-z]?))?$" ;
      ] ; 
   ]   
   [ 
      dr:name "EQUAL_ADDRESSES" ;    
      dr:function drop:equals ; 
      dr:parameterBinding [ 
         dr:parameter "input" ;
         rr:template "{postal_address} {postal_post_code} {postal_city}"  ;
      ] ;
      dr:parameterBinding [ 
         dr:parameter "comparand" ;
         rr:template "{street_address} {street_post_code} {street_city}"  ;
      ] ;
   ]      
   ) ;

   dr:transformations ( 
   [
      dr:dataref <#CountryTransformation> ;
      dr:transformationName "PostalCountryTransformation";
      dr:parameterBinding [ 
         dr:parameter "TEXT" ;
         rr:column "POSTAL_COUNTRY" ;
      ]  ;
   ]
   [
      dr:dataref <#PostCodeTransformation> ;
      dr:transformationName "PostalPostCodeTransformation";
      dr:parameterBinding [ 
         dr:parameter "COUNTRY" ;
         rr:constant "FI" ;
      ] ;
      dr:parameterBinding [ 
         dr:parameter "POSTCODE" ;
         rr:column "POSTAL_POST_CODE" ;
         dr:condition [
            rr:column "POSTAL_POST_CODE" ;
            drop:neq "";
         ] ; 		 
      ]  ;
   ]
   [
      dr:dataref <#NutsTransformation> ;
      dr:transformationName "PostalNutsTransformation";
      dr:parameterBinding [ 
         dr:parameter "TEXT" ;
         rr:column "PostalPostCodeTransformation~~$.NUTS3" ;
      ]  ;
   ]   
  [
      dr:dataref <#CountryTransformation> ;
      dr:transformationName "StreetCountryTransformation";
      dr:parameterBinding [ 
         dr:parameter "TEXT" ;
         rr:column "STREET_COUNTRY" ;
      ]  ;
   ]
   [
      dr:dataref <#PostCodeTransformation> ;
      dr:transformationName "StreetPostCodeTransformation";
      dr:parameterBinding [ 
         dr:parameter "COUNTRY" ;
         rr:constant "FI" ;
      ] ;
      dr:parameterBinding [ 
         dr:parameter "POSTCODE" ;
         rr:column "STREET_POST_CODE" ;
         dr:condition [
            rr:column "STREET_POST_CODE" ;
            drop:neq "";
         ] ; 		 
      ]  ;
   ]
   [
      dr:dataref <#NutsTransformation> ;
      dr:transformationName "StreetNutsTransformation";
      dr:parameterBinding [ 
         dr:parameter "TEXT" ;
         rr:column "StreetPostCodeTransformation~~$.NUTS3" ;
      ]  ;
   ]   ) ;   
   
   dr:triplesMap [
      # ID - CLASS
      rr:subjectMap [ 
         rr:template  "{@sd-organization-fi}{business_id}" ;
         rr:class legal:LegalEntity ;
      ] ;
   
#      # SYSTEM IDENTIFIER
#      rr:predicateObjectMap [ 
#         rr:predicate  dct:identifier ;
#         rr:objectMap  [ 
#            rr:column "business_id" ;
#            rr:termType  rr:Literal ;
#         ] ;
#      ] ; 
   
#      # THIRD PARTY WEBPAGES ( NON-LINKED DATA )
#      rr:predicateObjectMap [ 
##         rr:predicate  schema:sameAs ;
#         rr:predicate  owl:sameAs ;
#         rr:objectMap  [ 
#            rr:template "https://avoindata.prh.fi/bis/v1/{business_id}" ;
##            rr:datatype xsd:anyURI ;
#            rr:termType rr:IRI ;
#         ] ;
##        rr:objectMap  [ 
##           rr:template "https://opencorporates.com/companies/fi/{$.businessId}" ;
###          rr:datatype xsd:anyURI ;
##           rr:termType rr:IRI ;
##       ] ;   
#      ] ;   
   
      # PRH REGISTRATION IDENTIFIER
      rr:predicateObjectMap [
         rr:predicate legal:legalIdentifier ;
         rr:objectMap [
            rr:parentTriplesMap [
               rr:subjectMap [
                  rr:template "{@sd-identifier-fi}prh/{business_id}" ;
                  rr:class adms:Identifier ;
               ] ;

               # DATE ISSUED
               rr:predicateObjectMap [
                  rr:predicate dct:issued;
                  rr:objectMap [
                     rr:column "registration_date" ;
                     rr:datatype xsd:date;
                  ] ;
               ] ;

               # REGISTRATION NUMBER 
               rr:predicateObjectMap [
               rr:predicate skos:notation;
                  rr:objectMap [
                     rr:column "business_id" ;
                     rr:termType rr:Literal;
                  ] ;
               ] ;

               # ISSUING BODY
               rr:predicateObjectMap [
                  rr:predicate dct:creator;
                  rr:objectMap [
                     rr:constant sd-agency-fi:prh ;
                  ] ;
               ] ;
            ] ;
         ] ;
      ] ;     
   
      # COMPANY NAME
      rr:predicateObjectMap [ 
         rr:predicate  legal:legalName ;
         rr:objectMap  [ 
            rr:column "company_name" ;
            rr:termType  rr:Literal ;
         ] ;
      ] ;   
   
#      # JURISDICTION (ASSUME FINLAND FOR ALL) // check if
#      rr:predicateObjectMap [ 
#         rr:predicate  dbo:jurisdiction ;
#         rr:objectMap  [ 
#            rr:constant <http://publications.europa.eu/resource/authority/country/FIN> ;
#            rr:termType  rr:IRI ;
#         ]   ;
#      ] ;     
   
      # LEGAL FORM (TEMPORARY UNTIL SKOS VOCABULARY DECIDED)
      rr:predicateObjectMap [ 
         rr:predicate  legal:companyType ;
         rr:objectMap  [ 
            rr:template "{@sd-companytype-item-fi}{company_form}" ;
            rr:termType  rr:IRI ;
         ] ;
      ] ;  
   
      # FOUNDATION DATE (use registration date)
      rr:predicateObjectMap [ 
         rr:predicate  schema:foundingDate ;
         rr:objectMap  [ 
            rr:column "registration_date" ;
            rr:datatype xsd:date ;
         ] ;         
      ] ;        
   
      # ACTIVITY   
      # Almost all have 5-digit codes. Very few have upper level codes.
      rr:predicateObjectMap [
         rr:predicate legal:companyActivity ;
         rr:objectMap [
            rr:template "{@sd-nace-item-fi}{business_line_code}" ;
            rr:termType rr:IRI ;
         ] ;
      ]  ;  
   
      # POSTAL ADDRESS (use postal address only : fix after defining d2rml conditions)
      rr:predicateObjectMap [ 
         rr:predicateMap [
            rr:constant legal:registeredAddress ;
            dr:condition [
               rr:column "postal_post_code" ;
               drop:neq "";
               dr:booleanOperator drop:and ;
               dr:operand [
                  rr:column "postal_address" ;
                  drop:neq "";
                  dr:booleanOperator drop:and ;
                  dr:operand [
                     rr:column "postal_post_code" ;
                     drop:neq "00000";
                  ] ;
               ] ;
            ] ;
         ] ;    
         rr:objectMap [
            rr:parentTriplesMap [
               rr:subjectMap [
                  rr:template "{@sd-address-fi}{business_id}";
                  rr:class locn:Address ;
               ] ;
		   	
               rr:predicateObjectMap [ 
                  rr:predicate  locn:fullAddress ;
                  rr:objectMap  [ 
                     rr:template  "<<{postal_address} >><<{POSTAL_POST_CODE} >><<{postal_city}>>" ;
                     rr:termType  rr:Literal ;
                  ] ;
               ] ;

               rr:predicateObjectMap [ 
                  rr:predicate  locn:thoroughfare ;
                  rr:objectMap  [ 
                     rr:column "POSTAL_ADDRESS.match#1" ;
                     rr:termType  rr:Literal ;
                  ] ;      
               ] ;   

               rr:predicateObjectMap [ 
                  rr:predicate  locn:locatorDesignator ;
                  rr:objectMap  [ 
                     rr:column "POSTAL_ADDRESS.match#2" ;
                     rr:termType  rr:Literal ;
                  ] ;      
               ]  ;   

               rr:predicateObjectMap [ 
                  rr:predicate  locn:locatorName ;
                  rr:objectMap  [ 
                     rr:template "{POSTAL_ADDRESS.match#3}<< {POSTAL_ADDRESS.match#4}>>" ;
                     rr:termType  rr:Literal ;
                  ] ;      
               ] ;   

               rr:predicateObjectMap [ 
                  rr:predicate  locn:postCode ;
                  rr:objectMap  [ 
                     rr:column "POSTAL_POST_CODE" ;
                     rr:termType  rr:Literal ;
                  ] ;      
               ] ;   
			
               rr:predicateObjectMap [ 
                  rr:predicate  locn:postName ;
                  rr:objectMap  [ 
                     rr:column "postal_city" ;
                     rr:termType  rr:Literal ;
                  ] ;      
               ] ;                       
			
               rr:predicateObjectMap [ 
                  rr:predicate  locn:adminUnitL1 ;
                  rr:objectMap  [ 
                     dr:cases ( [
                        rr:constant "FI" ;
                        rr:termType  rr:Literal ;
                        dr:condition [
                           rr:column "POSTAL_POST_CODE" ;
                           drop:neq "";
                       ] ;
                     ] [
                        rr:column "PostalCountryTransformation~~$.alpha2[0]" ;
                        rr:termType  rr:Literal ;
                     ] )
                  ] ;      
               ] ;                    

               rr:predicateObjectMap [ 
                  rr:predicate  locn:adminUnitL2 ;
                  rr:objectMap  [ 
                     rr:column "PostalNutsTransformation~~$.label[0]" ;
                     rr:termType  rr:Literal ;
                     dr:condition [
                        rr:column "POSTAL_POST_CODE" ;
                        drop:neq "";
                     ] ;
                  ] ;
               ] ; 

               rr:predicateObjectMap [ 
                  rr:predicate  m8g:adminUnit ;
                  rr:objectMap  [ 
                     dr:cases ( [
                        rr:constant <http://publications.europa.eu/resource/authority/country/FIN> ;
                        rr:termType  rr:IRI ;
                        dr:condition [
                           rr:column "POSTAL_POST_CODE" ;
                           drop:neq "";
                        ]
                     ] [
                        rr:column "PostalCountryTransformation~~$.nal[0]" ;
                        rr:termType  rr:IRI ;
                     ] ) ;
                 ] ;    
               ] ;

               rr:predicateObjectMap [ 
                 rr:predicate  m8g:adminUnit ;
                 rr:objectMap  [ 
                    rr:template "{@nuts-item}{PostalPostCodeTransformation~~$.NUTS3}" ;
                    rr:termType  rr:IRI ;
                 ]
               ] ;

               rr:predicateObjectMap [ 
                  rr:predicate  m8g:adminUnit ;
                  rr:objectMap  [ 
                     rr:template "{@lau-item}{PostalPostCodeTransformation~~$.LAU}" ;
                     rr:termType  rr:IRI ;
                  ]
               ] ;
            ] ;
         ] ;
      ]
   ]  ;
   
#      # STREET ADDRESS 
#      rr:predicateObjectMap [ 
#         rr:predicateMap [
#            rr:constant sd-voc:streetAddress ;
#            dr:condition [
#               rr:column "street_post_code" ;
#               drop:neq "";
#               dr:booleanOperator drop:and ;
#               dr:operand [
#                  rr:column "street_address" ;
#                  drop:neq "";
#                  dr:booleanOperator drop:and ;
#                  dr:operand [
#                     rr:column "street_post_code" ;
#                     drop:neq "00000";
#                     dr:booleanOperator drop:and ;
#                     dr:operand [
#                        rr:column "EQUAL_ADDRESSES" ;
#                        drop:eq true;
#                     ] ;
#                  ] ;
#               ] ;
#            ]    ;
#         ] ;    
#         rr:objectMap [
#            rr:parentTriplesMap [
#               rr:subjectMap [
#                  rr:template "{@sd-address-fi}{business_id}/p";
#               ] ;
#            ]  ;
#         ]
#      ] ;
#
#      # STREET ADDRESS 
#      rr:predicateObjectMap [ 
#         rr:predicateMap [
#            rr:constant sd-voc:streetAddress ;
#            dr:condition [
#               rr:column "street_post_code" ;
#               drop:neq "";
#               dr:booleanOperator drop:and ;
#               dr:operand [
#                  rr:column "street_address" ;
#                  drop:neq "";
#                  dr:booleanOperator drop:and ;
#                  dr:operand [
#                     rr:column "street_post_code" ;
#                     drop:neq "00000";
#                     dr:booleanOperator drop:and ;
#                     dr:operand [
#                        rr:column "EQUAL_ADDRESSES" ;
#                        drop:eq false;
#                     ] ;
#                  ] ;
#               ] ;
#            ] ;
#         ] ;    
#         rr:predicateMap [
#            rr:constant legal:registeredAddress ;
#            dr:condition [
#               rr:column "street_post_code" ;
#               drop:neq "";
#               dr:booleanOperator drop:and ;
#               dr:operand [
#                  rr:column "street_address" ;
#                  drop:neq "";
#                  dr:booleanOperator drop:and ;
#                  dr:operand [
#                     rr:column "street_post_code" ;
#                     drop:neq "00000";
#                     dr:booleanOperator drop:and ;
#                     dr:operand [
#                        rr:column "EQUAL_ADDRESSES" ;
#                        drop:neq "true";
#                     ] ;
#                  ] ;
#               ] ;
#            ] ;
#         ] ;    
#         rr:objectMap [
#            rr:parentTriplesMap [
#               rr:subjectMap [
#                  rr:template "{@sd-address-fi}{business_id}/s";
#                  rr:class locn:Address ;
#               ] ;
#			
#               rr:predicateObjectMap [ 
#                  rr:predicate  locn:fullAddress ;
#                  rr:objectMap  [ 
#                     rr:template  "<<{street_address} >><<{STREET_POST_CODE} >><<{street_city}>>" ;
#                     rr:termType  rr:Literal ;
#                  ] ;
#               ] ;
#
#               rr:predicateObjectMap [ 
#                  rr:predicate  locn:thoroughfare ;
#                  rr:objectMap  [ 
#                     rr:column "STREET_ADDRESS.match#1" ;
#                     rr:termType  rr:Literal ;
#                  ] ;      
#               ] ;   
#
#               rr:predicateObjectMap [ 
#                  rr:predicate  locn:locatorDesignator ;
#                  rr:objectMap  [ 
#                     rr:column "STREET_ADDRESS.match#2" ;
#                     rr:termType  rr:Literal ;
#                  ] ;      
#               ] ;   
#
#               rr:predicateObjectMap [ 
#                  rr:predicate  locn:locatorName ;
#                  rr:objectMap  [ 
#                     rr:template "{STREET_ADDRESS.match#3}<< {STREET_ADDRESS.match#4}>>" ;
#                     rr:termType  rr:Literal ;
#                  ] ;      
#               ] ;   
#
#               rr:predicateObjectMap [ 
#                  rr:predicate  locn:postCode ;
#                  rr:objectMap  [ 
#                     rr:column "STREET_POST_CODE" ;
#                     rr:termType  rr:Literal ;
#                  ] ;      
#               ] ;   
#			
#               rr:predicateObjectMap [ 
#                  rr:predicate  locn:postName ;
#                  rr:objectMap  [ 
#                     rr:column "street_city" ;
#                     rr:termType  rr:Literal ;
#                  ] ;      
#               ] ;                       
#			
#               rr:predicateObjectMap [ 
#                  rr:predicate  locn:adminUnitL1 ;
#                  rr:objectMap  [ 
#                     dr:cases ( [
#                        rr:constant "FI" ;
#                        rr:termType  rr:Literal ;
#                        dr:condition [
#                           rr:column "STREET_POST_CODE" ;
#                           drop:neq "";
#                       ] ;
#                     ] [
#                        rr:column "StreetCountryTransformation~~$.alpha2[0]" ;
#                        rr:termType  rr:Literal ;
#                     ] )
#                  ] ;      
#               ] ;                    
#
#               rr:predicateObjectMap [ 
#                  rr:predicate  locn:adminUnitL2 ;
#                  rr:objectMap  [ 
#                     rr:column "StreetNutsTransformation~~$.label[0]" ;
#                     rr:termType  rr:Literal ;
#                     dr:condition [
#                        rr:column "STREET_POST_CODE" ;
#                        drop:neq "";
#                     ] ;
#                  ] ;
#               ] ; 
#
#               rr:predicateObjectMap [ 
#                  rr:predicate  m8g:adminUnit ;
#                  rr:objectMap  [ 
#                     dr:cases ( [
#                        rr:constant <http://publications.europa.eu/resource/authority/country/FIN> ;
#                        rr:termType  rr:IRI ;
#                        dr:condition [
#                           rr:column "STREET_POST_CODE" ;
#                           drop:neq "";
#                        ]
#                     ] [
#                        rr:column "StreetCountryTransformation~~$.nal[0]" ;
#                        rr:termType  rr:IRI ;
#                     ] ) ;
#                 ] ;    
#               ] ;
#
#               rr:predicateObjectMap [ 
#                 rr:predicate  m8g:adminUnit ;
#                 rr:objectMap  [ 
#                    rr:template "{@nuts-item}{StreetPostCodeTransformation~~$.NUTS3}" ;
#                    rr:termType  rr:IRI ;
#                 ]
#               ] ;
#
#               rr:predicateObjectMap [ 
#                  rr:predicate  m8g:adminUnit ;
#                  rr:objectMap  [ 
#                     rr:template "{@lau-item}{StreetPostCodeTransformation~~$.LAU}" ;
#                     rr:termType  rr:IRI ;
#                  ]
#               ] ;
#            ] ;
#         ] ;
#      ] ;
#   ];

   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
         dr:cases ( [
            rr:constant <http://publications.europa.eu/resource/authority/country/FIN> ;
            rr:termType  rr:IRI ;
            rr:class sd-voc-adminlevel:Country ;
            dr:condition [
               rr:column "POSTAL_POST_CODE" ;
               drop:neq "";
            ]
        ] [
           rr:column "PostalCountryTransformation~~$.nal[0]" ;
           rr:termType  rr:IRI ;
           rr:class sd-voc-adminlevel:Country ;
        ] ) ;
      ] ;
   ] ;
   
#   dr:triplesMap [
#      dr:logicalOutput <#AdminLevelModel> ;
#      rr:subjectMap [ 
#         dr:cases ( [
#            rr:constant <http://publications.europa.eu/resource/authority/country/FIN> ;
#            rr:termType  rr:IRI ;
#            rr:class sd-voc-adminlevel:Country ;
#            dr:condition [
#               rr:column "STREET_POST_CODE" ;
#               drop:neq "";
#            ]
#        ] [
#           rr:column "StreetCountryTransformation~~$.nal[0]" ;
#           rr:termType  rr:IRI ;
#           rr:class sd-voc-adminlevel:Country ;
#        ] ) ;
#      ] ;
#   ] ;   
   
   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
         rr:template "{@nuts-item}{PostalPostCodeTransformation~~$.NUTS3}";
         rr:class sd-voc-adminlevel:NUTS-3 ;
      ] ;
   ] ;
   
#   dr:triplesMap [
#      dr:logicalOutput <#AdminLevelModel> ;
#         rr:subjectMap [ 
#         rr:template "{@nuts-item}{StreetPostCodeTransformation~~$.NUTS3}";
#         rr:class sd-voc-adminlevel:NUTS-3 ;
#      ] ;
#   ] ;

   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
         rr:template "{@lau-item}{PostalPostCodeTransformation~~$.LAU}" ;
         rr:class sd-voc-adminlevel:LAU ;
      ] ;
   ] .
   
#   dr:triplesMap [
#      dr:logicalOutput <#AdminLevelModel> ;
#      rr:subjectMap [ 
#         rr:template "{@lau-item}{StreetPostCodeTransformation~~$.LAU}" ;
#         rr:class sd-voc-adminlevel:LAU ;
#      ] ;
#   ] .

<#NutsMapping>
   rr:logicalTable [ 
      dr:source <#AdminLevelModel> ;
      dr:sparqlQuery "SELECT ?uri ?type WHERE { ?uri a ?type } "; 
   ] ;
   
   dr:triplesMap[ 
      rr:subjectMap [ 
         rr:column "uri";
         rr:class m8g:AdminUnit ;
      ] ;

      rr:predicateObjectMap [ 
         rr:predicate  m8g:code ;
         rr:objectMap  [ 
            rr:column "uri" ;
            rr:termType  rr:IRI ;
         ]
      ] ;   

      rr:predicateObjectMap [ 
         rr:predicate  m8g:level ;
         rr:objectMap  [ 
            rr:column "type" ;
            rr:termType rr:IRI ;
         ]
      ] ;   
   ] .