From 07f5bd313bd08aa8c0193c832b9219f72f2704f7 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Thu, 9 Apr 2015 16:48:40 +0200 Subject: added support for typesystem constants in serialization. --- src/plugins/xml/XmlOutput.cpp | 22 ++++++++++++++++++++++ .../ontology_serialization/typesystem.in.osml | 6 ++++-- .../ontology_serialization/typesystem.out.osxml | 8 +++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/plugins/xml/XmlOutput.cpp b/src/plugins/xml/XmlOutput.cpp index e67ec19..80b3af5 100644 --- a/src/plugins/xml/XmlOutput.cpp +++ b/src/plugins/xml/XmlOutput.cpp @@ -514,6 +514,23 @@ static Rooted transformEnumType(Handle parent, return enumType; } +static Rooted transformConstant(Handle parent, + Handle t, + Handle c, TransformParams &P) +{ + // create an xml element for the constant. + Rooted constant{new Element(P.mgr, parent, "constant")}; + addNameAttribute(c, constant->getAttributes()); + // add the type reference + { + std::string typeRef = getTypeRef(t, c->getType()); + constant->getAttributes().emplace("type", typeRef); + } + // add the value + constant->getAttributes().emplace("value", toString(c->getValue(), P)); + return constant; +} + Rooted transformTypesystem(Handle parent, Handle t, TransformParams &P) { @@ -560,6 +577,11 @@ Rooted transformTypesystem(Handle parent, typesystem->addChild(type); } } + // transform all constants. + for (auto c : t->getConstants()) { + Rooted constant = transformConstant(typesystem, t, c, P); + typesystem->addChild(constant); + } // return the transformed Ontology. return typesystem; } diff --git a/testdata/integration/ontology_serialization/typesystem.in.osml b/testdata/integration/ontology_serialization/typesystem.in.osml index 3adbe2e..1f0ccd5 100644 --- a/testdata/integration/ontology_serialization/typesystem.in.osml +++ b/testdata/integration/ontology_serialization/typesystem.in.osml @@ -1,8 +1,10 @@ \document \typesystem#bla - \struct#myStruct + \struct#myParentStruct \field#i[type=int,default=1] + \struct#myStruct[parent=myParentStruct] \field#s[type=string,default=""] + \constant#C[type=myStruct, value=[4,"bla"]] \enum#myEnum \entry STRICT \entry FUZZY @@ -13,4 +15,4 @@ \attribute#type[type=myEnum] \primitive[type=myStruct] -\a[type=STRICT] [4,"bla"] +\a[type=STRICT] C diff --git a/testdata/integration/ontology_serialization/typesystem.out.osxml b/testdata/integration/ontology_serialization/typesystem.out.osxml index e3652e3..a822191 100644 --- a/testdata/integration/ontology_serialization/typesystem.out.osxml +++ b/testdata/integration/ontology_serialization/typesystem.out.osxml @@ -12,8 +12,10 @@ - + + + @@ -21,6 +23,10 @@ PROBABILISTIC STRICT + [ "i"= 4, -- cgit v1.2.3