package rcsc; import ihic.FileReadWrite; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.eclipse.emf.ecore.util.EcoreUtil; import schemas.templatedefiner._goonyhwmeekedd8bbul2tg._40.AdvancedTemplate; import schemas.templatedefiner._goonyhwmeekedd8bbul2tg._40.Taml; import uml.Model; import xmi.XMI; import xmi.XMIMarshaller; import xmm.General; import xmm.Generalization; import xmm.OwnedComment; import xmm.OwnedRule; import xmm.PackagedElement; import xmm.Specification; public class RCSZCGen { static Map listOIDValueset = new HashMap(); static{ String csv = null; try{ csv = FileReadWrite.readDoc("/home/aboufahj/Téléchargements/Value Lists for RCS-C.csv"); }catch(Exception e){} String[] lines = csv.split("\n"); for (String string2 : lines) { String[] eles = string2.split(";"); if (eles.length>2){ listOIDValueset.put(eles[1], eles[0]); } } } public static void main(String[] args) throws Exception { List listValueDesc = getListValueDescFromCSVFile("/home/aboufahj/Téléchargements/Value Lists for RCS-C.csv"); XMI xmi = generateXMI(listValueDesc); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMIMarshaller.printXMI(xmi, baos); System.out.println(baos.toString()); } private static XMI generateXMI(List listValueDesc) throws Exception { XMI xmi = new XMI(); xmi.setModel(new Model()); for (ValueDesc valueDesc : listValueDesc) { if (valueDesc.getOid().equals("OID")) continue; PackagedElement pe = getPackagedElementFromTemplate(valueDesc, xmi); if (pe != null){ xmi.getModel().getPackagedElement().add(pe); } } return xmi; } private static PackagedElement getPackagedElementFromTemplate( ValueDesc valueDesc, XMI xmi) throws Exception { PackagedElement pe = new PackagedElement(); pe.setId(EcoreUtil.generateUUID()); pe.setName(valueDesc.getValueSetName().replaceAll("(\\s+|/|\\(|\\)|,|\\-)", "") + "_" + valueDesc.getPreferredName().replaceAll("(\\s+|/|\\(|\\)|,|\\-)", "")); pe.setType("uml:Class"); addConstraintRelatedToType(valueDesc, pe, xmi); addConstraintRelatedToUnit(valueDesc, pe, xmi); addConstraintRelatedToVS(valueDesc, pe, xmi); addConstraintRelatedToModifier(valueDesc, pe, xmi); if (pe.getOwnedRule().size()==0) return null; addGeneralization(pe, valueDesc); addAdvancedTemplateDesc(pe, xmi, valueDesc); return pe; } private static void addConstraintRelatedToModifier(ValueDesc valueDesc, PackagedElement pe, XMI xmi) { if (valueDesc.getValueSetModifier() == null || valueDesc.getValueSetModifier().trim().equals("")) return; OwnedRule or = new OwnedRule(); or.setId(EcoreUtil.generateUUID()); or.setName(valueDesc.getPreferredName().replaceAll("(\\s+|/|\\(|\\)|,|\\-)", "") + "_ModifierValue"); pe.getOwnedRule().add(or); or.setOwnedComment(new OwnedComment()); or.getOwnedComment().setBody(generateConstraintDescForModifier(valueDesc)); or.setSpecification(new Specification()); or.getSpecification().setType("uml:OpaqueExpression"); or.getSpecification().setId(EcoreUtil.generateUUID()); or.getSpecification().setName(or.getName()+ "_body"); or.getSpecification().setLanguage("OCL"); or.getSpecification().setBody(generateBodyFromConstraintForModifier(valueDesc)); Taml taml = new Taml(); taml.setTargetIDScheme("RCS-C"); taml.setBaseConstraint(or.getId()); taml.setIDs(getIDFromValueDesc(valueDesc)); xmi.getTaml().add(taml); } private static String generateConstraintDescForModifier(ValueDesc valueDesc) { String res = "The modifier element of the statement described by the code " + valueDesc.getCode() + "(" + valueDesc.getPreferredName() + ") " + "SHALL have a code element with the attribute @code='" + valueDesc.getValueSetModifier().trim().replace("#", "' or @code='") + "'"; return res; } private static String generateBodyFromConstraintForModifier( ValueDesc valueDesc) { String res = "self.entryRelationship->forAll(entrr : POCDMT000040EntryRelationship | \n" + " (not entrr.observation.oclIsUndefined()) and\n" + " (not entrr.observation.code.oclIsUndefined()) and\n" + " (not entrr.observation.code.code.oclIsUndefined()) and\n" + " entrr.observation.code.code='" + valueDesc.getValueSetModifier().trim().replace("#", "' or entrr.observation.code.code='") + "'\n" + ")"; return res; } private static void addConstraintRelatedToVS(ValueDesc valueDesc, PackagedElement pe, XMI xmi) throws Exception { if (valueDesc.getUnitOfMesureOrValueSetName() == null) return; if (valueDesc.getUnitOfMesureOrValueSetName().contains("VS:")){ if (!valueDesc.getValueDataType().toUpperCase().equals("CD")){ System.out.println(valueDesc); throw new Exception(); } OwnedRule or = new OwnedRule(); or.setId(EcoreUtil.generateUUID()); or.setName(valueDesc.getPreferredName().replaceAll("(\\s+|/|\\(|\\)|,|\\-)", "") + "_VSValue"); pe.getOwnedRule().add(or); or.setOwnedComment(new OwnedComment()); or.getOwnedComment().setBody("The observation described by the code " + valueDesc.getCode() + "(" + valueDesc.getPreferredName() + ") SHALL have a value element with type CD coming from the valueSet '" + valueDesc.getUnitOfMesureOrValueSetName().split(":")[1].trim() + "' (" + listOIDValueset.get(valueDesc.getUnitOfMesureOrValueSetName().split(":")[1].trim()) + ")" + "\n" + generateDescriptionOfTable(valueDesc)); if (listOIDValueset.get(valueDesc.getUnitOfMesureOrValueSetName().split(":")[1].trim()) == null){ System.err.println(valueDesc.getUnitOfMesureOrValueSetName()); } or.setSpecification(new Specification()); or.getSpecification().setType("uml:OpaqueExpression"); or.getSpecification().setId(EcoreUtil.generateUUID()); or.getSpecification().setName(or.getName()+ "_body"); or.getSpecification().setLanguage("OCL"); or.getSpecification().setBody(generateBodyFromConstraintForVS(valueDesc)); Taml taml = new Taml(); taml.setTargetIDScheme("RCS-C"); taml.setBaseConstraint(or.getId()); taml.setIDs(getIDFromValueDesc(valueDesc)); xmi.getTaml().add(taml); } } private static String generateBodyFromConstraintForVS(ValueDesc valueDesc) { String res = "self.value->forAll(val : ANY | (not val.oclIsKindOf(CD)) or " + "val.oclAsType(CD).code.oclIsUndefined() or val.oclAsType(CD).matchesValueSet('" + listOIDValueset.get(valueDesc.getUnitOfMesureOrValueSetName().split(":")[1].trim()) + "', val.oclAsType(CD).code, val.oclAsType(CD).codeSystem, null, null))"; return res; } private static void addConstraintRelatedToUnit(ValueDesc valueDesc, PackagedElement pe, XMI xmi) throws Exception { if (valueDesc.getUnitOfMesureOrValueSetName() == null || valueDesc.getUnitOfMesureOrValueSetName().trim().equals("")) return; if (valueDesc.getUnitOfMesureOrValueSetName().contains("UOM:")){ if (!valueDesc.getValueDataType().equals("PQ")){ throw new Exception(); } OwnedRule or = new OwnedRule(); or.setId(EcoreUtil.generateUUID()); or.setName(valueDesc.getPreferredName().replaceAll("(\\s+|/|\\(|\\)|,|\\-)", "") + "_UnitValue"); pe.getOwnedRule().add(or); or.setOwnedComment(new OwnedComment()); or.getOwnedComment().setBody("The observation described by the code " + valueDesc.getCode() + "(" + valueDesc.getPreferredName() + ") SHALL have a value element with type PQ and the unit of value SHALL be '" + valueDesc.getUnitOfMesureOrValueSetName().split(":")[1].trim() + "'" + "\n" + generateDescriptionOfTable(valueDesc)); or.setSpecification(new Specification()); or.getSpecification().setType("uml:OpaqueExpression"); or.getSpecification().setId(EcoreUtil.generateUUID()); or.getSpecification().setName(or.getName()+ "_body"); or.getSpecification().setLanguage("OCL"); or.getSpecification().setBody(generateBodyFromConstraintForUnit(valueDesc)); Taml taml = new Taml(); taml.setTargetIDScheme("RCS-C"); taml.setBaseConstraint(or.getId()); taml.setIDs(getIDFromValueDesc(valueDesc)); xmi.getTaml().add(taml); } } private static String generateBodyFromConstraintForUnit(ValueDesc valueDesc) { String res = "self.value->forAll(val : ANY | (not val.oclIsKindOf(PQ)) or " + "( (not val.oclAsType(PQ).unit.oclIsUndefined()) and val.oclAsType(PQ).unit='" + valueDesc.getUnitOfMesureOrValueSetName().split(":")[1].trim() + "' )" + ")"; return res; } private static void addAdvancedTemplateDesc(PackagedElement pe, XMI xmi, ValueDesc valueDesc) { AdvancedTemplate at = new AdvancedTemplate(); at.setBaseClass(pe.getId()); at.setConstraint(createConstraintForAT(valueDesc)); xmi.getAdvancedTemplate().add(at); } private static String createConstraintForAT(ValueDesc valueDesc) { String res = ""; res = "(not self.code.oclIsUndefined()) and (not self.code.code.oclIsUndefined()) and " + "self.code.code='" + valueDesc.getCode() + "'"; return res; } private static void addConstraintRelatedToType(ValueDesc valueDesc, PackagedElement pe, XMI xmi) { if (valueDesc.getValueDataType() == null || valueDesc.getValueDataType().trim().equals("")) return; OwnedRule or = new OwnedRule(); or.setId(EcoreUtil.generateUUID()); or.setName(valueDesc.getPreferredName().replaceAll("(\\s+|/|\\(|\\)|,|\\-)", "") + "_ValueType"); pe.getOwnedRule().add(or); or.setOwnedComment(new OwnedComment()); or.getOwnedComment().setBody("The observation described by the code " + valueDesc.getCode() + "(" + valueDesc.getPreferredName() + ") SHALL have a value element with type : " + valueDesc.getValueDataType() + "\n" + generateDescriptionOfTable(valueDesc)); or.setSpecification(new Specification()); or.getSpecification().setType("uml:OpaqueExpression"); or.getSpecification().setId(EcoreUtil.generateUUID()); or.getSpecification().setName(or.getName()+ "_body"); or.getSpecification().setLanguage("OCL"); or.getSpecification().setBody(generateBodyFromConstraintForValueType(valueDesc)); Taml taml = new Taml(); taml.setTargetIDScheme("RCS-C"); taml.setBaseConstraint(or.getId()); taml.setIDs(getIDFromValueDesc(valueDesc)); xmi.getTaml().add(taml); } private static String getIDFromValueDesc(ValueDesc valueDesc) { String paragraph = TableDescEnum.getParagraphNumFromValueSetName(valueDesc.getValueSetName().trim()); if (paragraph == null){ System.err.println(valueDesc); return null; } return "RCS-VAL" + paragraph.split("\\.")[paragraph.split("\\.").length-1]; } private static String generateDescriptionOfTable(ValueDesc valueDesc){ String res = " coming from the table of description of the value set : " + valueDesc.getValueSetName() + "(" + valueDesc.getOid() + ") : table number is " + TableDescEnum.getTableFromValueSetName(valueDesc.getValueSetName()); if (TableDescEnum.getTableFromValueSetName(valueDesc.getValueSetName()) == null){ System.err.println(valueDesc.getValueSetName()); } return res; } private static String generateBodyFromConstraintForValueType( ValueDesc valueDesc) { return "self.value->forAll(val : ANY | val.oclIsKindOf(" + valueDesc.getValueDataType() + "))"; } private static void addGeneralization(PackagedElement pe, ValueDesc valueDesc) { pe.setGeneralization(new Generalization()); pe.getGeneralization().setGeneral(new General()); pe.getGeneralization().getGeneral().setType("uml:Class"); String gen = TableDescEnum.getTypeFromValueSetName(valueDesc.getValueSetName().trim()); if (gen == null){ System.out.println("gen null = " + valueDesc.getValueSetName()); } String baba = null; if (gen.equals("observation")){ baba = "_DAKEwq70EeGxJei_o6JmIA"; } else if(gen.equals("procedure")){ baba = "_DAKrSa70EeGxJei_o6JmIA"; } else{ System.out.println("big probelem : " + gen); } pe.getGeneralization().getGeneral().setHref("../../cda-model/models/cda.uml#" + baba); } private static List getListValueDescFromCSVFile(String string) throws IOException { List res = new ArrayList(); String csv = FileReadWrite.readDoc(string); String[] lines = csv.split("\n"); for (String string2 : lines) { String[] eles = string2.split(";"); if (hasInterestingElements(eles)){ ValueDesc vd = new ValueDesc(); vd.setOid(eles[0]); vd.setValueSetName(eles[1]); vd.setCode(eles[2]); vd.setCodeSystem(eles[3]); vd.setPreferredName(eles[4]); vd.setValueDataType(eles.length>5?eles[5]:null); vd.setUnitOfMesureOrValueSetName(eles.length>6?eles[6]:null); vd.setValueSetModifier(eles.length>7?eles[7]:null); res.add(vd); } } return res; } private static boolean hasInterestingElements(String[] eles){ try{ return ( (eles[5] != null && !eles[5].equals("")) || (eles[6] != null && !eles[6].equals("")) || (eles[7] != null && !eles[7].equals("")) ) && ( (eles[0] != null && !eles[0].equals("")) ); } catch(Exception e){ return false; } } }