@prefix rdf:           <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs:          <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd:           <http://www.w3.org/2001/XMLSchema#> .
@prefix owl:           <http://www.w3.org/2002/07/owl#> .
@prefix skos:          <http://www.w3.org/2004/02/skos/core#> .
@prefix prov:          <http://www.w3.org/ns/prov#> .
@prefix sio:           <http://semanticscience.org/resource/> .
@prefix dcat:          <http://www.w3.org/ns/dcat#> .
@prefix dcterms:       <http://purl.org/dc/terms/> .
@prefix void:          <http://rdfs.org/ns/void#> .
@prefix foaf:          <http://xmlns.com/foaf/0.1/> .
@prefix ov:            <http://open.vocab.org/terms/> .
@prefix setl:          <http://purl.org/twc/vocab/setl/> .
@prefix csvw:          <http://www.w3.org/ns/csvw#> .
@prefix pv:            <http://purl.org/net/provenance/ns#>.
@prefix bibo:          <http://purl.org/ontology/bibo/>.

@prefix :              <{{prefix}}> .

:data a {{data_type}}, setl:Table;
  {% if delimiter %} csvw:delimiter "{{delimiter}}";{% endif %}
  {% if sheetname %} csvw:sheetname "{{sheetname}}";{% endif %}
  prov:wasGeneratedBy [
    a setl:Extract;
    prov:used <{{data}}>;
  ].

:codebook a owl:Class, prov:SoftwareAgent, setl:PythonScript;
    rdfs:subClassOf prov:Activity;
    prov:value '''
result = {{str(sdd.codebook)}}
'''.

:resource_codebook a owl:Class, prov:SoftwareAgent, setl:PythonScript;
    rdfs:subClassOf prov:Activity;
    prov:value '''
result = {{str(sdd.resource_codebook)}}
'''.


:setl a setl:SemanticETLScript;
  rdfs:label "Semantic Data Dictionary-generated SETL Script".

<{{dataset}}> a void:Dataset;
  prov:wasGeneratedBy :setl, [
    a setl:Transform, setl:JSLDT;
    prov:used :data;
    prov:qualifiedUsage [ a prov:Use; prov:entity :codebook; prov:hadRole [ dcterms:identifier "codebook"]];
    prov:qualifiedUsage [ a prov:Use; prov:entity :resource_codebook; prov:hadRole [ dcterms:identifier "resource_codebook"]];
    setl:hasContext '''
    {{ sdd.context | tojson }}
    ''';
    prov:value '''[{
{#    "@id" : "nanopub/{% raw %}{{name}}{% endraw %}",
    "@graph" : {
        "@id" : "nanopub/{% raw %}{{name}}{% endraw %}",
        "@type" : "np:Nanopublication",
        "np:hasAssertion" : {
            "@type" : "np:Assertion",
            "@id" : "nanopub/{% raw %}{{name}}{% endraw %}_assertion",#}
            "@graph" : [
                {%for column in sdd.columns.values() %}
                {%- if ('Attribute' in column and not isempty(column['Attribute'])) or
                       ('Entity' in column and not isempty(column['Entity'])) %} {
                    {%for predicate_column in sdd.columns.values() %}
                    {%- if 'Subject' in predicate_column and predicate_column['Subject'] == column['Column'] %}
                    "{{predicate_column['Predicate']}}" : [{
                        {%- if 'Format' in predicate_column and not isempty(predicate_column['Format']) %}
                        "@type" : "{{predicate_column.get('@type')}}",
                        {%- endif %}
                        "@if" : "not isempty(row.get('{{predicate_column['Column']}}'))",
                        "@value" : "{{predicate_column['@value']}}"
                    }],
                    {% endif -%}
                    {% endfor %}
                    "attributeOf" : [
                        {%- for attr in column['attributeOf'] %}
                        {
                          "@if" : "'owl:Nothing' not in codebook.get(('{{sdd.columns[attr]['Column']}}', row['{{sdd.columns[attr]['Column']}}']), [])",
                          "@id" : "{{sdd.columns[attr]['uri_template']}}"
                        }
                        {%- if not loop.last %},{% endif %}
                        {%- endfor %}
                    ],
                    {%- if 'Unit' in column and not isempty(column['Unit']) %}
                    "Unit" : { "@id" : "{{column['Unit']}}" },
                    {%- endif %}
                    {%- if 'Time' in column and not isempty(column['Time']) %}
                    "Time" : [
                      {
                          "@if" : "'owl:Nothing' not in codebook.get(('{{sdd.columns[column['Time']]['Column']}}', row['{{sdd.columns[column['Time']]['Column']}}']), [])",
                        "@id" : "{{sdd.columns[column['Time']]['uri_template']}}"
                      }
                    ],
                    {%- endif %}
                    {%- if 'wasDerivedFrom' in column and not isempty(column['wasDerivedFrom']) %}
                    "wasDerivedFrom" : [
                        {% for entity in column['wasDerivedFrom'] -%}
                        {
                          "@if" : "'owl:Nothing' not in codebook.get(('{{sdd.columns[entity]['Column']}}', row['{{sdd.columns[entity]['Column']}}']), [])",
                          "@id" : "{{sdd.columns[entity]['uri_template']}}"
                        }
                        {%- if not loop.last %},{% endif %}
                        {%- endfor %}
                    ],
                    {%- endif %}
                    {%- if 'wasGeneratedBy' in column and not isempty(column['wasGeneratedBy']) %}
                    "wasGeneratedBy" : [
                        {% for entity in column['wasGeneratedBy'] -%}
                            {
                              "@if" : "'owl:Nothing' not in codebook.get(('{{sdd.columns[entity]['Column']}}', row['{{sdd.columns[entity]['Column']}}']), [])",
                              "@id" : "{{sdd.columns[entity]['uri_template']}}"
                            }
                            {%- if not loop.last %},{% endif %}
                        {%- endfor %}
                    ],
                    {%- endif %}
                    {%- if 'Role' in column and not isempty(column['Role']) %}
                    "Role" : {
                        {% if not isempty(column['inRelationTo']) -%}
                            "inRelationTo" : [
                                {% for entity in column['inRelationTo'] -%}
                                    {
                                      "@if" : "'owl:Nothing' not in codebook.get(('{{sdd.columns[entity]['Column']}}', row['{{sdd.columns[entity]['Column']}}']), [])",
                                      "@id" : "{{sdd.columns[entity]['uri_template']}}"
                                    }
                                    {%- if not loop.last %},{% endif %}
                                {%- endfor %}
                            ],
                        {% endif -%}
                        "@type" : {{column['Role'] | tojson}}
                    },
                    {%- elif 'Relation' in column and not isempty(column['Relation']) and not isempty(column['inRelationTo']) -%}
                    "{{column['Relation']}}" : [
                        {% for entity in column['inRelationTo'] -%}
                            {
                              "@if" : "'owl:Nothing' not in codebook.get(('{{sdd.columns[entity]['Column']}}', row['{{sdd.columns[entity]['Column']}}']), [])",
                              "@id" : "{{sdd.columns[entity]['uri_template']}}"
                            }
                            {%- if not loop.last %},{% endif %}
                        {%- endfor %}
                    ],
                    {%- else -%}
                    "inRelationTo" : [
                        {% for entity in column['inRelationTo'] -%}
                            {
                              "@if" : "'owl:Nothing' not in codebook.get(('{{sdd.columns[entity]['Column']}}', row['{{sdd.columns[entity]['Column']}}']), [])",
                              "@id" : "{{sdd.columns[entity]['uri_template']}}"
                            }
                            {%- if not loop.last %},{% endif %}
                        {%- endfor %}
                    ],
                    {%- endif %}
                    {%- if not column['Column'].startswith("??") %}
                    "rdf:type" : [
                        {
                            "@for" : "class in codebook.get(('{{column['Column']}}', row['{{column['Column']}}']), [])",
                            {% raw %}"@do" : { "@id" : "{{class}}"}{% endraw %}
                        }
                    ],
                    "Value" : [{
                        {%- if 'Format' in column and not isempty(column['Format']) %}
                        "@type" : "{{column.get('@type')}}",
                        {%- endif %}
                        "@if" : "not isempty(row.get('{{column['Column']}}'))",
                        "@value" : "{{column['@value']}}"
                    }],
                    {%- endif %}
                    {%- if 'Start' in column and not isempty(column['Start']) and 'End' in column and not isempty(column['End']) %}
                    {%- if column['End'] == column['Start'] %}
                    "Value" : {
                        "@type" : "xsd:decimal",
                        "@value" : "{{column['Start']}}"
                    },
                    {%- else -%}
                    "hasStart" : {
                        "@type" : "TimeInterval",
                        "Value" : {
                            "@type" : "xsd:decimal",
                            "@value" : "{{column['Start']}}"
                        }
                    },
                    "hasEnd" : {
                        "@type" : "TimeInterval",
                        "Value" : {
                            "@type" : "xsd:decimal",
                            "@value" : "{{column['End']}}"
                        }
                    },
                    {%- endif %}
                    {%- endif %}
                    {%- if 'Attribute' in column and not isempty(column['Attribute']) %}
                    "Attribute" : {{column['Attribute'] | tojson }},
                    {%- endif %}
                    {%- if 'Entity' in column and not isempty(column['Entity']) %}
                    "Entity" : {{column['Entity'] | tojson }},
                    {%- endif %}
                    {%- if 'Type' in column and not isempty(column['Type']) %}
                    "Entity" : {{column['Type'] | tojson }},
                    {%- endif %}
                    "@if" : "'owl:Nothing' not in codebook.get(('{{column['Column']}}', row['{{column['Column']}}']), [])",
                    {%- if column['Column'] in sdd.resource_codebook %}
                    "@id" : "{% raw %}{{{%endraw%} resource_codebook['{{column['Column']}}'][row['{{column['Column']}}']] {%raw%}}}{% endraw %}"
                    {%- else -%}
                    "@id" : "{{column['uri_template']}}"
                    {%- endif %}
                }{#}{% if not loop.last %} ,{% endif %} #},{% endif %}{% endfor %}{}
            ]
{#        },
        "np:hasProvenance" : {
            "@type" : "np:Provenance",
            "@id" : "nanopub/{% raw %}{{name}}{% endraw %}_provenance",
            "@graph" : [
            ]
        },
        "np:hasPublicationInfo" : {
            "@type" : "np:PublicationInfo",
            "@id" : "nanopub/{% raw %}{{name}}{% endraw %}_pubinfo",
            "@graph" : [
                {
                    "@id" : "nanopub/{% raw %}{{name}}{% endraw %}",
                    "void:inDataset" : {"@id" : "{{dataset}}"},
                    "prov:wasDerivedFrom" : {
                        "@type" : "csvw:Row",
                        "@reverse" : {"csvw:row" : {"@id" : "{{data}}"} },
                        {% raw %}"csvw:rownum" : { "@value" : "{{name}}", "@type" : "xsd:integer"}{% endraw %}
                    }
                }
            ]
        }
    } #}
}]'''].

{% if data_out %}
<{{data_out}}> a pv:File;
    dcterms:format "application/trig";
    prov:wasGeneratedBy [
      a setl:Load;
      prov:used <{{dataset}}> ;
    ].
{% endif %}