-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial version of operation parameter typing with operation overload…
… support #11
- Loading branch information
Showing
9 changed files
with
358 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...labs.uml.ralf/src/com/incquerylabs/uml/ralf/scoping/ReducedAlfLanguageLinkingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.incquerylabs.uml.ralf.scoping; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.emf.ecore.EReference; | ||
import org.eclipse.uml2.uml.Class; | ||
import org.eclipse.uml2.uml.Classifier; | ||
import org.eclipse.uml2.uml.Operation; | ||
import org.eclipse.uml2.uml.Parameter; | ||
import org.eclipse.uml2.uml.Type; | ||
import org.eclipse.xtext.linking.impl.DefaultLinkingService; | ||
import org.eclipse.xtext.linking.impl.IllegalNodeException; | ||
import org.eclipse.xtext.nodemodel.INode; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.google.common.collect.Sets; | ||
import com.google.inject.Inject; | ||
import com.incquerylabs.uml.ralf.ReducedAlfSystem; | ||
import com.incquerylabs.uml.ralf.reducedAlfLanguage.Expression; | ||
import com.incquerylabs.uml.ralf.reducedAlfLanguage.FeatureInvocationExpression; | ||
import com.incquerylabs.uml.ralf.reducedAlfLanguage.Tuple; | ||
import com.incquerylabs.uml.ralf.resource.ReducedAlfLanguageResource; | ||
|
||
import it.xsemantics.runtime.Result; | ||
|
||
public class ReducedAlfLanguageLinkingService extends DefaultLinkingService { | ||
|
||
|
||
private boolean parametersMatch(List<Parameter> parameters, List<Parameter> otherParameters) { | ||
if (parameters.size() != otherParameters.size()) { | ||
return false; | ||
} | ||
for (int i = 0; i < parameters.size(); i++) { | ||
Parameter param = parameters.get(i); | ||
Parameter otherParam = otherParameters.get(i); | ||
if (param.getDirection() != otherParam.getDirection() || | ||
param.getType().conformsTo(otherParam.getType())) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private boolean operationRedefines(Operation op, Operation redefinedOp) { | ||
if (op.getRedefinedOperations().contains(redefinedOp)) { | ||
return true; | ||
} | ||
Class opClass = op.getClass_(); | ||
Class redefinedClass = redefinedOp.getClass_(); | ||
if (!opClass.allParents().contains(redefinedClass)) { | ||
return false; | ||
} | ||
return parametersMatch(op.getOwnedParameters(), redefinedOp.getOwnedParameters()); | ||
} | ||
|
||
private boolean operationMatchesParameters(Operation op, Tuple parameters) { | ||
Result<Boolean> result = typeSystem.operationParametersType(op, parameters); | ||
return !result.failed(); | ||
} | ||
|
||
@Inject | ||
ReducedAlfSystem typeSystem; | ||
|
||
@Override | ||
public List<EObject> getLinkedObjects(EObject context, EReference ref, INode node) throws IllegalNodeException { | ||
List<EObject> linkedObjects = super.getLinkedObjects(context, ref, node); | ||
//If the linked object is an operation, search for possible alternates | ||
if (linkedObjects.size() == 1 && linkedObjects.get(0) instanceof Operation) { | ||
Operation op = (Operation) linkedObjects.get(0); | ||
IUMLContextProvider umlContext = ((ReducedAlfLanguageResource)context.eResource()).getUmlContextProvider(); | ||
Expression ctx = null; | ||
Tuple parameters = null; | ||
if (context instanceof FeatureInvocationExpression) { | ||
FeatureInvocationExpression featureInvocationExpression = (FeatureInvocationExpression) context; | ||
ctx = featureInvocationExpression.getContext(); | ||
parameters = featureInvocationExpression.getParameters(); | ||
} | ||
Type contextType = typeSystem.type(ctx).getValue().getUmlType(); | ||
Set<Operation> candidates = umlContext.getOperationCandidatesOfClass((Classifier) contextType, op.getName()); | ||
if (candidates.size() > 1) { | ||
linkedObjects = calculateBestCandidates(candidates, parameters); | ||
} | ||
} | ||
return linkedObjects; | ||
} | ||
|
||
private List<EObject> calculateBestCandidates(Set<Operation> candidates, Tuple parameters) { | ||
Set<Operation> remainingCandidates = Sets.newHashSet(candidates); | ||
|
||
for (Operation op : candidates) { | ||
Iterator<Operation> it = remainingCandidates.iterator(); | ||
while (it.hasNext()) { | ||
Operation next = it.next(); | ||
if (operationRedefines(op, next)) { | ||
it.remove(); | ||
} | ||
} | ||
} | ||
|
||
Iterator<Operation> it = remainingCandidates.iterator(); | ||
while(it.hasNext()) { | ||
Operation next = it.next(); | ||
if (!operationMatchesParameters(next, parameters)) { | ||
it.remove(); | ||
} | ||
} | ||
|
||
return Lists.newArrayList(remainingCandidates); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.