@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/> .
@prefix org: <http://www.w3.org/ns/org#> .
@prefix http: 	<http://www.w3.org/2011/http#> .
@prefix cnt: <http://www.w3.org/2011/content#> .

######## 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-be: <http://be.data.stirdata.eu/resource/agency/> .
@prefix sd-organization-be: <http://be.data.stirdata.eu/resource/organization/> .
@prefix sd-unit-be: <http://be.data.stirdata.eu/resource/unit/> .
@prefix sd-site-be: <http://be.data.stirdata.eu/resource/site/> .
@prefix sd-address-be: <http://be.data.stirdata.eu/resource/address/> .
@prefix sd-identifier-be: <http://be.data.stirdata.eu/resource/identifier/> .

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

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

<#Order>
   a dr:LogicalDatasetOrder ;
   dr:logicalDatasets ( 
                        <#ECBPageMapping> # Get most recent link
                        <#EnterpriseMapping>
                        <#EstablishmentMapping>  
                        <#BranchMapping>  
                        <#DenominationMapping>  
                        <#ContactMapping>  
                        <#ActivityMapping>  
                        <#AddressMapping>
                        <#NutsMapping>
                      ) ;
   dr:parameter [
      drop:name "KBO_USERNAME" ;
   ] ;
   dr:parameter [
      drop:name "KBO_PASSWORD" ;
   ] .	                      

######## DATA SOURCES ###########

<#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,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@@}&fields=label&type=match&keys=notation" .
   
<#ECBPageSource>
   a dris:HTTPSource ;
   dris:preHttpRequests ( [   
      http:absoluteURI "https://kbopub.economie.fgov.be/kbo-open-data/static/j_spring_security_check" ;
      http:methodName "POST" ;
      http:headers ( [ http:fieldName "Content-Type" ;
                       http:fieldValue "application/x-www-form-urlencoded" ; ] ) ;
      http:body [ a cnt:ContentAsText ;
                  cnt:chars "j_username={@@KBO_USERNAME@@}&j_password={@@KBO_PASSWORD@@}" ] ;
   ] ) ;
   dris:uri "https://kbopub.economie.fgov.be/kbo-open-data/affiliation/xml/?files" .

   
<#AllDataSource>
   a dris:HTTPSource ;
   dris:preHttpRequests ( [   
      http:absoluteURI "https://kbopub.economie.fgov.be/kbo-open-data/static/j_spring_security_check" ;
      http:methodName "POST" ;
      http:headers ( [ http:fieldName "Content-Type" ;
                       http:fieldValue "application/x-www-form-urlencoded" ; ] ) ;
      http:body [ a cnt:ContentAsText ;
                  cnt:chars "j_username={@@KBO_USERNAME@@}&j_password={@@KBO_PASSWORD@@}" ] ;
   ] ) .

<#EnterpriseSource>
   a dris:FileSource ;
   dris:containerSource <#AllDataSource> ;
   dris:path "enterprise.csv" .

<#EstablishmentSource>
   a dris:FileSource ;
   dris:containerSource <#AllDataSource> ;
   dris:path "establishment.csv" .

<#BranchSource>
   a dris:FileSource ;
   dris:containerSource <#AllDataSource> ;
   dris:path "branch.csv" .
   
<#DenominationSource>
   a dris:FileSource ;
   dris:containerSource <#AllDataSource> ;
   dris:path "denomination.csv" .

<#ContactSource>
   a dris:FileSource ;
   dris:containerSource <#AllDataSource> ;
   dris:path "contact.csv" .

<#ActivitySource>
   a dris:FileSource ;
   dris:containerSource <#AllDataSource> ;
   dris:path "activity.csv" .

<#AddressSource>
   a dris:FileSource ;
   dris:containerSource <#AllDataSource> ;
   dris:path "address.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 MAPPINGS ###########

<#ECBPageMapping>
   dr:logicalArray [ 
      a dr:RegExItemArray ;
      dr:source <#ECBPageSource> ;
      dr:iterator "(?s)(<html>.*</html>)" ; 
   ] ;
   
   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 "href=\"(.*?_Full\\.zip)\">"  ;
     ] ;    
   ] ;
   
   dr:triplesMap [
      dr:logicalOutput dris:CurrentD2RMLDocument ;
	  
	  rr:subject <#AllDataSource> ;
	  
	  rr:predicateObjectMap [
	     rr:predicate dris:uri ;
		 rr:objectMap [
		    rr:template "https://kbopub.economie.fgov.be/kbo-open-data/affiliation/xml/{LINK}" ;
			rr:termType rr:Literal ;
		 ] ;
	  ] ;
   ] .
   
<#EnterpriseMapping>  
# ++ EnterpriseNumber
#    Status (can be only AC:active)
# ++ JuridicalSituation
# +  TypeOfEnterprise
# ++ JuridicalForm
# ++ StartDate

   rr:logicalTable [ 
      a dr:CSVTable;
      dr:source <#EnterpriseSource> ;
	  dr:headerRecord true;
	  dr:delimiter ",";
	  dr:quoteCharacter "\"";
   ] ;
   
   dr:definedColumn [ 
      dr:name "EnterpriseNumber-NODOT" ;
      dr:function drop:replace ; 
      dr:parameterBinding [ 
  	     dr:parameter "input" ;
         rr:column "EnterpriseNumber"  ;
      ] ;
      dr:parameterBinding [			
         dr:parameter "regex" ;
         rr:constant "\\."  ;
      ] ; 
      dr:parameterBinding [			
         dr:parameter "replacement" ;
         rr:constant ""  ;
      ] ; 
   ] ;
   
   dr:definedColumns ( [ 
      dr:name "StartDate-YEAR" ;
      dr:function drop:extractMatch ; 
      dr:parameterBinding [ 
	     dr:parameter "input" ;
         rr:column "StartDate"  ;
      ] ;
      dr:parameterBinding [			
	     dr:parameter "regex" ;
         rr:constant "^[0-9]{2}-[0-9]{2}-([0-9]{4})$"  ;
      ] ; 
   ] [ 
     dr:name "StartDate-MONTH" ;
     dr:function drop:extractMatch ; 
     dr:parameterBinding [ 
	    dr:parameter "input" ;
        rr:column "StartDate"  ;
     ] ;
     dr:parameterBinding [  
	    dr:parameter "regex" ;
        rr:constant "^[0-9]{2}-([0-9]{2})-([0-9]{4})$"  ;
     ] ; 
   ] [ 
     dr:name "StartDate-DAY" ;
     dr:function drop:extractMatch ; 
        dr:parameterBinding [ 
		   dr:parameter "input" ;
           rr:column "StartDate"  ;
        ] ;
        dr:parameterBinding [ 
		   dr:parameter "regex" ;
           rr:constant "^([0-9]{2})-[0-9]{2}-([0-9]{4})$"  ;
        ] ; 
   ] ) ;
   
   # ID - CLASS
   rr:subjectMap [ 
      rr:template  "{@sd-organization-be}{EnterpriseNumber}" ;
      rr:class legal:LegalEntity ;
   ] ;
   
#   # SYSTEM IDENTIFIER
#   rr:predicateObjectMap [ 
#      rr:predicate  dcterms:identifier ;
#      rr:objectMap  [ 
#         rr:column "EnterpriseNumber" ;
#         rr:termType  rr:Literal ;
#      ] ;
#   ] ;   
   
   # THIRD PARTY WEBPAGES ( NON-LINKED DATA )
   rr:predicateObjectMap [ 
      rr:predicate  schema:sameAs ;
      rr:objectMap  [ 
         rr:template "https://kbopub.economie.fgov.be/kbopub/toonondernemingps.html?ondernemingsnummer={EnterpriseNumber-NODOT}" ;
#         rr:datatype xsd:anyURI ;
         rr:termType rr:IRI ;
      ] ;
   ] ;    
   
   # CBE REGISTRATION IDENTIFIER
   rr:predicateObjectMap [
      rr:predicate legal:legalIdentifier ;
      rr:objectMap [
         rr:parentTriplesMap [
            rr:subjectMap [
               rr:template "{@sd-identifier-be}{EnterpriseNumber}" ;
               rr:class adms:Identifier ;
            ] ;

            # DATE ISSUED
            rr:predicateObjectMap [
               rr:predicate dct:issued;
               rr:objectMap [
                  rr:template "{StartDate-YEAR}-{StartDate-MONTH}-{StartDate-DAY}" ;
                  rr:datatype xsd:date;
               ] ;
            ] ;

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

            # ISSUING BODY
            rr:predicateObjectMap [
               rr:predicate dct:creator;
               rr:objectMap [
                  rr:constant sd-agency-be:cbe ;
               ] ;
            ] ;
         ] ;
      ] ;
   ] ;    

#   # JURISDICTION: BELGIUM FOR NATURAL PERSONS
#   rr:predicateObjectMap [ 
#      rr:predicate  dbo:jurisdiction ;
#      rr:objectMap  [ 
#         rr:constant <http://publications.europa.eu/resource/authority/country/BEL> ;
#         rr:termType  rr:IRI ;
#         dr:condition [
#            rr:column "TypeOfEnterprise" ;
#            drop:eq "1" ;
#        ] ;
#      ] ;
#   ] ;  
   
   # LEGAL FORM (TEMPORARY UNTIL SKOS VOCABULARY DECIDED)
   rr:predicateObjectMap [ 
      rr:predicate  legal:companyType ;
      rr:objectMap  [ 
         dr:cases ( [
            rr:template "{@sd-companytype-item-be}{JuridicalForm}" ;
            rr:termType  rr:IRI ;
            dr:condition [
               rr:column "TypeOfEnterprise" ;
               drop:eq "2" ;
           ] ;
         ] [
            rr:template "{@sd-companytype-item-be}000" ;
            rr:termType  rr:IRI ;
            dr:condition [
               rr:column "TypeOfEnterprise" ;
               drop:eq "1" ;
           ] ;
        ] )
      ] ;
   ] ;   
   
   # STATUS (TEMPORARY UNTIL SKOS VOCABULARY DECIDED)
   rr:predicateObjectMap [ 
      rr:predicate  legal:companyStatus ;
      rr:objectMap  [ 
         rr:template "{@sd-companystatus-item-be}{JuridicalSituation}" ;
         rr:termType  rr:IRI ;
      ] ;
   ] ;  
   
   # FOUNDATION DATE (THIS IS IN FACT LAST REGISTRATION DATE)
   rr:predicateObjectMap [ 
      rr:predicate  m8g:registrationDate ;
      rr:objectMap  [ 
         rr:template "{StartDate-YEAR}-{StartDate-MONTH}-{StartDate-DAY}" ;
         rr:datatype  xsd:date ;      
      ] ;
   ] .


<#EstablishmentMapping>  
# ++ EstablishmentNumber
# ++ StartDate
# ++ EnterpriseNumber

   rr:logicalTable [ 
      a dr:CSVTable;
      dr:source <#EstablishmentSource> ;
	  dr:headerRecord true;
	  dr:delimiter ",";
	  dr:quoteCharacter "\"";
#      dr:limit 1 ;
   ] ;
   
   dr:definedColumn [ 
      dr:name "EnterpriseNumber-NODOT" ;
      dr:function drop:replace ; 
      dr:parameterBinding [ 
  	     dr:parameter "input" ;
         rr:column "EnterpriseNumber"  ;
      ] ;
      dr:parameterBinding [			
         dr:parameter "regex" ;
         rr:constant "\\."  ;
      ] ; 
      dr:parameterBinding [			
         dr:parameter "replacement" ;
         rr:constant ""  ;
      ] ; 
   ] ;
   
   dr:definedColumn [ 
      dr:name "EstablishmentNumber-NODOT" ;
      dr:function drop:replace ; 
      dr:parameterBinding [ 
  	     dr:parameter "input" ;
         rr:column "EstablishmentNumber"  ;
      ] ;
      dr:parameterBinding [			
         dr:parameter "regex" ;
         rr:constant "\\."  ;
      ] ; 
      dr:parameterBinding [			
         dr:parameter "replacement" ;
         rr:constant ""  ;
      ] ; 
   ] ;
   
   dr:definedColumns ( [ 
      dr:name "StartDate-YEAR" ;
      dr:function drop:extractMatch ; 
      dr:parameterBinding [ 
	     dr:parameter "input" ;
         rr:column "StartDate"  ;
      ] ;
      dr:parameterBinding [			
	     dr:parameter "regex" ;
         rr:constant "^[0-9]{2}-[0-9]{2}-([0-9]{4})$"  ;
      ] ; 
   ] [ 
     dr:name "StartDate-MONTH" ;
     dr:function drop:extractMatch ; 
     dr:parameterBinding [ 
	    dr:parameter "input" ;
        rr:column "StartDate"  ;
     ] ;
     dr:parameterBinding [  
	    dr:parameter "regex" ;
        rr:constant "^[0-9]{2}-([0-9]{2})-([0-9]{4})$"  ;
     ] ; 
   ] [ 
     dr:name "StartDate-DAY" ;
     dr:function drop:extractMatch ; 
        dr:parameterBinding [ 
		   dr:parameter "input" ;
           rr:column "StartDate"  ;
        ] ;
        dr:parameterBinding [ 
		   dr:parameter "regex" ;
           rr:constant "^([0-9]{2})-[0-9]{2}-([0-9]{4})$"  ;
        ] ; 
   ] ) ;
   
   # ID - CLASS
   rr:subjectMap [ 
      rr:template  "{@sd-unit-be}{EstablishmentNumber}" ;
      rr:class org:Site ;
   ] ;
   
#   # SYSTEM IDENTIFIER
#   rr:predicateObjectMap [ 
#      rr:predicate  dct:identifier ;
#      rr:objectMap  [ 
#         rr:column "EstablishmentNumber" ;
#         rr:termType  rr:Literal ;
#      ] ;
#   ] ; 
   
   # THIRD PARTY WEBPAGES ( NON-LINKED DATA )
   rr:predicateObjectMap [ 
      rr:predicate  schema:sameAs ;
      rr:objectMap  [ 
         rr:template "https://kbopub.economie.fgov.be/kbopub/toonvestigingps.html?vestigingsnummer={EstablishmentNumber-NODOT}" ;
#         rr:datatype xsd:anyURI ;
         rr:termType rr:IRI ;
      ] ;
   ] ;     
   
   # ENTERPISE
   rr:predicateObjectMap [ 
#      rr:predicate  org:isUnitOf;
#      rr:predicate  org:isSubOrganizationOf;
      rr:predicate  org:isSiteOf;
      rr:objectMap  [ 
         rr:template  "{@sd-organization-be}{EnterpriseNumber}" ;
         rr:termType  rr:IRI ;
      ] ;
   ] ;   
   
   dr:inversePredicateObjectMap [ 
#      rr:predicate  org:hasUnit;
#      rr:predicate  org:hasSubOrganization;
      rr:predicate  org:hasSite;
      rr:objectMap  [ 
         rr:template  "{@sd-organization-be}{EnterpriseNumber}" ;
         rr:termType  rr:IRI ;
      ] ;
   ] ;   
   
   # CBE REGISTRATION IDENTIFIER
   rr:predicateObjectMap [
      rr:predicate legal:legalIdentifier ;
      rr:objectMap [
         rr:parentTriplesMap [
            rr:subjectMap [
               rr:template "{@sd-identifier-be}{EstablishmentNumber}" ;
               rr:class adms:Identifier ;
            ] ;

            # DATE ISSUED
            rr:predicateObjectMap [
               rr:predicate dct:issued;
               rr:objectMap [
                  rr:template "{StartDate-YEAR}-{StartDate-MONTH}-{StartDate-DAY}" ;
                  rr:datatype xsd:date;
               ] ;
            ] ;

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

            # ISSUING BODY
            rr:predicateObjectMap [
               rr:predicate dct:creator;
               rr:objectMap [
                  rr:constant sd-agency-be:cbe ;
               ] ;
            ] ;
         ] ;
      ] ;
   ] ;    

   # FOUNDATION DATE 
   rr:predicateObjectMap [ 
      rr:predicate  m8g:registrationDate ;
      rr:objectMap  [ 
         rr:template "{StartDate-YEAR}-{StartDate-MONTH}-{StartDate-DAY}" ;
         rr:datatype  xsd:date ;      
      ] ;
   ] .
   
<#BranchMapping>  
# ++ Id
# ++ StartDate
# ++ EnterpriseNumber

   rr:logicalTable [ 
      a dr:CSVTable;
      dr:source <#BranchSource> ;
	  dr:headerRecord true;
	  dr:delimiter ",";
	  dr:quoteCharacter "\"";
#      dr:limit 1 ;
   ] ;
   
#   dr:definedColumn [ 
#      dr:name "EnterpriseNumber-NODOT" ;
#      dr:function drop:replace ; 
#      dr:parameterBinding [ 
#  	     dr:parameter "input" ;
#         rr:column "EnterpriseNumber"  ;
#      ] ;
#      dr:parameterBinding [			
#         dr:parameter "regex" ;
#         rr:constant "\\."  ;
#      ] ; 
#      dr:parameterBinding [			
#         dr:parameter "replacement" ;
#         rr:constant ""  ;
#      ] ; 
#   ] ;
#   
#   dr:definedColumn [ 
#      dr:name "Id-NODOT" ;
#      dr:function drop:replace ; 
#      dr:parameterBinding [ 
#  	     dr:parameter "input" ;
#         rr:column "Id"  ;
#      ] ;
#      dr:parameterBinding [			
#         dr:parameter "regex" ;
#         rr:constant "\\."  ;
#      ] ; 
#      dr:parameterBinding [			
#         dr:parameter "replacement" ;
#         rr:constant ""  ;
#      ] ; 
#   ] ;
   
   dr:definedColumns ( [ 
      dr:name "StartDate-YEAR" ;
      dr:function drop:extractMatch ; 
      dr:parameterBinding [ 
	     dr:parameter "input" ;
         rr:column "StartDate"  ;
      ] ;
      dr:parameterBinding [			
	     dr:parameter "regex" ;
         rr:constant "^[0-9]{2}-[0-9]{2}-([0-9]{4})$"  ;
      ] ; 
   ] [ 
     dr:name "StartDate-MONTH" ;
     dr:function drop:extractMatch ; 
     dr:parameterBinding [ 
	    dr:parameter "input" ;
        rr:column "StartDate"  ;
     ] ;
     dr:parameterBinding [  
	    dr:parameter "regex" ;
        rr:constant "^[0-9]{2}-([0-9]{2})-([0-9]{4})$"  ;
     ] ; 
   ] [ 
     dr:name "StartDate-DAY" ;
     dr:function drop:extractMatch ; 
        dr:parameterBinding [ 
		   dr:parameter "input" ;
           rr:column "StartDate"  ;
        ] ;
        dr:parameterBinding [ 
		   dr:parameter "regex" ;
           rr:constant "^([0-9]{2})-[0-9]{2}-([0-9]{4})$"  ;
        ] ; 
   ] ) ;
   
   # ID - CLASS
   rr:subjectMap [ 
      rr:template  "{@sd-unit-be}{Id}" ;
      rr:class org:Site ;
   ] ;
   
#   # SYSTEM IDENTIFIER
#   rr:predicateObjectMap [ 
#      rr:predicate  dct:identifier ;
#      rr:objectMap  [ 
#         rr:column "Id" ;
#         rr:termType  rr:Literal ;
#      ] ;
#   ] ; 
   
#   # THIRD PARTY WEBPAGES ( NON-LINKED DATA )
#   rr:predicateObjectMap [ 
#      rr:predicate  schema:sameAs ;
#      rr:objectMap  [ 
#         rr:template "https://kbopub.economie.fgov.be/kbopub/toonvestigingps.html?vestigingsnummer={EstablishmentNumber-NODOT}" ;
##         rr:datatype xsd:anyURI ;
#         rr:termType rr:IRI ;
#      ] ;
#   ] ;     
   
   # ENTERPISE
   rr:predicateObjectMap [ 
#      rr:predicate  org:isUnitOf;
#      rr:predicate  org:isSubOrganizationOf;
      rr:predicate  org:isSiteOf;
      rr:objectMap  [ 
         rr:template  "{@sd-organization-be}{EnterpriseNumber}" ;
         rr:termType  rr:IRI ;
      ] ;
   ] ;   
   
   dr:inversePredicateObjectMap [ 
#      rr:predicate  org:hasUnit;
#      rr:predicate  org:hasSubOrganization;
      rr:predicate  org:hasSite;
      rr:objectMap  [ 
         rr:template  "{@sd-organization-be}{EnterpriseNumber}" ;
         rr:termType  rr:IRI ;
      ] ;
   ] ;   
   
   # CBE REGISTRATION IDENTIFIER
   rr:predicateObjectMap [
      rr:predicate legal:legalIdentifier ;
      rr:objectMap [
         rr:parentTriplesMap [
            rr:subjectMap [
               rr:template "{@sd-identifier-be}{Id}" ;
               rr:class adms:Identifier ;
            ] ;

            # DATE ISSUED
            rr:predicateObjectMap [
               rr:predicate dct:issued;
               rr:objectMap [
                  rr:template "{StartDate-YEAR}-{StartDate-MONTH}-{StartDate-DAY}" ;
                  rr:datatype xsd:date;
               ] ;
            ] ;

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

            # ISSUING BODY
            rr:predicateObjectMap [
               rr:predicate dct:creator;
               rr:objectMap [
                  rr:constant sd-agency-be:cbe ;
               ] ;
            ] ;
         ] ;
      ] ;
   ] ;    

   # FOUNDATION DATE 
   rr:predicateObjectMap [ 
      rr:predicate  m8g:registrationDate ;
      rr:objectMap  [ 
         rr:template "{StartDate-YEAR}-{StartDate-MONTH}-{StartDate-DAY}" ;
         rr:datatype  xsd:date ;      
      ] ;
   ] .
   
   
<#DenominationMapping>  
# ++ EntityNumber
# ++ Language
# ++ TypeOfDenomination
# ++ Denomination

   rr:logicalTable [ 
      a dr:CSVTable;
      dr:source <#DenominationSource> ;
	  dr:headerRecord true;
	  dr:delimiter ",";
	  dr:quoteCharacter "\"";
#      dr:limit 10 ;
#	  dr:offset 2300000 ;
   ] ;
   
   # ID - CLASS
   rr:subjectMap [ 
      dr:cases ( [
         rr:template  "{@sd-unit-be}{EntityNumber}" ;
         dr:condition [
            rr:column "EntityNumber" ;
            drop:matches "^\\d\\.\\d{3}\\.\\d{3}\\.\\d{3}$";
         ] ;
      ] [
         rr:template  "{@sd-organization-be}{EntityNumber}" ;
      ] ) ;
   ] ;
   
   rr:predicateObjectMap [ 
      rr:predicateMap [
        rr:constant legal:legalName ;
        dr:condition [
           rr:column "TypeOfDenomination" ;
           drop:eq "001" ;
        ] ;
      ] ;
      rr:objectMap [
         rr:column "Denomination" ;
         rr:termType  rr:Literal ;
         dr:languageMap [
            dr:cases ( [
               rr:constant "" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "0" ;
		       ] ;
		    ] [
               rr:constant "fr" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "1" ;
		       ] ;
		    ] [
               rr:constant "nl" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "2" ;
		       ] ;
		    ] [
               rr:constant "de" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "3" ;
		       ] ;
		     ] [
               rr:constant "en" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "4" ;
		       ] ;
		     ] ) ;
	      ] ;
      ] ;
   ] ;
   
   rr:predicateObjectMap [ 
      rr:predicateMap [
        rr:constant sd-voc:abbreviatedName ;
        dr:condition [
           rr:column "TypeOfDenomination" ;
           drop:eq "002" ;
        ] ;
      ] ;
      rr:objectMap [
         rr:column "Denomination" ;
         rr:termType  rr:Literal ;
         dr:languageMap [
            dr:cases ( [
               rr:constant "" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "0" ;
		       ] ;
		    ] [
               rr:constant "fr" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "1" ;
		       ] ;
		    ] [
               rr:constant "nl" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "2" ;
		       ] ;
		    ] [
               rr:constant "de" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "3" ;
		       ] ;
		     ] [
               rr:constant "en" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "4" ;
		       ] ;
		     ] ) ;
	      ] ;
      ] ;
   ] ;
   
   rr:predicateObjectMap [ 
      rr:predicateMap [
        rr:constant sd-voc:tradingName ;
        dr:condition [
           rr:column "TypeOfDenomination" ;
           drop:matches "^00[34]$" ;
        ] ;
      ] ;
      rr:objectMap [
         rr:column "Denomination" ;
         rr:termType  rr:Literal ;
         dr:languageMap [
            dr:cases ( [
               rr:constant "" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "0" ;
		       ] ;
		    ] [
               rr:constant "fr" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "1" ;
		       ] ;
		    ] [
               rr:constant "nl" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "2" ;
		       ] ;
		    ] [
               rr:constant "de" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "3" ;
		       ] ;
		     ] [
               rr:constant "en" ;
               dr:condition [
		          rr:column  "Language" ;
		          drop:eq "4" ;
		       ] ;
		     ] ) ;
	      ] ;
      ] ;
   ] .
   
   
<#ContactMapping>  
# ++ EntityNumber
# -- EntityContact
# ++ ContactType
# ++ Value

   rr:logicalTable [ 
      a dr:CSVTable;
      dr:source <#ContactSource> ;
	  dr:headerRecord true;
	  dr:delimiter ",";
	  dr:quoteCharacter "\"";
#      dr:limit 10 ;
#	  dr:offset 350000;
   ] ;
   
   # ID - CLASS
   rr:subjectMap [ 
      dr:cases ( [
         rr:template  "{@sd-unit-be}{EntityNumber}" ;
         dr:condition [
            rr:column "EntityNumber" ;
            drop:matches "^\\d\\.\\d{3}\\.\\d{3}\\.\\d{3}$";
         ] ;
      ] [
         rr:template  "{@sd-organization-be}{EntityNumber}" ;
      ] ) ;
   ] ;
   
   rr:predicateObjectMap [ 
      rr:predicateMap [
         dr:cases ( [
            rr:constant schema:email ;
            dr:condition [
		       rr:column  "ContactType" ;
		       drop:eq "EMAIL" ;
		    ] ;
         ] [
            rr:constant schema:telephone ; # phone normalization
            dr:condition [
		       rr:column  "ContactType" ;
		       drop:eq "TEL" ;
		    ] ;
         ] [
            rr:constant schema:url ;  #not valid urls
            dr:condition [
		       rr:column  "ContactType" ;
		       drop:eq "WEB" ;
	  	    ] ;
         ] ) ;
      ] ;
      rr:objectMap  [ 
         rr:column "Value" ;
         rr:termType  rr:Literal ;
      ] ;
   ] .
   

<#ActivityMapping>  
# ++ EntityNumber
#    ActivityGroup
# ++ NaceVersion
# ++ NaceCode
# ++ Classification

   rr:logicalTable [ 
      a dr:CSVTable;
      dr:source <#ActivitySource> ;
	  dr:headerRecord true;
	  dr:delimiter ",";
	  dr:quoteCharacter "\"";
#      dr:limit 10 ;
   ] ;
   
   # ID - CLASS
   rr:subjectMap [ 
      dr:cases ( [
         rr:template  "{@sd-unit-be}{EntityNumber}" ;
         dr:condition [
            rr:column "EntityNumber" ;
            drop:matches "^\\d\\.\\d{3}\\.\\d{3}\\.\\d{3}$";
         ] ;
      ] [
         rr:template  "{@sd-organization-be}{EntityNumber}" ;
      ] ) ;
   ] ;
   
   # ACTIVITY   
   rr:predicateObjectMap [
      rr:predicateMap [
	     rr:constant legal:companyActivity ;
         dr:condition [
            rr:column "NaceVersion" ;
            drop:eq "2008";
         ] ;
      ] ;
      rr:predicateMap [
         dr:cases ( [ 
            rr:constant sd-voc:primaryActivity ;
            dr:condition [
               rr:column "Classification" ;
               drop:eq "MAIN" ;
               dr:booleanOperator drop:and ;
               dr:operand [
                  rr:column "NaceVersion" ;
                  drop:eq "2008";
               ] ;
            ] ;
         ] [ 
            rr:constant sd-voc:secondaryActivity ;
            dr:condition [
               rr:column "Classification" ;
               drop:eq "SECO" ;
               dr:booleanOperator drop:and ;
               dr:operand [
                  rr:column "NaceVersion" ;
                  drop:eq "2008";
               ] ;
            ] ;
         ] [ 
            rr:constant sd-voc:auxiliaryActivity ;
            dr:condition [
               rr:column "Classification" ;
               drop:eq "ANCI" ;
               dr:booleanOperator drop:and ;
               dr:operand [
                  rr:column "NaceVersion" ;
                  drop:eq "2008";
               ] ;
            ] ;
         ] ) ;
      ] ;
      rr:objectMap [
         rr:template "{@sd-nace-item-be}{NaceCode}" ;
         rr:termType rr:IRI ;
      ] ;
   ] .
   
   
<#AddressMapping>  
# ++ EntityNumber
# ++ TypeOfAddress
# ++ CountryNL
# ++ CountryFR
# ++ Zipcode
# ++ MunicipalityNL
# ++ MunicipalityFR
# ++ StreetNL
# ++ StreetFR
# ++ HouseNumber
# ++ Box
#    ExtraAddressInfo
#    DateStrikingOff

   rr:logicalTable [ 
      a dr:CSVTable;
      dr:source <#AddressSource> ;
	  dr:headerRecord true;
	  dr:delimiter ",";
	  dr:quoteCharacter "\"";
#	  dr:offset 1109000 ;
#      dr:limit 10 ;
   ] ;
   
   dr:transformations ( 
   [
      dr:dataref <#CountryTransformation> ;
      dr:transformationName "CountryTransformation";
      dr:parameterBinding [ 
         dr:parameter "TEXT" ;
         rr:column "CountryFR" ;
         dr:condition [
            rr:column  "CountryNL" ;
            drop:nmatches "^|Indéterminé$" ;
         ] ;
      ]  ;
   ]
   [
      dr:dataref <#PostCodeTransformation> ;
      dr:transformationName "PostCodeTransformation";
      dr:parameterBinding [ 
         dr:parameter "COUNTRY" ;
         rr:constant "BE" ;
		 dr:condition [
		    rr:column "CountryFR" ;
			drop:eq "" ;
		 ]		 
      ] ;
      dr:parameterBinding [ 
         dr:parameter "POSTCODE" ;
         rr:column "Zipcode" ;
         dr:condition [
            rr:column "Zipcode" ;
            drop:neq "";
         ] ; 		 
      ]  ;
   ]
   [
      dr:dataref <#NutsTransformation> ;
      dr:transformationName "NutsTransformation";
      dr:parameterBinding [ 
         dr:parameter "TEXT" ;
         rr:column "PostCodeTransformation~~$.NUTS3" ;
      ]  ;
   ] ) ;  
      
   dr:triplesMap [
      # ID - CLASS
   rr:subjectMap [ 
         dr:cases ( [
            rr:template  "{@sd-unit-be}{EntityNumber}" ;
            dr:condition [
               rr:column "EntityNumber" ;
               drop:matches "^\\d\\.\\d{3}\\.\\d{3}\\.\\d{3}$";
            ] ;
         ] [
            rr:template  "{@sd-organization-be}{EntityNumber}" ;
         ] ) ;
      ] ;

#   rr:predicateObjectMap [ 
#      rr:predicateMap [
#         rr:constant dbo:jurisdiction ;
#		 rr:termType rr:IRI ;
#         dr:condition [
#            rr:column "EntityNumber" ;
#            drop:matches "^\\d{4}\\.\\d{3}\\.\\d{3}$";
#         ] ;
#      ] ;
#
#      rr:objectMap  [ 
#         dr:cases ( [
#            rr:constant <http://publications.europa.eu/resource/authority/country/BEL> ;
#            rr:termType  rr:IRI ;
#            dr:condition [
#               rr:column "CountryFR" ;
#               drop:eq "";
#            ]
#         ] [
#            rr:column "CountryTransformation~~$.r2-uri[0]" ;
#            rr:termType  rr:IRI ;
#         ] ) ; 
#     ] ;
#   ] ;  
					
            rr:predicateObjectMap [
               rr:predicateMap [
			      rr:constant m8g:registeredAddress;
				  dr:condition [
				     rr:column "Zipcode" ;
					 drop:neq "0999" ;
					 dr:booleanOperator drop:or ;
					 dr:operand [
  				        rr:column "CountryFR" ;
					    drop:neq "" ;
					 ] ;
				  ] ;
			   ] ;
               rr:objectMap [
                  rr:parentTriplesMap [
                     rr:subjectMap [
                        rr:template  "{@sd-address-be}{EntityNumber}" ;
                        rr:class locn:Address ;
                    ] ;

                    rr:predicateObjectMap [ 
                       rr:predicate  locn:thoroughfare ;
                       rr:objectMap  [ 
                          rr:column "StreetFR" ;
                          rr:language "fr";
                       ] ;      
                       rr:objectMap  [ 
                          rr:column "StreetNL" ;
                          rr:language "nl";
                       ] ;                        
                    ] ;   

                    rr:predicateObjectMap [ 
                       rr:predicate  locn:locatorDesignator ;
                       rr:objectMap  [ 
                          rr:column "HouseNumber" ;
                          rr:termType  rr:Literal ;
                          dr:condition [
                             rr:column "HouseNumber" ;
                             drop:matches "\\d.*" ;
                          ] ;
                       ] ;      
                    ] ;   

                    rr:predicateObjectMap [ 
                       rr:predicate  locn:postCode ;
                       rr:objectMap  [ 
                          rr:column "Zipcode" ;
                          rr:termType  rr:Literal ;
                       ] ;      
                    ] ;   
                    
                    rr:predicateObjectMap [ 
                       rr:predicate  locn:postName ;
                       rr:objectMap  [ 
                          rr:column "MunicipalityFR" ;
                          rr:language "fr";
                       ] ;      
                       rr:objectMap  [ 
                          rr:column "MunicipalityNL" ;
                          rr:language "nl";
                       ] ;      
                    ] ;   
                    
                    rr:predicateObjectMap [ 
                       rr:predicate  locn:poBox ;
                       rr:objectMap  [ 
                          rr:column "Box" ;
                          rr:termType  rr:Literal ;
                       ] ;      
                    ] ;                       
                    
                    rr:predicateObjectMap [ 
                       rr:predicateMap  [
                          rr:constant locn:fullAddress ;  # Belgium addresses
                          dr:condition [
                             rr:column  "CountryFR" ;
							 drop:matches "" ;
                          ] ;
                       ] ;
                       rr:objectMap  [ 
                          rr:language  "fr" ;
                          dr:cases ( [
                             rr:template  "{StreetFR} {HouseNumber}<<, boîte {Box}>>, {Zipcode}<< {MunicipalityFR}>>, Belgique" ;
                             rr:termType  rr:Literal ;
                             dr:condition [
                                rr:column "HouseNumber" ;
                                drop:matches "\\d.*" ;
                             ] ;
                          ] [
                             rr:template  "<<{StreetFR}, >>{Zipcode}<< {MunicipalityFR}>>, Belgique" ;
                             rr:termType  rr:Literal ;
                          ] )
                       ] ;
                       rr:objectMap  [ 
                          rr:language  "nl" ;
                          dr:cases ( [
                             rr:template  "{StreetNL} {HouseNumber}<<, bus {Box}>>, {Zipcode}<< {MunicipalityNL}>>, België" ;
                             rr:termType  rr:Literal ;
                             dr:condition [
                                rr:column "HouseNumber" ;
                                drop:matches "\\d.*" ;
                             ] ;
                          ] [
                             rr:template  "<<{StreetFR}, >>{Zipcode}<< {MunicipalityNL}>>, België" ;
                             rr:termType  rr:Literal ;
                          ] )
                       ] ;
                    ] ;  
                    
                    rr:predicateObjectMap [ 
                       rr:predicateMap  [
                          rr:constant locn:fullAddress ;  # Non - Belgium addressess
                          dr:condition [
                             rr:column  "CountryFR" ;
                             drop:nmatches "^|Indéterminé$" ;
                          ] ;
                       ] ;
                       rr:objectMap  [ 
                          rr:language  "fr" ;
                          dr:cases ( [
						     rr:template  "{StreetFR} {HouseNumber}<<, boîte {Box}>><<, {Zipcode}>><< {MunicipalityFR}>>, {CountryFR}" ;
                             rr:termType  rr:Literal ;
                             dr:condition [
                                rr:column "HouseNumber" ;
                                drop:matches "^[0-9].*$" ;
                             ] ;
                          ] [
                             rr:template  "<<{StreetFR}, >><<{Zipcode} >><<{MunicipalityFR}, >>{CountryFR}" ;
                             rr:termType  rr:Literal ;
                          ]  )
                       ] ;
                       rr:objectMap  [ 
                          rr:language  "nl" ;
                          dr:cases ( [
                             rr:template  "{StreetNL} {HouseNumber}<<, bus {Box}>><<, {Zipcode}>><< {MunicipalityNL}>>, {CountryNL}" ;
                             rr:termType  rr:Literal ;
                             dr:condition [
                                rr:column "HouseNumber" ;
                                drop:matches "^[0-9].*$" ;
                             ] ;
                          ] [
                             rr:template  "<<{StreetNL}, >><<{Zipcode} >><<{MunicipalityNL}, >>{CountryNL}" ;
                             rr:termType  rr:Literal ;
                          ] )
                       ] ;
                    ] ;                       
                                    
                    rr:predicateObjectMap [ 
                       rr:predicate  locn:adminUnitL1 ;
                       rr:objectMap  [ 
                          dr:cases ( [
                             rr:constant "BE" ;
                             rr:termType  rr:Literal ;
                             dr:condition [
                                rr:column "CountryFR" ;
                                drop:eq "";
                             ] ;
                          ] [
                             rr:column "CountryTransformation~~$.alpha2[0]" ;
                             rr:termType  rr:Literal ;
                          ] )
                       ] ; 
                    ] ;   
					
                    rr:predicateObjectMap [ 
                       rr:predicate  locn:adminUnitL2 ;
                       rr:objectMap  [ 
                          rr:column "NutsTransformation~~$.label[0]" ;
                          rr:termType  rr:Literal ;
                       ] ;      
                    ] ;   
                    rr:predicateObjectMap [ 
                       rr:predicate  m8g:adminUnit ;
                       rr:objectMap  [ 
                          dr:cases ( [
                             rr:constant <http://publications.europa.eu/resource/authority/country/BEL> ;
                             rr:termType  rr:IRI ;
                             dr:condition [
                                rr:column "CountryFR" ;
                                drop:eq "";
                             ]
                          ] [
                             rr:column "CountryTransformation~~$.nal[0]" ;
                             rr:termType  rr:IRI ;
                          ] ) ; 
                       ] ;
                    ] ;   
					
                    rr:predicateObjectMap [ 
                       rr:predicate  m8g:adminUnit ;
                       rr:objectMap  [ 
                          rr:template "{@nuts-item}{PostCodeTransformation~~$.NUTS3}" ;
                          rr:termType  rr:IRI ;
                       ]
                     ] ;

                     rr:predicateObjectMap [ 
                        rr:predicate  m8g:adminUnit ;
                        rr:objectMap  [ 
                           rr:template "{@lau-item}{PostCodeTransformation~~$.LAU}" ;
                           rr:termType  rr:IRI ;
                        ]
                    ] ;
               ];
            ] ;
         ] ;
   ] ;
   
   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
         dr:cases ( [
            rr:constant <http://publications.europa.eu/resource/authority/country/BEL> ;
            rr:class sd-voc-adminlevel:Country ;
            dr:condition [
               rr:column "CountryFR" ;
               drop:eq "";
            ]
         ] [
           rr:column "CountryTransformation~~$.nal[0]" ;
           rr:class sd-voc-adminlevel:Country ;
         ] ) ; 
      ] ;
   ] ;
   
   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
         rr:template "{@nuts-item}{PostCodeTransformation~~$.NUTS3}";
         rr:class sd-voc-adminlevel:NUTS-3 ;
      ] ;
   ] ;
   
   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
         rr:template "{@lau-item}{PostCodeTransformation~~$.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 ;
         ]
      ] ;   
   ] .      
