Skip to content

Commit

Permalink
Merge pull request cyclus#186 from alexravitz/source
Browse files Browse the repository at this point in the history
writing_input2
  • Loading branch information
scopatz committed Feb 2, 2016
2 parents 33ae586 + e463c5a commit 5cf8053
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 31 deletions.
119 changes: 103 additions & 16 deletions source/cyclusagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
.. cyclus-agent:: tests:TestFacility:TestFacility
"""
from __future__ import print_function
from __future__ import print_function, unicode_literals
import sys
import os.path
import re
Expand Down Expand Up @@ -161,7 +161,7 @@ def _type(cpp, given=None):
return default_types[cpp]


def buildsample(cpptype, schematype=None, uitype=None, names=None, units=None):
def build_xml_sample(cpptype, schematype=None, uitype=None, names=None, units=None):
schematype = prepare_type(cpptype, schematype)
uitype = prepare_type(cpptype, uitype)
names = prepare_type(cpptype, names)
Expand All @@ -183,8 +183,8 @@ def buildsample(cpptype, schematype=None, uitype=None, names=None, units=None):
elif t in ['std::list', 'std::set', 'std::vector']:
name = 'list' if names[0] is None else names[0]
impl += '<{0}>'.format(name)
impl += buildsample(cpptype[1], schematype[1], uitype[1], names[1], units[1])
impl += buildsample(cpptype[1], schematype[1], uitype[1], names[1], units[1])
impl += build_xml_sample(cpptype[1], schematype[1], uitype[1], names[1], units[1])
impl += build_xml_sample(cpptype[1], schematype[1], uitype[1], names[1], units[1])
impl += '...'
impl += '</{0}>'.format(name)
elif t == 'std::map':
Expand All @@ -204,12 +204,12 @@ def buildsample(cpptype, schematype=None, uitype=None, names=None, units=None):
valnames = names[2]
impl += '<{0}>'.format(name)
impl += '<{0}>'.format(itemname)
impl += buildsample(cpptype[1], schematype[1], uitype[1], keynames, units[1])
impl += buildsample(cpptype[2], schematype[2], uitype[2], valnames, units[2])
impl += build_xml_sample(cpptype[1], schematype[1], uitype[1], keynames, units[1])
impl += build_xml_sample(cpptype[2], schematype[2], uitype[2], valnames, units[2])
impl += '</{0}>'.format(itemname)
impl += '<{0}>'.format(itemname)
impl += buildsample(cpptype[1], schematype[1], uitype[1], keynames, units[1])
impl += buildsample(cpptype[2], schematype[2], uitype[2], valnames, units[2])
impl += build_xml_sample(cpptype[1], schematype[1], uitype[1], keynames, units[1])
impl += build_xml_sample(cpptype[2], schematype[2], uitype[2], valnames, units[2])
impl += '</{0}>'.format(itemname)
impl += '...'
impl += '</{0}>'.format(name)
Expand All @@ -224,19 +224,89 @@ def buildsample(cpptype, schematype=None, uitype=None, names=None, units=None):
if names[2] is not None:
secondname = names[2]
impl += '<{0}>'.format(name)
impl += buildsample(cpptype[1], schematype[1], uitype[1], firstname, units[1])
impl += buildsample(cpptype[2], schematype[2], uitype[2], secondname, units[2])
impl += build_xml_sample(cpptype[1], schematype[1], uitype[1], firstname, units[1])
impl += build_xml_sample(cpptype[2], schematype[2], uitype[2], secondname, units[2])
impl += '</{0}>'.format(name)
else:
msg = 'Unsupported type {1}'.format(t)
raise RuntimeError(msg)


s = xml.dom.minidom.parseString(impl)
s = s.toprettyxml(indent=' ')
lines = s.splitlines()
lines = lines[1:] # remove initial xml version tag
return '\n'.join(lines)
s = s.toprettyxml(indent=' ')
_, lines = s.split("\n", 1)
return lines

def build_json_sample(cpptype, schematype=None, uitype=None, names=None, units=None, default=None):
schematype = prepare_type(cpptype, schematype)
uitype = prepare_type(cpptype, uitype)
names = prepare_type(cpptype, names)
units = prepare_type(cpptype, units)

impl = ''
t = cpptype if isinstance(cpptype, STRING_TYPES) else cpptype[0]
if t in PRIMITIVES:
name = 'val'
if names is not None:
name = names
d_type = _type(t, schematype or uitype)
d_type = uitype if uitype in special_uitypes else d_type
defstr = repr(default.encode()) if isinstance(default, STRING_TYPES) else default

if isinstance(units, STRING_TYPES):
impl += '{{"{0}": {1}}} # {2}, {3}'.format(name, defstr, d_type, units)
else:
impl += '{{"{0}": {1}}} # {2}'.format(name, defstr, d_type)
elif t in ['std::list', 'std::set', 'std::vector']:
name = 'list' if names[0] is None else names[0]
impl += '"{0}"'.format(name)
impl += build_json_sample(cpptype[1], schematype[1], uitype[1], names[1], units[1])
impl += build_json_sample(cpptype[1], schematype[1], uitype[1], names[1], units[1])
impl += '...'
impl += '"/{0}"'.format(name)
elif t == 'std::map':
name = 'map'
if isinstance(names[0], STRING_TYPES):
names[0] = [names[0], None]
elif names[0] is None:
names[0] = [name, None]
if names[0][0] is not None:
name = names[0][0]
itemname = 'item' if names[0][1] is None else names[0][1]
keynames = 'key' if isinstance(cpptype[1], STRING_TYPES) else ['key']
if names[1] is not None:
keynames = names[1]
valnames = 'val' if isinstance(cpptype[2], STRING_TYPES) else ['val']
if names[1] is not None:
valnames = names[2]
impl += '{0}'.format(name)
impl += '{0}'.format(itemname)
impl += build_json_sample(cpptype[1], schematype[1], uitype[1], keynames, units[1])
impl += build_json_sample(cpptype[2], schematype[2], uitype[2], valnames, units[2])
impl += '/{0}'.format(itemname)
impl += '{0}'.format(itemname)
impl += build_json_sample(cpptype[1], schematype[1], uitype[1], keynames, units[1])
impl += build_json_sample(cpptype[2], schematype[2], uitype[2], valnames, units[2])
impl += '/{0}'.format(itemname)
impl += '...'
impl += '/{0}'.format(name)
elif t == 'std::pair':
name = 'pair'
if names[0] is not None:
name = names[0]
firstname = 'first' if isinstance(cpptype[1], STRING_TYPES) else ['first']
if names[1] is not None:
firstname = names[1]
secondname = 'second' if isinstance(cpptype[2], STRING_TYPES) else ['second']
if names[2] is not None:
secondname = names[2]
impl += '{0}'.format(name)
impl += build_json_sample(cpptype[1], schematype[1], uitype[1], firstname, units[1])
impl += build_json_sample(cpptype[2], schematype[2], uitype[2], secondname, units[2])
impl += '/{0}'.format(name)
else:
msg = 'Unsupported type {1}'.format(t)
raise RuntimeError(msg)
return impl

class CyclusAgent(Directive):
"""The cyclus-agent directive, which is based on constructing a list of
Expand Down Expand Up @@ -378,7 +448,7 @@ def append_statevars(self):
self.lines.append('')

self.lines += ['', ind + '.. code-block:: xml', '']
schemalines = buildsample(t, schematype, uitype, labels, units).split('\n')
schemalines = build_xml_sample(t, schematype, uitype, labels, units).split('\n')
previndent = ''
for l in schemalines:
if len(l.strip()) > 0:
Expand All @@ -388,6 +458,18 @@ def append_statevars(self):
previndent = ' ' * (len(l) - len(l.lstrip()))
self.lines.append('')

self.lines += ['', ind + '.. code-block:: yaml', '']
schemalines = build_json_sample(t, schematype, uitype, labels, units, default=info.get('default', 'null')).split('\n')
previndent = ''
for l in schemalines:
if len(l.strip()) > 0:
if l.strip() == '...':
l = previndent + l.strip()
self.lines.append(ind + ' ' + l)
previndent = ' ' * (len(l) - len(l.lstrip()))
self.lines.append('')



def append_schema(self):
header = 'XML Input Schema'
Expand Down Expand Up @@ -439,3 +521,8 @@ def run(self):
def setup(app):
app.add_directive('cyclus-agent', CyclusAgent)

if __name__ == "__main__":
t = ["std::vector", "double"]
#t = 'double'
s = build_json_sample(t, default=[42.0])
print(s)
16 changes: 16 additions & 0 deletions source/user/find_agents.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ section in the input file:
...
</simulation>
.. code-block:: json
{"simulation": {
"archetypes": {
"spec": {
"path": "",
"lib": "agents",
"name": "KFacility",
"alias": "myfac1" },
"spec": {
}
}
}
}
The ``path`` tag can be omitted if it is empty (as in the case of |Cyclus|'
default agents library). If the ``lib`` tag is omitted, it defaults to the
value of the ``name`` tag. The ``alias`` tag may also be omitted, in which case
Expand Down
8 changes: 5 additions & 3 deletions source/user/input_specs/control.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ tag, and has the following sections in any order:
Example
+++++++


**XML:**

.. code-block:: xml
<control>
Expand All @@ -37,8 +40,8 @@ Example
<decay>lazy</decay>
</control>
This example starts in November 2007, and runs for 100 years (1200 months).
**JSON:**

.. code-block:: json
Expand All @@ -52,8 +55,7 @@ This example starts in November 2007, and runs for 100 years (1200 months).
}
This is what the example above would look like if written in JSON.

This example starts in November 2007, and runs for 100 years (1200 months).


.. rst-class:: html-toggle
Expand Down
21 changes: 21 additions & 0 deletions source/user/input_specs/facility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Each ``facility`` block has the follwoing sections in any order:
Example
+++++++

**XML:**

.. code-block:: xml
<facility>
Expand All @@ -38,6 +40,25 @@ Example
</config>
</facility>
**JSON:**

.. code-block:: json
{"facility": {
"config": {
"Sink": "(... archetype-specific input for a `Sink` archetype)"},
"name": "LongTermStorage"
}
}
{"facility": {
"config": {
"RecipeReactor": "(... archetype-specific input for a `RecipeReactor` archetype)"},
"lifetime": 720,
"name": "PBMR"
}
}
This example introduces two facility prototypes. The first has the name
`LongTermStorage`, and is configured from the :term:`archetype` with the name
Expand Down
27 changes: 27 additions & 0 deletions source/user/input_specs/inst.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ each contain the following sections, in the following order:
Example
+++++++

**XML:**

.. code-block:: xml
<institution>
Expand All @@ -49,6 +51,29 @@ Example
<config> <NullInst/> </config>
</institution>
**JSON:**

.. code-block:: json
{"institution": {
"config": {"NullInst": null},
"initialfacilitylist": {
"entry": [
{"number": 1, "prototype": "FacilityA"},
{"number": 1, "prototype": "FacilityB"}
]
},
"name": "SingleInstitution"
}
}
{"institution": {
"config": {"NullInst": null},
"name": "AnotherInstitution"
}
}
This example introduces two institution agents (the region section that
encloses them is not shown). The first institution has the name
Expand All @@ -60,6 +85,7 @@ the ``FacilityB`` prototype. The second institution has the name
`AnotherInstitution`, is also configured from the archetype with the name (or
alias) ``NullInst``. This institution has no initial facilities.


.. code-block:: json
{
Expand All @@ -84,6 +110,7 @@ alias) ``NullInst``. This institution has no initial facilities.
This is what the example above would look like if written in JSON.


.. rst-class:: html-toggle

Grammar Definition
Expand Down
15 changes: 8 additions & 7 deletions source/user/input_specs/recipe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ A ``recipe`` block has the following sections in the following order:
Example
+++++++

**XML:**

.. code-block:: xml
<recipe>
Expand All @@ -59,12 +61,7 @@ Example
</nuclide>
</recipe>
This example defines two material compositions. The first has the name
`commod_recipe`, is defined using mass fractions, and contains a single
nuclide of H-1. The second recipe is named `natU_recipe`, is defined using
atom fractions, and contains two nuclides: 0.7% of the atoms are U-235 and
99.3% of the atoms are U-238.

**JSON:**

.. code-block:: json
Expand Down Expand Up @@ -92,8 +89,12 @@ atom fractions, and contains two nuclides: 0.7% of the atoms are U-235 and
}
}
This example defines two material compositions. The first has the name
`commod_recipe`, is defined using mass fractions, and contains a single
nuclide of H-1. The second recipe is named `natU_recipe`, is defined using
atom fractions, and contains two nuclides: 0.7% of the atoms are U-235 and
99.3% of the atoms are U-238.

This is what the example above would look like if written in JSON.


.. rst-class:: html-toggle
Expand Down
12 changes: 11 additions & 1 deletion source/user/input_specs/region.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Each ``region`` block has the following sections in any order:
Example
+++++++

**XML:**

.. code-block:: xml
<region>
Expand All @@ -40,6 +42,8 @@ Example
</institution>
</region>
**JSON:**


This example introduces two region agents. The first has the name
`MyHomeRegion`, and is configured from the :term:`archetype` with the name (or
Expand Down Expand Up @@ -68,7 +72,13 @@ contents of the ``GrowthRegion`` section are defined by the author of the
}
This is what the example above would look like if written in JSON.
This example introduces two region agents. The first has the name
`MyHomeRegion`, and is configured from the :term:`archetype` with the name (or
alias) `NullRegion`. The author of the ``NullRegion`` archetype has defined
no archetype-specific data. The second has the name ``MyNeighborRegion`` and
is based on the archetype with the name (or alias) `GrowthRegion`. The
contents of the ``GrowthRegion`` section are defined by the author of the
`GrowthRegion` archetype.


.. rst-class:: html-toggle
Expand Down
Loading

0 comments on commit 5cf8053

Please sign in to comment.