Found shared references to a collection in Grails - grails

In my app, the user makes a search. That search gives back a list of people that can be hired for a duty. But, user can make more than one search for the same duty (say, i need two persons for this city, and three for that city). Every search makes a "Prepresupuesto", and every Prepresupuesto makes a "Cocontrato". All "Cocontrato" makes a "Contrato", and all "Prepresupuestos" makes a single "Presupuesto". And all together conforms the entire duty, called "Campaign" in the app.
When i am trying to save that "Cocontrato", I receive the exception. This is the code:
ArrayList<Prepresupuesto> prepresus=Prepresupuesto.findAllByCamp(campid)
Presupuesto pres=new Presupuesto()
for(int i=0;i<prepresus.size();i++){
pres.prepresu.add(prepresus[i])
pres.camp=Campaign.get(prepres.camp.id)
pres.estado="Pending"
total=total+prepresus[i].total
crearCocontrato(prepresus[i])
}
Method crearCocontrato():
public crearCocontrato(Prepresupuesto prep){
Set <Azafata>azas=prep.azafatas
Cocontrato cocon=new Cocontrato()
cocon.contrato=con
cocon.precio=prep.precio
cocon.azafatas=azas
cocon.total=prep.total
cocon.horas=prep.horas
cocon.horario=prep.horario
cocon.localidad=prep.localidad
cocon.fechaInicio=prep.fechaInicio
cocon.fechaFin=prep.fechaFin
cocon.presu=prep
cocon.camp=prep.camp
if(!(cocon.save(flush:true))){
render (view:"fallos", model:[azafataInstance:cocon])
}
}
The Exception is launched in the if.
Any help? I am a bit lost, I am a newcomer to Grails, and I think i don't understand completely the exception.
Thank you!
EDIT: The Exception:
Found shared references to a collection: com.publidirecta.Cocontrato.azafatas. Stacktrace follows:
Message: Found shared references to a collection: com.publidirecta.Cocontrato.azafatas
Line | Method
->> 2448 | crearCocontrato in com.publidirecta.EntidadController$$ENmku0D2
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 2394 | boton in ''
| 886 | runTask . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
| 908 | run in ''
^ 680 | run . . . . . . in java.lang.Thread

You're probably trying to save more than one entity (cocon) when both refer to the same collection. It's hard to detect that in this code you posted and it could be somewhere else, but I would say it's when you do this:
Set <Azafata>azas=prep.azafatas
cocon.presu=prep
Then when you save cocon, it points to the same collection that prep does. Take a look at this link, that says the same thing.
A way out of this would be to add element by element to the list you want to copy to instead of sharing the reference by doing a.list = b.list.
This can also help you.

Yes, Tiago, after diving a bit in the code (it is inherited code...) i found that the problem was what you are saying. I solved it by doing this:
ArrayList <Azafata> azas=new ArrayList<Azafata>()
for(int i=0;i<prep.azafatas.size(); i++){
azas.add(prep.azafatas.toArray()[i])
}
And everything was ok :) Thank you for your answer.

Related

Is there any way to run perticular scenario in karate more than one time if specific condition met true?

I want to run my perticular scenario or feature file more than one time.
Let's say if user enter 5 then i want my url to be hit 5 times.
is it possible in karate? Any help would be appreciated
Yes, read the docs: https://github.com/intuit/karate#loops
But also see example below using dynamic scenario outlines:
EDIT: using a Background will not work in Karate 1.3.0 onwards, please refer to this example: https://stackoverflow.com/a/75155712/143475
Background:
* def fun = function(i){ return { name: 'User ' + (i + 1) } }
* def data = karate.repeat(5, fun)
Scenario Outline:
* url 'http://httpbin.org/anything'
* request __row
* method post
Examples:
| data |
So run this, see how it works and study how it works as well.
Note that data driven features is an alternate approach where you can call a second feature file in a loop. So for example after using karate.repeat() 5 times like in the above Background, you use data as the argument to a second feature file that hits your url.

Rasa Core - Understanding Stories

I am having a difficult time understanding how rasa core interprets stories. Say I have the following:
Slot:
name:
type: text
animal:
type: categorical
values:
- dog
- cat
How do I write my stories to handle the sad path for a categorical slot?
*greet
- utter_greet
- utter_please_give_name
*inform{"Name":"Name"}
- utter_hello
- utter_ask_animal
*inform{"Animal": "Dog"}
- utter_hello_fido
- action_restart
*greet
- utter_greet
- utter_please_give_name
*inform{"Name":"Name"}
- utter_hello
- utter_ask_animal
*inform{"Animal": "Cat"}
- utter_hello_kitty
- action_restart
*greet
- utter_greet
- utter_please_give_name
*inform{"Name": null}
-utter_please_give_name
*greet
- utter_greet
- utter_please_give_name
*inform{"Name": "Name"}
-utter_ask_animal
*inform{"Animal": **"?????"**}
- utter_please_tell_animal
Also if I give a partial story in stories.md, like below, how does rasa connect the graph on the back to know what to do next? Does it read each story as an independent flow?
*greet
- utter_greet
- utter_please_give_name
*inform{"Name": null}
-utter_please_give_name
Thank you, any advice is appreciated.
To handle the sad path simply omit the slot annotation, e.g.:
## sad path
*greet
- utter_greet
- utter_please_give_name
*inform
-utter_please_give_name
Depending whether you use augmentation the single stories are glued together during the training to provide more training data.
If you want to handle all animals names so simply you can add more categories or make a custom action to fetch some api to call an animal name or something, Rasa is learning by examples anything not entered in data or domain will act as strange text.

Getting run-time error for 'rotate the doubly linked list'

I am posting this question because i am getting run-time error for any linked list problem that i code. There must be a generic mistake that I must be doing. Please help me figure out what's that mistake
This is just one of those problems:
https://practice.geeksforgeeks.org/problems/rotate-doubly-linked-list-by-p-nodes/1
Here is my solution:(I was only supposed to complete the function)
struct node*update(struct node* start,int p)
{
//Add your code here
node *last=start,*nxt,*i=start;
int n=1;;
while(last!=NULL){
last=last->next;
n+=1;
}
cout<<last->data<<"\n";
p=p%n;
for(int i=0;i<p;i++){
nxt=start->next;
last->next=start;
start->next=NULL;
start->prev=last;
nxt->prev=NULL;
last=last->next;
start=nxt;
}
return start;
}
Please let me know what is wrong with this code.
Thank you in advance
So , you're trying your hands on linked-lists ,that's great .
Linked list is like a game , a 'game of pointers' . You seems to very good in the 'game of logics' while you lack just a few things in ' pointers game'.
Going through your mentioned statement- " i am getting run-time error for any linked list problem that i code.", I would suggest you to learn how to debug your code and how self testing of code is done .
Coming back to your code , you are traversing the whole list to get access of the last node . To achieve that goal - you have used while loop . But look what conditional statement you've used in that , and what you'll be left with after execution of while loop .
node *last=start,*nxt,*i=start;
int n=1;;
while(last!=NULL){
last=last->next;
n+=1;
}
At first try to check you code for a linked list of size 2 or 3 only.
Isn't your code giving the value of n as [size(of linked-list) +1] and you are left with last pointer pointing to NULL. So how will you be able to get last->data when last is NULL.
Just a simple mishandling of pointers . Otherwise the logic of your later code part is fine .
Modification needed
Last node is the one , whose next is pointing to NULL . So run your loop till that node only .
int n=1;;
while(last->next!=NULL){ //last->next instead of last
last=last->next;
n+=1;
}
Hope this will help .
Keep asking , keep growing :)

Running FitNesse tests stored in variable

I'm trying to get FitNesse (slim tests running via fitSharp) to process tables stored in a variable. Both approach A & B below render the same on the page, but only approach B will run.
Approach A
!define test (
| Table:myTest | someValue |
)
${test}
Approach B
| Table:myTest | someValue |
This example is rather superficial, but in my tests I'm looking to vary some parameters and reexecute the same test (without a lot of copy and paste).
Adding additional details requested;
Approach A renders this to page when saving;
<br><span class="meta">variable defined: test=
| Table:myTest | someValue |
</span>
<br><br><table>
<tbody><tr class="slimRowTitle">
<td>Table:myTest</td>
<td>someValue</td>
</tr>
</tbody></table>
<br>
...but when running the test the page doesn't seem to process the table and shows just the variable definition
<br><span class="meta">variable defined: test=
| Table:myTest | someValue |
</span>
<br><br><br></div>
Try creating a separate page with the test table.
In your real test page you can include this page multiple times, after assigning values to the variables.

GGTS Null Pointer Exception

I'm a newbie here, and hopefully I can explain this thoroughly.
Working through Grails in Action book, Second Edition using the Groovy Grails Tool Suite - GGTS (aka Spring Tool Suite - STS).
GGTS release 3.6.4. Grails version 2.4.4
I'm still on Chapter 1. By this time, I've added several 'quotes' to my database. When I do a "println Quote.count()" through the Grails Console I see I have 4 quotes.
I try to run my random GSP and receive the following error:
Line | Method
->> 7 | doCall in C:/Users/donahujc/Documents/workspace-ggts-3.6.4.RELEASE/qotd/grails-app/views/quote/random.gsp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Caused by NullPointerException: Cannot get property 'content' on null object**
->> 7 | doCall in C__Users_donahujc_Documents_workspace_ggts_3_6_4_RELEASE_qotd_grails_app_views_quote_random_gsp$_run_closure2_closure4
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 10 | run in C__Users_donahujc_Documents_workspace_ggts_3_6_4_RELEASE_qotd_grails_app_views_quote_random_gsp
| 198 | doFilter in PageFragmentCachingFilter.java
| 63 | doFilter in AbstractFilter.java
| 1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 617 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run . . . in java.lang.Thread
What this is telling me is that my call is pointing to nothing. So, I go into the DBConsole back end, and sure enough, my Quote table (which contains Content and Author) isn't there.
How's that possible, when my DataSource.groovy file was changed to the following:
// environment specific settings
environments {
development {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
I changed "create-drop" to "update" and removed reference to memory (mem:).
I know the data is there, because I can use the Grails Console to query it.
The complicated part is that I can't go through the book all at once. So I've had to close and re-launch GGTS multiple times over several days. I thought that re-running the app would re-initialize the table, but that doesn't seem to be the case.
How can I get this table initialized? I tried adding a new quote. (Somehow my index went from quote #4 to quote #33.) But still no table for my GSP to pull from.
I'm just at a loss at how to get this table (and the data that's in there somewhere) initialized. This is something I'm going to have consistent problems with, because I'll be constantly closing/re-opening GGTS.
Help
EDIT: (Adding more of my code)
Quote Controller:
def random(){
def allQuotes = Quote.list()
def randomQuote
if (allQuotes.size() > 0){
def randomIdx = new Random().nextInt(allQuotes.size())
}else {
randomQuote = new Quote(author:"Anonymous",
content:"Real Programmers double peace out on quiche")
}
[quote:randomQuote]
}
Random.gsp
<html>
<head>
<title>Random Quote</title>
</head>
<body>
<div id="quote">
<q>${quote.content}</q>
<p>${quote.author}</p>
</div>
</body>
</html>
Quote.groovy
class Quote {
String author
String content
Date created = new Date()
static constraints = {
}
}
Everything has worked fine until now. I know my data is in the DB because I can query it from the Grails Console. But DB console doesn't even show my Quote table :(
You're going to kick yourself if that is in fact the code that you're using :). Note that uninitialised variables in Groovy are given a default value of null. So the initial value for the randomQuote variable in the random action is null.
The value of the randomQuote variable is then assigned to the quote variable in the view's model. Looking at the view, I can tell that the NullPointerException is being thrown by the ${quote.content} expression. So if quote is null in the model, that must mean that randomQuote is null in the action.
So what happens when there are quotes in the database? The action takes this branch:
if (allQuotes.size() > 0) {
def randomIdx = new Random().nextInt(allQuotes.size())
}
As you can see, there is no assignment to the randomQuote variable, so it remains null. The code from listing 1.3 of the book has this:
if (allQuotes.size() > 0) {
 def randomIdx = new Random().nextInt(allQuotes.size())
randomQuote = allQuotes[randomIdx]
}
Or at least I hope it does. It's showing up in my PDF version.
This was a little long-winded, but I'm hoping that you can follow the reasoning and use that to diagnose other issues that you encounter. I recognise that it's not always easy for newcomers to interpret the various errors they come across.

Resources