# Path: lems/base/util.py
# def validate_lems(file_name):
# 
#     from lxml import etree
# 
#     try:
#         from urllib2 import urlopen  # Python 2
#     except:
#         from urllib.request import urlopen  # Python 3
# 
#     schema_file = urlopen(__schema_location__)
#     xmlschema = etree.XMLSchema(etree.parse(schema_file))
#     print("Validating {0} against {1}".format(file_name, schema_file.geturl()))
#     xmlschema.assertValid(etree.parse(file_name))
#     print("It's valid!")


import lems.api as lems
from lems.base.util import validate_lems

iaf1.add(lems.Parameter("threshold", "voltage"))
iaf1.add(lems.Parameter("reset", "voltage"))
iaf1.add(lems.Parameter("refractoryPeriod", "time"))
iaf1.add(lems.Parameter("capacitance", "capacitance"))
iaf1.add(lems.Exposure("vexp", "voltage"))
dp = lems.DerivedParameter("range", "threshold - reset", "voltage")
iaf1.add(dp)

iaf1.dynamics.add(lems.StateVariable("v", "voltage", "vexp"))
iaf1.dynamics.add(lems.DerivedVariable("v2", dimension="voltage", value="v*2"))
cdv = lems.ConditionalDerivedVariable("v_abs", "voltage")
cdv.add(lems.Case("v .geq. 0", "v"))
cdv.add(lems.Case("v .lt. 0", "-1*v"))
iaf1.dynamics.add(cdv)


model.add(lems.Component("celltype_a", iaf1.name))
model.add(lems.Component("celltype_b", iaf1.name, threshold="20mV"))

fn = "/tmp/model.xml"
model.export_to_file(fn)

print("----------------------------------------------")
print(open(fn, "r").read())
print("----------------------------------------------")

print("Written generated LEMS to %s" % fn)



# Path: lems/base/util.py
# def validate_lems(file_name):
# 
#     from lxml import etree
# 
#     try:
#         from urllib2 import urlopen  # Python 2
#     except:
#         from urllib.request import urlopen  # Python 3
# 
#     schema_file = urlopen(__schema_location__)
#     xmlschema = etree.XMLSchema(etree.parse(schema_file))
#     print("Validating {0} against {1}".format(file_name, schema_file.geturl()))
#     xmlschema.assertValid(etree.parse(file_name))
#     print("It's valid!")


# Path: examples/apitest2.py
import lems.api as lems
from lems.base.util import validate_lems


iaf1.add(lems.Parameter("threshold", "voltage"))
iaf1.add(lems.Parameter("reset", "voltage"))
iaf1.add(lems.Parameter("refractoryPeriod", "time"))
iaf1.add(lems.Parameter("capacitance", "capacitance"))
iaf1.add(lems.Exposure("vexp", "voltage"))
dp = lems.DerivedParameter("range", "threshold - reset", "voltage")
iaf1.add(dp)

iaf1.dynamics.add(lems.StateVariable("v", "voltage", "vexp"))
iaf1.dynamics.add(lems.DerivedVariable("v2", dimension="voltage", value="v*2"))
cdv = lems.ConditionalDerivedVariable("v_abs", "voltage")
cdv.add(lems.Case("v .geq. 0", "v"))
cdv.add(lems.Case("v .lt. 0", "-1*v"))
iaf1.dynamics.add(cdv)


model.add(lems.Component("celltype_a", iaf1.name))
model.add(lems.Component("celltype_b", iaf1.name, threshold="20mV"))

fn = "/tmp/model.xml"
model.export_to_file(fn)

print("----------------------------------------------")
print(open(fn, "r").read())
print("----------------------------------------------")

print("Written generated LEMS to %s" % fn)



validate_lems(fn)