Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » getPictogramElements returning too many objects
getPictogramElements returning too many objects [message #700555] Sat, 23 July 2011 17:50 Go to next message
chris
Messages: 3
Registered: July 2011
Junior Member
Hi,

is Graphiti.getLinkService().getPictogramElements intended to be kind of inverse function for IMappingProvider#getBusinessObjectForPictogramElement? (of course only in cases where exactly one business object is linked to the PE)
I've noticed that it does not only return the pictogram elements that are linked to the business object but all PEs linked to an object equal to the BO.

In the current implementation (org.eclipse.graphiti.internal.services.impl.LinkServiceImpl), the method uses "equalsBusinessObjects" of the ToolBehaviorProvider which is itself based on an equality check. Of course, it's no big deal to override the equalsBusinessObjects method (the method's spec even suggests this), but getPictogramElements's implementation does not precisely conform to its specification. ("get all pictogram elements which references the given eObject")

If you need an example where this is relevant, here is what I mean:
The custom feature below is based on the tutorial, but the idea also works in other scenarios where the model is separated from the view. To reproduce, just add two equal EClass representations (i.e. same name) to your diagram and execute the custom feature. In the tutorial, each EClass representation has two links (name label + container) to the BO. In my example, "getPeEquals" returns 4. "getPeSame" has an additional identity check returns the right pictogram elements and therefore the output is 2.

public class GetPictogramElementForBusinessObjectCustomFeature extends
		AbstractCustomFeature {

	public GetPictogramElementForBusinessObjectCustomFeature(IFeatureProvider fp) {
		super(fp);
	}

	@Override
	public boolean canExecute(ICustomContext context) {
		return getBusinessObjectForPictogramElement(context
				.getPictogramElements()[0]) instanceof EClass;
	}

	@Override
	public String getName() {
		return "GetPictogramElementsForBO";
	}

	@Override
	public void execute(ICustomContext context) {
		EObject o = (EObject) getBusinessObjectForPictogramElement(context
				.getPictogramElements()[0]);

		List<PictogramElement> elementsEqual = getPeEquals(o);
		List<PictogramElement> elementsSame = getPeSame(o);

		System.out.println("Equal: " + elementsEqual.size());
		System.out.println("Same: " + elementsSame.size());
	}

	private List<PictogramElement> getPeEquals(EObject object) {
		return Graphiti.getLinkService().getPictogramElements(getDiagram(),
				object);
	}

	private List<PictogramElement> getPeSame(EObject object) {
		List<PictogramElement> elements = Graphiti.getLinkService()
				.getPictogramElements(getDiagram(), object);
		List<PictogramElement> result = new ArrayList<PictogramElement>();

		for (PictogramElement pictogramElement : elements) {
			if (getBusinessObjectForPictogramElement(pictogramElement) == object) {
				result.add(pictogramElement);
			}
		}

		return result;
	}

}

// alternatively, overriding a method in the tool behavior provider works as well
/*
@Override
public boolean equalsBusinessObjects(Object o1, Object o2) {
	if (o1 instanceof EClass && o2 instanceof EClass) {
		return o1 == o2;
	}
	return super.equalsBusinessObjects(o1, o2);
}
*/


Is this a bug or is this intended behavior? In the latter case, I guess it would be reasonable to adapt the specification, e.g. include a reference to the method to override.
Re: getPictogramElements returning too many objects [message #701645 is a reply to message #700555] Mon, 25 July 2011 11:33 Go to previous message
Michael Wenz is currently offline Michael Wenz
Messages: 1618
Registered: July 2009
Location: Walldorf, Germany
Senior Member
Chris,

there's a bugzilla discussing this, see
https://bugs.eclipse.org/bugs/show_bug.cgi?id=335828. In fact in the course
of this bug we introduced the possiblity to redefine the equals check.

Graphiti's linking mechanism is based upon object equality and not identity,
simply to avoid issues with reloading of objects (happens when external
changes are done and the editor was notified about those) and the usage of
several editing domains.

Michael


"chris" schrieb im Newsbeitrag news:j0f124$j8a$1@news.eclipse.org...

Hi,

is Graphiti.getLinkService().getPictogramElements intended to be kind of
inverse function for IMappingProvider#getBusinessObjectForPictogramElement?
(of course only in cases where exactly one business object is linked to the
PE)
I've noticed that it does not only return the pictogram elements that are
linked to the business object but all PEs linked to an object equal to the
BO.

In the current implementation
(org.eclipse.graphiti.internal.services.impl.LinkServiceImpl), the method
uses "equalsBusinessObjects" of the ToolBehaviorProvider which is itself
based on an equality check. Of course, it's no big deal to override the
equalsBusinessObjects method (the method's spec even suggests this), but
getPictogramElements's implementation does not precisely conform to its
specification. ("get all pictogram elements which references the given
eObject")

If you need an example where this is relevant, here is what I mean:
The custom feature below is based on the tutorial, but the idea also works
in other scenarios where the model is separated from the view. To reproduce,
just add two equal EClass representations (i.e. same name) to your diagram
and execute the custom feature. In the tutorial, each EClass representation
has two links (name label + container) to the BO. In my example,
"getPeEquals" returns 4. "getPeSame" has an additional identity check
returns the right pictogram elements and therefore the output is 2.


public class GetPictogramElementForBusinessObjectCustomFeature extends
AbstractCustomFeature {

public GetPictogramElementForBusinessObjectCustomFeature(IFeatureProvider
fp) {
super(fp);
}

@Override
public boolean canExecute(ICustomContext context) {
return getBusinessObjectForPictogramElement(context
..getPictogramElements()[0]) instanceof EClass;
}

@Override
public String getName() {
return "GetPictogramElementsForBO";
}

@Override
public void execute(ICustomContext context) {
EObject o = (EObject) getBusinessObjectForPictogramElement(context
..getPictogramElements()[0]);

List<PictogramElement> elementsEqual = getPeEquals(o);
List<PictogramElement> elementsSame = getPeSame(o);

System.out.println("Equal: " + elementsEqual.size());
System.out.println("Same: " + elementsSame.size());
}

private List<PictogramElement> getPeEquals(EObject object) {
return Graphiti.getLinkService().getPictogramElements(getDiagram(),
object);
}

private List<PictogramElement> getPeSame(EObject object) {
List<PictogramElement> elements = Graphiti.getLinkService()
..getPictogramElements(getDiagram(), object);
List<PictogramElement> result = new ArrayList<PictogramElement>();

for (PictogramElement pictogramElement : elements) {
if (getBusinessObjectForPictogramElement(pictogramElement) == object) {
result.add(pictogramElement);
}
}

return result;
}

}

// alternatively, overriding a method in the tool behavior provider works as
well
/*
@Override
public boolean equalsBusinessObjects(Object o1, Object o2) {
if (o1 instanceof EClass && o2 instanceof EClass) {
return o1 == o2;
}
return super.equalsBusinessObjects(o1, o2);
}
*/


Is this a bug or is this intended behavior? In the latter case, I guess it
would be reasonable to adapt the specification, e.g. include a reference to
the method to override.
Previous Topic:Undo History
Next Topic:Graphiti planning for Juno
Goto Forum:
  


Current Time: Fri Oct 24 21:23:00 GMT 2014

Powered by FUDForum. Page generated in 0.04473 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software