{ "cells": [ { "cell_type": "markdown", "id": "45b8ba5d", "metadata": {}, "source": [ "# Perform a BoM compliance query" ] }, { "cell_type": "markdown", "id": "a06c6dc3", "metadata": {}, "source": [ "Compliance queries can be performed on an XML BoM instead of a list\n", "of Granta MI records. The BoM must be in any valid Granta XML BoM format. This example shows how to use\n", "the ``lxml`` package with the XSD XML schema file to validate the XML format." ] }, { "cell_type": "markdown", "id": "7414a225", "metadata": {}, "source": [ "For help on constructing an XML BoM, see [BoM examples](../6_BoMs/index.rst)." ] }, { "cell_type": "markdown", "id": "ea4d6f95", "metadata": {}, "source": [ "If the XML file is generated by a Granta MI product and has not been modified, it is possible to skip this step before\n", "submitting the query. However, it is strongly advised to validate the XML BoM in all situations to avoid unexpected\n", "server-side failures. If an invalid XML file is used in a query, an exception is raised by the ``requests`` HTTP\n", "package, but it does not contain information about why the XML is non-compliant. A more detailed log is\n", "reported on the server in the MI Service Layer log." ] }, { "cell_type": "markdown", "id": "db9571be", "metadata": {}, "source": [ "The following supporting files are required for this example:\n", "\n", "* [compliance-bom.xml](../supporting-files/compliance-bom.xml)\n", "* [invalid-bom.xml](../supporting-files/invalid-bom.xml)" ] }, { "cell_type": "markdown", "id": "27086d5e", "metadata": {}, "source": [ "The XSD XML schema is included in the library in the ``schemas`` subpackage." ] }, { "cell_type": "markdown", "id": "9a82f086", "metadata": {}, "source": [ "## Validate an XML file with an XSD schema" ] }, { "cell_type": "markdown", "id": "5f794cdf", "metadata": {}, "source": [ "The ``lxml`` package provides a similar API to the standard library XML module, but it includes some additional\n", "functionality, including schema validation. First import the ``lxml`` package and then build a simple validator\n", "function that takes both the XML file and schema file and returns the result." ] }, { "cell_type": "code", "execution_count": null, "id": "1aca368a", "metadata": { "lines_to_end_of_cell_marker": 0, "lines_to_next_cell": 1 }, "outputs": [], "source": [ "from lxml import etree\n", "\n", "\n", "def xml_validator(xml: str, schema_file: str) -> bool:\n", " schema = etree.XMLSchema(file=schema_file)\n", " doc = etree.fromstring(xml)\n", " return schema.validate(doc)" ] }, { "cell_type": "markdown", "id": "dff5b7ed", "metadata": {}, "source": [ "You can now use this function to test the validity of a BoM generated by BoM Analyzer." ] }, { "cell_type": "code", "execution_count": null, "id": "1e4e42a5", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.bomanalytics.schemas import bom_schema_1711\n", "\n", "valid_xml_file = \"../supporting-files/compliance-bom.xml\"\n", "with open(valid_xml_file) as f:\n", " valid_xml = f.read()\n", "result = xml_validator(valid_xml, bom_schema_1711)\n", "result" ] }, { "cell_type": "markdown", "id": "5fbf11f9", "metadata": {}, "source": [ "You can also test a BoM that is valid XML but is not compliant with the schema." ] }, { "cell_type": "code", "execution_count": null, "id": "5796fa10", "metadata": {}, "outputs": [], "source": [ "invalid_xml_file = \"../supporting-files/invalid-bom.xml\"\n", "with open(invalid_xml_file) as f:\n", " invalid_xml = f.read()\n", "result = xml_validator(invalid_xml, bom_schema_1711)\n", "result" ] }, { "cell_type": "markdown", "id": "f5703a73", "metadata": {}, "source": [ "## Run a compliance XML-based query" ] }, { "cell_type": "markdown", "id": "a04b445c", "metadata": {}, "source": [ "Now that you have validated the XML, you can build your XML BoM-based query.\n", "First, connect to Granta MI." ] }, { "cell_type": "code", "execution_count": null, "id": "550c89aa", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import Connection\n", "\n", "server_url = \"http://my_grantami_server/mi_servicelayer\"\n", "cxn = Connection(server_url).with_credentials(\"user_name\", \"password\").connect()" ] }, { "cell_type": "markdown", "id": "0e250bd7", "metadata": {}, "source": [ "Running a BoM compliance query produces the same result as if you had stored the BoM structure as linked **Products\n", "and Parts** records in Granta MI." ] }, { "cell_type": "code", "execution_count": null, "id": "865d465a", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import indicators, queries\n", "\n", "CANDIDATE_LIST = \"Candidate_AnnexXV\"\n", "svhc = indicators.WatchListIndicator(\n", " name=\"SVHC\",\n", " legislation_ids=[CANDIDATE_LIST],\n", " default_threshold_percentage=0.1,\n", ")\n", "compliance_query = (\n", " queries.BomComplianceQuery()\n", " .with_bom(valid_xml)\n", " .with_indicators([svhc])\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "f5539d76", "metadata": {}, "outputs": [], "source": [ "compliance_result = cxn.run(compliance_query)\n", "compliance_result" ] }, { "cell_type": "markdown", "id": "d546659c", "metadata": {}, "source": [ "The ``BomComplianceQueryResult`` object returned after running the compliance query contains a list of\n", "``PartWithComplianceResult`` objects, the behavior of which is covered in a previous example.\n", "The following cell prints the compliance status of the BoM." ] }, { "cell_type": "code", "execution_count": null, "id": "58dadef4", "metadata": {}, "outputs": [], "source": [ "root_part = compliance_result.compliance_by_part_and_indicator[0]\n", "print(f\"BoM Compliance Status: {root_part.indicators['SVHC'].flag.name}\")" ] }, { "cell_type": "markdown", "id": "ed3f00ca", "metadata": {}, "source": [ "## Raise an invalid BoM exception" ] }, { "cell_type": "markdown", "id": "5f2b54d1", "metadata": {}, "source": [ "If you were to try the same query with an invalid BoM, you would see a stack trace informing you that the MI Service\n", "Layer responded with a 500 HTTP response code. To raise the exception, change the following ``RUN_QUERY`` constant to\n", "``True``." ] }, { "cell_type": "code", "execution_count": null, "id": "c7feec12", "metadata": {}, "outputs": [], "source": [ "broken_query = (\n", " queries.BomImpactedSubstancesQuery()\n", " .with_bom(invalid_xml)\n", " .with_legislation_ids([CANDIDATE_LIST])\n", ")\n", "\n", "RUN_QUERY = False\n", "\n", "if RUN_QUERY:\n", " cxn.run(broken_query)" ] } ], "metadata": { "jupytext": { "formats": "ipynb,py:light" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }