@@ -1167,6 +1167,45 @@ def get_direct_dor_list(obj: Any) -> List[Any]:
11671167 return search_attribute_matching_type (obj , "DataObjectreference" )
11681168
11691169
1170+ def get_obj_usable_class (o : Any ) -> Optional [type ]:
1171+ """Used only for resqml201 that has classes Obj_TriangulatedSetRepresentation and TriangulatedSetRepresentation for example.
1172+ This function will return Obj_TriangulatedSetRepresentation class
1173+ """
1174+
1175+ if o is not None :
1176+ if not isinstance (o , type ):
1177+ o = type (o )
1178+ if isinstance (o , type ):
1179+ if o .__bases__ is not None :
1180+ for bc in o .__bases__ :
1181+ # print(bc)
1182+ if bc .__name__ .lower () == f"obj{ get_obj_type (o ).lower ()} " :
1183+ return bc
1184+ return o if isinstance (o , type ) else None
1185+ return None
1186+
1187+
1188+ def as_obj_prefixed_class_if_possible (o : Any ) -> Any :
1189+ """Used only for resqml201 that has classes Obj_TriangulatedSetRepresentation and TriangulatedSetRepresentation for example.
1190+ This function will return an instance of Obj_TriangulatedSetRepresentation if possible
1191+ """
1192+ if o is not None :
1193+ if not isinstance (o , type ):
1194+ o_type = type (o )
1195+ if o_type .__bases__ is not None :
1196+ for bc in o_type .__bases__ :
1197+ # print(bc)
1198+ if bc .__name__ .lower () == f"obj{ get_obj_type (o_type ).lower ()} " :
1199+ try :
1200+ return bc (** o .__dict__ )
1201+ except Exception as e :
1202+ logging .error (f"Failed to convert { o } to { bc } " )
1203+ logging .error (e )
1204+ return o
1205+ return o
1206+ return None
1207+
1208+
11701209def get_data_object_type (cls : Union [type , Any ], print_dev_version = True , nb_max_version_digits = 2 ):
11711210 return get_class_pkg (cls ) + "." + get_class_pkg_version (cls , print_dev_version , nb_max_version_digits )
11721211
@@ -1515,3 +1554,81 @@ def _random_value_from_class(
15151554
15161555 logging .error (f"@_random_value_from_class Not supported object type generation { cls } " )
15171556 return None
1557+
1558+
1559+ if __name__ == "__main__" :
1560+
1561+ from energyml .eml .v2_3 .commonv2 import *
1562+ from energyml .eml .v2_0 .commonv2 import Citation as Cit201
1563+ from energyml .resqml .v2_0_1 .resqmlv2 import TriangulatedSetRepresentation as Tr20 , ObjTriangulatedSetRepresentation
1564+ from energyml .resqml .v2_2 .resqmlv2 import (
1565+ TriangulatedSetRepresentation ,
1566+ FaultInterpretation ,
1567+ )
1568+ from .serialization import *
1569+
1570+ fi_cit = Citation (
1571+ title = "An interpretation" ,
1572+ originator = "Valentin" ,
1573+ creation = epoch_to_date (epoch ()),
1574+ editor = "test" ,
1575+ format = "Geosiris" ,
1576+ last_update = epoch_to_date (epoch ()),
1577+ )
1578+
1579+ fi = FaultInterpretation (
1580+ citation = fi_cit ,
1581+ uuid = gen_uuid (),
1582+ object_version = "0" ,
1583+ )
1584+
1585+ tr_cit = Citation (
1586+ title = "--" ,
1587+ # title="test title",
1588+ originator = "Valentin" ,
1589+ creation = epoch_to_date (epoch ()),
1590+ editor = "test" ,
1591+ format = "Geosiris" ,
1592+ last_update = epoch_to_date (epoch ()),
1593+ )
1594+
1595+ tr_cit201 = Cit201 (
1596+ title = "--" ,
1597+ # title="test title",
1598+ originator = "Valentin" ,
1599+ # creation=str(epoch_to_date(epoch()))
1600+ editor = "test" ,
1601+ format = "Geosiris" ,
1602+ # last_update=str(epoch_to_date(epoch())),
1603+ )
1604+ dor = DataObjectReference (
1605+ uuid = fi .uuid ,
1606+ title = "a DOR title" ,
1607+ object_version = "0" ,
1608+ qualified_type = "a wrong qualified type" ,
1609+ )
1610+ tr = TriangulatedSetRepresentation (
1611+ citation = tr_cit ,
1612+ uuid = gen_uuid (),
1613+ represented_object = dor ,
1614+ )
1615+
1616+ tr201 = Tr20 (
1617+ citation = tr_cit201 ,
1618+ uuid = gen_uuid (),
1619+ )
1620+ tr201_bis = ObjTriangulatedSetRepresentation (
1621+ citation = tr_cit201 ,
1622+ uuid = gen_uuid (),
1623+ )
1624+ # print(get_obj_uri(tr201, "coucou"))
1625+
1626+ print (get_obj_usable_class (tr ))
1627+ print (get_obj_usable_class (tr201 ))
1628+
1629+ print (serialize_xml (tr201_bis ))
1630+ print (serialize_xml (tr201 ))
1631+ print (serialize_json (tr201 ))
1632+ print (serialize_xml (as_obj_prefixed_class_if_possible (tr201 )))
1633+ print ("--> " , serialize_json (tr ))
1634+ # print(serialize_xml((get_usable_class(tr201))(tr201)))
0 commit comments