Home » Eclipse Projects » Sirius » Labeling Relation Edges(Using a Java Service to return a String label based on a source and a target)
Labeling Relation Edges [message #1854377] |
Tue, 16 August 2022 11:49  |
Eclipse User |
|
|
|
I am trying to compute labels for my graphical representation
I have an abstract class displayed in my diagram, its children declare their references, I can get the references using a Service:
@SuppressWarnings("unchecked")
public Collection<EObject> flowTargets(EObject self) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
Class<?> objclass = self.getClass();
String methodName = "getTargetObjects";
return (Collection<EObject>) objclass.getMethod(methodName).invoke(self);
}
And using the "Target Finder Expression" of a relation edge I can use "service:flowTargets" to point to the right objects.
Now I want to add a label to those edges.
I implemented the method to get a label based on the target in my class, I just want to be able to call this service
@SuppressWarnings("unchecked")
public String flowLabel(EObject self, Object target) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
Class<?> objclass = self.getClass();
String methodName = "getTargetRelation";
return (String) objclass.getMethod(methodName).invoke(self, target);
}
I found this post https://www.eclipse.org/forums/index.php/t/844952/ explaining how to find the target of an edge, simply using [view.oclAsType(DEdge).targetNode.oclAsType(DSemanticDecorator).target/] to get the object it points to, that works, except in service calls I can't use service:flowLabel(self, view.oclAsType(DEdge).targetNode.oclAsType(DSemanticDecorator).target)
as the documentation says "The service interpreter supports parameters, but only variables can be used as parameter (no literals or complex expressions), for example: service:serviceName(view, diagram)."
How can I resolve my problem of calling service flowLabel?
|
|
|
Re: Labeling Relation Edges [message #1854385 is a reply to message #1854377] |
Wed, 17 August 2022 01:27   |
Eclipse User |
|
|
|
Solution:
public String flowLabel(EObject self, Object target) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
target = invoke(target, "getTargetNode");
target = invoke(target, "getTarget");
return (String) self.getClass().getMethod("getTargetRelation", EObject.class).invoke(self, target);
}
Object invoke(Object obj, String method, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
return obj.getClass().getMethod(method).invoke(obj, args);
}
used by expression: "service:flowLabel(view)"
If, like me, you can't debug the java services, you can instead manually "debug" the valid methods on whatever object simply use a service such as
public String flowLabel2(EObject self, Object target) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
// view.oclAsType(DEdge).targetNode.oclAsType(DSemanticDecorator).target.toString()
Class<?> objclass = target.getClass();
Method[] ms = objclass.getMethods();
return Arrays.asList(ms).stream().map(m->m.getName()).collect(Collectors.joining(", "));
}
and display the result on a label in the diagram
i also suggest filtering after the map to name to narrow your search, such as .contains("node") or "Node"
[Updated on: Wed, 17 August 2022 01:27] by Moderator
|
|
| | |
Re: Labeling Relation Edges [message #1854449 is a reply to message #1854447] |
Thu, 18 August 2022 16:06  |
Eclipse User |
|
|
|
> Also, unless you are in a specific setting which prevents it, you can use more precise Java classes from your metamodel instead of plain "EObject self", and use the EMF-generated Java APIs directly in your services instead of Java reflection.
Thanks for that too, I had tried before when developing the viewpoint in a different runtime but placing it in the original workspace solves that problem
|
|
|
Goto Forum:
Current Time: Sat May 17 08:25:18 EDT 2025
Powered by FUDForum. Page generated in 0.02776 seconds
|