# baseURI: http://inbas.cz/form-generation-eccairs-0.1
# imports: http://onto.fel.cvut.cz/ontologies/aviation
# imports: http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form-0.1
# imports: http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form-generated-0.1
# imports: http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form-stable-0.1
# imports: http://onto.fel.cvut.cz/ontologies/aviation/form-376-0.1
# imports: http://topbraid.org/sparqlmotionfunctions
# imports: http://topbraid.org/sparqlmotionlib-tbc

@prefix aviation: <http://onto.fel.cvut.cz/ontologies/aviation/> .
@prefix doc: <http://onto.fel.cvut.cz/ontologies/aviation/documentation/> .
@prefix eccairs: <http://onto.fel.cvut.cz/ontologies/eccairs/model/> .
@prefix form: <http://onto.fel.cvut.cz/ontologies/form/> .
@prefix form-ecc: <http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form/> .
@prefix formlt: <http://onto.fel.cvut.cz/ontologies/form-layout/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sm: <http://topbraid.org/sparqlmotion#> .
@prefix sml: <http://topbraid.org/sparqlmotionlib#> .
@prefix sp: <http://spinrdf.org/sp#> .
@prefix spin: <http://spinrdf.org/spin#> .
@prefix spl: <http://spinrdf.org/spl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://inbas.cz/form-generation-eccairs-0.1>
  rdf:type owl:Ontology ;
  owl:imports <http://onto.fel.cvut.cz/ontologies/aviation> ;
  owl:imports <http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form-0.1> ;
  owl:imports <http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form-generated-0.1> ;
  owl:imports <http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form-stable-0.1> ;
  owl:imports <http://onto.fel.cvut.cz/ontologies/aviation/form-376-0.1> ;
  owl:imports <http://topbraid.org/sparqlmotionfunctions> ;
  owl:imports <http://topbraid.org/sparqlmotionlib-tbc> ;
  owl:versionInfo "Created with TopBraid Composer"^^xsd:string ;
.
<http://inbas.cz/form-generation-eccairs-0.1#CreateForm>
  rdf:type sml:ApplyConstruct ;
  sm:next <http://inbas.cz/form-generation-eccairs-0.1#generateEccairsForms_Return> ;
  sm:nodeX 402 ;
  sm:nodeY 357 ;
  sml:constructQuery [
      rdf:type sp:Construct ;
      sp:templates (
          [
            sp:object form:form ;
            sp:predicate rdf:type ;
            sp:subject [
                sp:varName "formEccairs"^^xsd:string ;
              ] ;
          ]
          [
            sp:object [
                sp:varName "section"^^xsd:string ;
              ] ;
            sp:predicate form:has-section ;
            sp:subject [
                sp:varName "formEccairs"^^xsd:string ;
              ] ;
          ]
          [
            sp:object "wizard-step" ;
            sp:predicate formlt:has-layout-class ;
            sp:subject [
                sp:varName "section"^^xsd:string ;
              ] ;
          ]
        ) ;
      sp:text """# add form as a root element 
CONSTRUCT {
   ?formEccairs a <http://onto.fel.cvut.cz/ontologies/form/form> . 
   ?formEccairs   <http://onto.fel.cvut.cz/ontologies/form/has-section> ?section .
   ?section    <http://onto.fel.cvut.cz/ontologies/form-layout/layout-class> \"wizard-step\" .
}
WHERE {
#   GRAPH <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext> {
#        ?event_type <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext/has-relevant-part> ?section .
#    }
    GRAPH <http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form-generated> { 
		    ?section a <http://onto.fel.cvut.cz/ontologies/form/section> .
    }
    BIND(<http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form/form> as ?formEccairs)
}""" ;
      sp:where (
          [
            rdf:type sp:NamedGraph ;
            sp:elements (
                [
                  sp:object form:section ;
                  sp:predicate rdf:type ;
                  sp:subject [
                      sp:varName "section"^^xsd:string ;
                    ] ;
                ]
              ) ;
            sp:graphNameNode aviation:eccairs-form-generated ;
          ]
          [
            rdf:type sp:Bind ;
            sp:expression form-ecc:form ;
            sp:variable [
                sp:varName "formEccairs"^^xsd:string ;
              ] ;
          ]
        ) ;
      rdfs:comment "add form as a root element"^^xsd:string ;
    ] ;
  sml:constructQuery [
      rdf:type sp:Construct ;
      sp:templates (
          [
            sp:object "true"^^xsd:boolean ;
            sp:predicate <http://onto.fel.cvut.cz/ontologies/aviation/form-376/is-disabled> ;
            sp:subject [
                sp:varName "formItem"^^xsd:string ;
              ] ;
          ]
        ) ;
      sp:text """CONSTRUCT {
   ?formItem <http://onto.fel.cvut.cz/ontologies/aviation/form-376/is-disabled> \"true\"^^xsd:boolean .
}
WHERE {
     BIND(<http://onto.fel.cvut.cz/ontologies/eccairs-3.4.0.2/vl-a-390/v-2020400>  as ?event_type )
    GRAPH <http://onto.fel.cvut.cz/ontologies/aviation/mapping-airport-eccairs-events-and-attributes> {
      ?e_t <http://onto.fel.cvut.cz/ontologies/documentation/has_question> ?question .  
      MINUS {       
           ?event_type <http://onto.fel.cvut.cz/ontologies/documentation/has_question> ?question .    
      }
    } 
  GRAPH <http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form-generated> {        
        ?formItem <http://onto.fel.cvut.cz/ontologies/form/origin> ?question .
    }    
}"""^^xsd:string ;
      sp:where (
          [
            rdf:type sp:Bind ;
            sp:expression <http://onto.fel.cvut.cz/ontologies/eccairs-3.4.0.2/vl-a-390/v-2020400> ;
            sp:variable [
                sp:varName "event_type"^^xsd:string ;
              ] ;
          ]
          [
            rdf:type sp:NamedGraph ;
            sp:elements (
                [
                  sp:object [
                      sp:varName "question"^^xsd:string ;
                    ] ;
                  sp:predicate <http://onto.fel.cvut.cz/ontologies/documentation/has_question> ;
                  sp:subject [
                      sp:varName "e_t"^^xsd:string ;
                    ] ;
                ]
                [
                  rdf:type sp:Minus ;
                  sp:elements (
                      [
                        sp:object [
                            sp:varName "question"^^xsd:string ;
                          ] ;
                        sp:predicate <http://onto.fel.cvut.cz/ontologies/documentation/has_question> ;
                        sp:subject [
                            sp:varName "event_type"^^xsd:string ;
                          ] ;
                      ]
                    ) ;
                ]
              ) ;
            sp:graphNameNode aviation:mapping-airport-eccairs-events-and-attributes ;
          ]
          [
            rdf:type sp:NamedGraph ;
            sp:elements (
                [
                  sp:object [
                      sp:varName "question"^^xsd:string ;
                    ] ;
                  sp:predicate form:origin ;
                  sp:subject [
                      sp:varName "formItem"^^xsd:string ;
                    ] ;
                ]
              ) ;
            sp:graphNameNode aviation:eccairs-form-generated ;
          ]
        ) ;
    ] ;
  sml:constructQuery [
      rdf:type sp:Construct ;
      sp:templates (
          [
            sp:object "true"^^xsd:boolean ;
            sp:predicate <http://onto.fel.cvut.cz/ontologies/aviation/form-376/is-disabled> ;
            sp:subject [
                sp:varName "formItem"^^xsd:string ;
              ] ;
          ]
        ) ;
      sp:text """CONSTRUCT {
   ?formItem <http://onto.fel.cvut.cz/ontologies/aviation/form-376/is-disabled> \"true\"^^xsd:boolean .
}
WHERE {
     BIND(<http://onto.fel.cvut.cz/ontologies/eccairs-3.4.0.2/vl-a-390/v-2020400>  as ?event_type )
    GRAPH <http://onto.fel.cvut.cz/ontologies/aviation/mapping-ans-eccairs-events-and-attributes> {
      ?e_t <http://onto.fel.cvut.cz/ontologies/documentation/has_question> ?question .  
      MINUS {       
           ?event_type <http://onto.fel.cvut.cz/ontologies/documentation/has_question> ?question .    
      }
    } 
  GRAPH <http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form-generated> {        
        ?formItem <http://onto.fel.cvut.cz/ontologies/form/origin> ?question .
    }    
}"""^^xsd:string ;
      sp:where (
          [
            rdf:type sp:Bind ;
            sp:expression <http://onto.fel.cvut.cz/ontologies/eccairs-3.4.0.2/vl-a-390/v-2020400> ;
            sp:variable [
                sp:varName "event_type"^^xsd:string ;
              ] ;
          ]
          [
            rdf:type sp:NamedGraph ;
            sp:elements (
                [
                  sp:object [
                      sp:varName "question"^^xsd:string ;
                    ] ;
                  sp:predicate <http://onto.fel.cvut.cz/ontologies/documentation/has_question> ;
                  sp:subject [
                      sp:varName "e_t"^^xsd:string ;
                    ] ;
                ]
                [
                  rdf:type sp:Minus ;
                  sp:elements (
                      [
                        sp:object [
                            sp:varName "question"^^xsd:string ;
                          ] ;
                        sp:predicate <http://onto.fel.cvut.cz/ontologies/documentation/has_question> ;
                        sp:subject [
                            sp:varName "event_type"^^xsd:string ;
                          ] ;
                      ]
                    ) ;
                ]
              ) ;
            sp:graphNameNode aviation:mapping-ans-eccairs-events-and-attributes ;
          ]
          [
            rdf:type sp:NamedGraph ;
            sp:elements (
                [
                  sp:object [
                      sp:varName "question"^^xsd:string ;
                    ] ;
                  sp:predicate form:origin ;
                  sp:subject [
                      sp:varName "formItem"^^xsd:string ;
                    ] ;
                ]
              ) ;
            sp:graphNameNode aviation:eccairs-form-generated ;
          ]
        ) ;
    ] ;
  sml:constructQuery [
      rdf:type sp:Construct ;
      sp:templates (
          [
            sp:object "wizard-step" ;
            sp:predicate formlt:has-layout-class ;
            sp:subject [
                sp:varName "topSection"^^xsd:string ;
              ] ;
          ]
        ) ;
      sp:text """# create wizard step layout for top level sections
CONSTRUCT {
    ?topSection    <http://onto.fel.cvut.cz/ontologies/form-layout/layout-class> \"wizard-step\" .
}
WHERE {
    <http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form/form>   <http://onto.fel.cvut.cz/ontologies/form/has-section> ?topSection .
}""" ;
      sp:where (
          [
            sp:object [
                sp:varName "topSection"^^xsd:string ;
              ] ;
            sp:predicate form:has-section ;
            sp:subject form-ecc:form ;
          ]
        ) ;
      rdfs:comment "create wizard step layout for top level sections"^^xsd:string ;
    ] ;
  sml:constructQuery [
      rdf:type sp:Construct ;
      sp:templates (
          [
            sp:object [
                sp:varName "o"^^xsd:string ;
              ] ;
            sp:predicate [
                sp:varName "p"^^xsd:string ;
              ] ;
            sp:subject [
                sp:varName "s"^^xsd:string ;
              ] ;
          ]
        ) ;
      sp:text """# copy created form
CONSTRUCT {
    ?s ?p ?o.
}
WHERE {
    ?s ?p ?o.
}"""^^xsd:string ;
      sp:where (
          [
            sp:object [
                sp:varName "o"^^xsd:string ;
              ] ;
            sp:predicate [
                sp:varName "p"^^xsd:string ;
              ] ;
            sp:subject [
                sp:varName "s"^^xsd:string ;
              ] ;
          ]
        ) ;
      rdfs:comment "copy created form"^^xsd:string ;
    ] ;
  sml:replace "true"^^xsd:boolean ;
  rdfs:label "Create form"^^xsd:string ;
.
<http://inbas.cz/form-generation-eccairs-0.1#CreateForm_Clone>
  rdf:type sml:ApplyConstruct ;
  sm:nodeX 115 ;
  sm:nodeY 350 ;
  sml:constructQuery [
      rdf:type sp:Construct ;
      sp:templates (
          [
            sp:object <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext/passenger-flight> ;
            sp:predicate form:has-possible-value ;
            sp:subject <http://onto.fel.cvut.cz/ontologies/aviation/form-376/type-of-operation> ;
          ]
          [
            sp:object "Passenger flight" ;
            sp:predicate rdfs:label ;
            sp:subject <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext/passenger-flight> ;
          ]
          [
            sp:object <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext/cargo-flight> ;
            sp:predicate form:has-possible-value ;
            sp:subject <http://onto.fel.cvut.cz/ontologies/aviation/form-376/type-of-operation> ;
          ]
          [
            sp:object "Cargo flight" ;
            sp:predicate rdfs:label ;
            sp:subject <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext/cargo-flight> ;
          ]
        ) ;
      sp:text """CONSTRUCT {
     <http://onto.fel.cvut.cz/ontologies/aviation/form-376/type-of-operation> <http://onto.fel.cvut.cz/ontologies/form/has-possible-value> <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext/passenger-flight> .
     <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext/passenger-flight> rdfs:label \"Passenger flight\" .
          <http://onto.fel.cvut.cz/ontologies/aviation/form-376/type-of-operation> <http://onto.fel.cvut.cz/ontologies/form/has-possible-value> <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext/cargo-flight> .
          <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext/cargo-flight> rdfs:label \"Cargo flight\" .
}
WHERE {
    
}"""^^xsd:string ;
      sp:where () ;
    ] ;
  sml:constructQuery [
      rdf:type sp:Construct ;
      sp:templates (
          [
            sp:object form:form ;
            sp:predicate rdf:type ;
            sp:subject [
                sp:varName "form376"^^xsd:string ;
              ] ;
          ]
          [
            sp:object [
                sp:varName "section"^^xsd:string ;
              ] ;
            sp:predicate form:has-section ;
            sp:subject [
                sp:varName "form376"^^xsd:string ;
              ] ;
          ]
        ) ;
      sp:text """CONSTRUCT {
   ?form376 a <http://onto.fel.cvut.cz/ontologies/form/form> . 
   ?form376   <http://onto.fel.cvut.cz/ontologies/form/has-section> ?section .
}
WHERE {
    GRAPH <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext> {
        ?event_type <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext/has-relevant-part> ?section .
    }
    BIND(<http://onto.fel.cvut.cz/ontologies/aviation/form-376/form> as ?form376)
}"""^^xsd:string ;
      sp:where (
          [
            rdf:type sp:NamedGraph ;
            sp:elements (
                [
                  sp:object [
                      sp:varName "section"^^xsd:string ;
                    ] ;
                  sp:predicate <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext/has-relevant-part> ;
                  sp:subject [
                      sp:varName "event_type"^^xsd:string ;
                    ] ;
                ]
              ) ;
            sp:graphNameNode aviation:form-376-ext ;
          ]
          [
            rdf:type sp:Bind ;
            sp:expression <http://onto.fel.cvut.cz/ontologies/aviation/form-376/form> ;
            sp:variable [
                sp:varName "form376"^^xsd:string ;
              ] ;
          ]
        ) ;
    ] ;
  sml:constructQuery [
      rdf:type sp:Construct ;
      sp:templates (
          [
            sp:object "true"^^xsd:boolean ;
            sp:predicate <http://onto.fel.cvut.cz/ontologies/aviation/form-376/is-disabled> ;
            sp:subject [
                sp:varName "question"^^xsd:string ;
              ] ;
          ]
        ) ;
      sp:text """CONSTRUCT {
    ?question <http://onto.fel.cvut.cz/ontologies/aviation/form-376/is-disabled> \"true\"^^xsd:boolean .
}
WHERE {
    GRAPH <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext> {
        ?event_type <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext/has-irrelevant-part> ?question .
    } 
    
}"""^^xsd:string ;
      sp:where (
          [
            rdf:type sp:NamedGraph ;
            sp:elements (
                [
                  sp:object [
                      sp:varName "question"^^xsd:string ;
                    ] ;
                  sp:predicate <http://onto.fel.cvut.cz/ontologies/aviation/form-376-ext/has-irrelevant-part> ;
                  sp:subject [
                      sp:varName "event_type"^^xsd:string ;
                    ] ;
                ]
              ) ;
            sp:graphNameNode aviation:form-376-ext ;
          ]
        ) ;
    ] ;
  sml:constructQuery [
      rdf:type sp:Construct ;
      sp:templates (
          [
            sp:object [
                sp:varName "o"^^xsd:string ;
              ] ;
            sp:predicate [
                sp:varName "p"^^xsd:string ;
              ] ;
            sp:subject [
                sp:varName "s"^^xsd:string ;
              ] ;
          ]
        ) ;
      sp:text """CONSTRUCT {
    ?s ?p ?o.
}
WHERE {
    ?s ?p ?o.
}"""^^xsd:string ;
      sp:where (
          [
            sp:object [
                sp:varName "o"^^xsd:string ;
              ] ;
            sp:predicate [
                sp:varName "p"^^xsd:string ;
              ] ;
            sp:subject [
                sp:varName "s"^^xsd:string ;
              ] ;
          ]
        ) ;
    ] ;
  sml:replace "true"^^xsd:boolean ;
  rdfs:label "Create form"^^xsd:string ;
.
<http://inbas.cz/form-generation-eccairs-0.1#ImportRDFFromWorkspace_1>
  rdf:type sml:ImportRDFFromWorkspace ;
  sm:next <http://inbas.cz/form-generation-eccairs-0.1#CreateForm> ;
  sm:next <http://inbas.cz/form-generation-eccairs-0.1#CreateForm_Clone> ;
  sm:nodeX 337 ;
  sm:nodeY 210 ;
  sml:baseURI "http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form"^^xsd:string ;
  sml:ignoreImports "false"^^xsd:boolean ;
  rdfs:label "Import RDFFrom workspace"^^xsd:string ;
.
<http://inbas.cz/form-generation-eccairs-0.1#ImportReport>
  rdf:type sml:ImportTextFromURL ;
  sm:nodeX 193 ;
  sm:nodeY 65 ;
  sm:outputVariable "text"^^xsd:string ;
  rdfs:label "Import report"^^xsd:string ;
.
<http://inbas.cz/form-generation-eccairs-0.1#generateEccairsForms>
  rdf:type sm:Function ;
  spin:constraint [
      rdf:type spl:Argument ;
      spl:optional "false"^^xsd:boolean ;
      spl:predicate <http://inbas.cz/form-generation-eccairs/event_type> ;
      sm:next <http://inbas.cz/form-generation-eccairs-0.1#ImportRDFFromWorkspace_1> ;
      rdfs:comment "Event type."^^xsd:string ;
    ] ;
  sm:returnModule <http://inbas.cz/form-generation-eccairs-0.1#generateEccairsForms_Return> ;
  rdfs:comment "Generate forms from occurrence category."^^xsd:string ;
  rdfs:subClassOf sm:Functions ;
.
<http://inbas.cz/form-generation-eccairs-0.1#generateEccairsForms_Return>
  rdf:type sml:ReturnRDF ;
  sm:nodeX 78 ;
  sm:nodeY 618 ;
  sml:baseURI "http://miro.example.org"^^xsd:string ;
  sml:serialization sml:JSONLD ;
.
<http://inbas.cz/form-generation-eccairs/event_type>
  rdf:type owl:DatatypeProperty ;
  rdfs:label "Datatype property 1"^^xsd:string ;
.
<http://inbas.cz/form-generation-test/event_type>
  rdf:type owl:DatatypeProperty ;
  rdfs:label "event type"^^xsd:string ;
  rdfs:range xsd:string ;
.
rdfs:Resource
  rdfs:comment """#SELECT ?eccTerm
INSERT  {
    graph <http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form-generated> {   
		?formItem rdfs:label ?label ;        
		rdfs:comment ?comment ;
    }
} 
WHERE {
GRAPH <http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form-generated> {
   {	?formItem a form:section .
		?formItem <http://onto.fel.cvut.cz/ontologies/form/origin> ?eccTerm .
    } UNION  {
       ?formItem a form:question .
       	?formItem <http://onto.fel.cvut.cz/ontologies/form/origin> ?eccTerm . 
    } 
 }
 GRAPH <http://onto.fel.cvut.cz/ontologies/eccairs-3.4.0.2-v-2016-02-23> {
	?eccTerm <http://onto.fel.cvut.cz/ontologies/eccairs/model/has-description> ?label.
	?eccTerm <http://onto.fel.cvut.cz/ontologies/eccairs/model/has-explanation> ?comment.
    }
} 
"""^^xsd:string ;
  rdfs:comment """INSERT {
      GRAPH <http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form-generated> {
          ?section form:has-question ?question .
          ?section a owl:NamedIndividual .
          ?question a owl:NamedIndividual .
          ?section a form:section .
          ?question a form:question .
          ?question <http://onto.fel.cvut.cz/ontologies/form/origin> ?attribute .
          ?section <http://onto.fel.cvut.cz/ontologies/form/origin> ?entity .
       }
} WHERE {
   GRAPH <http://onto.fel.cvut.cz/ontologies/eccairs-3.4.0.2> {
	    ?entity <http://onto.fel.cvut.cz/ontologies/eccairs/model/has-child> ?attribute .
        BIND(\"http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form/\" AS ?eccairsFormIRI) 
        BIND( afn:localname(?entity) AS ?entityName)
 		BIND(iri(concat(?eccairsFormIRI, ?entityName)) AS ?section)
   }
   GRAPH <http://onto.fel.cvut.cz/ontologies/aviation/mapping-ans-eccairs-events-and-attributes> {
	     ?event <http://onto.fel.cvut.cz/ontologies/documentation/has_question> ?attribute .
        BIND(\"http://onto.fel.cvut.cz/ontologies/aviation/eccairs-form/\" AS ?eccairsFormIRI)     
        BIND( afn:localname(?attribute) AS ?attributeName)
        BIND(iri(concat(?eccairsFormIRI, ?attributeName)) AS ?question)
   }
}"""^^xsd:string ;
.
