Queries

Queries allow performing calculations on a set of values, optionally applying some conditions. You can think of a query as a structured statement similar to:

[COMPUTE_VALUE] FROM [SCOPE] WHERE [CONDITION]

where [SCOPE] is one of:

  • NODE: The current artefact

  • CHILDREN: All artefacts that are direct children of the current artefact

  • DESCENDANTS: All children of the current artefact, and their descendants

  • TREE: The full tree of artefacts, starting from the current node. This is equivalent to NODE and DESCENDANT

  • RAKE: The current artefact and all its children. This is equivalent to NODE and CHILDREN

Note that XML does not allow using < directly in an attribute, therefore you will need to insert it using an entity: &lt;.

In this section, you will learn how to compute values, define a scope and write conditions for your queries following the syntax supported in Squore to:

  1. count artefacts

  2. compute mathematical results on metrics using SUM, MAX, MIN, AVR, MED, MOD, DEV or MUL

  3. count rules and rule occurrences

Querying artefacts

Counting artefacts

Return the number of artefacts of a certain type using the following syntax:

COUNT <ArtefactType|ALL>

Where ArtefactType is one of FOLDER, APPLICATION, C_FILE, or other type (or alias) defined in your model. ALL can be used as a shortcut for all artefact types for which the measure exists.

Counting artefacts supports specifying a condition in the form of a computation, following the syntax described in Computations, as demonstrated by the examples below.

Find the number of programs with a rating of LEVELG, starting from the children of the considered artefact:
COUNT PROGRAM FROM DESCENDANTS WHERE LEVEL=LEVELG
Find the number of artefacts not rated C or UNKNOWN that have more than 10 lines of code:
COUNT FILE FROM DESCENDANTS WHERE LEVEL!=LEVELC OR LEVEL!=UNKNOWN AND B.LC>10
Find the number of artefacts where LC is greater than -1:
COUNT FILE FROM DESCENDANTS WHERE LC
=> is shorthand for
COUNT FILE FROM DESCENDANTS WHERE LC >=1
Find the number of issues with the status "FIXED" created in the last 60 days:
COUNT ISSUE FROM TREE WHERE EQUALS(INFO(STATUS), 'FIXED') AND DATE_SUBMITTED >= TODAY() - DAYS(60)
Computing based on artefacts hierarchy

Perform mathematical operations on artefact hierarchies using the following syntax:

SUM|MAX|MIN|AVR|MED|MOD|DEV|MUL <ArtefactType|ALL>.<MeasureId>

Mathematical queries support specifying a condition in the form of a computation, following the syntax described in Computations, as demonstrated by the examples below.

  • SUM, returns the sum of values returned for a set. The SUM of values [1, 3, 3, 3, 5, 6] is 21.

    Compute VG for a folder as the sum for VG for all functions in the folder:
    <Measure measureId="VG" defaultValue="1">
    	<Computation targetArtefactTypes="FOLDER" result="SUM FUNCTION.VG FROM DESCENDANTS" />
    </Measure>
  • MAX, MIN, returns the maximum or minimum value of a set. The MAX and MIN of values [1, 3, 3, 3, 5, 6] are 6 and 1 respectively.

    Assign the maximum value for VG from all functions in a folder as VG_MAX for the folder:
    <Measure measureId="VG_MAX" defaultValue="0">
    	<Computation targetArtefactTypes="FOLDER" result="MAX FUNCTION.VG FROM DESCENDANTS" />
    </Measure>
  • AVR, returns the mean of all the values returned for a set. The AVR of values [1, 3, 3, 3, 5, 6] is 3.5.

  • MED, returns the median of all the values returned for a set. The MED of values [1, 3, 3, 3, 5, 6] is 3.

  • MOD, returns the mode or modal value of all the values returned for a set. The MOD of values [1, 3, 3, 3, 5, 6] is 3.

  • DEV, returns the standard deviation of all the values returned for a set. The DEV of values [1, 3, 3, 3, 5, 6] is 1.607.

  • MUL, returns the product of all the values returned for a set. The MUL of values [1, 3, 3, 3, 5, 6] is 810.

Querying rules and occurrences

Counting Rules

Return the number of rules using the following syntax:

COUNT RULE([<scope>])
Counting Occurrences

Return the number of times a rule is violated (i.e. the number of occurrences), using the following syntax:

COUNT RULE([<scope>]).OCCURRENCES([<type>])
Computing based on occurrences

Return the sum of each occurrence’s rank in the provided scale (<scaleId>), using the following syntax:

SUM RULE([<scope>]).OCCURRENCES([<type>]).<scaleId>

In all these queries, ou can specify the ruleset to consider by specifying a <scope>. Accepted values are:

  • ALL is the entire ruleset for the model, ignoring whether rules are enabled or not

  • STANDARD is the model ruleset minus the rules that are deactivated by default

  • CUSTOMER is the ruleset as configured in the web interface using the Ruleset Editor

  • PROJECT (default) is the ruleset as configured by the user when going through the project wizard

In some you can also filter on the desired <type> of the violations status. Accepted values are:

  • ALL to find all violations irrespective of their status type

  • OPEN (default) to find all open violations

  • CONFIRMED (default) to find all confirmed violations

  • RELAXED to find all relaxed violations

In all these queries you can define one or more conditions in order to filter the results:

Parentheses are not allowed in the body of a condition, but multiple conditions can be combined using AND and OR operators. In this case, OR takes priority over AND.

  • =, !=, <, <=, >, >=

    To filter based on a measure’s value.

    Count all violations in artefacts where VG is more than 10:
    COUNT RULE.OCCURRENCES FROM TREE WHERE VG>10
  • LEVEL|I.<indicatorId> =|!= <levelId>

    To filter based on indicator level.

    Count all violations in artefacts with low self-descriptiveness:
    COUNT RULE.OCCURRENCES FROM TREE WHERE I.SDESCR = LEVELF
    Count all violations in artefacts not rated UNKNOWN (LEVEL is a keyword representing the root indicator for an artefact):
    COUNT RULE.OCCURRENCES FROM TREE WHERE LEVEL != UNKNOWN
  • HAS_OCCURRENCE([<findingStatus>], [<relaxedInSourceCode>], [<isSuspicious>])

    To filter based on occurrences properties, where:

    • <findingStatus>, is the violations status. Accepted values are:

      • ALL to find all violations irrespective of their status

      • OPEN to find all open violations

      • CONFIRMED to find all confirmed violations

      • RELAXED to find all relaxed violations

      • RELAXED_DEROGATION to find violations with the Relaxed (Derogation) relaxation status

      • RELAXED_LEGACY to find violations with the Relaxed (Legacy system) relaxation status

      • RELAXED_FALSE_POSITIVE to find violations with the Relaxed (False positive) relaxation status

      • <CUSTOM_STATUS_ID> to find violations with the specified custom status

      By default, all opened and confirmed findings are returned.

    • <relaxedInSourceCode>, to specify whether the violation was relaxed from source code, true, or from the user interface false (default).

    • <isSuspicious>, to specify whether the violation is flagged as suspicious, true, or not, false (default).

    HAS_OCCURRENCE() replaces the now deprecated NBOCCURRENCES.

    Count the number of rules in the "required" family that were violated in the selected artefact and all its descendants:
    COUNT RULE FROM TREE WHERE HAS_OCCURRENCE() AND FAMILY=REQUIRED
    Count MISRA rules violated with the Open status:
    COUNT RULE FROM DESCENDANTS WHERE HAS_OCCURRENCE(OPEN) AND FAMILY=MISRA
    Count rules where violations were relaxed because they appear in legacy code:
    COUNT RULE FROM DESCENDANTS WHERE HAS_OCCURRENCE(RELAXED_LEGACY)
    Count rules where violations were relaxed directly in the source code:
    COUNT RULE FROM DESCENDANTS WHERE HAS_OCCURRENCE(RELAXED, TRUE)
  • CATEGORY (=, !=)

    To filter based on the category of a rule.

    Count violations not in the REQUIRED category:
    COUNT RULE.OCCURRENCES FROM DESCENDANTS WHERE CATEGORY!=SCALE_PRIORITY.REQUIRED
  • FAMILY (=, !=)

    To filter based on the rule’s families setup in your model.

    Count rules with the REQUIRED family in the selected artefact and all its descendants:
    COUNT RULE FROM DESCENDANTS WHERE FAMILY=REQUIRED
    Count rules in the MISRA family in the model:
    COUNT RULE WHERE FAMILY=MISRA
    Count rules in the REQUIRED family that were violated in the selected artefact and all its descendants:
    COUNT RULE FROM TREE WHERE HAS_OCCURRENCE() AND FAMILY=REQUIRED
  • MEASUREID (=, !=)

    To filter based on a measure from your analysis model.

    Count rules that aren’t R_NOGOTO
    COUNT RULE FROM DESCENDANTS WHERE MEASUREID!=R_NOGOTO
    Count violations of R_COMPOUNDELSE in the children of the selected artefact:
    COUNT RULE.OCCURRENCES FROM DESCENDANTS WHERE MEASUREID=R_COMPOUNDELSE
    Count relaxed violations of R_COMPOUNDELSE in the children of the selected artefact:
    COUNT RULE.OCCURRENCES(RELAXED) FROM DESCENDANTS WHERE MEASUREID=R_COMPOUNDELSE
  • IS_STATUS_FINDING(<findingStatus>, [<RelaxedInSourceCode>], [<isSuspicious>])

    To filter based on the status of the finding occurrence. Accepted values are:

    • <findingStatus>, is the violations status. Accepted values are:

      • ALL to find all violations irrespective of their status

      • OPEN to find all open violations

      • CONFIRMED to find all confirmed violations

      • RELAXED to find all relaxed violations

      • RELAXED_DEROGATION to find violations with the Relaxed (Derogation) relaxation status

      • RELAXED_LEGACY to find violations with the Relaxed (Legacy system) relaxation status

      • RELAXED_FALSE_POSITIVE to find violations with the Relaxed (False positive) relaxation status

      • <CUSTOM_STATUS_ID> to find violations with the specified custom status

      By default, all opened and confirmed findings are returned.

    • <relaxedInSourceCode>, to specify whether the violation was relaxed from source code, true, or from the user interface false (default).

    • <isSuspicious>, to specify whether the violation is flagged as suspicious, true, or not, false (default).

    Find the number of legacy-code-relaxed violations of the R_COMPOUNDELSE rule in the children of the selected artefact:
    COUNT RULE.OCCURRENCES(RELAXED) FROM DESCENDANTS WHERE MEASUREID=R_COMPOUNDELSE AND IS_STATUS_FINDING(RELAXED_LEGACY)
  • IS_NEW_FINDING()

    To only consider findings that are new in the considered version.

    Count the number of new violations in the analysis:
    COUNT RULE.OCCURRENCES FROM TREE WHERE IS_NEW_FINDING()