Download this example as a Jupyter notebook or a
Python script.
Perform a BoM impacted substances query#
Impacted substances queries can be performed on an XML BoM instead of a list of Granta MI records. The BoM must be in any valid Granta XML BoM format. This example shows how to use the lxml package with the XSD XML schema file to validate the XML format.
For help on constructing an XML BoM, see BoM examples.
If the XML file is generated by a Granta MI product and has not been modified, it is possible to skip this step before submitting the query. However, it is strongly advised to validate the XML BoM in all situations to avoid unexpected server-side failures. If an invalid XML file is used in a query, an exception is raised by the requests HTTP package, but it does not contain information about why the XML is non-compliant. A more detailed log is reported on the server in the MI Service Layer
log.
The following supporting files are required for this example:
The XSD XML schema is included in the library in the schemas subpackage.
Validate an XML file with an XSD schema#
Info:
If you are planning on using a BoM that has been generated in a supported version of Granta MI BoM Analyzer or Materials Gateway, and the BoM has not been modified, you do not need to validate the XML. However, if invalid XML is used in a query, a requests exception is raised when the query is submitted, with a more detailed report available in the MI Service Layer log. Therefore, to avoid unexpected server-side failures, you should set up validation in all situations.
The lxml package provides a similar API to the standard library XML module, but it includes some additional functionality, including schema validation. First import the lxml package and then build a simple validator function that takes both the XML file and schema file and returns the result.
[1]:
from lxml import etree
def xml_validator(xml: str, schema_file: str) -> bool:
schema = etree.XMLSchema(file=schema_file)
doc = etree.fromstring(xml)
return schema.validate(doc)
You can now use this function to test the validity of a BoM generated by BoM Analyzer.
[2]:
from ansys.grantami.bomanalytics.schemas import bom_schema_1711
valid_xml_file = "../supporting-files/compliance-bom.xml"
with open(valid_xml_file) as f:
valid_xml = f.read()
result = xml_validator(valid_xml, bom_schema_1711)
result
[2]:
True
You can also test a BoM that is valid XML but is not compliant with the schema.
[3]:
invalid_xml_file = "../supporting-files/invalid-bom.xml"
with open(invalid_xml_file) as f:
invalid_xml = f.read()
result = xml_validator(invalid_xml, bom_schema_1711)
result
[3]:
False
Run an impacted substances XML-based query#
Now that you have validated the XML, you can build your XML BoM-based query. First, connect to Granta MI.
[4]:
from ansys.grantami.bomanalytics import Connection
server_url = "http://my_grantami_server/mi_servicelayer"
cxn = Connection(server_url).with_credentials("user_name", "password").connect()
The impacted substances BoM query behaves similar to other impacted substances queries. However, a BoM query can only accept a single BoM at a time, and so you only ever receive a single list of substances impacted by the specified legislations.
[5]:
from ansys.grantami.bomanalytics import queries
SIN_LIST = "SINList"
impacted_substances_query = (
queries.BomImpactedSubstancesQuery()
.with_bom(valid_xml)
.with_legislation_ids([SIN_LIST])
)
[6]:
impacted_substances_result = cxn.run(impacted_substances_query)
impacted_substances_result
[6]:
<BomImpactedSubstancesQueryResult: 1 BomWithImpactedSubstances results>
The BomImpactedSubstancesQueryResult object returned after running the query for impacted substances now behaves similarly to the result object for any other query for impacted substances. For example, you can print all substances impacted by the legislation using an approach from the previous example.
[7]:
from tabulate import tabulate
rows = [
[substance.cas_number, substance.max_percentage_amount_in_material]
for substance in impacted_substances_result.impacted_substances
]
print(f'Substances impacted by "{SIN_LIST}" in a BoM (5/{len(rows)})')
print(tabulate(rows[:5], headers=["CAS Number", "Amount (wt. %)"]))
Substances impacted by "SINList" in a BoM (5/44)
CAS Number Amount (wt. %)
------------ ----------------
10043-35-3
131-56-6 1.7
131-57-7 1.7
15087-24-8 1.7
25973-55-1 1.7
Raise an invalid BoM exception#
If you were to try the same query with an invalid BoM, you would see a stack trace informing you that the MI Service Layer responded with a 500 HTTP response code. To raise the exception, change the following RUN_QUERY constant to True.
[8]:
broken_query = (
queries.BomImpactedSubstancesQuery()
.with_bom(invalid_xml)
.with_legislation_ids([SIN_LIST])
)
RUN_QUERY = False
if RUN_QUERY:
cxn.run(broken_query)