<commit_before>#
# Copyright (c) 2017-2018, RTE (http://www.rte-france.com)
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

# query: graph
SELECT DISTINCT  ?graph
 WHERE
{ GRAPH ?graph
       { ?s  ?p  ?o }
}

# query: fullModel
prefix md: <http://iec.ch/TC57/61970-552/ModelDescription/1#>
SELECT *
{ GRAPH ?graph {
    ?FullModel
        a md:FullModel ;
        md:Model.scenarioTime ?scenarioTime ;
        md:Model.created ?created ;
        md:Model.description ?description ;
        md:Model.version ?version ;
        md:Model.DependentOn ?DependentOn ;
        md:Model.modelingAuthoritySet ?modelingAuthoritySet ;
        md:Model.profile ?profile .
    FILTER (regex(?profile, "{0}", "i"))
}}

# query: modelIds
prefix md: <http://iec.ch/TC57/61970-552/ModelDescription/1#>
SELECT *
{ GRAPH ?graph {
    # We will return all model identifiers for FullModel objects of profile EquipmentCore
    ?FullModel
        a md:FullModel ;
        md:Model.modelingAuthoritySet ?modelingAuthoritySet ;
        md:Model.profile ?profile .
    FILTER (regex(?profile, "EquipmentCore", "i")
        && !regex(?profile, "EquipmentBoundary", "i")
        && !regex(?profile, "TopologyBoundary", "i"))
}}

# query: modelDates
prefix md: <http://iec.ch/TC57/61970-552/ModelDescription/1#>
SELECT *
{ GRAPH ?graph {
    # There are multiple FullModel instances.
    # We return all dates from all Model instances that are not a boundary model
    ?FullModel
        a md:FullModel ;
        md:Model.modelingAuthoritySet ?modelingAuthoritySet ;
        md:Model.created ?created ;
        md:Model.scenarioTime ?scenarioTime ;
        md:Model.profile ?profile .
    FILTER (!regex(?profile, "EquipmentBoundary", "i")
        && !regex(?profile, "TopologyBoundary", "i"))
}}

# query: modelProfiles
prefix md: <http://iec.ch/TC57/61970-552/ModelDescription/1#>
SELECT *
{ GRAPH ?graph {
    ?FullModel
        a md:FullModel ;
        md:Model.profile ?profile .
}}

# query: version
SELECT ?version
{ GRAPH ?graph {
    ?versionId cim:IEC61970CIMVersion.version ?version
}}

# query: numObjectsByType
# Only types in a namespace given as parameter
SELECT ?Type (COUNT (DISTINCT ?object) AS ?numObjects)
{ GRAPH ?graph {
     ?object a ?Type .
    FILTER (STRSTARTS(STR(?Type), "{0}"))
}}
GROUP BY ?Type

# query: allObjectsOfType
SELECT * 
{ GRAPH ?graph {
     ?object a cim:{0} ;
     ?attribute ?value .
}}

# query: boundaryNodes
# All topological nodes belonging to a graph that has a modelProfile that contains the string "TopologyBoundary"
# Union all connectivity nodes belonging to a graph that has profile EquipmentBoundaryOperation
prefix md: <http://iec.ch/TC57/61970-552/ModelDescription/1#>
SELECT *
{
{ GRAPH ?graphBDTP {
    ?FullModel md:Model.profile ?modelProfile .
    FILTER (regex(?modelProfile, "TopologyBoundary", "i"))
    ?Node
        a cim:TopologicalNode ;
        cim:IdentifiedObject.name ?Name
}}
UNION
{ GRAPH ?graphBDEQ {
    ?FullModel md:Model.profile ?modelProfile .
    FILTER (regex(?modelProfile, "EquipmentBoundaryOperation", "i"))
    ?Node
        a cim:ConnectivityNode ;
        cim:IdentifiedObject.name ?Name
}}
}

# query: baseVoltages
SELECT *
{ GRAPH ?graph {
    ?BaseVoltage cim:BaseVoltage.nominalVoltage ?nominalVoltage .
}}

# query: substations
SELECT ?Substation ?name ?SubRegion ?subRegionName ?Region ?regionName
{ GRAPH ?graph {
    ?Substation
        a cim:Substation ;
        cim:IdentifiedObject.name ?name ;
        cim:Substation.Region ?SubRegion .
    ?SubRegion
        cim:SubGeographicalRegion.Region ?Region ;
        cim:IdentifiedObject.name ?subRegionName .
    ?Region cim:IdentifiedObject.name ?regionName
}}

# query: voltageLevels
SELECT *
WHERE {
{ GRAPH ?graph {
    ?VoltageLevel
        a cim:VoltageLevel ;
        cim:VoltageLevel.Substation ?Substation ;
        cim:IdentifiedObject.name ?name ;
        cim:VoltageLevel.BaseVoltage ?BaseVoltage .
    OPTIONAL { ?VoltageLevel cim:VoltageLevel.lowVoltageLimit ?lowVoltageLimit }
    OPTIONAL { ?VoltageLevel cim:VoltageLevel.highVoltageLimit ?highVoltageLimit }
}}
OPTIONAL { GRAPH ?graph2 {
    ?BaseVoltage cim:BaseVoltage.nominalVoltage ?nominalVoltage .
}}
}

# query: terminals
SELECT *
WHERE {
{ GRAPH ?graph {
    ?Terminal
        a cim:Terminal ;
        cim:Terminal.ConductingEquipment ?ConductingEquipment .
    ?ConductingEquipment a ?conductingEquipmentType .
    OPTIONAL { ?Terminal cim:IdentifiedObject.name ?name }
}}
OPTIONAL { GRAPH ?graphSSH {
    ?Terminal cim:ACDCTerminal.connected ?connected
}}
OPTIONAL { GRAPH ?graphSV {
    ?SvPowerFlow
        a cim:SvPowerFlow ;
        cim:SvPowerFlow.Terminal ?Terminal ;
        cim:SvPowerFlow.p ?p ;
        cim:SvPowerFlow.q ?q
} OPTIONAL {?SvStatus
        a cim:SvStatus ;
        cim:SvStatus.ConductingEquipment ?ConductingEquipment ;
        cim:SvStatus.inService ?inService . 
}}
OPTIONAL { GRAPH ?graphCN {
        ?Terminal cim:Terminal.ConnectivityNode ?ConnectivityNode .
}}
OPTIONAL { GRAPH ?graphTPT {
        ?Terminal cim:Terminal.TopologicalNode ?TopologicalNode .
}}
}

# query: connectivityNodes
SELECT *
WHERE {
{ GRAPH ?graphCN2 {
    ?ConnectivityNode cim:ConnectivityNode.ConnectivityNodeContainer ?ConnectivityNodeContainer
    OPTIONAL {
        ?ConnectivityNode cim:IdentifiedObject.name ?name
    }
    OPTIONAL { ?ConnectivityNode entsoe:ConnectivityNode.fromEndIsoCode ?fromEndIsoCode }<commit_msg>Optimize performance spqrql query "terminals" (#1246)<commit_after>#
# Copyright (c) 2017-2018, RTE (http://www.rte-france.com)
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

# query: graph
SELECT DISTINCT  ?graph
 WHERE
{ GRAPH ?graph
       { ?s  ?p  ?o }
}

# query: fullModel
prefix md: <http://iec.ch/TC57/61970-552/ModelDescription/1#>
SELECT *
{ GRAPH ?graph {
    ?FullModel
        a md:FullModel ;
        md:Model.scenarioTime ?scenarioTime ;
        md:Model.created ?created ;
        md:Model.description ?description ;
        md:Model.version ?version ;
        md:Model.DependentOn ?DependentOn ;
        md:Model.modelingAuthoritySet ?modelingAuthoritySet ;
        md:Model.profile ?profile .
    FILTER (regex(?profile, "{0}", "i"))
}}

# query: modelIds
prefix md: <http://iec.ch/TC57/61970-552/ModelDescription/1#>
SELECT *
{ GRAPH ?graph {
    # We will return all model identifiers for FullModel objects of profile EquipmentCore
    ?FullModel
        a md:FullModel ;
        md:Model.modelingAuthoritySet ?modelingAuthoritySet ;
        md:Model.profile ?profile .
    FILTER (regex(?profile, "EquipmentCore", "i")
        && !regex(?profile, "EquipmentBoundary", "i")
        && !regex(?profile, "TopologyBoundary", "i"))
}}

# query: modelDates
prefix md: <http://iec.ch/TC57/61970-552/ModelDescription/1#>
SELECT *
{ GRAPH ?graph {
    # There are multiple FullModel instances.
    # We return all dates from all Model instances that are not a boundary model
    ?FullModel
        a md:FullModel ;
        md:Model.modelingAuthoritySet ?modelingAuthoritySet ;
        md:Model.created ?created ;
        md:Model.scenarioTime ?scenarioTime ;
        md:Model.profile ?profile .
    FILTER (!regex(?profile, "EquipmentBoundary", "i")
        && !regex(?profile, "TopologyBoundary", "i"))
}}

# query: modelProfiles
prefix md: <http://iec.ch/TC57/61970-552/ModelDescription/1#>
SELECT *
{ GRAPH ?graph {
    ?FullModel
        a md:FullModel ;
        md:Model.profile ?profile .
}}

# query: version
SELECT ?version
{ GRAPH ?graph {
    ?versionId cim:IEC61970CIMVersion.version ?version
}}

# query: numObjectsByType
# Only types in a namespace given as parameter
SELECT ?Type (COUNT (DISTINCT ?object) AS ?numObjects)
{ GRAPH ?graph {
     ?object a ?Type .
    FILTER (STRSTARTS(STR(?Type), "{0}"))
}}
GROUP BY ?Type

# query: allObjectsOfType
SELECT * 
{ GRAPH ?graph {
     ?object a cim:{0} ;
     ?attribute ?value .
}}

# query: boundaryNodes
# All topological nodes belonging to a graph that has a modelProfile that contains the string "TopologyBoundary"
# Union all connectivity nodes belonging to a graph that has profile EquipmentBoundaryOperation
prefix md: <http://iec.ch/TC57/61970-552/ModelDescription/1#>
SELECT *
{
{ GRAPH ?graphBDTP {
    ?FullModel md:Model.profile ?modelProfile .
    FILTER (regex(?modelProfile, "TopologyBoundary", "i"))
    ?Node
        a cim:TopologicalNode ;
        cim:IdentifiedObject.name ?Name
}}
UNION
{ GRAPH ?graphBDEQ {
    ?FullModel md:Model.profile ?modelProfile .
    FILTER (regex(?modelProfile, "EquipmentBoundaryOperation", "i"))
    ?Node
        a cim:ConnectivityNode ;
        cim:IdentifiedObject.name ?Name
}}
}

# query: baseVoltages
SELECT *
{ GRAPH ?graph {
    ?BaseVoltage cim:BaseVoltage.nominalVoltage ?nominalVoltage .
}}

# query: substations
SELECT ?Substation ?name ?SubRegion ?subRegionName ?Region ?regionName
{ GRAPH ?graph {
    ?Substation
        a cim:Substation ;
        cim:IdentifiedObject.name ?name ;
        cim:Substation.Region ?SubRegion .
    ?SubRegion
        cim:SubGeographicalRegion.Region ?Region ;
        cim:IdentifiedObject.name ?subRegionName .
    ?Region cim:IdentifiedObject.name ?regionName
}}

# query: voltageLevels
SELECT *
WHERE {
{ GRAPH ?graph {
    ?VoltageLevel
        a cim:VoltageLevel ;
        cim:VoltageLevel.Substation ?Substation ;
        cim:IdentifiedObject.name ?name ;
        cim:VoltageLevel.BaseVoltage ?BaseVoltage .
    OPTIONAL { ?VoltageLevel cim:VoltageLevel.lowVoltageLimit ?lowVoltageLimit }
    OPTIONAL { ?VoltageLevel cim:VoltageLevel.highVoltageLimit ?highVoltageLimit }
}}
OPTIONAL { GRAPH ?graph2 {
    ?BaseVoltage cim:BaseVoltage.nominalVoltage ?nominalVoltage .
}}
}

# query: terminals
SELECT *
WHERE {
{ GRAPH ?graph {
    ?Terminal
        a cim:Terminal ;
        cim:Terminal.ConductingEquipment ?ConductingEquipment .
    ?ConductingEquipment a ?conductingEquipmentType .
    OPTIONAL { ?Terminal cim:IdentifiedObject.name ?name }
}}
OPTIONAL { GRAPH ?graphSSH {
    ?Terminal cim:ACDCTerminal.connected ?connected
}}
OPTIONAL { GRAPH ?graphSV {
    ?SvPowerFlow
        a cim:SvPowerFlow ;
        cim:SvPowerFlow.Terminal ?Terminal ;
        cim:SvPowerFlow.p ?p ;
        cim:SvPowerFlow.q ?q .
}}
OPTIONAL { GRAPH ?graphSV2 {
    ?SvStatus
        a cim:SvStatus ;
        cim:SvStatus.ConductingEquipment ?ConductingEquipment ;
        cim:SvStatus.inService ?inService . 
}}
OPTIONAL { GRAPH ?graphCN {
        ?Terminal cim:Terminal.ConnectivityNode ?ConnectivityNode .
}}
OPTIONAL { GRAPH ?graphTPT {
        ?Terminal cim:Terminal.TopologicalNode ?TopologicalNode .
}}
}

# query: connectivityNodes
SELECT *
WHERE {
{ GRAPH ?graphCN2 {
    ?ConnectivityNode cim:ConnectivityNode.ConnectivityNodeContainer ?ConnectivityNodeContainer
    OPTIONAL {
        ?ConnectivityNode cim:IdentifiedObject.name ?name
    }
    OPTIONAL { ?ConnectivityNode entsoe:ConnectivityNode.fromEndIsoCode ?fromEndIsoCode }
{'fraction_non_alphanumeric': 0.11046822147053588, 'fraction_numerical': 0.014153462702510236, 'mean_word_length': 3.4547977795400477, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3219527', 'n_tokens_mistral': 3903, 'n_tokens_neox': 3714, 'n_words': 849}