How to copy or clone an object ?

Posted by apoorvajoshi on 18-Nov-2013 00:40

Hi, 

I need to copy or clone an object in my vocabulary. It should be done either in a rulesheet or in a service callout without manually copying each attribute value. Can someone provide a solution for this ?

Thanks,
Apoorva

Posted by Chris S. Hogan on 18-Nov-2013 10:13

Apoorva,

If you need to create a new instance of an entity in working memory, and you want this new instance to have data values based on an existing instance of an entity then you will need to use the .new[] operator, and manually set the individual attribute values. Unfortunately there is not an automatic way to do this.

All Replies

Posted by Harold-Jan Verlee on 18-Nov-2013 03:27

Entities in the Corticon vocabulary can be quite easily copied just using right mouse click - copy and pasting the entity (including all attributes) by positioning the cursor on the vocabulary name. Obviously the copied entity should be renamed to something meaningful as it can't have the same name as the original entity. At run time, this new entity is now available in Corticon working memory. Hence, a Service Callout (SCO) can be programmed in Java to work against the new object.

Posted by apoorvajoshi on 18-Nov-2013 03:33

I need to copy an object during runtime and not an Entity from the vocabulary while authoring rule system. So in my rulesheet the object that I access should be copied to another instance by value to have two objects with exactly same values of all attributes without having to manually copy all values.

Posted by Harold-Jan Verlee on 18-Nov-2013 03:47

Thanks for the clarification of your question. It looks like using the scope section of the Corticon rulesheet might cater for your desired functionality. At design time, just copy/paste the entity in the scope section (extended mode in Studio) and give the new entity a new alias. You can write rules against this cloned entity in your rulesheet. Filters allow you to specify which members of the collection should be included/excluded. Please have a look at the example below.

Posted by apoorvajoshi on 18-Nov-2013 03:53

Thank you for the prompt reply. However what I need to do is, say instead of the above example I have an entity with an alias MaleApplicants1. I need to clone this and get another instance MaleApplicants2 which has all the attribute values same as MaleApplicants1. But if I just write MaleApplicants2=MaleApplicants1 it will copy the reference of the original entity. I need to have another instance in the memory just like what a clone() method in Java does.

Posted by Harold-Jan Verlee on 18-Nov-2013 04:41

By defining two scope aliases in the scope section, you accomplish precisely what you are after: at run time, two objects with the same members (depending on filters) are instantiated. 

Posted by apoorvajoshi on 18-Nov-2013 04:44

The two will have same members but not the same values in those attributes. Say I already have MaleApplicants1 populated with some values from the earlier rulesheet in the ruleflow. And in the current rulesheet I need to clone this entity to get another entity with alias MaleApplicants2 with exactly the same attribute values without having to manually copy every attribute value. How can this be accomplished ?

Posted by Harold-Jan Verlee on 18-Nov-2013 05:04

I may not fully understand you as I believe my previous answer pointed to the solution. If you set attribute values in rulesheet1 for an Applicant, these values are effectively set in Corticon working memory. In rulesheet 2, you define two scope aliases for the same Applicant entity, and two collections with the Applicant entity values from Rulesheet1 are instantiated in working memory. If this answer is not what you are after, please just send me a copy of  a few sample rules and let me know what you'd like to accomplish in terms of decision service output. I'd be happy to show you the design pattern.

Posted by Chris S. Hogan on 18-Nov-2013 10:13

Apoorva,

If you need to create a new instance of an entity in working memory, and you want this new instance to have data values based on an existing instance of an entity then you will need to use the .new[] operator, and manually set the individual attribute values. Unfortunately there is not an automatic way to do this.

This thread is closed