Skip to content

Commit

Permalink
Sample generation for submodel templates
Browse files Browse the repository at this point in the history
  • Loading branch information
otto-ifak committed May 2, 2024
1 parent ed3bda6 commit 652f80f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
19 changes: 13 additions & 6 deletions aas_test_engines/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

class AasSchema:

def __init__(self, validator: SchemaValidator, graph: FlowGraph, submodel_templates: Dict[str, SchemaValidator]):
def __init__(self, validator: SchemaValidator, schema: JSON, submodel_templates: Dict[str, SchemaValidator], submodel_schemas: Dict[str, any]):
self.validator = validator
self.graph = graph
self.schema = schema
self.submodel_templates = submodel_templates
self.submodel_schemas = submodel_schemas


def _find_schemas() -> Dict[str, AasSchema]:
Expand All @@ -38,13 +39,14 @@ def _find_schemas() -> Dict[str, AasSchema]:
format_validators=validators
)
validator = parse_schema(schema, config)
graph = generate_graph(schema)
submodel_templates = {}
submodel_schemas = {}
for key, submodel_schema in schema['$defs']['SubmodelTemplates'].items():
submodel_schema['$defs'] = schema['$defs']
submodel_schema['$schema'] = 'https://json-schema.org/draft/2020-12/schema'
submodel_templates[key] = parse_schema(submodel_schema, config)
result[i[:-4]] = AasSchema(validator, graph, submodel_templates)
submodel_schemas[key] = submodel_schema
result[i[:-4]] = AasSchema(validator, schema, submodel_templates, submodel_schemas)
return result


Expand Down Expand Up @@ -361,8 +363,13 @@ def check_aasx_file(file: TextIO, version: str = _DEFAULT_VERSION) -> AasTestRes
return check_aasx_data(zip, version)


def generate(version: str = _DEFAULT_VERSION) -> Generator[str, None, None]:
graph = _get_schema(version, set()).graph
def generate(version: str = _DEFAULT_VERSION, submodel_template: Optional[str] = None) -> Generator[str, None, None]:
if submodel_template is None:
aas = _get_schema(version, set())
graph = generate_graph(aas.schema)
else:
aas = _get_schema(version, set([submodel_template]))
graph = generate_graph(aas.submodel_schemas[submodel_template])
for i in graph.generate_paths():
sample = graph.execute(i.path)
yield json.dumps(sample)
23 changes: 18 additions & 5 deletions test/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json
from xml.etree import ElementTree

from aas_test_engines import file
from aas_test_engines import file, exception

script_dir = os.path.dirname(os.path.realpath(__file__))

Expand Down Expand Up @@ -132,13 +132,26 @@ def test_invoke(self):

class GenerateTest(TestCase):

def test_a_few(self):
def check(self, generator):
i = 0
for sample in file.generate():
for _ in generator:
i += 1
if i > 100:
if i > 10:
break

def test_meta_model(self):
generator = file.generate()
self.check(generator)

def test_generate_contact_info(self):
generator = file.generate(submodel_template='ContactInformation')
self.check(generator)

def test_invalid_name(self):
with self.assertRaises(exception.AasTestToolsException):
generator = file.generate(submodel_template="xyz")
self.check(generator)


class CheckSubmodelTemplate(TestCase):

Expand All @@ -153,7 +166,7 @@ def test_contact_info(self):
result = file.check_json_data(data, submodel_templates=templates)
result.dump()
self.assertFalse(result.ok())

def test_no_submodels(self):
data = {}
# is compliant to meta-model...
Expand Down

0 comments on commit 652f80f

Please sign in to comment.