private void SetBindings(QVTRelations.IRelation relation, QVTBase.IPattern pattern, EssentialOCL.CSharpOpaqueExpression cSharpOpaqueExpression, ExpressionSyntax parsedExpression) { ISet <string> extractedIdentifiers = CSharpParser.ExtractNonMethodIdentifiersFromExpression(parsedExpression); foreach (string extractedIdentifier in extractedIdentifiers) { EssentialOCL.IVariable variable = ConstructVariable(relation, extractedIdentifier); pattern?.BindsTo.Add(variable); cSharpOpaqueExpression.BindsTo.Add(variable); } }
private QVTRelations.IRelationDomain ConstructPrimitiveRelationDomain(QVTRelations.IRelation relation, EnAr.Connector qvtTransformationLinkConnector, EnAr.Element domainObjectElement) { EssentialOCL.IVariable variable = ConstructVariable(relation, domainObjectElement); QVTRelations.IRelationDomain relationDomain = new QVTRelations.RelationDomain { Name = qvtTransformationLinkConnector.Name, // a primitive domain is supposed not to have a name, but we use it for ordering IsCheckable = true, IsEnforceable = false, //DefaultAssignment = null // TODO? Rule = relation, RootVariable = variable }; relation.Variable.Add(variable); return(relationDomain); }
private EssentialOCL.IVariable ConstructVariable(QVTRelations.IRelation relation, string name, EMOF.IType type = null) { EssentialOCL.IVariable variable = relation.Variable.FirstOrDefault(v => v.Name == name); if (variable == null) { variable = new EssentialOCL.Variable() { Name = name, Type = type, InitExpression = null, // Not taken into account }; relation.Variable.Add(variable); } // If there was an existing variable, but with no type yet, we give it the input type else if (variable.Type == null && type != null) { variable.Type = type; } return(variable); }
private QVTTemplate.IObjectTemplateExp ConstructObjectTemplateExp(QVTRelations.IRelation relation, QVTRelations.IDomainPattern domainPattern, EnAr.Element objectElement, ISet <EnAr.Connector> visitedConnectors = null) { EssentialOCL.IVariable variable = null; EMOF.IType type = emofImporter.ConstructTypeOfTyped(objectElement); // TODO manage the not ? if (objectElement.Name != "{not}") { variable = ConstructVariable(relation, objectElement); domainPattern?.BindsTo.Add(variable); } QVTTemplate.IObjectTemplateExp objectTemplateExp = new QVTTemplate.ObjectTemplateExp() { BindsTo = variable, ReferredClass = type as EMOF.IClass, Where = null // TODO }; objectTemplateExp.SetAntiTemplate(variable == null); objectElementToObjectTemplate.Add(objectElement, objectTemplateExp); foreach (RunStateField runStateField in EnArExplorer.GetRunState(objectElement)) { ConstructPropertyTemplateItem(relation, domainPattern, objectTemplateExp, runStateField); } foreach (EnAr.Connector connector in explorer.GetConnectorsLinkedTo(objectElement).FindAll(c => c.Stereotype != "qvtTransformationLink")) { Tuple <EnAr.ConnectorEnd, EnAr.Element> linked = explorer.GetElementOppositeTo(objectElement, connector); EnAr.ConnectorEnd connectorEnd = linked.Item1; EnAr.Element linkedElement = linked.Item2; //if (!string.IsNullOrWhiteSpace(connectorEnd.Role)) //{ ConstructPropertyTemplateItem(relation, domainPattern, objectTemplateExp, connector, connectorEnd, linkedElement, visitedConnectors); //} } return(objectTemplateExp); }
private QVTTemplate.IPropertyTemplateItem ConstructPropertyTemplateItem(QVTRelations.IRelation relation, QVTRelations.IDomainPattern domainPattern, QVTTemplate.IObjectTemplateExp objectTemplateExp, EnAr.Connector connector, EnAr.ConnectorEnd connectorEnd, EnAr.Element linkedElement, ISet <EnAr.Connector> visitedConnectors) { EssentialOCL.IOclExpression value; EMOF.IType type; QVTTemplate.IObjectTemplateExp targetObjectTemplateExp; ISet <EnAr.Connector> realVisitedConnectors = visitedConnectors ?? new HashSet <EnAr.Connector>(); ISet <EnAr.Connector> realVisitedConnectorsPropagated = new HashSet <EnAr.Connector>(realVisitedConnectors); realVisitedConnectorsPropagated.Add(connector); // Case cycle among object templates: simple variable expression if (objectElementToObjectTemplate.ContainsKey(linkedElement)) { targetObjectTemplateExp = objectElementToObjectTemplate[linkedElement]; EssentialOCL.IVariable existingVariable = targetObjectTemplateExp.BindsTo; value = new EssentialOCL.VariableExp() { ReferredVariable = existingVariable }; type = existingVariable.Type; } // Case no cycle; recursive creation of object template else { targetObjectTemplateExp = ConstructObjectTemplateExp(relation, domainPattern, linkedElement, realVisitedConnectorsPropagated); value = targetObjectTemplateExp; type = ((QVTTemplate.IObjectTemplateExp)value).BindsTo.Type; } EMOF.IProperty property = null; // If the connector end has a role, we use it to find the corresponding EMOF property if (!string.IsNullOrWhiteSpace(connectorEnd.Role)) { property = objectTemplateExp.ReferredClass.GetAllInheritedAttributes().Single(p => p.Name == connectorEnd.Role); } // If the connector end has no role (due to the else) // AND if we haven't visited the connector yet // AND if the connector has no roles whatsoever, we try to guess the type else if (!realVisitedConnectors.Contains(connector) && connector.ClientEnd.Role.IsNullOrEmpty() && connector.SupplierEnd.Role.IsNullOrEmpty()) { IList <EMOF.IProperty> candidateProperties = objectTemplateExp.ReferredClass.GetAllInheritedAttributes().Where(p => (p.Type as EMOF.IClass)?.GetAllSubTypes().Contains(type) ?? p.Type == type).ToList(); if (candidateProperties.Count == 0) { throw new InvalidQVTRelationsModelException("Relation " + relation.Name + ", invalid property connector between " + objectTemplateExp.BindsTo.Name + " and " + targetObjectTemplateExp.BindsTo.Name + ", no possible property", connector); } if (candidateProperties.Count > 1) { throw new InvalidQVTRelationsModelException("Relation " + relation.Name + ", ambiguous property connector between " + objectTemplateExp.BindsTo.Name + " and " + targetObjectTemplateExp.BindsTo.Name + ", cannot choose a property between the following: [" + string.Join(";", candidateProperties.Select(p => p.Name)) + "]", connector); } property = candidateProperties.Single(); } QVTTemplate.PropertyTemplateItem propertyTemplateItem = null; if (property != null) { propertyTemplateItem = new QVTTemplate.PropertyTemplateItem() { ReferredProperty = property, Value = value, IsOpposite = false, // TODO? ObjContainer = objectTemplateExp }; } return(propertyTemplateItem); }
private EssentialOCL.IOclExpression ConstructOCLExpression(QVTRelations.IRelation relation, ExpressionSyntax parsedExpression, QVTBase.IPattern pattern, EMOF.IType type = null) { // Case single identifier => OCL VariableExp if (parsedExpression is IdentifierNameSyntax) { EssentialOCL.IVariable variable = ConstructVariable(relation, parsedExpression.ToString(), type); pattern?.BindsTo.Add(variable); return(new EssentialOCL.VariableExp() { ReferredVariable = variable }); } // Case method call => QVT RelationCallExp (if the relation exists) of function call (if the function exists) if (parsedExpression is InvocationExpressionSyntax) { InvocationExpressionSyntax invocationExpressionSyntax = (InvocationExpressionSyntax)parsedExpression; // We are only interested in direct calls if (invocationExpressionSyntax.Expression is IdentifierNameSyntax) { IdentifierNameSyntax methodIdentifier = (IdentifierNameSyntax)invocationExpressionSyntax.Expression; ArgumentListSyntax argumentList = invocationExpressionSyntax.ArgumentList; QVTRelations.IRelation calledRelation = FindRelation((QVTRelations.IRelationalTransformation)(relation.Transformation), methodIdentifier.ToString()); QVTBase.IFunction calledFunction = FindFunction((QVTRelations.IRelationalTransformation)(relation.Transformation), methodIdentifier.ToString()); if (calledRelation != null) { QVTRelations.RelationCallExp call = new QVTRelations.RelationCallExp { ReferredRelation = calledRelation }; if (argumentList.Arguments.Count != calledRelation.Domain.Count) { throw new InvalidQVTRelationsModelException("Relation " + relation.Name + ": wrong number of arguments in relation call " + calledRelation.Name); } foreach (ArgumentSyntax argumentSyntax in argumentList.Arguments) { QVTRelations.IRelationDomain correspondingDomain = (QVTRelations.IRelationDomain)calledRelation.Domain[argumentList.Arguments.IndexOf(argumentSyntax)]; ExpressionSyntax argumentExpression = argumentSyntax.Expression; EssentialOCL.IOclExpression argumentOCLExpression = ConstructOCLExpression(relation, argumentExpression, pattern, correspondingDomain.RootVariable.Type); call.Argument.Add(argumentOCLExpression); } return(call); } else if (calledFunction != null) { string methodname = methodIdentifier.ToString(); EssentialOCL.IOperationCallExp call = new EssentialOCL.OperationCallExp() { Type = calledFunction.Type, ReferredOperation = calledFunction, Name = calledFunction.Name, }; foreach (ArgumentSyntax argumentSyntax in argumentList.Arguments) { ExpressionSyntax argumentExpression = argumentSyntax.Expression; EssentialOCL.IOclExpression argumentOCLExpression = ConstructOCLExpression(relation, argumentExpression, pattern, calledFunction.Type); call.Argument.Add(argumentOCLExpression); } return(call); } } } // Case assignment => Custom Assignment //TODO replace by OCL '=='? Meaning having to provide basic parts of OCL standard lib if (parsedExpression is AssignmentExpressionSyntax) { AssignmentExpressionSyntax assignmentExpressionSyntax = (AssignmentExpressionSyntax)parsedExpression; IdentifierNameSyntax leftIdentifier = (IdentifierNameSyntax)assignmentExpressionSyntax.Left; ExpressionSyntax right = assignmentExpressionSyntax.Right; EssentialOCL.IVariable variable = ConstructVariable(relation, leftIdentifier.ToString()); pattern?.BindsTo.Add(variable); return(new EssentialOCL.Assignment() { AssignedVariable = variable, Value = ConstructOCLExpression(relation, right, pattern) }); } // Any other case => Custom CSharpOpaqueExpression // TODO replace by QVT "Function" with a black box implementation? EssentialOCL.CSharpOpaqueExpression cSharpOpaqueExpression = new EssentialOCL.CSharpOpaqueExpression() { Code = parsedExpression.ToString() }; SetBindings(relation, pattern, cSharpOpaqueExpression, parsedExpression); return(cSharpOpaqueExpression); }