<commit_before>#!/usr/bin/python

"""
Module defining report templates for Traffic light reports of checklist evaluation.

This module primarily defines data that are used by other modules to create traffic light reports

Report definition structure:

    report-defn     = { 'report': template-item }   // May add more later

    template-item   = sequence | query-template     // Bindings to date are fed down into template-item processing

    sequence        = [ template-item, ... ]

    query-template  = { 'query':      sparql-query [None],            # Probe query for input RDF data
                        'max':        integer [1],                    # Max numberof query matches to use
                        'output':     python-format-string [None],    # Output format string for query results
                        'report':     template-item [None],           # Sub-report used with query results
                        'alt':        python-format-string [None],    # Alternate string if querry not matched
                        'altreport':  template-item [None],           # Alternate sub-report for query not matched
                        'sep':        python-format-string [None],    # Separator between query result outputs
                      }

"""

import os, os.path
import sys
import logging
import json

import rdflib

from rocommand.ro_namespaces import RDF, DCTERMS, RO, AO, ORE

log = logging.getLogger(__name__)

def LIT(l): return rdflib.Literal(l)
def REF(u): return rdflib.URIRef(u)

sparql_prefixes = """
    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 xml:     <http://www.w3.org/XML/1998/namespace>
    PREFIX ao:      <http://purl.org/ao/>
    PREFIX dcterms: <http://purl.org/dc/terms/>
    PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
    PREFIX ro:      <http://purl.org/wf4ever/ro#>
    PREFIX wfprov:  <http://purl.org/wf4ever/wfprov#>
    PREFIX wfdesc:  <http://purl.org/wf4ever/wfdesc#>
    PREFIX wf4ever: <http://purl.org/wf4ever/wf4ever#>
    PREFIX minim:   <http://purl.org/minim/minim#>
    PREFIX result:  <http://www.w3.org/2001/sw/DataAccess/tests/result-set#>
    """

# Report structure used to get evaluation result URI from result graph
# Query idiom adapted from http://lists.w3.org/Archives/Public/public-sparql-dev/2006JulSep/0000.html
#
# Report a inserts one of the following status URIs:
#   http://purl.org/minim/minim#fullySatifies
#   http://purl.org/minim/minim#nominallySatifies
#   http://purl.org/minim/minim#minimallySatifies
#   http://purl.org/minim/minim#potentiallySatisfies  (actually: did not satisfy)
#
# Assumed incoming bindings:
#   target  URI of target resource of evaluation
#   minim   URI of minim model for which evaluation has been performed
#
# @@TODO:
# Consider refactoring this to follow the report/altreport pattern used for
# generating result label text
#
EvalTargetResultUri = (
    { 'report':
      { 'output': "%(satisfaction)s"
      , 'alt':    "http://purl.org/minim/minim#potentiallySatisfies"
      , 'query':
        """
        SELECT ?target ?satisfaction ?minim WHERE
        {
          {
            ?target ?satisfaction ?minim .
            FILTER ( ?satisfaction = <http://purl.org/minim/minim#fullySatisfies> )
          }
          UNION
          {
            ?target ?satisfaction ?minim .
            FILTER ( ?satisfaction = <http://purl.org/minim/minim#nominallySatisfies> )
            OPTIONAL
            {
              ?target ?altsat ?minim .
              FILTER ( ?altsat = <http://purl.org/minim/minim#fullySatisfies> )
            }
            FILTER ( ! bound(?altsat) )
          }
          UNION
          {
            ?target ?satisfaction ?minim .
            FILTER ( ?satisfaction = <http://purl.org/minim/minim#minimallySatisfies> )
            OPTIONAL
            {
              ?target ?altsat ?minim .
              FILTER ( ?altsat = <http://purl.org/minim/minim#nominallySatisfies> )
            }
            FILTER ( ! bound(?altsat) )
          }
        }
        """
      }
    })

# Report structure used to get evaluation result label from result graph
#
# Report a inserts one of the following strings:
#   "fully satisfies"       (no item failures)
#   "nominally satisfies"   (failed MAY items only)
#   "minimally satisfies"   (failed SHOULD items)
#   "does not satisfy"      (failed MUST items)
#
# Assumed incoming bindings:
#   target  URI of target resource of evaluation
#   minim   URI of minim model for which evaluation has been performed
#
EvalTargetResultLabel = (
    { 'report':
      { 'output': "fully satisfies"
      , 'query':  """ASK { ?target <http://purl.org/minim/minim#fullySatisfies> ?minim }"""
      , 'altreport':
        { 'output': "nominally satisfies"
        , 'query':  """ASK { ?target <http://purl.org/minim/minim#nominallySatisfies> ?minim }"""
        , 'altreport':
          { 'output': "minimally satisfies"
          , 'query':  """ASK { ?target <http://purl.org/minim/minim#minimallySatisfies> ?minim }"""
          , 'alt': "does not satisfy"
          }
        }
      }
    })

# Report structure used to get evaluation result class list from result graph
# The intent is that these class labels can be used in conjunction with CSS-directed
# styling when generating a web page display of a checklist evaluation result.
#
# Report a inserts one of the following strings:
#   ["pass"]                (no item failures)
#   ["fail" "may"]          (failed MAY items only)
#   ["fail", "should"]      (failed SHOULD items)
#   ["fail", "must"]        (failed MUST items)
#
# Assumed incoming bindings:
#   target  URI of target resource of evaluation
#   minim   URI of minim model for which evaluation has been performed
#
EvalTargetResultClass = (
    { 'report':
      { 'output': '"pass"'
      , 'query':  """ASK { ?target <http://purl.org/minim/minim#fullySatisfies> ?minim }"""
      , 'altreport':
        { 'output': '"fail", "may"'
        , 'query':  """ASK { ?target <http://purl.org/minim/minim#nominallySatisfies> ?minim }"""
        , 'altreport':
          { 'output': '"fail", "should"'
          , 'query':  """ASK { ?target <http://purl.org/minim/minim#minimallySatisfies> ?minim }"""
          , 'alt': '"fail", "must"'
          }
        }
      }
    })

# Report inserts result of checklist item evaluation item as JSON
#
# Example of output:
#
#     { "itemuri":        "URI of checklist item"
#     , "itemlabel":      "Result label text generated from checklist item"
#     , "itemlevel":      "http://purl.org/minim/minim#hasShouldRequirement" 
#     , "itemsatisfied":  false
#     , "itemclass":      ["fail", "should"]
#     }
#
# Assumed incoming bindings:
#   rouri     URI of RO being evaluated
#   modeluri  URI of Minim model defining the evaluated checklist
#   itemuri   URI or Minim model checklist item whose result is reported
#   itemlevel URI of satisfaction level asspociated with this item:
#             minim:hasMustRequirement, minim:hasShouldRequirement or minim:hasMayRequirement
#
EvalItemJson = (
    { 'report':
      [
        { 'output':
            '''\n    { "itemuri":        "%(itemuri)s"'''+
            '''\n    , "itemlabel":      "%(itemlabel)s"'''+
            '''\n    , "itemlevel":      "%(itemlevel)s" '''
        , 'alt':  '''\n    *** no match for message/label ***'''
        , 'query': sparql_prefixes+
            """
            SELECT * WHERE
            {
            ?s minim:tryRequirement ?itemuri ;
               minim:tryMessage ?itemlabel .
            }"""
        },
        { 'output':
            '''\n    , "itemsatisfied":  true'''
        , 'alt':
            '''\n    , "itemsatisfied":  false'''
        , 'query': sparql_prefixes+
            """
            SELECT * WHERE
            {
              ?rouri minim:satisfied [ minim:tryRequirement ?itemuri ]
            }
            """
        },<commit_msg>Escape ItemLabel quotes in JSON output<commit_after>#!/usr/bin/python

"""
Module defining report templates for Traffic light reports of checklist evaluation.

This module primarily defines data that are used by other modules to create traffic light reports

Report definition structure:

    report-defn     = { 'report': template-item }   // May add more later

    template-item   = sequence | query-template     // Bindings to date are fed down into template-item processing

    sequence        = [ template-item, ... ]

    query-template  = { 'query':      sparql-query [None],            # Probe query for input RDF data
                        'max':        integer [1],                    # Max numberof query matches to use
                        'output':     python-format-string [None],    # Output format string for query results
                        'report':     template-item [None],           # Sub-report used with query results
                        'alt':        python-format-string [None],    # Alternate string if querry not matched
                        'altreport':  template-item [None],           # Alternate sub-report for query not matched
                        'sep':        python-format-string [None],    # Separator between query result outputs
                      }

"""

import os, os.path
import sys
import logging
import json

import rdflib

from rocommand.ro_namespaces import RDF, DCTERMS, RO, AO, ORE

log = logging.getLogger(__name__)

def LIT(l): return rdflib.Literal(l)
def REF(u): return rdflib.URIRef(u)

sparql_prefixes = """
    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 xml:     <http://www.w3.org/XML/1998/namespace>
    PREFIX ao:      <http://purl.org/ao/>
    PREFIX dcterms: <http://purl.org/dc/terms/>
    PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
    PREFIX ro:      <http://purl.org/wf4ever/ro#>
    PREFIX wfprov:  <http://purl.org/wf4ever/wfprov#>
    PREFIX wfdesc:  <http://purl.org/wf4ever/wfdesc#>
    PREFIX wf4ever: <http://purl.org/wf4ever/wf4ever#>
    PREFIX minim:   <http://purl.org/minim/minim#>
    PREFIX result:  <http://www.w3.org/2001/sw/DataAccess/tests/result-set#>
    """

# Report structure used to get evaluation result URI from result graph
# Query idiom adapted from http://lists.w3.org/Archives/Public/public-sparql-dev/2006JulSep/0000.html
#
# Report a inserts one of the following status URIs:
#   http://purl.org/minim/minim#fullySatifies
#   http://purl.org/minim/minim#nominallySatifies
#   http://purl.org/minim/minim#minimallySatifies
#   http://purl.org/minim/minim#potentiallySatisfies  (actually: did not satisfy)
#
# Assumed incoming bindings:
#   target  URI of target resource of evaluation
#   minim   URI of minim model for which evaluation has been performed
#
# @@TODO:
# Consider refactoring this to follow the report/altreport pattern used for
# generating result label text
#
EvalTargetResultUri = (
    { 'report':
      { 'output': "%(satisfaction)s"
      , 'alt':    "http://purl.org/minim/minim#potentiallySatisfies"
      , 'query':
        """
        SELECT ?target ?satisfaction ?minim WHERE
        {
          {
            ?target ?satisfaction ?minim .
            FILTER ( ?satisfaction = <http://purl.org/minim/minim#fullySatisfies> )
          }
          UNION
          {
            ?target ?satisfaction ?minim .
            FILTER ( ?satisfaction = <http://purl.org/minim/minim#nominallySatisfies> )
            OPTIONAL
            {
              ?target ?altsat ?minim .
              FILTER ( ?altsat = <http://purl.org/minim/minim#fullySatisfies> )
            }
            FILTER ( ! bound(?altsat) )
          }
          UNION
          {
            ?target ?satisfaction ?minim .
            FILTER ( ?satisfaction = <http://purl.org/minim/minim#minimallySatisfies> )
            OPTIONAL
            {
              ?target ?altsat ?minim .
              FILTER ( ?altsat = <http://purl.org/minim/minim#nominallySatisfies> )
            }
            FILTER ( ! bound(?altsat) )
          }
        }
        """
      }
    })

# Report structure used to get evaluation result label from result graph
#
# Report a inserts one of the following strings:
#   "fully satisfies"       (no item failures)
#   "nominally satisfies"   (failed MAY items only)
#   "minimally satisfies"   (failed SHOULD items)
#   "does not satisfy"      (failed MUST items)
#
# Assumed incoming bindings:
#   target  URI of target resource of evaluation
#   minim   URI of minim model for which evaluation has been performed
#
EvalTargetResultLabel = (
    { 'report':
      { 'output': "fully satisfies"
      , 'query':  """ASK { ?target <http://purl.org/minim/minim#fullySatisfies> ?minim }"""
      , 'altreport':
        { 'output': "nominally satisfies"
        , 'query':  """ASK { ?target <http://purl.org/minim/minim#nominallySatisfies> ?minim }"""
        , 'altreport':
          { 'output': "minimally satisfies"
          , 'query':  """ASK { ?target <http://purl.org/minim/minim#minimallySatisfies> ?minim }"""
          , 'alt': "does not satisfy"
          }
        }
      }
    })

# Report structure used to get evaluation result class list from result graph
# The intent is that these class labels can be used in conjunction with CSS-directed
# styling when generating a web page display of a checklist evaluation result.
#
# Report a inserts one of the following strings:
#   ["pass"]                (no item failures)
#   ["fail" "may"]          (failed MAY items only)
#   ["fail", "should"]      (failed SHOULD items)
#   ["fail", "must"]        (failed MUST items)
#
# Assumed incoming bindings:
#   target  URI of target resource of evaluation
#   minim   URI of minim model for which evaluation has been performed
#
EvalTargetResultClass = (
    { 'report':
      { 'output': '"pass"'
      , 'query':  """ASK { ?target <http://purl.org/minim/minim#fullySatisfies> ?minim }"""
      , 'altreport':
        { 'output': '"fail", "may"'
        , 'query':  """ASK { ?target <http://purl.org/minim/minim#nominallySatisfies> ?minim }"""
        , 'altreport':
          { 'output': '"fail", "should"'
          , 'query':  """ASK { ?target <http://purl.org/minim/minim#minimallySatisfies> ?minim }"""
          , 'alt': '"fail", "must"'
          }
        }
      }
    })

# Report inserts result of checklist item evaluation item as JSON
#
# Example of output:
#
#     { "itemuri":        "URI of checklist item"
#     , "itemlabel":      "Result label text generated from checklist item"
#     , "itemlevel":      "http://purl.org/minim/minim#hasShouldRequirement" 
#     , "itemsatisfied":  false
#     , "itemclass":      ["fail", "should"]
#     }
#
# Assumed incoming bindings:
#   rouri     URI of RO being evaluated
#   modeluri  URI of Minim model defining the evaluated checklist
#   itemuri   URI or Minim model checklist item whose result is reported
#   itemlevel URI of satisfaction level asspociated with this item:
#             minim:hasMustRequirement, minim:hasShouldRequirement or minim:hasMayRequirement
#
EvalItemJson = (
    { 'report':
      [
        { 'output':
            '''\n    { "itemuri":        "%(itemuri)s"'''+
            '''\n    , "itemlabel":      "%(itemlabel_esc)s"'''+
            '''\n    , "itemlevel":      "%(itemlevel)s" '''
        , 'alt':  '''\n    *** no match for message/label ***'''
        , 'query': sparql_prefixes+
            """
            SELECT * WHERE
            {
            ?s minim:tryRequirement ?itemuri ;
               minim:tryMessage ?itemlabel .
            }"""
        },
        { 'output':
            '''\n    , "itemsatisfied":  true'''
        , 'alt':
            '''\n    , "itemsatisfied":  false'''
        , 'query': sparql_prefixes+
            """
            SELECT * WHERE
            {
              ?rouri minim:satisfied [ minim:tryRequirement ?itemuri ]
            }
            """
        },
{'fraction_non_alphanumeric': 0.1288446067207129, 'fraction_numerical': 0.006064731728448542, 'mean_word_length': 2.141524105754277, 'pattern_counts': {'":': 20, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, 'xml': 4}, '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': '5303220', 'n_tokens_mistral': 5132, 'n_tokens_neox': 4837, 'n_words': 1267}