FIND instances of a referred object

If you have questions or if you want to share your opinion about Aware IM post your message on this forum
Post Reply
edvwruhs
Posts: 20
Joined: Wed Jan 18, 2023 11:18 pm

FIND instances of a referred object

Post by edvwruhs »

I have two objects called "Projects" and "Landfills", both are multiple allowed and mapped to the RegularUser. What I am trying to do is to FIND the "Landfills" in the Projects (In a Project one or more landfills can be defined for the project) that are defined in the RegularUser.
Projects defined in the RegularUser
Projects defined in the RegularUser
regularUserProjects.PNG (23.1 KiB) Viewed 10039 times


The problem I have is within my rule. I do find the landfills, but only for the first two Projects and not for all Projects that are defined. I am not really sure how to write the Code, because I thought the 'Other'-Keyword refers to all non-first instances of a Object, so I thought the ThisProject.Landfill would find the first Project with all its instances of landfill and than OtherProject.Landfill would find all other Projects with all instances of landfill.

Any ideas how can I achieve this?

Code: Select all

If ThisRegularUser.Landfill IS UNDEFINED then
FIND Project WHERE (Project IN ThisRegularUser.Project)
FIND Landfill WHERE (Landfill IN ThisProject.Landfill OR Landfill IN OtherProject.Landfill)
INSERT Landfill IN RegularUser.Landfill

This is the result, it finds and inserts 6 records, but it should find and insert about 12 records.
Landfills of the defined Projects.
Landfills of the defined Projects.
landfillsRegularUser.PNG (11.83 KiB) Viewed 10039 times
PointsWell
Posts: 1457
Joined: Tue Jan 24, 2017 5:51 am
Location: 'Stralya

Re: FIND instances of a referred object

Post by PointsWell »

Multiple relationships don't work with IS DEFINED

See this
edvwruhs
Posts: 20
Joined: Wed Jan 18, 2023 11:18 pm

Re: FIND instances of a referred object

Post by edvwruhs »

PointsWell wrote: Mon Apr 03, 2023 9:20 am Multiple relationships don't work with IS DEFINED

See this
I am not really sure what you mean in this context :| , I want to find the landfills that are attached to the Projects (every Project has at least one landfill)
PointsWell
Posts: 1457
Joined: Tue Jan 24, 2017 5:51 am
Location: 'Stralya

Re: FIND instances of a referred object

Post by PointsWell »

Your rule is triggered by

Code: Select all

If ThisRegularUser.Landfill IS UNDEFINED
And you said
"Landfills", both are multiple allowed and mapped to the RegularUser
If RegularUser.Landfill is a peer multiple relationship, then the trigger for your rule is going to give you unpredictable results.
edvwruhs
Posts: 20
Joined: Wed Jan 18, 2023 11:18 pm

Re: FIND instances of a referred object

Post by edvwruhs »

I see, I tried it without the if-condition but I get the same result :(
PointsWell
Posts: 1457
Joined: Tue Jan 24, 2017 5:51 am
Location: 'Stralya

Re: FIND instances of a referred object

Post by PointsWell »

edvwruhs wrote: Mon Apr 03, 2023 1:44 pm I see, I tried it without the if-condition but I get the same result :(
What does the log file say?

When the FINDs run, how many Project and Landfill BOs does the log say that it has found?

What does the log report is happening when the INSERT statement is called?
Rennur
Posts: 1191
Joined: Thu Mar 01, 2012 5:13 am
Location: Sydney, Australia

Re: FIND instances of a referred object

Post by Rennur »

If ThisRegularUser.Landfill IS UNDEFINED then
FIND Project WHERE (Project IN ThisRegularUser.Project)
FIND Landfill WHERE (Landfill IN ThisProject.Landfill OR Landfill IN OtherProject.Landfill)
INSERT Landfill IN RegularUser.Landfill
Try replacing ThisRegularUser WITH LoggedInRegularUser in your query.

This might work:

Code: Select all

FIND Landfill WHERE EXISTS Project WHERE (Project IN LoggedInRegularUser.Projects) AND EXISTS Landfill WHERE (Landfill in Projects.Landfill) 
INSERT Landfill IN LoggedIRegularUser.Landfill
edvwruhs
Posts: 20
Joined: Wed Jan 18, 2023 11:18 pm

Re: FIND instances of a referred object

Post by edvwruhs »

PointsWell wrote: Mon Apr 03, 2023 10:33 pm
edvwruhs wrote: Mon Apr 03, 2023 1:44 pm I see, I tried it without the if-condition but I get the same result :(
What does the log file say?

When the FINDs run, how many Project and Landfill BOs does the log say that it has found?

What does the log report is happening when the INSERT statement is called?
Well, it finds 5 Projects (as expected), but only 6 landfills out of 12 (I checked it the first 2 Projects have in total 6 landfills, so it doesnt find the landfills of the other 3 Projects). And it inserts the 6 landfills that are found.
Last edited by edvwruhs on Tue Apr 04, 2023 10:34 am, edited 1 time in total.
edvwruhs
Posts: 20
Joined: Wed Jan 18, 2023 11:18 pm

Re: FIND instances of a referred object

Post by edvwruhs »

Rennur wrote: Tue Apr 04, 2023 2:09 am
If ThisRegularUser.Landfill IS UNDEFINED then
FIND Project WHERE (Project IN ThisRegularUser.Project)
FIND Landfill WHERE (Landfill IN ThisProject.Landfill OR Landfill IN OtherProject.Landfill)
INSERT Landfill IN RegularUser.Landfill
Try replacing ThisRegularUser WITH LoggedInRegularUser in your query.

This might work:

Code: Select all

FIND Landfill WHERE EXISTS Project WHERE (Project IN LoggedInRegularUser.Projects) AND EXISTS Landfill WHERE (Landfill in Projects.Landfill) 
INSERT Landfill IN LoggedIRegularUser.Landfill
Unfortunately that doesnt work :(
PointsWell
Posts: 1457
Joined: Tue Jan 24, 2017 5:51 am
Location: 'Stralya

Re: FIND instances of a referred object

Post by PointsWell »

Code: Select all

If ThisRegularUser.Landfill IS UNDEFINED then
FIND Project WHERE (Project IN ThisRegularUser.Project)
FIND Landfill WHERE (Landfill IN ThisProject.Landfill OR Landfill IN OtherProject.Landfill)
INSERT Landfill IN RegularUser.Landfill
Just looking at the FIND Landfill statement, why are you using This and Other for Project?

This and Other are typically used for distinguishing between different BOs being brought into context. For example when you have a Project already in context but then bring another into context, but in this case you are bringing all of the Projects into context at once.

I don’t know what your data model is but it seems that you have

RegularUser has many Projects and
Projects has many Landfills and
RegularUer has many Landfills

This works out as

RegularUser -< Projects -< Landfills >- RegularUser

Is this correct?

You need to look at the log and work out which of the landfills are being brought into context via the FIND and which Project these Landfills are associated with. Without knowing more my gut feeling is that This and Other are causing problems. Try just

Code: Select all

FIND Landfill WHERE (Landfill IN Project.Landfill)
edvwruhs
Posts: 20
Joined: Wed Jan 18, 2023 11:18 pm

Re: FIND instances of a referred object

Post by edvwruhs »

Yes exactly this is my data model structure, I tried This and Other because I thought the "this" would find the first project in context and the "other" would find the other 4 projects in context, but unfortunately it only found the second project in context. So to summarize it only found the first and the second project and than inserted the landfills of those 2 projects.

I tried your suggestion (FIND Landfill WHERE (Landfill IN Project.Landfill), but unfortunately it finds 0 records.
PointsWell
Posts: 1457
Joined: Tue Jan 24, 2017 5:51 am
Location: 'Stralya

Re: FIND instances of a referred object

Post by PointsWell »

Use the data model tool and diagram the relationship between RegularUser, Project and Landfill and post the image.

If you have multiple BOs that are reference attributes to multiple other BOs you're going to find it hard to build these queries.

Another tip is that using attributes set as multiple references which then are related to other BOs through multiple reference attributes then these are often better offloaded to another BO as this allows you to track when a relationship is created and ended and by whom. It also makes it easier to uniquely reference the relationship

For example

Project >-< Landfill becomes

Project >- ProjectLandfill -< Landfill

ProjectLandfill then is uniquely referenceable via Landfill and Project and can have a lifecycle within that relationship (e.g. opened, in use, decommissioned) as well as having other attributes assigned.

Also, be aware that if you place long chains of related BOs into the LoggedInRegularUser (LIRU) then AIM will evaluate the SQL for that chain all the time. This may cause efficiency issues especially where you have got a recursive loop in your datamodel between LIRU Project Landfill. On your server go to the Logging settings and turn on the SQL logging, this will show you the SQL being executed.
Last edited by PointsWell on Tue Apr 11, 2023 9:51 pm, edited 1 time in total.
customaware
Posts: 2391
Joined: Mon Jul 02, 2012 12:24 am
Location: Ulaanbaatar, Mongolia

Re: FIND instances of a referred object

Post by customaware »

Not sure it this is what you are looking for but....

There is a single RegularUser (Admin)
It Creates 3 Projects... Project A, Project B and Project C
Project A and project B are Owned by RegularUser. Project C is not Owned by RegularUser
It Creates 3 Landfills that are Owned by RegularUser... Landfill 1, Landfill 2 and Landfill 3
If Creates 3 Landfills that have a Peer relationship with Project A.... Landfill ProjectA -X, Landfill ProjectA -Y and Landfill ProjectA -Z
It Creates 3 Landfills that have a Peer relationship with Project B.... Landfill ProjectB -R, Landfill ProjectB -S and Landfill ProjectB -T
It Creates 3 Landfills that have a Peer relationship with Project C.... Landfill ProjectC -M, Landfill ProjectC -N and Landfill ProjectC - O

So, from the RegularUser we want to... FIND the "Landfills" in the Projects that are defined in the RegularUser.
Hence, if Project A and Project B are Owned By RegularUser.... Then we want a query that shows....
Landfill ProjectA -X,
Landfill ProjectA -Y,
Landfill ProjectA -Z,
Landfill ProjectB -R,
Landfill ProjectB -S,
Landfill ProjectB -T

We should NOT see...
Landfill ProjectC -M,
Landfill ProjectC -N,
Landfill ProjectC - O
As these are Owned by a Project NOT Owned by Regular User

And we should NOT see....
Landfill 1,
Landfill 2,
Landfill 3
As these are Owned By RegularUser but not related to a Project.

Here is the self data generating Demo...

The Test Query is launched from the Button at top left the RegularUser Edit Form... "Show Landfills In this user Projects"

Let me know if that is what you wanted?
Landfill.zip
(42.09 KiB) Downloaded 979 times
Cheers,
Mark
_________________
AwareIM 6.0, 8.7, 8.8, 9.0 , MariaDB, Windows 10, Ubuntu Linux. Theme: Default, Browser: Arc
Upcloud, Obsidian....
Image
edvwruhs
Posts: 20
Joined: Wed Jan 18, 2023 11:18 pm

Re: FIND instances of a referred object

Post by edvwruhs »

eagles9999 wrote: Tue Apr 11, 2023 2:56 pm Not sure it this is what you are looking for but....

There is a single RegularUser (Admin)
It Creates 3 Projects... Project A, Project B and Project C
Project A and project B are Owned by RegularUser. Project C is not Owned by RegularUser
It Creates 3 Landfills that are Owned by RegularUser... Landfill 1, Landfill 2 and Landfill 3
If Creates 3 Landfills that have a Peer relationship with Project A.... Landfill ProjectA -X, Landfill ProjectA -Y and Landfill ProjectA -Z
It Creates 3 Landfills that have a Peer relationship with Project B.... Landfill ProjectB -R, Landfill ProjectB -S and Landfill ProjectB -T
It Creates 3 Landfills that have a Peer relationship with Project C.... Landfill ProjectC -M, Landfill ProjectC -N and Landfill ProjectC - O

So, from the RegularUser we want to... FIND the "Landfills" in the Projects that are defined in the RegularUser.
Hence, if Project A and Project B are Owned By RegularUser.... Then we want a query that shows....
Landfill ProjectA -X,
Landfill ProjectA -Y,
Landfill ProjectA -Z,
Landfill ProjectB -R,
Landfill ProjectB -S,
Landfill ProjectB -T

We should NOT see...
Landfill ProjectC -M,
Landfill ProjectC -N,
Landfill ProjectC - O
As these are Owned by a Project NOT Owned by Regular User

And we should NOT see....
Landfill 1,
Landfill 2,
Landfill 3
As these are Owned By RegularUser but not related to a Project.

Here is the self data generating Demo...

The Test Query is launched from the Button at top left the RegularUser Edit Form... "Show Landfills In this user Projects"

Let me know if that is what you wanted?

Landfill.zip
Yes! Thank you that solved my Problem, you are great!
edvwruhs
Posts: 20
Joined: Wed Jan 18, 2023 11:18 pm

Re: FIND instances of a referred object

Post by edvwruhs »

PointsWell wrote: Tue Apr 11, 2023 12:35 pm Use the data model tool and diagram the relationship between RegularUser, Project and Landfill and post the image.

If you have multiple BOs that are reference attributes to multiple other BOs you're going to find it hard to build these queries.

Another tip is that using attributes set as multiple references which then are related to other BOs through multiple reference attributes then these are often better offloaded to another BO as this allows you to track when a relationship is created and ended and by whom. It also makes it easier to uniquely reference the relationship

For example

Project >-< Landfill becomes

Project >- ProjectLandfill -< Landfill

ProjectLandfill then is uniquely referenceable via Landfill and Project and can have a lifecycle within that relationship (e.g. opened, in use, decommissioned) as well as having other attributes assigned.

Also, be aware that if you place long chains of related BOs into the LoggedInRegularUser (LIRU) then AIM will evaluate the SQL for that chain all the time. This may cause efficiency issues especially where you have got a recursive loop in your datamodel between LIRU Project Landfill. On your server go to the Logging settings and turn on the SQL logging, this will show you the SQL being executed.
My Problem is already solved, but still thank you for your input, it was very helpful!
Post Reply