public static DomainVariablesBindingsResult AnalyzeDomain(IRelationDomain domain) { DomainVariablesBindingsResult result = new DomainVariablesBindingsResult { AnalyzedDomain = domain }; result.VariablesItCanBind.Add(domain.RootVariable); if (domain.Pattern != null) { ObjectTemplateExp objectTemplateExp = (ObjectTemplateExp)domain.Pattern.TemplateExpression; AnalyzeObjectTemplateExpression(objectTemplateExp, result, domain.IsEnforceable.GetValueOrDefault()); } // if we can be self provided with a variable, we don't require it HashSet <KeyValuePair <IPropertyTemplateItem, ISet <IVariable> > > toRemove = new HashSet <KeyValuePair <IPropertyTemplateItem, ISet <IVariable> > >(); foreach (KeyValuePair <IPropertyTemplateItem, ISet <IVariable> > required in result.IPropertyTemplateItemToVariablesRequired) { required.Value.ExceptWith(result.VariablesItCanBind); if (required.Value.Count == 0) { toRemove.Add(required); } } result.IPropertyTemplateItemToVariablesRequired.RemoveRange(toRemove); return(result); }
public static ISet <IRelationDomain> GetValidSourcesOfDomain(IRelationDomain domain) { HashSet <IRelationDomain> result = new HashSet <IRelationDomain>(); result.UnionWith(((IRelation)domain.Rule).Domain.Cast <IRelationDomain>().Where(source => source != domain && IsValidSourceDomain(source))); return(result); }
public static ISet <IVariable> FindAllVariables(IRelationDomain domain, ISet <IVariable> bindedVariables) { ISet <IVariable> result = new HashSet <IVariable>(); result.Add(domain.RootVariable); bindedVariables.Add(domain.RootVariable); foreach (IPropertyTemplateItem prop in FindAllPropertyTemplates(domain)) { if (prop.Value is IVariableExp) { IVariableExp cast = (IVariableExp)prop.Value; result.Add(cast.ReferredVariable); bindedVariables.Add(cast.ReferredVariable); } else if (prop.Value is IObjectTemplateExp) { IObjectTemplateExp cast = (IObjectTemplateExp)prop.Value; result.Add(cast.BindsTo); bindedVariables.Add(cast.BindsTo); } else if (prop.Value is CSharpOpaqueExpression) { CSharpOpaqueExpression cast = (CSharpOpaqueExpression)prop.Value; result.UnionWith(cast.BindsTo); } } return(result); }
/// <summary> /// Adds the given element to the collection /// </summary> /// <param name="item">The item to add</param> public override void Add(IModelElement item) { if ((this._parent.Owner == null)) { IRelationDomain ownerCasted = item.As <IRelationDomain>(); if ((ownerCasted != null)) { this._parent.Owner = ownerCasted; return; } } if ((this._parent.ValueExp == null)) { IOclExpression valueExpCasted = item.As <IOclExpression>(); if ((valueExpCasted != null)) { this._parent.ValueExp = valueExpCasted; return; } } if ((this._parent.Variable == null)) { IVariable variableCasted = item.As <IVariable>(); if ((variableCasted != null)) { this._parent.Variable = variableCasted; return; } } }
public static bool IsValidSourceDomain(IRelationDomain domain) { ISet <IVariable> bindedVariables = new HashSet <IVariable>(); ISet <IVariable> variables = QvtModelExplorer.FindAllVariables(domain, bindedVariables); // The domain is valid if all variables are directly binded in the pattern return(variables.All(v => bindedVariables.Contains(v))); }
public static List <IPropertyTemplateItem> FindAllPropertyTemplates(IRelationDomain domain) { List <IPropertyTemplateItem> result = new List <IPropertyTemplateItem>(); foreach (IObjectTemplateExp objectTemplateExp in FindAllObjectTemplates(domain)) { result.AddRange(objectTemplateExp.Part); } return(result); }
public static List <IObjectTemplateExp> FindAllObjectTemplates(IRelationDomain domain) { IObjectTemplateExp domainTemplate = domain.Pattern?.TemplateExpression as IObjectTemplateExp; if (domainTemplate != null) { return(FindAllObjectTemplates(domainTemplate)); } return(new List <IObjectTemplateExp>()); }
/// <summary> /// Gets called when the parent model element of the current model element changes /// </summary> /// <param name="oldParent">The old parent model element</param> /// <param name="newParent">The new parent model element</param> protected override void OnParentChanged(IModelElement newParent, IModelElement oldParent) { IRelationDomain oldOwner = ModelHelper.CastAs <IRelationDomain>(oldParent); IRelationDomain newOwner = ModelHelper.CastAs <IRelationDomain>(newParent); if ((oldOwner != null)) { oldOwner.DefaultAssignment.Remove(this); } if ((newOwner != null)) { newOwner.DefaultAssignment.Add(this); } ValueChangedEventArgs e = new ValueChangedEventArgs(oldOwner, newOwner); this.OnOwnerChanged(e); this.OnPropertyChanged("Owner", e); }
/// <summary> /// Gets called when the parent model element of the current model element changes /// </summary> /// <param name="oldParent">The old parent model element</param> /// <param name="newParent">The new parent model element</param> protected override void OnParentChanged(IModelElement newParent, IModelElement oldParent) { IRelationDomain oldRelationDomain = ModelHelper.CastAs <IRelationDomain>(oldParent); IRelationDomain newRelationDomain = ModelHelper.CastAs <IRelationDomain>(newParent); if ((oldRelationDomain != null)) { oldRelationDomain.Pattern = null; } if ((newRelationDomain != null)) { newRelationDomain.Pattern = this; } ValueChangedEventArgs e = new ValueChangedEventArgs(oldRelationDomain, newRelationDomain); this.OnRelationDomainChanged(e); this.OnPropertyChanged("RelationDomain", e); }
/// <summary> /// Adds the given element to the collection /// </summary> /// <param name="item">The item to add</param> public override void Add(IModelElement item) { if ((this._parent.RelationDomain == null)) { IRelationDomain relationDomainCasted = item.As <IRelationDomain>(); if ((relationDomainCasted != null)) { this._parent.RelationDomain = relationDomainCasted; return; } } if ((this._parent.TemplateExpression == null)) { ITemplateExp templateExpressionCasted = item.As <ITemplateExp>(); if ((templateExpressionCasted != null)) { this._parent.TemplateExpression = templateExpressionCasted; return; } } }
public static string GenerateConstructTarget(IRelationDomain targetDomain, bool useMetamodelInterface) { StringBuilder stringBuilder = new StringBuilder(); ISet <string> postPonedSets = new HashSet <string>(); List <IObjectTemplateExp> objectTemplates = QvtModelExplorer.FindAllObjectTemplates(targetDomain).Where(o => !o.IsAntiTemplate()).ToList(); foreach (IObjectTemplateExp objectTemplate in objectTemplates) { IVariable variable = objectTemplate.BindsTo; stringBuilder.AppendLine("\n// Contructing " + variable.Name); foreach (IPropertyTemplateItem propertyTemplateItem in objectTemplate.Part) { string setStatement = GenerateSetValue(propertyTemplateItem, variable.Name, (IRelation)targetDomain.Rule, useMetamodelInterface); IVariableExp targetVariableValue = propertyTemplateItem.Value as IVariableExp; IObjectTemplateExp targetObjTemplateValue = objectTemplates.FirstOrDefault(o => o.BindsTo == targetVariableValue?.ReferredVariable); bool ok = targetObjTemplateValue == null || objectTemplates.IndexOf(targetObjTemplateValue) < objectTemplates.IndexOf(objectTemplate); if (ok) { stringBuilder.AppendLine(setStatement); } else { postPonedSets.Add(setStatement); } } } if (!postPonedSets.IsNullOrEmpty()) { stringBuilder.AppendLine("// Setting cycling properties"); foreach (string setStatement in postPonedSets) { stringBuilder.AppendLine(setStatement); } } return(stringBuilder.ToString()); }
public static ISet <IVariable> FindAllVariables(IRelationDomain domain) { ISet <IVariable> binded = new HashSet <IVariable>(); return(FindAllVariables(domain, binded)); }
public static object CheckDomainMethodName(IRelationDomain domain) { return("CheckDomain" + Util.ToFirstUpper(domain.RootVariable.Name)); }
//public readonly ISet<IKey> EffectiveKeys = new HashSet<IKey>(); public DomainVariablesBindingsResult GetResultOf(IRelationDomain domain) { return(DomainAnalysisResults.Single(r => r.AnalyzedDomain == domain)); }
public static string MatchDomainClassName(IRelationDomain domain) { return(Util.ToFirstUpper(MatchDomainFieldName(domain))); }
public static string EnforceDomainMethodName(IRelationDomain targetDomain) { return("Enforce" + ToFirstUpper(targetDomain.RootVariable.Name)); }
public static bool IsValidTargetDomain(IRelationDomain domain) { return(((IRelation)domain.Rule).Domain.Where(source => source != domain).ToList().TrueForAll (d => IsValidSourceDomain((IRelationDomain)d))); }
public static string GenerateDomainCheckMethodContent(IRelationDomain sourceDomain, ISet <IVariable> variablesBindedSoFar, DomainVariablesBindingsResult analysis, List <IObjectTemplateExp> remaining = null, StringBuilder stringBuilder = null, ISet <IPropertyTemplateItem> postPonedPropertiesToCheck = null) { if (remaining == null) { remaining = QvtModelExplorer.FindAllObjectTemplates(sourceDomain).Where(o => !o.IsAntiTemplate()).ToList(); } if (stringBuilder == null) { stringBuilder = new StringBuilder(); } if (postPonedPropertiesToCheck == null) { postPonedPropertiesToCheck = new HashSet <IPropertyTemplateItem>(); } if (remaining.Count > 0) { IObjectTemplateExp current = remaining[0]; remaining.RemoveAt(0); string currentVariableName = current.BindsTo.Name; variablesBindedSoFar.Add(current.BindsTo); // Generate conditional for the object template stringBuilder.AppendLine("if (" + currentVariableName + " != null) {"); // Generate bindings for each non-many free variables ISet <IPropertyTemplateItem> managedProps = new HashSet <IPropertyTemplateItem>(); foreach (IPropertyTemplateItem nonManyProp in current.Part.Where(prop => (prop.Value is ObjectTemplateExp || prop.Value is VariableExp) && !prop.ReferredProperty.isMany())) { IVariable bindedVariable = QvtModelExplorer.FindBindedVariables(nonManyProp).Single(); if (bindedVariable != null && !variablesBindedSoFar.Contains(bindedVariable)) { managedProps.Add(nonManyProp); stringBuilder.AppendLine(GenerateBindingFreeNonMany(nonManyProp, bindedVariable)); variablesBindedSoFar.Add(bindedVariable); } } // We compute the checks that we can do right now, and the ones that must be post poned because their variables are not binded yet // For now we only do checks on single value properties, the many valued one are simply exhaustively explored/binded later IEnumerable <IPropertyTemplateItem> candidatesInit = current.Part.Where(prop => !prop.ReferredProperty.isMany() && (prop.Value is CSharpOpaqueExpression || prop.Value is IVariableExp)); ISet <IPropertyTemplateItem> candidates = new HashSet <IPropertyTemplateItem>(); candidates.UnionWith(candidatesInit); candidates.UnionWith(postPonedPropertiesToCheck); candidates.ExceptWith(managedProps); ISet <IPropertyTemplateItem> propsToCheck = new HashSet <IPropertyTemplateItem>(); foreach (IPropertyTemplateItem candidate in candidates) { if (!variablesBindedSoFar.IsSupersetOf(QvtModelExplorer.FindBindedVariables(candidate))) { propsToCheck.Remove(candidate); postPonedPropertiesToCheck.Add(candidate); } else { propsToCheck.Add(candidate); postPonedPropertiesToCheck.Remove(candidate); } } // We generate the checks for all the ones that can be made now if (propsToCheck.Count > 0) { IEnumerable <string> conditions = propsToCheck.Select(u => GenerateConditionnalProperty(u, true)); string condition = string.Join(" && ", conditions); stringBuilder.AppendLine("if (" + condition + ") {"); } // We make a recursion for each object template not managed yet // - If the ref is many, then we make the binding first using a loop // - If the ref is not many, the binding was done before when managing non-many List <IPropertyTemplateItem> objectTemplatesManyRemaining = current.Part.Where(p => p.Value is ObjectTemplateExp && p.ReferredProperty.isMany() && remaining.Contains(p.Value)).ToList(); foreach (IPropertyTemplateItem propWithTemplate in objectTemplatesManyRemaining) { // Generate start for each, which binds the variable associated with the object template ObjectTemplateExp objectTemplate = (ObjectTemplateExp)propWithTemplate.Value; stringBuilder.AppendLine("foreach (" + objectTemplate.BindsTo.Type.GetRealTypeName() + " " + objectTemplate.BindsTo.Name + " in " + currentVariableName + "." + propWithTemplate.ReferredProperty.Name + ".OfType<" + propWithTemplate.ReferredProperty.Type.GetRealTypeName() + ">()) {"); variablesBindedSoFar.Add(objectTemplate.BindsTo); } GenerateDomainCheckMethodContent(sourceDomain, variablesBindedSoFar, analysis, remaining, stringBuilder, postPonedPropertiesToCheck); foreach (IPropertyTemplateItem _ in objectTemplatesManyRemaining) { // Generate end for each stringBuilder.AppendLine("}"); } // Generate end if checks all c# expressions if (propsToCheck.Count > 0) { stringBuilder.Append("}"); } // End conditional on the object template stringBuilder.AppendLine("}"); } // We stop the recursion if there are no more object templates to manage else { string matchClassName = QvtCodeGeneratorStrings.MatchDomainClassName(sourceDomain); // Now we can finally create the Match object stringBuilder.AppendLine(matchClassName + " match = new " + matchClassName + "() {"); foreach (IVariable variable in analysis.VariablesItCanBind) { stringBuilder.AppendLine(variable.Name + " = " + variable.Name + ","); } stringBuilder.AppendLine("};"); stringBuilder.AppendLine("result.Add(match);"); } return(stringBuilder.ToString()); }
public static string MatchDomainFieldName(IRelationDomain domain) { return("matchDomain" + Util.ToFirstUpper(domain.RootVariable.Name)); }