public static string getID(EObject eObject) { EClass eClass = eObject.eClass(); EAttribute eIDAttribute = eClass.eIDAttribute; return(eIDAttribute == null || !eObject.eIsSet(eIDAttribute) ? null : convertToString( eIDAttribute.eAttributeType, eObject.eGet(eIDAttribute))); }
public static string getIdentification(EObject eObject) { StringBuilder result = new StringBuilder(eObject.eClass().name); EClass eClass = eObject.eClass(); if (eClass.instanceClassName == null) { result.Append('/'); result.Append(eClass.ePackage.nsURI); result.Append('#'); result.Append(eClass.name); } result.Append('@'); result.Append(eObject.GetHashCode().ToString("X")); result.Append('{'); //TODO result.Append(getURI(eObject)); result.Append('}'); return(result.ToString()); }
public bool validate_EveryDataValueConforms(EObject eObject, DiagnosticChain diagnostics, Dictionary <object, object> context) { bool result = true; foreach (EAttribute eAttribute in eObject.eClass().eAllAttributes) { result &= validate_DataValueConforms(eObject, eAttribute, diagnostics, context); if (!result && diagnostics == null) { return(false); } } return(result); }
public bool validate_EveryMultiplicityConforms(EObject eObject, DiagnosticChain diagnostics, Dictionary <object, object> context) { bool result = true; EClass eClass = eObject.eClass(); for (int i = 0, size = eClass.getFeatureCount(); i < size; ++i) { result &= validate_MultiplicityConforms(eObject, eClass.getEStructuralFeature(i), diagnostics, context); if (!result && diagnostics == null) { return(false); } } return(result); }
private EObject resolveRecurr(Queue <String> path, EObject current) { if (path.Count == 0) { return(current); } String segment = path.Dequeue(); foreach (EStructuralFeature feature in current.eClass().eAllContainments) { if (segment.StartsWith("@")) { var startIndex = 1; var endIndex = segment.LastIndexOf("."); var length = endIndex - startIndex; var featurename = segment.Substring(startIndex, length); if (feature.name == featurename) { var length2 = segment.Length - endIndex - 1; var array_index = Int32.Parse(segment.Substring(endIndex + 1, length2)); var enumerable = current.eGet(feature) as IEnumerable <EObject>; var list = enumerable.ToList(); //TODO list of primitives/objects? var item = list[array_index]; return(resolveRecurr(path, item)); } } else if (feature.name == segment) { return(resolveRecurr(path, current.eGet(feature) as EObject)); } } return(null); }
public bool validate_EveryBidirectionalReferenceIsPaired(EObject eObject, DiagnosticChain diagnostics, Dictionary <object, object> context) { bool result = true; foreach (EReference eReference in eObject.eClass().eAllReferences) { if (eReference.resolveProxies) { EReference eOpposite = eReference.eOpposite; if (eOpposite != null) { result &= validate_BidirectionalReferenceIsPaired(eObject, eReference, eOpposite, diagnostics, context); if (!result && diagnostics == null) { return(false); } } } } return(result); }
public bool validate_EveryKeyUnique(EObject eObject, DiagnosticChain diagnostics, Dictionary <object, object> context) { bool result = true; EClass eClass = eObject.eClass(); for (int i = 0, size = eClass.getFeatureCount(); i < size; ++i) { EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(i); if (eStructuralFeature is EReference) { EReference eReference = (EReference)eStructuralFeature; if (eReference.many && !eReference.eKeys.isEmpty()) { result &= validate_KeyUnique(eObject, eReference, diagnostics, context); if (!result && diagnostics == null) { return(false); } } } } return(result); }
public T doSwitch(EObject eObject) { return(doSwitch(eObject.eClass(), eObject)); }
public String serialize(EObject eobject) { var eClass = eobject.eClass(); var references = validEAllReferences(eobject, eClass.eAllReferences); var attributes = validEAllAttributes(eobject, eClass.eAllAttributes); var sb = new StringBuilder(); sb.Append("{"); { sb.Append($"\"{eClass.name}\":"); sb.Append("{"); { var i3 = 0; foreach (EReference ereference in references) { if (ereference.containment) { if (ereference.many) { sb.Append($"\"{ereference.name}\":"); var items = eobject.eGet(ereference) as Collection <EObject>; sb.Append("["); { var i = 0; foreach (EObject item in items) { sb.Append(serialize(item)); if (i < items.size() - 1) { sb.Append(", "); } i++; } } sb.Append("]"); } else { sb.Append($"\"{ereference.name}\": {serialize(eobject.eGet(ereference) as EObject)}"); } } else { if (ereference.many) { var items = eobject.eGet(ereference) as Collection <EObject>; sb.Append("["); { var i = 0; foreach (EObject item in items) { sb.Append(item.GetHashCode()); if (i < items.size() - 1) { sb.Append(", "); } i++; } } sb.Append("]"); } else { sb.Append($"\"{ereference.name}\": \"{eobject.eGet(ereference).GetHashCode()}\""); } } if (i3 < attributes.size() - 1) { sb.Append(", "); } i3++; } if (!references.isEmpty() && !attributes.isEmpty()) { sb.Append(", "); } var i2 = 0; foreach (var attribute in attributes) { sb.Append($"\"{attribute.name}\": \"{eobject.eGet(attribute).ToString()}\""); if (i2 < attributes.size() - 1) { sb.Append(", "); } i2++; } } sb.Append("}"); } sb.Append("}"); return(sb.ToString()); }
private void saveRecurr(EObject eobject, EReference relation) { if (eobject == null) { return; } var xmlAttributes = new Dictionary <string, string>(); //xsi:type="ecore:EAttribute" var eclass = eobject.eClass(); var epackag = eclass.ePackage; if (epackag == null) { return; } var prefix = epackag.nsPrefix; xmlAttributes["xsi:type"] = eobject.eClass().ePackage.nsPrefix + ":" + eobject.eClass().name; foreach (EStructuralFeature feature in eobject.eClass().eAllStructuralFeatures) { if (feature is EReference) { var erference = feature as EReference; if (erference.many) { //TODO } else { var value = eobject.eGet(erference) as EObject; if (value != null) { xmlAttributes[erference.name] = getFragment(value); } } if (erference.containment) { var child = eobject.eGet(erference); if (child != null) { if (erference.many) { var list = (IList)child; var list2 = list.Cast <EObject>(); foreach (EObject c in list) { saveRecurr(c, erference); } } else { saveRecurr(child as EObject, erference);//TODO is casting safe here? } } } } else if (feature is EAttribute) { var eattribute = feature as EAttribute; if (!eattribute.transient && !eattribute.derived && eattribute.defaultValue != eobject.eGet(eattribute)) { //TODO many xmlAttributes[eattribute.name] = eobject.eGet(eattribute).ToString(); } } } if (relation != null) { var sb = new StringBuilder(); sb.Append($"<{relation.name}"); foreach (String key in xmlAttributes.Keys) { sb.Append($"{key}=\"{xmlAttributes[key]}\""); } sb.Append($">"); //TODO childs sb.Append($"</{relation.name}>"); Console.WriteLine(sb.ToString()); } }
private string getFragment(EObject eobject) { EAttribute idfeature = eobject.eClass().eIDAttribute; return($"#//{eobject.eGet(idfeature)}"); }
private void addEStructuralFeatures(EObject eobject, XmlNode node) { foreach (XmlAttribute attribute in node.Attributes) { var name = attribute.Name; var estructuralfeature = eobject.eClass().getEStructuralFeature(name); if (estructuralfeature is EAttribute && (estructuralfeature as EAttribute).iD) { var id = attribute.InnerText; registry[id] = eobject; } if (estructuralfeature is EAttribute) { if (!estructuralfeature.many) { var etype = estructuralfeature.eType; var value = attribute.InnerText; if (etype?.ePackage?.nsURI == "http://www.eclipse.org/emf/2002/Ecore") { if (etype.name == "EBigDecimal") { throw new NotImplementedException(); } else if (etype.name == "EBigInteger") { throw new NotImplementedException(); } else if (etype.name == "EBoolean") { eobject.eSet(estructuralfeature, value == "true" ? true : false); } else if (etype.name == "EBooleanObject") { throw new NotImplementedException(); } else if (etype.name == "EByteArray") { throw new NotImplementedException(); } else if (etype.name == "EByteObject") { throw new NotImplementedException(); } else if (etype.name == "EChar") { eobject.eSet(estructuralfeature, value[0]); } else if (etype.name == "ECharacterObject") { throw new NotImplementedException(); } else if (etype.name == "EDateEDiagnosticChain") { throw new NotImplementedException(); } else if (etype.name == "EDiagnosticChain") { throw new NotImplementedException(); } else if (etype.name == "EDouble") { eobject.eSet(estructuralfeature, Double.Parse(value)); } else if (etype.name == "EDoubleObject") { throw new NotImplementedException(); } //EEList //EEnumerator //EFeatureMap //EFeatureMapEntry else if (etype.name == "EFloat") { eobject.eSet(estructuralfeature, Single.Parse(value)); } else if (etype.name == "EFloatObject") { throw new NotImplementedException(); } else if (etype.name == "EInt") { eobject.eSet(estructuralfeature, Int32.Parse(value)); } else if (etype.name == "EIntegerObject") { throw new NotImplementedException(); } //EJavaClass //EJavaObject else if (etype.name == "ELong") { eobject.eSet(estructuralfeature, Int64.Parse(value)); } else if (etype.name == "ELongObject") { throw new NotImplementedException(); } //EMap //EResource //EResourceSet else if (etype.name == "EShort") { eobject.eSet(estructuralfeature, Int16.Parse(value)); } else if (etype.name == "EShortObject") { throw new NotImplementedException(); } else if (etype.name == "EString") { eobject.eSet(estructuralfeature, value); } //ETreeIterator //EInvocationTargetException } else if (etype is EDataType) { var literalvalue = this.factory.createFromString(etype as EDataType, value); eobject.eSet(estructuralfeature, literalvalue); } } else if (estructuralfeature.many) { var y = 3; } else { var x = 2; } } else if (estructuralfeature is EReference) { this.resolve(eobject, estructuralfeature, attribute.InnerText); } } foreach (XmlNode child in node.ChildNodes) { var containment_name = child.Name; var containment = eobject.eClass().getEStructuralFeature(containment_name); if (containment is EReference) { var containment_er = containment as EReference; if (containment_er.containment) { var classifierId2 = containment.eType.name; if (child.Attributes["xsi:type"] != null) { //use xsi:type to declare a more specific type that is declared in EType of the EReference classifierId2 = child.Attributes["xsi:type"]?.InnerText?.Split(':')[1];//TODO there might be more elegant ways } var eclassifier2 = epackage.getEClassifier(classifierId2); if (eclassifier2 is EClass) { if (containment_er.eType.name == "EStringToStringMapEntry") { //new EcoreEMap<string, string> x = new EcoreEMap<string, string>(containment_er.eType as EClass, containment_er.eType.instanceClass, eobject as InternalEObject, containment_er.getFeatureID()); var key = child.Attributes["key"].InnerText; var value = child.Attributes["value"].InnerText; dynamic bla = eobject; //FIXME why is eGet(containment_er) not working? dynamic map = bla.eGet(containment_er); //var map = bla.eGet(containment_er.getFeatureID(), false, false); //var ecoremap = map as EcoreEMap<string, string>; //https://stackoverflow.com/questions/19283349/c-sharp-generics-wildcards map.put(key, value); } else { var eclass2 = eclassifier2 as EClass; var eobject2 = factory.create(eclass2); if (containment_er.many) { addEStructuralFeatures(eobject2, child); var list = (eobject.eGet(containment_er) as IEnumerable <EObject>).ToList(); list.Add(eobject2); eobject.eSet(containment_er, list); } else { addEStructuralFeatures(eobject2, child); eobject.eSet(containment_er, eobject2); } } } } else { //TODO error } } else { //TODO error } } }
public bool validate_EveryReferenceIsContained(EObject eObject, DiagnosticChain diagnostics, Dictionary <object, object> context) { bool result = true; if (eObject.eResource() != null) { foreach (EReference reference in eObject.eClass().eAllReferences) { if (eObject.eIsSet(reference)) { EObject eCrossReferenceObject = eObject.eGet(reference) as EObject; if (eCrossReferenceObject.eResource() == null && !eCrossReferenceObject.eIsProxy() && !reference.transient) { result = false; if (diagnostics != null) { diagnostics.add (createDiagnostic (BasicDiagnostic.ERROR, DIAGNOSTIC_SOURCE, EOBJECT__EVERY_REFERENCE_IS_CONTAINED, "_UI_DanglingReference_diagnostic", new Object[] { getFeatureLabel(reference, context), getObjectLabel(eObject, context), getObjectLabel(eCrossReferenceObject, context) }, new Object[] { eObject, reference, eCrossReferenceObject }, context)); } else { break; } } } } /* * for (EContentsEList.FeatureIterator<EObject> i = (EContentsEList.FeatureIterator<EObject>)eObject.eCrossReferences().iterator(); i.hasNext();) * { * EObject eCrossReferenceObject = i.next(); * if (eCrossReferenceObject.eResource() == null && !eCrossReferenceObject.eIsProxy() && !i.feature().isTransient()) * { * result = false; * if (diagnostics != null) * { * diagnostics.add * (createDiagnostic * (BasicDiagnostic.ERROR, * DIAGNOSTIC_SOURCE, * EOBJECT__EVERY_REFERENCE_IS_CONTAINED, * "_UI_DanglingReference_diagnostic", * new Object[] * { * getFeatureLabel(i.feature(), context), * getObjectLabel(eObject, context), * getObjectLabel(eCrossReferenceObject, context) * }, * new Object[] { eObject, i.feature(), eCrossReferenceObject }, * context)); * } * else * { * break; * } * } * } */ } return(result); }
public bool validate_EveryProxyResolves(EObject eObject, DiagnosticChain diagnostics, Dictionary <object, object> context) { bool result = true; foreach (EReference reference in eObject.eClass().eAllReferences) { if (eObject.eIsSet(reference)) { EObject eCrossReferenceObject = eObject.eGet(reference) as EObject; if (eCrossReferenceObject.eIsProxy()) { result = false; if (diagnostics != null) { diagnostics.add (createDiagnostic (BasicDiagnostic.ERROR, DIAGNOSTIC_SOURCE, EOBJECT__EVERY_PROXY_RESOLVES, "_UI_UnresolvedProxy_diagnostic", new Object[] { getFeatureLabel(reference, context), getObjectLabel(eObject, context), getObjectLabel(eCrossReferenceObject, context) }, new Object[] { eObject, reference, eCrossReferenceObject }, context)); } else { break; } } } } //for (EContentsEList.FeatureIterator<EObject> i = (EContentsEList.FeatureIterator<EObject>)eObject.eCrossReferences().iterator(); i.hasNext();) /* * foreach (EObject eCrossReferenceObject in eObject.eCrossReferences()) * { * //EObject eCrossReferenceObject = i.next(); * if (eCrossReferenceObject.eIsProxy()) * { * result = false; * if (diagnostics != null) * { * diagnostics.add * (createDiagnostic * (BasicDiagnostic.ERROR, * DIAGNOSTIC_SOURCE, * EOBJECT__EVERY_PROXY_RESOLVES, * "_UI_UnresolvedProxy_diagnostic", * new Object[] * { * getFeatureLabel(i.feature(), context), * getObjectLabel(eObject, context), * getObjectLabel(eCrossReferenceObject, context) * }, * new Object[] { eObject, i.feature(), eCrossReferenceObject }, * context)); * } * else * { * break; * } * } * } */ return(result); }
public virtual bool validate(EObject eObject, DiagnosticChain diagnostics, Dictionary <object, object> context) { return(validate(eObject.eClass(), eObject, diagnostics, context)); }
private void addEStructuralFeatures(EObject eobject, XmlNode node) { foreach (XmlAttribute attribute in node.Attributes) { var name = attribute.Name; var estructuralfeature = eobject.eClass().getEStructuralFeature(name); if (estructuralfeature is EAttribute) { if (!estructuralfeature.many) { var etype = estructuralfeature.eType; var value = attribute.InnerText; if (etype?.ePackage?.nsURI == "http://www.eclipse.org/emf/2002/Ecore") { if (etype.name == "EBoolean") { eobject.eSet(estructuralfeature, value == "true" ? true : false); } else if (etype.name == "EInt") { eobject.eSet(estructuralfeature, Int32.Parse(value)); } else if (etype.name == "EString") { eobject.eSet(estructuralfeature, value); } } } else if (estructuralfeature.many) { } } else if (estructuralfeature is EReference) { resolveJobs.Add(new Tuple <EObject, EStructuralFeature, String>(eobject, estructuralfeature, attribute.InnerText)); } } foreach (XmlNode child in node.ChildNodes) { var containment_name = child.Name; var containment = eobject.eClass().getEStructuralFeature(containment_name); if (containment is EReference) { var containment_er = containment as EReference; if (containment_er.containment) { var classifierId2 = containment.eType.name; if (child.Attributes["xsi:type"] != null) { //use xsi:type to declare a more specific type that is declared in EType of the EReference classifierId2 = child.Attributes["xsi:type"]?.InnerText?.Split(':')[1];//TODO there might be more elegant ways } var eclassifier2 = epackage.getEClassifier(classifierId2); if (eclassifier2 is EClass) { if (containment_er.eType.name == "EStringToStringMapEntry") { //new EcoreEMap<string, string> x = new EcoreEMap<string, string>(containment_er.eType as EClass, containment_er.eType.instanceClass, eobject as InternalEObject, containment_er.getFeatureID()); var key = child.Attributes["key"].InnerText; var value = child.Attributes["value"].InnerText; dynamic bla = eobject; //FIXME why is eGet(containment_er) not working? dynamic map = bla.eGet(containment_er); //var map = bla.eGet(containment_er.getFeatureID(), false, false); //var ecoremap = map as EcoreEMap<string, string>; //https://stackoverflow.com/questions/19283349/c-sharp-generics-wildcards map.put(key, value); } else { var eclass2 = eclassifier2 as EClass; var eobject2 = factory.create(eclass2); if (containment_er.many) { addEStructuralFeatures(eobject2, child); var list = new List <EObject>(); list.Add(eobject2); eobject.eSet(containment_er, list); } else { addEStructuralFeatures(eobject2, child); eobject.eSet(containment_er, eobject2); } } } } else { //TODO error } } else { //TODO error } } }