Parsing du modèle utilisateur

This commit is contained in:
Adrien Bourmault 2021-12-07 18:45:15 +00:00
parent 0b140a54c6
commit 929fc53e2c
4 changed files with 415 additions and 2 deletions

View File

@ -118,4 +118,3 @@
</transitions>
</gem-graph-model>

View File

@ -207,3 +207,4 @@
</xs:complexType>
</xs:element>
</xs:schema>

View File

@ -24,7 +24,6 @@ import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
glade_path = "../ui/GLArea.glade"
## Main window

414
src/model.py Normal file
View File

@ -0,0 +1,414 @@
from lxml import etree
import datetime as DT
## Identity
class G_ref:
def __init__(self, node):
self.node = node
def get_id(self):
return self.node.get("id")
def set_id(self, id):
self.node.attrib["id"] = id
def get_author(self):
return self.node.get("author")
def set_author(self, author):
self.node.attrib["author"] = author
def get_date(self):
return DT.datetime.fromtimestamp(int(self.node.get("date")))
def set_date(self, date):
self.node.attrib["date"] = str(int(date.timestamp()))
def get_lang(self):
return self.node.get("lang")
def set_lang(self, lang):
if(len(lang) > 2):
print("lang must be 2 characters")
return
self.node.attrib["lang"] = lang
def get_text(self):
return self.node.text
def set_text(self, text):
self.node.text = text
class Identity:
def __init__(self, node):
self.node = node
for child in node:
if(child.tag == "name"):
self.__name = child
if(child.tag == "owner"):
self.__owner = child
if(child.tag == "owner_id"):
self.__owner_id = child
if(child.tag == "date"):
self.__date = child
if(child.tag == "version"):
self.__version = child
if(child.tag == "g_ref"):
self.__g_ref = child
def get_g_ref(self):
return G_ref(self.__g_ref)
def get_name(self):
return self.__name.text
def get_owner(self):
return self.__owner.text
def get_owner_id(self):
return self.__owner_id.text
def get_date(self):
return DT.datetime.fromtimestamp(int(self.__date.text))
def get_version(self):
return self.__version.text
def set_name(self, text):
self.__name.text = text
def set_owner(self, text):
self.__owner.text = text
def set_owner_id(self, text):
self.__owner_id.text = text
def set_date(self, date):
self.__date.text = str(int(date.timestamp()))
def set_version(self, text):
self.__version.text = text
## Parameters
class Simulation:
def __init__(self, node):
self.node = node
for child in node:
if(child.tag == "max_thread"):
self.__max_thread = child
if(child.tag == "max_cycles"):
self.__max_cycles = child
def get_max_thread(self):
return int(self.__max_thread.text)
def set_max_thread(self, value):
if(not isinstance(value, int)):
print("max_thread must be an int")
return
self.__max_thread.text = str(value)
def get_max_cycles(self):
return int(self.__max_cycles.text)
def set_max_cycles(self, value):
if(not isinstance(value, int)):
print("max_cycles must be an int")
return
self.__max_cycles.text = str(value)
class Spaceparam:
def __init__(self, node):
self.node = node
for child in node:
if(child.tag == "dimension"):
self.__dimension = Dimension(child)
if(child.tag == "site_multiplicity"):
self.__site_multiplicity = child
def get_site_multiplicity(self):
return self.__site_multiplicity.text
def set_site_multiplicity(self, value):
self.__site_multiplicity.text = str(value)
def get_dimension(self):
return self.__dimension
class Axis:
def __init__(self, node):
self.node = node
def get_name(self):
return self.node.attrib["name"]
def get_size(self):
return int(self.node.attrib["size"])
class Dimension:
__axis = []
def __init__(self, node):
self.node = node
for child in node:
if(child.tag == "axis"):
self.__axis.append(Axis(child))
def get_axis(self, i):
return self.__axis[i]
class Parameters:
def __init__(self, node):
self.node = node
for child in node:
if(child.tag == "simulation"):
self.simulation = Simulation(child)
if(child.tag == "space-param"):
self.spaceparam = Spaceparam(child)
def get_id(self):
return self.node.attrib["id"]
def set_id(self, text):
self.node.attrib["id"] = text
def get_author(self):
return self.node.attrib["author"]
def get_date(self):
return self.node.attrib["date"]
def set_author(self, text):
self.node.attrib["author"] = text
def set_date(self, text):
self.node.attrib["date"] = text
## Objects
class Object:
__arrows = []
def __init__(self, node):
self.node = node
for child in node:
if(child.tag == "arrow"):
self.__arrows.append(Arrow(child))
def get_arrow(self, i):
return self.__arrows[i]
class Objects:
__objects = []
def __init__(self, node):
self.node = node
for child in node:
if(child.tag == "object"):
self.__objects.append(Object(child))
def get_object(self, i):
return self.__objects[i]
## Savestates
class Space:
__arrows = []
def __init__(self, node):
self.node = node
for child in node:
if(child.tag == "arrow"):
self.__arrows.append(Arrow(child))
def get_arrow(self, i):
return self.__arrows[i]
class Savestates:
__space = []
def __init__(self, node):
self.node = node
for child in node:
if(child.tag == "space"):
self.__space.append(Space(child))
def get_space(self, i):
return self.__space[i]
## Conditions
class Condition:
def __init__(self, node):
self.node = node
def get_coord(self, axis):
return int(self.node.get(axis))
def set_coord(self, axis, value):
self.node.attrib[axis] = str(value)
def get_site(self):
return int(self.node.get("site"))
def set_site(self, site):
self.node.attrib["site"] = str(site)
def get_weight(self):
return int(self.node.get("weight"))
def set_weight(self, weight):
self.node.attrib["weight"] = str(weight)
def get_node_id(self):
return int(self.node.get("node_id"))
def set_node_id(self, node_id):
self.node.attrib["node_id"] = str(node_id)
def get_parent(self):
return int(self.node.get("parent"))
def set_parent(self, parent):
self.node.attrib["parent"] = str(parent)
class Conditions:
__conditions = []
def __init__(self, node):
self.node = node
for child in node:
if(child.tag == "condition"):
self.__conditions.append(Condition(child))
class Transition:
__arrows = []
def __init__(self, node):
self.node = node
for child in node:
if(child.tag == "arrow"):
self.__arrows.append(Arrow(child))
def get_arrow(self, i):
return self.__arrows[i]
class Transitions:
__transitions = []
def __init__(self, node):
self.node = node
for child in node:
if(child.tag == "transition"):
self.__transition.append(Transition(child))
def get_transition(self, i):
return self.__transitions[i]
def get_transition_array(self, i):
return self.__transitions
## Model
class Arrow:
def __init__(self, node):
self.node = node
def get_coord(self, axis):
return int(self.node.get(axis))
def set_coord(self, axis, value):
self.node.attrib[axis] = str(value)
def get_site(self):
return int(self.node.get("site"))
def set_site(self, site):
self.node.attrib["site"] = str(site)
def get_weight(self):
return int(self.node.get("weight"))
def set_weight(self, weight):
self.node.attrib["weight"] = str(weight)
class Model:
def __init__(self, modelpath, schemapath):
schema = etree.parse(schemapath)
xmlschema = etree.XMLSchema(schema)
self.parser = etree.XMLParser(
remove_blank_text=True,
remove_pis=True,
compact=True,
schema=xmlschema)
self.tree = etree.parse(modelpath, self.parser)
self.node = self.tree.getroot()
self.create_all_leaves()
def create_all_leaves(self):
for child in self.node:
if(isinstance(child.tag, str)):
if(child.tag == "identity"):
self.__identity = child
if(child.tag == "parameters"):
self.__parameters = child
if(child.tag == "objects"):
self.__objects = child
if(child.tag == "savestates"):
self.__savestates = child
if(child.tag == "conditions"):
self.__conditions = child
if(child.tag == "transition"):
self.__transitions = child
def get_identity(self):
return Identity(self.__identity)
def get_parameters(self):
return Parameters(self.__parameters)
def get_objects(self):
return Objects(self.__objects)
def get_savestates(self):
return Savestates(self.__savestates)
def get_conditions(self):
return Conditions(self.__conditions)
def get_transitions(self):
return Transitions(self.__transitions)
def saveas(self, name):
self.tree.write("../models/" + name + ".xml", encoding="utf-8")
modelPath = "../models/dimers random walk.xml"
schemaPath = "../schemas/models_0.2.xmls"
testmodel = Model(modelPath, schemaPath)