Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Pretty Print
Pretty Print [message #673578] Tue, 24 May 2011 07:31 Go to next message
David Michonneau is currently offline David Michonneau
Messages: 85
Registered: July 2009
Member
Is there any helper to do a pretty print of an EObject instance, that would print its attributes and contents recursively in a human-readable format (not XML, and without going through the Resource mechanism)? The purpose is to log it, so it should have no side-effect.

Thanks,

David
Re: Pretty Print [message #687039 is a reply to message #673578] Tue, 24 May 2011 13:38 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26137
Registered: July 2009
Senior Member
David,

No there isn't. Such a thing could be quite huge; I'm not sure it would
make sense to log so much...


David Michonneau wrote:
> Is there any helper to do a pretty print of an EObject instance, that
> would print its attributes and contents recursively in a
> human-readable format (not XML, and without going through the Resource
> mechanism)? The purpose is to log it, so it should have no side-effect.
>
> Thanks,
>
> David
Re: Pretty Print [message #687041 is a reply to message #687039] Tue, 24 May 2011 16:34 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4093
Registered: July 2009
Senior Member
Hi Ed

Are you sure there isn't? I thought the reflective printer in the OCL
Console was borrowed from Ecore. It has a very small amount of code
wrapping the provision of an IItemLabelProvider by a
ReflectiveItemProviderAdapterFactory. See
org.eclipse.ocl.examples.interpreter.console.OCLConsolePage.toString(Object).

Or, there is also HUTN that I understand forms part of the Epsilon project.

Or, there's custom code that I wouldn't have thought would be as much as
50 lines.

Regards

Ed Willink

On 24/05/2011 14:38, Ed Merks wrote:
> David,
>
> No there isn't. Such a thing could be quite huge; I'm not sure it
> would make sense to log so much...
>
>
> David Michonneau wrote:
>> Is there any helper to do a pretty print of an EObject instance, that
>> would print its attributes and contents recursively in a
>> human-readable format (not XML, and without going through the
>> Resource mechanism)? The purpose is to log it, so it should have no
>> side-effect.
>>
>> Thanks,
>>
>> David
Re: Pretty Print [message #687043 is a reply to message #687041] Wed, 25 May 2011 07:28 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26137
Registered: July 2009
Senior Member
Ed,

Well, there isn't one in EMF. :-P I'm not sure what's everywhere else...

The comment about size was about the size of what's printed for logging
purposes, not about the size of the code to produce it.


Ed Willink wrote:
> Hi Ed
>
> Are you sure there isn't? I thought the reflective printer in the OCL
> Console was borrowed from Ecore. It has a very small amount of code
> wrapping the provision of an IItemLabelProvider by a
> ReflectiveItemProviderAdapterFactory. See
> org.eclipse.ocl.examples.interpreter.console.OCLConsolePage.toString(Object).
>
>
> Or, there is also HUTN that I understand forms part of the Epsilon
> project.
>
> Or, there's custom code that I wouldn't have thought would be as much
> as 50 lines.
>
> Regards
>
> Ed Willink
>
> On 24/05/2011 14:38, Ed Merks wrote:
>> David,
>>
>> No there isn't. Such a thing could be quite huge; I'm not sure it
>> would make sense to log so much...
>>
>>
>> David Michonneau wrote:
>>> Is there any helper to do a pretty print of an EObject instance,
>>> that would print its attributes and contents recursively in a
>>> human-readable format (not XML, and without going through the
>>> Resource mechanism)? The purpose is to log it, so it should have no
>>> side-effect.
>>>
>>> Thanks,
>>>
>>> David
>
[CDO] memoryanalyzer [message #687049 is a reply to message #687043] Wed, 25 May 2011 10:33 Go to previous messageGo to next message
John Smith is currently offline John Smith
Messages: 137
Registered: July 2009
Senior Member
is the Eclipse memoryanalyer project the correct tool to analyse memory
behavior of CDO models? CDO has its own garbage collector, so reports
could be more useful if CDO is explicitely handled, could they not?

Is there a static method to force to garbage collect all CDO model
elements which can be garbage collected (like System.gc() in Java)?

Is there a static method to get an iterator over all CDO model elements?
And for one such model element (which is not garbage collected), is it
possible to know the reason why it is not garbage collected?

A last question: I want to add a new CDO element to the contents of an
CLEAN CDO element, so if I do this, is the whole content list retrieved
from the database only to add the element to the list, or can I say to
the CDO server: "add this new element to the containment feature of this
persistent element". I dont want to "check out" the containment list on
client side, since even though it may contain only CDO proxies, the list
may be sized several gigs.
Re: [CDO] memoryanalyzer [message #687051 is a reply to message #687049] Wed, 25 May 2011 11:18 Go to previous messageGo to next message
Eike Stepper is currently offline Eike Stepper
Messages: 5538
Registered: July 2009
Senior Member
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Am 25.05.2011 12:33, schrieb exquisitus:
<blockquote cite="mid:irilf3$b34$1@news.eclipse.org" type="cite">
<div class="moz-text-flowed" style="font-family: -moz-fixed;
font-size: 14px;" lang="x-unicode">is the Eclipse memoryanalyer
project the correct tool to analyse memory behavior of CDO
models? </div>
</blockquote>
I don't know that tool, yet.<br>
<br>
<blockquote cite="mid:irilf3$b34$1@news.eclipse.org" type="cite">
<div class="moz-text-flowed" style="font-family: -moz-fixed;
font-size: 14px;" lang="x-unicode">CDO has its own garbage
collector, </div>
</blockquote>
No, it does not. It only implements the references between EObjects
(and some other more internal classes) in a way that the normal JVM
garbage collector can reclaim them once the application does not
reference them anymore.<br>
<br>
<blockquote cite="mid:irilf3$b34$1@news.eclipse.org" type="cite">
<div class="moz-text-flowed" style="font-family: -moz-fixed;
font-size: 14px;" lang="x-unicode">so reports
could be more useful if CDO is explicitely handled, could they not?
<br>
</div>
</blockquote>
I don't see how or why.<br>
<br>
<blockquote cite="mid:irilf3$b34$1@news.eclipse.org" type="cite">
<div class="moz-text-flowed" style="font-family: -moz-fixed;
font-size: 14px;" lang="x-unicode">
<br>
Is there a static method to force to garbage collect all CDO
model
elements which can be garbage collected (like System.gc() in Java)?
<br>
</div>
</blockquote>
No.<br>
<br>
<blockquote cite="mid:irilf3$b34$1@news.eclipse.org" type="cite">
<div class="moz-text-flowed" style="font-family: -moz-fixed;
font-size: 14px;" lang="x-unicode">
<br>
Is there a static method to get an iterator over all CDO model
elements? And for one such model element (which is not garbage
collected), is it
possible to know the reason why it is not garbage collected?
<br>
</div>
</blockquote>
If I don't miss something there are exactly 3 conditions that
prevent a CDOObject from being garbage collected:<br>
<br>
1) The application holds on this object.<br>
2) You've called
CDOView.Options.setCacheReferenceType(ReferenceType.STRONG)<br>
3) You're using EObject adapters in combination with
CDOView.Options.setStrongReferencePolicy(CDOAdapterPolicy)<br>
<br>
You have read access to the internal objects map of a CDOView
through InternalCDOView.getObjects().<br>
<br>
<blockquote cite="mid:irilf3$b34$1@news.eclipse.org" type="cite">
<div class="moz-text-flowed" style="font-family: -moz-fixed;
font-size: 14px;" lang="x-unicode">
<br>
A last question: I want to add a new CDO element to the contents
of an CLEAN CDO element, so if I do this, is the whole content
list retrieved from the database only to add the element to the
list, or  can I say to the CDO server: "add this new element to
the containment feature of this persistent element". I dont want
to "check out" the containment list on client side, since even
though it may contain only CDO proxies, the list
may be sized several gigs.
<br>
</div>
</blockquote>
With partial collection loading you could avoid to fill the list
with proper CDOIDs but, as you've noticed, it will be filled with
(non-singleton) marker elements. You may be interested in this
bugzilla, although there's no effort planned in the near future:<br>
<br>
214477: Better CDORevision list implementation for UNINITIALIZED
data<br>
<a class="moz-txt-link-freetext" href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=214477">https://bugs.eclipse.org/bugs/show_bug.cgi?id=214477</a><br>
<br>
Cheers<br>
/Eike<br>
<br>
----<br>
<a class="moz-txt-link-freetext" href="http://www.esc-net.de">http://www.esc-net.de</a><br>
<a class="moz-txt-link-freetext" href="http://thegordian.blogspot.com">http://thegordian.blogspot.com</a><br>
<a class="moz-txt-link-freetext" href="http://twitter.com/eikestepper">http://twitter.com/eikestepper</a><br>
<br>
<br>
</body>
</html>
Re: [CDO] memoryanalyzer [message #687061 is a reply to message #687051] Thu, 26 May 2011 10:19 Go to previous messageGo to next message
John Smith is currently offline John Smith
Messages: 137
Registered: July 2009
Senior Member
> proper CDOIDs but, as you've noticed, it will be filled with
> (non-singleton) marker elements.
Can you say how big in bytes one such marker element is?

What exactly can be garbage collected from one CDO element? features
independently? String typed features? a reference list? or only single
references in the reference list?

An maximal garbage-collected CDO element would only contain the CDOID
and a revision number? if the CDO element has an attribute "name" of
type String, and I call getName() once: can the name string be garbage
collected and the CDO element not?
Re: Pretty Print [message #687240 is a reply to message #673578] Tue, 24 May 2011 13:38 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26137
Registered: July 2009
Senior Member
David,

No there isn't. Such a thing could be quite huge; I'm not sure it would
make sense to log so much...


David Michonneau wrote:
> Is there any helper to do a pretty print of an EObject instance, that
> would print its attributes and contents recursively in a
> human-readable format (not XML, and without going through the Resource
> mechanism)? The purpose is to log it, so it should have no side-effect.
>
> Thanks,
>
> David
Re: Pretty Print [message #687243 is a reply to message #687039] Tue, 24 May 2011 16:34 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4093
Registered: July 2009
Senior Member
Hi Ed

Are you sure there isn't? I thought the reflective printer in the OCL
Console was borrowed from Ecore. It has a very small amount of code
wrapping the provision of an IItemLabelProvider by a
ReflectiveItemProviderAdapterFactory. See
org.eclipse.ocl.examples.interpreter.console.OCLConsolePage.toString(Object).

Or, there is also HUTN that I understand forms part of the Epsilon project.

Or, there's custom code that I wouldn't have thought would be as much as
50 lines.

Regards

Ed Willink

On 24/05/2011 14:38, Ed Merks wrote:
> David,
>
> No there isn't. Such a thing could be quite huge; I'm not sure it
> would make sense to log so much...
>
>
> David Michonneau wrote:
>> Is there any helper to do a pretty print of an EObject instance, that
>> would print its attributes and contents recursively in a
>> human-readable format (not XML, and without going through the
>> Resource mechanism)? The purpose is to log it, so it should have no
>> side-effect.
>>
>> Thanks,
>>
>> David
Re: Pretty Print [message #687245 is a reply to message #687041] Wed, 25 May 2011 07:28 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26137
Registered: July 2009
Senior Member
Ed,

Well, there isn't one in EMF. :-P I'm not sure what's everywhere else...

The comment about size was about the size of what's printed for logging
purposes, not about the size of the code to produce it.


Ed Willink wrote:
> Hi Ed
>
> Are you sure there isn't? I thought the reflective printer in the OCL
> Console was borrowed from Ecore. It has a very small amount of code
> wrapping the provision of an IItemLabelProvider by a
> ReflectiveItemProviderAdapterFactory. See
> org.eclipse.ocl.examples.interpreter.console.OCLConsolePage.toString(Object).
>
>
> Or, there is also HUTN that I understand forms part of the Epsilon
> project.
>
> Or, there's custom code that I wouldn't have thought would be as much
> as 50 lines.
>
> Regards
>
> Ed Willink
>
> On 24/05/2011 14:38, Ed Merks wrote:
>> David,
>>
>> No there isn't. Such a thing could be quite huge; I'm not sure it
>> would make sense to log so much...
>>
>>
>> David Michonneau wrote:
>>> Is there any helper to do a pretty print of an EObject instance,
>>> that would print its attributes and contents recursively in a
>>> human-readable format (not XML, and without going through the
>>> Resource mechanism)? The purpose is to log it, so it should have no
>>> side-effect.
>>>
>>> Thanks,
>>>
>>> David
>
[CDO] memoryanalyzer [message #687252 is a reply to message #687043] Wed, 25 May 2011 10:33 Go to previous messageGo to next message
John Smith is currently offline John Smith
Messages: 137
Registered: July 2009
Senior Member
is the Eclipse memoryanalyer project the correct tool to analyse memory
behavior of CDO models? CDO has its own garbage collector, so reports
could be more useful if CDO is explicitely handled, could they not?

Is there a static method to force to garbage collect all CDO model
elements which can be garbage collected (like System.gc() in Java)?

Is there a static method to get an iterator over all CDO model elements?
And for one such model element (which is not garbage collected), is it
possible to know the reason why it is not garbage collected?

A last question: I want to add a new CDO element to the contents of an
CLEAN CDO element, so if I do this, is the whole content list retrieved
from the database only to add the element to the list, or can I say to
the CDO server: "add this new element to the containment feature of this
persistent element". I dont want to "check out" the containment list on
client side, since even though it may contain only CDO proxies, the list
may be sized several gigs.
Re: [CDO] memoryanalyzer [message #687254 is a reply to message #687049] Wed, 25 May 2011 11:18 Go to previous messageGo to next message
Eike Stepper is currently offline Eike Stepper
Messages: 5538
Registered: July 2009
Senior Member
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Am 25.05.2011 12:33, schrieb exquisitus:
<blockquote cite="mid:irilf3$b34$1@news.eclipse.org" type="cite">
<div class="moz-text-flowed" style="font-family: -moz-fixed;
font-size: 14px;" lang="x-unicode">is the Eclipse memoryanalyer
project the correct tool to analyse memory behavior of CDO
models? </div>
</blockquote>
I don't know that tool, yet.<br>
<br>
<blockquote cite="mid:irilf3$b34$1@news.eclipse.org" type="cite">
<div class="moz-text-flowed" style="font-family: -moz-fixed;
font-size: 14px;" lang="x-unicode">CDO has its own garbage
collector, </div>
</blockquote>
No, it does not. It only implements the references between EObjects
(and some other more internal classes) in a way that the normal JVM
garbage collector can reclaim them once the application does not
reference them anymore.<br>
<br>
<blockquote cite="mid:irilf3$b34$1@news.eclipse.org" type="cite">
<div class="moz-text-flowed" style="font-family: -moz-fixed;
font-size: 14px;" lang="x-unicode">so reports
could be more useful if CDO is explicitely handled, could they not?
<br>
</div>
</blockquote>
I don't see how or why.<br>
<br>
<blockquote cite="mid:irilf3$b34$1@news.eclipse.org" type="cite">
<div class="moz-text-flowed" style="font-family: -moz-fixed;
font-size: 14px;" lang="x-unicode">
<br>
Is there a static method to force to garbage collect all CDO
model
elements which can be garbage collected (like System.gc() in Java)?
<br>
</div>
</blockquote>
No.<br>
<br>
<blockquote cite="mid:irilf3$b34$1@news.eclipse.org" type="cite">
<div class="moz-text-flowed" style="font-family: -moz-fixed;
font-size: 14px;" lang="x-unicode">
<br>
Is there a static method to get an iterator over all CDO model
elements? And for one such model element (which is not garbage
collected), is it
possible to know the reason why it is not garbage collected?
<br>
</div>
</blockquote>
If I don't miss something there are exactly 3 conditions that
prevent a CDOObject from being garbage collected:<br>
<br>
1) The application holds on this object.<br>
2) You've called
CDOView.Options.setCacheReferenceType(ReferenceType.STRONG)<br>
3) You're using EObject adapters in combination with
CDOView.Options.setStrongReferencePolicy(CDOAdapterPolicy)<br>
<br>
You have read access to the internal objects map of a CDOView
through InternalCDOView.getObjects().<br>
<br>
<blockquote cite="mid:irilf3$b34$1@news.eclipse.org" type="cite">
<div class="moz-text-flowed" style="font-family: -moz-fixed;
font-size: 14px;" lang="x-unicode">
<br>
A last question: I want to add a new CDO element to the contents
of an CLEAN CDO element, so if I do this, is the whole content
list retrieved from the database only to add the element to the
list, or  can I say to the CDO server: "add this new element to
the containment feature of this persistent element". I dont want
to "check out" the containment list on client side, since even
though it may contain only CDO proxies, the list
may be sized several gigs.
<br>
</div>
</blockquote>
With partial collection loading you could avoid to fill the list
with proper CDOIDs but, as you've noticed, it will be filled with
(non-singleton) marker elements. You may be interested in this
bugzilla, although there's no effort planned in the near future:<br>
<br>
214477: Better CDORevision list implementation for UNINITIALIZED
data<br>
<a class="moz-txt-link-freetext" href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=214477">https://bugs.eclipse.org/bugs/show_bug.cgi?id=214477</a><br>
<br>
Cheers<br>
/Eike<br>
<br>
----<br>
<a class="moz-txt-link-freetext" href="http://www.esc-net.de">http://www.esc-net.de</a><br>
<a class="moz-txt-link-freetext" href="http://thegordian.blogspot.com">http://thegordian.blogspot.com</a><br>
<a class="moz-txt-link-freetext" href="http://twitter.com/eikestepper">http://twitter.com/eikestepper</a><br>
<br>
<br>
</body>
</html>
Re: [CDO] memoryanalyzer [message #687266 is a reply to message #687051] Thu, 26 May 2011 10:19 Go to previous messageGo to next message
John Smith is currently offline John Smith
Messages: 137
Registered: July 2009
Senior Member
> proper CDOIDs but, as you've noticed, it will be filled with
> (non-singleton) marker elements.
Can you say how big in bytes one such marker element is?

What exactly can be garbage collected from one CDO element? features
independently? String typed features? a reference list? or only single
references in the reference list?

An maximal garbage-collected CDO element would only contain the CDOID
and a revision number? if the CDO element has an attribute "name" of
type String, and I call getName() once: can the name string be garbage
collected and the CDO element not?
Re: Pretty Print [message #985477 is a reply to message #673578] Thu, 15 November 2012 12:29 Go to previous message
Martin Domig is currently offline Martin Domig
Messages: 12
Registered: August 2010
Junior Member
This is an old topic, I know, but since it is googles first hit when searching for "EMF pretty print" I'll just post my solution here so that others won't have to do it all over.

It's just some java thrown together to pretty-print an EMF model tree. When used in combination with junit Assert.equals(String, String) you're a double-click away from having the differences in your model highlighted in red, without using EMF.compare. Can be used for logging purposes as well.

A simplified use case:

@Test
public void areModelsEqual() {
  EObject o1 = ...;
  EObject o2 = ...;
  if(!ECoreUtils.equals(o1, o2)) {
    assertEquals(PrettyPrinter.prettyPrint(o1), PrettyPrinter.prettyPrint(o2));
    fail("different objects, but same pretty print output");
  }
}


And here the printer:

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;

public class PrettyPrinter {
	private static final String INDENT = "    "; //$NON-NLS-1$

	public static String prettyPrint(EObject object) {
		List<String> lines = prettyPrintAny(object, ""); //$NON-NLS-1$
		StringBuilder sb = new StringBuilder();
		for (String line : lines) {
			sb.append(line).append('\n');
		}
		return sb.toString();
	}

	private static List<String> prettyPrintAny(Object o, String indent) {
		List<String> lines = new ArrayList<>();
		if (o instanceof EObject) {
			EObject object = (EObject) o;
			EClass eClass = object.eClass();
			lines.add(eClass.getName() + " [" + o.getClass().getCanonicalName() + "] {"); //$NON-NLS-1$ //$NON-NLS-2$
			lines.addAll(prettyPrintRecursive(object, INDENT));
			lines.add("}"); //$NON-NLS-1$
		} else if (o instanceof Iterable) {
			lines.add("["); //$NON-NLS-1$
			for (Object obj : (Iterable<?>) o) {
				lines.addAll(prettyPrintAny(obj, INDENT));
			}
			lines.add("]"); //$NON-NLS-1$
		} else {
			String line = String.valueOf(o) + ' ';
			if (o != null) {
				line += '[' + o.getClass().getCanonicalName() + ']';
			}
			lines.add(line);
		}
		return indentLines(lines, indent);
	}

	private static List<String> indentLines(List<String> lines, String indent) {
		List<String> result = new ArrayList<>();
		for (String l : lines) {
			result.add(indent + l);
		}
		return result;
	}

	private static List<String> prettyPrintRecursive(EObject o, String indent) {
		EClass eClass = o.eClass();
		List<String> result = new ArrayList<>();
		for (EStructuralFeature feature : eClass.getEAllStructuralFeatures()) {
			Object value = o.eGet(feature);
			String line = feature.getName() + " = "; //$NON-NLS-1$
			List<String> list = prettyPrintAny(value, INDENT);
			list.set(0, list.get(0).trim());
			result.add(line + list.get(0));
			list.remove(0);
			result.addAll(list);
		}
		return indentLines(result, indent);
	}
}


Hope this will help somebody.

[Updated on: Thu, 15 November 2012 12:53]

Report message to a moderator

Previous Topic:[CDO] CDO and transaction isolation levels
Next Topic:boolean qualify in getCreateChildText
Goto Forum:
  


Current Time: Thu Oct 23 05:31:51 GMT 2014

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

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