Re: Automatic data generation
Re: Automatic data generation
- Subject: Re: Automatic data generation
- From: Florijan Stamenkovic <email@hidden>
- Date: Thu, 21 Aug 2008 11:37:43 -0400
Hi Kieran,
I was thinking of something a bit more elaborate, and covering wider
ground. Attribute generation would be an aspect of it.
What I'd like to have:
1. Isolating logic from EO classes, the framework would encapsulate
all the functionality
2. Desired value hints (ranges, scales and precision for numeric
info, length for data and strings, ranges for dates)
3. The possibility to override any autogenerated value with a preset
one, maybe the possibility to define property interdependencies
4. Maybe image data autogeneration
5. Relationship handling, maybe with autodetection of
interdependencies between entities to creates EOs and auto-relate, if
not autodetected then still handled but manually
6. Hints for the desired number of EOs to be created
...
In short, I'd like to be able to set a few hints, then call one line
of code, have the framework generate who knows how many EOs, save
them, and voila. No extra dependencies on the project itself. I still
think it could be done, but it will require a bit more effort ;)
Well, if I make it, I'll give it away, since apparently there is a
need for something like this. It will depend on my boss basically,
but we might require something like this fairly soon, so the odds are
good.
F
Also, it would be wonder independent.
And, it might be fun! :))))))))
On Aug 21, 2008, at 11:15, Kieran Kelleher wrote:
Well, you can roll your own pretty easily.
Just make a ValueGenerator class that makes values, taking
advantage of the ERXLoremIpsumGenerator, have a
myapp.setDevelopmentDefaults=true/false entry in Properties. Then
in your base EOF class, have awakeFromInsertion call a custom
setDevelopmentDefaults method that *only generates* fill-in values
(unique, random or otherwise) where the values are null so you
don't override real awakeFromInsertion defaults.
In setDevelopment defaults, you can then finally call a catch-all
method to do the trivial attribute fillins and do sth like this:
<snip>
public class MyGenericRecord extends ERXGenericRecord {
private static final Logger log = Logger.getLogger
(MyGenericRecord.class);
@Override
public void awakeFromInsertion(EOEditingContext editingContext) {
super.awakeFromInsertion(editingContext);
setDefaultValues();
if (shouldSetDevelopmentDefaults()) {
setDevelopmentDefaults();
} //~ if (shouldSetDevelopmentDefaults())
}
/**
* Can be overridden to set default values. Will get called by
awakeFromInsertion.
* The Velocity EOGen template (_Enity.java) overrides this method
and sets
* default values stored in the attribute userInfo in the model
under the key
* 'defaultValue'.
*/
public void setDefaultValues() {
//intentionally blank
}
/**
* Don't cache this since we may want to change the system
property while doing development.
* It is already cached in system properties.
* @return true if setDevelopmentDefaults should be called when
new entities are created.
*/
public boolean shouldSetDevelopmentDefaults(){
return ERXProperties.booleanForKeyWithDefault
("myGreatApp.setDevelopmentDefaults", false);
}
/**
* Hook method that can be optionally overridden for classes.
* Default is to put lorem ipsum filler garbage. However, you can
override this method and fully
* control the defaults or call super and then customize the
defaults on the attributes you want.
*/
public void setDevelopmentDefaults(){
String TYPE_STRING = "java.lang.String";
String TYPE_DATETIME = "com.webobjects.foundation.NSTimestamp";
String TYPE_DATA = "com.webobjects.foundation.NSData";
String TYPE_NUMBER = "java.lang.Number";
// Grab the entity for this EO
EOEntity entity = ERXEOAccessUtilities.entityForEo(this);
// Get the visible attributes
NSArray attributes = ModelUtilities.publicAttributesForEntity
(entity);
for (java.util.Enumeration attributesEnumerator =
attributes.objectEnumerator(); attributesEnumerator
.hasMoreElements();) {
EOAttribute attribute = (EOAttribute)
attributesEnumerator.nextElement();
// The key
String key = attribute.name();
// Current value
Object currentValue = valueForKey(attribute.name());
if (log.isDebugEnabled())
log.debug(attribute.name() + " has initial value of " +
(currentValue == null ? "null" : currentValue.toString()));
// We only want to update attributes in certain situations
String className = attribute.className();
String valueType = attribute.valueType();
// Check if we should set a default value
// We don't want to stomp over any values set already
if (currentValue == null
&& !attribute.isDerived()
&& !attribute.isReadOnly() ) {
if (log.isDebugEnabled())
log.debug("Processing attribute: " + attribute);
if (TYPE_STRING.equals(className)) {
// String type
String value = ERXLoremIpsumGenerator.words(1, attribute.width
(), attribute.width());
takeValueForKey(value, key);
} else if (TYPE_DATETIME.equals(className)) {
// NSTimestamp - set the current time
takeValueForKey(new NSTimestamp(), key);
} else if (TYPE_DATA.equals(className)){
// NSData type
String value = ERXLoremIpsumGenerator.words(10, 100,
attribute.width());
takeValueForKey(new NSData(value.getBytes()), key);
} else if (TYPE_NUMBER.equals(className)){
// Number type
// Which type of number?
if ("i".equals(valueType)) {
// Integer
takeValueForKey(ValueGenerator.sharedInstance().integerValue
(), key);
} else if ("d".equals(valueType)){
takeValueForKey(ValueGenerator.sharedInstance().doubleValue
(), key);
} else if ("B".equals(valueType)){
takeStoredValueForKey(ValueGenerator.sharedInstance
().bigDecimalValue(), key);
} else if ("c".equals(valueType)) {
takeValueForKey(Boolean.FALSE, key);
} //~ if ("i".equals(valueType))
} //~ if ....
} //~ if (currentValue == null)
} //~ for (java.util.Enumeration attributesEnumerator = ...
}
}
</snip>
On Aug 21, 2008, at 10:56 AM, Florijan Stamenkovic wrote:
Not sure if this has been discussed already, but I could not find
anything in the list archives, nor by quickly googling.
So, I'm thinking of making a framework for automated data
generation, based on a model and some runtime settings. This
would, obviously, be used for testing. Specifically: filling up a
database with arbitrary numbers of nonsense rows, that are however
legal, and proceeding from there.
Has anyone made this already? Skimmed through the wonder API, but
did not see anything that struck a chord...
If nobody ever made it, then why not? It seems a very nice thing
to have, and off the top of my head I can not think of any
insurmountable obstacle to making it...
F
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40mac.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden