Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to explicitly create SimpleType classes #692

Closed
RootMeanSqr opened this issue Aug 1, 2022 · 7 comments
Closed

Option to explicitly create SimpleType classes #692

RootMeanSqr opened this issue Aug 1, 2022 · 7 comments

Comments

@RootMeanSqr
Copy link

I understand the desire to minimize "unnecessary" class types, but in some cases, there is reason to create all SimpleType classes from an XSD. For example, if classes are created for SimpleTypes, then they can be used as classes in the rest of the code. For example, I have a SimpleType called DistanceType which for my use is defined with units of Meters, and is non-negative. I want a class for this so that I can use this type anywhere else in the schema that uses DistanceType.

Here is an example:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="BagOfBlocks" type="BagOfBlocksType">
		<xs:annotation>
			<xs:documentation>Comment describing your root element</xs:documentation>
		</xs:annotation>
	</xs:element>
	<xs:complexType name="BagOfBlocksType">
		<xs:sequence>
			<xs:element name="Block" type="BlockType" maxOccurs="unbounded"/>
		</xs:sequence>
	</xs:complexType>
	<xs:complexType name="BlockType">
		<xs:choice>
			<xs:element name="Cylinder" type="CylinderType"/>
			<xs:element name="Box" type="BoxType"/>
		</xs:choice>
	</xs:complexType>
	<xs:complexType name="BoxType">
		<xs:sequence>
			<xs:element name="Length" type="DistanceType"/>
			<xs:element name="Width" type="DistanceType"/>
			<xs:element name="Height" type="DistanceType"/>
		</xs:sequence>
	</xs:complexType>
	<xs:complexType name="CylinderType">
		<xs:sequence>
			<xs:element name="Diameter" type="DistanceType"/>
			<xs:element name="Height" type="DistanceType"/>
		</xs:sequence>
	</xs:complexType>
	<xs:simpleType name="DistanceType">
		<xs:annotation>
			<xs:documentation>Units of Meters</xs:documentation>
		</xs:annotation>
		<xs:restriction base="xs:double">
			<xs:minInclusive value="0"/>
		</xs:restriction>
	</xs:simpleType>
</xs:schema>

I would like and option to explicitly create the DistanceType class.

@tefra
Copy link
Owner

tefra commented Aug 8, 2022

This will be tricky to implement, throughtout the process there is a naive approach of keep what it's really needed discard or flatten everything else.

I wouldn't discard any contribution on this one, based on some sort of config of course.

@tefra
Copy link
Owner

tefra commented May 14, 2023

closing this one

@tefra tefra closed this as completed May 14, 2023
@untereiner
Copy link

untereiner commented Jan 9, 2024

Hello @tefra, is it possible to reopen this one ? I am very interested to have simpleTypes as types. More precisely I have simpleTypes that I want to transform as numpy or specific python types.

For example, an excerpt of my schema:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<xsd:annotation>
		<xsd:documentation xml:lang="en">GEOSX Input Schema</xsd:documentation>
	</xsd:annotation>
	<xsd:simpleType name="R1Tensor">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value=".*[\[\]`$].*|\s*\{\s*([+-]?[\d]*([\d]\.?|\.[\d])[\d]*([eE][-+]?[\d]+|\s*)\s*,\s*){2}[+-]?[\d]*([\d]\.?|\.[\d])[\d]*([eE][-+]?[\d]+|\s*)\s*\}\s*" />
		</xsd:restriction>
	</xsd:simpleType>
	<xsd:simpleType name="R1Tensor32">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value=".*[\[\]`$].*|\s*\{\s*([+-]?[\d]*([\d]\.?|\.[\d])[\d]*([eE][-+]?[\d]+|\s*)\s*,\s*){2}[+-]?[\d]*([\d]\.?|\.[\d])[\d]*([eE][-+]?[\d]+|\s*)\s*\}\s*" />
		</xsd:restriction>
	</xsd:simpleType>
	<xsd:simpleType name="R2SymTensor">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value=".*[\[\]`$].*|\s*\{\s*([+-]?[\d]*([\d]\.?|\.[\d])[\d]*([eE][-+]?[\d]+|\s*)\s*,\s*){5}[+-]?[\d]*([\d]\.?|\.[\d])[\d]*([eE][-+]?[\d]+|\s*)\s*\}\s*" />
		</xsd:restriction>
	</xsd:simpleType>
	<xsd:simpleType name="geos_dataRepository_PlotLevel">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value=".*[\[\]`$].*|[+-]?[\d]+" />
		</xsd:restriction>
	</xsd:simpleType>
	<xsd:simpleType name="globalIndex">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value=".*[\[\]`$].*|[+-]?[\d]+" />
		</xsd:restriction>
	</xsd:simpleType>
	<xsd:simpleType name="globalIndex_array">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value=".*[\[\]`$].*|\s*\{\s*(([+-]?[\d]+\s*,\s*)*[+-]?[\d]+\s*)?\}\s*" />
		</xsd:restriction>
	</xsd:simpleType>
....
	<xsd:complexType name="ConstantPermeabilityType">
		<!--permeabilityComponents => xx, yy and zz components of a diagonal permeability tensor.-->
		<xsd:attribute name="permeabilityComponents" type="R1Tensor" use="required" />
		<!--name => A name is required for any non-unique nodes-->
		<xsd:attribute name="name" type="groupName" use="required" />
	</xsd:complexType>
....
	<xsd:complexType name="InternalWellType">
		<xsd:choice minOccurs="0" maxOccurs="unbounded">
			<xsd:element name="Perforation" type="PerforationType" />
		</xsd:choice>
		<!--logLevel => Log level-->
		<xsd:attribute name="logLevel" type="integer" default="0" />
		<!--minElementLength => Minimum length of a well element, computed as (segment length / number of elements per segment ) [m]-->
		<xsd:attribute name="minElementLength" type="real64" default="0.001" />
		<!--minSegmentLength => Minimum length of a well segment [m]-->
		<xsd:attribute name="minSegmentLength" type="real64" default="0.01" />
		<!--numElementsPerSegment => Number of well elements per polyline segment-->
		<xsd:attribute name="numElementsPerSegment" type="integer" use="required" />
		<!--polylineNodeCoords => Physical coordinates of the well polyline nodes-->
		<xsd:attribute name="polylineNodeCoords" type="real64_array2d" use="required" />
		<!--polylineSegmentConn => Connectivity of the polyline segments-->
		<xsd:attribute name="polylineSegmentConn" type="globalIndex_array2d" use="required" />
		<!--radius => Radius of the well [m]-->
		<xsd:attribute name="radius" type="real64" use="required" />
		<!--wellControlsName => Name of the set of constraints associated with this well-->
		<xsd:attribute name="wellControlsName" type="groupNameRef" use="required" />
		<!--wellRegionName => Name of the well element region-->
		<xsd:attribute name="wellRegionName" type="groupNameRef" use="required" />
		<!--name => A name is required for any non-unique nodes-->
		<xsd:attribute name="name" type="groupName" use="required" />
	</xsd:complexType>
....

For example I know that "globalIndex" is an integer or "R1Tensor" and "globalIndex_array" arenumpy 1d arrays.

In an xml I have:

    <ConstantPermeability
      name="rockPermeability"
      permeabilityComponents="{ 6.7593e-14, 6.7593e-14, 6.7593e-15 }"/>

I don't know what can be the most effective.. I imagine I need to write somewhere how to deserizlize the string into the class I want (numpy or array of strings or whatever) ?

@tefra tefra reopened this Jan 9, 2024
@untereiner
Copy link

untereiner commented Jan 16, 2024

I have a minimal reproducer: test_xsdata.zip
I added by myself the simpleTypes integer and real64. I replaced them in the generated classes and tried to deserialize them from a xml file. But it doesn't work. @tefra Do you have any idea how I could fix it ?

@untereiner
Copy link

untereiner commented Feb 2, 2024

I reworked the example a bit. If I replace np.float64 and np.int32 by python float and int types the example works. I don't understand why the first solution doesn't work.

@tefra I managed to get the numpy types work. I didn't know that dataclass is only for Elements and not for Attributes.
But the initial questions remains. I can I supply to xsdata the simpleTypes that are not generated.
Is it possible to provide a list of simpleTypes to keep in the generated classes and where to find a suitable implementation for them ?

@tefra
Copy link
Owner

tefra commented Feb 3, 2024

We need to extend the filter strategies of the configuration to allow people to define a custom list classes they want to keep to matter what.

@tefra
Copy link
Owner

tefra commented May 7, 2024

I have zero interest on this one, it requires a lot of work and goes against the xsdata architecture to simplify the generated types. I can see the argument that we are missing validations of restrictions but having standalone classes for primitive types is just too much.

Although if someone manages to implement this one as a feature behind a config, I will accept it as contribution.

@tefra tefra closed this as completed May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants