<jupyter_start><jupyter_text>In this worksheet we show how to connect to a BaseX database containing XML documents representing source code. Data from individual models are stored in databases by the same names. The name of the database is the same as the name of the model. The name of the database to open is given in the model variable below:<jupyter_code>model = 'ApplicationFoundation'
from BaseXClient import BaseXClient

# create session
session = BaseXClient.Session('localhost', 1984, 'admin', 'admin')

try:
    # create query instance
    input = '''let $results := 
<Results>
{
  for $c in /Class | /Table
  
    let $allMethods := count($c/Method)
    let $privateMethods := count($c/Method[@IsPrivate = 'True'])
    let $protectedMethods := count($c/Method[@IsProtected = 'True']) (: Explicitly marked with protected keyword :)
    let $publicMethods := count($c/Method[@IsPublic = 'True'])
    let $internalMethods := count($c[@IsInternal = 'True'])
          
    (: Compensate for methods that lack a visibility keyword: They are protected. :)
    let $protectedMethods := $protectedMethods + ($allMethods - $privateMethods - $protectedMethods - $publicMethods - $internalMethods)  
      
    return <Result Class='{$c/@Name}' PrivateMethodCount='{$privateMethods}' ProtectedMethodCount='{$protectedMethods}' PublicMethodCount='{$publicMethods}' InternalMethodCount='{$internalMethods}'/>
}
</Results>
return <Totals 
  PrivateMethodCount='{sum($results/Result/@PrivateMethodCount)}'
  ProtectedMethodCount='{sum($results/Result/@ProtectedMethodCount)}'
  PublicMethodCount='{sum($results/Result/@PublicMethodCount)}'
  InternalMethodCount='{sum($results/Result/@InternalMethodCount)}' />'''

    session.execute("open " + model);
    query = session.query(input)

    # There is only one result, an XML tag called Totals with the
    # attributes provided.
    res = query.execute()
    print(res)
    
    # close query object
    query.close()

finally:
    # close session
    if session:
        session.close()<jupyter_output><Totals PrivateMethodCount="2772" ProtectedMethodCount="4581" PublicMethodCount="13872" InternalMethodCount="2"/><jupyter_text>As you can see, the result of running the query in the BaseX database is stored in the res variable. For this demonstration, we will take these results and show then graphically using the mathplotlib library.<jupyter_code>import matplotlib.pyplot as plt

# Get the information from the XML using the lightweight Python XML library
import xml.etree.ElementTree as ET

tree = ET.fromstring(res)
privateCount = tree.get("PrivateMethodCount")
protectedCount = tree.get("ProtectedMethodCount")
publicCount = tree.get("PublicMethodCount")
internalCount = tree.get("InternalMethodCount")

fig = plt.figure(figsize=(5,5)) # In inches(!)
piechart = fig.add_subplot(111)

piechart.pie([privateCount, protectedCount, internalCount, publicCount], labels=("Private", "Protected", "Internal", "Public"),
        shadow=True, startangle=90, explode=(0.1, 0,0,0), autopct='%1.1f%%')
piechart.set_title ("Distribution of method visiblity\nModel: " + model, fontsize=20)
piechart.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.tight_layout()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11919315403422982, 'fraction_numerical': 0.01039119804400978, 'mean_word_length': 5.198863636363637, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, '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}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '338424', 'license': 'MIT', 'n_tokens_mistral': 946, 'n_tokens_neox': 911, 'n_words': 279, 'path': 'tools/Explorer/Jupyter notebooks/Method Visibility.ipynb', 'repo_name': 'RobinARH/Dynamics365FO-AppChecker', 'size': '3272'}