{ "cells": [ { "cell_type": "markdown", "id": "469c6622", "metadata": {}, "source": [ "# Perform a BoM query" ] }, { "cell_type": "markdown", "id": "2a902ea0", "metadata": {}, "source": [ "Both impacted substances queries and compliance queries can be performed on an XML BoM instead of a list\n", "of Granta MI records. The BoM must be in the Granta 17/11 format or Granta 23/01 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": "e42d28b0", "metadata": {}, "source": [ "For help on constructing an XML BoM, see the\n", "[Creating a Bill of Materials from an external data source](3-5_Creating_a_Bill_of_Materials.py) example." ] }, { "cell_type": "markdown", "id": "bbdf4db9", "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": "c8974844", "metadata": {}, "source": [ "The following supporting files are required for this example:\n", "\n", "* [bom-complex.xml](supporting-files/bom-complex.xml)\n", "* [invalid-bom.xml](supporting-files/invalid-bom.xml)" ] }, { "cell_type": "markdown", "id": "31ad802a", "metadata": {}, "source": [ "The XSD XML schema is included in the library in the ``schemas`` subpackage." ] }, { "cell_type": "markdown", "id": "fd79e84f", "metadata": {}, "source": [ "## Validate an XML file with an XSD schema" ] }, { "cell_type": "markdown", "id": "0353f34c", "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": "45c97bfb", "metadata": {}, "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)\n", "\n", "\n", "# You can now use this function to test the validity of a BoM generated by BoM Analyzer." ] }, { "cell_type": "code", "execution_count": null, "id": "a679edd0", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.bomanalytics.schemas import bom_schema_1711\n", "\n", "valid_xml_file = \"supporting-files/bom-complex.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": "1c32ade3", "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": "2e6df1bd", "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": "16aa4977", "metadata": {}, "source": [ "## Run an impacted substances XML-based query" ] }, { "cell_type": "markdown", "id": "3b27340d", "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": "d5759324", "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": "9c4af7fd", "metadata": {}, "source": [ "The impacted substances BoM query behaves similar to other impacted substances queries. However,\n", "a BoM query can only accept a single BoM at a time, and so you only ever receive a single list\n", "of substances impacted by the specified legislations." ] }, { "cell_type": "code", "execution_count": null, "id": "4288862f", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import queries\n", "\n", "SIN_LIST = \"SINList\"\n", "impacted_substances_query = (\n", " queries.BomImpactedSubstancesQuery()\n", " .with_bom(valid_xml)\n", " .with_legislation_ids([SIN_LIST])\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "76cbc91b", "metadata": {}, "outputs": [], "source": [ "impacted_substances_result = cxn.run(impacted_substances_query)\n", "impacted_substances_result" ] }, { "cell_type": "markdown", "id": "3cf20c0a", "metadata": {}, "source": [ "The ``BomImpactedSubstancesQueryResult`` object returned after running the query for impacted substances now behaves\n", "similarly to the result object for any other query for impacted substances. For example, you can print all substances\n", "impacted by the legislation using an approach from the previous example." ] }, { "cell_type": "code", "execution_count": null, "id": "0b448e6d", "metadata": {}, "outputs": [], "source": [ "from tabulate import tabulate\n", "\n", "rows = [\n", " [substance.cas_number, substance.max_percentage_amount_in_material]\n", " for substance in impacted_substances_result.impacted_substances\n", "]\n", "print(f'Substances impacted by \"{SIN_LIST}\" in a BoM (5/{len(rows)})')\n", "print(tabulate(rows[:5], headers=[\"CAS Number\", \"Amount (wt. %)\"]))" ] }, { "cell_type": "markdown", "id": "9224dec3", "metadata": {}, "source": [ "## Run a compliance XML-based query" ] }, { "cell_type": "markdown", "id": "4f70c8b5", "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": "875058b8", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import indicators\n", "\n", "svhc = indicators.WatchListIndicator(\n", " name=\"SVHC\",\n", " legislation_ids=[\"Candidate_AnnexXV\"],\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": "b2bf9a94", "metadata": {}, "outputs": [], "source": [ "compliance_result = cxn.run(compliance_query)\n", "compliance_result" ] }, { "cell_type": "markdown", "id": "8129878d", "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": "f4748c8c", "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": "0173e1d3", "metadata": {}, "source": [ "## Raise an invalid BoM exception" ] }, { "cell_type": "markdown", "id": "9894be86", "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": "463aa82a", "metadata": {}, "outputs": [], "source": [ "broken_query = (\n", " queries.BomImpactedSubstancesQuery()\n", " .with_bom(invalid_xml)\n", " .with_legislation_ids([SIN_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 }