specman e lists constraints references - verification

i am trying to do the following :
unit parent {
sons: list of sons is instance;
grands: list of grands is instance;
keep sons.size() == 4;
keep grands.size() == 4;
};
unit sons {
grands:list of grands is instance;
keep grands == get_enclosing_unit(parent).grands.all( .id > 3 );
//this is not working
keep for each in grands {
it.parent_age == 70;
};
};
unit grands {
id: uint;
parent_age:uint;
};
extend sys {
p : parent is instance;
run() is also {
print p;
for each (s) in p.sons {
print s;
};
for each (g) in p.grands {
print g;
};
};
};
in other words , i want the sons list to point to a part of the parents list , but still be able to constraint(the not working part) the list of grands from the sons unit/struct.
With PGen constraint engine on 9.20, the above code produces:
Starting the test ...
Running the test ...
p = parent-#0: parent e_path: sys.p
hdl_path:
---------------------------------------------- #tmp
0 sons: (4 items)
1 grands: (4 items)
s = sons-#1: sons e_path: sys.p.sons[0]
hdl_path:
---------------------------------------------- #tmp
0 grands: (empty)
s = sons-#2: sons e_path: sys.p.sons[1]
hdl_path:
---------------------------------------------- #tmp
0 grands: (empty)
s = sons-#3: sons e_path: sys.p.sons[2]
hdl_path:
---------------------------------------------- #tmp
0 grands: (empty)
s = sons-#4: sons e_path: sys.p.sons[3]
hdl_path:
---------------------------------------------- #tmp
0 grands: (empty)
g = grands-#5: grands e_path: sys.p.grands[0]
hdl_path:
---------------------------------------------- #tmp
0 id: 4107502109
1 parent_age: 3829340118
g = grands-#6: grands e_path: sys.p.grands[1]
hdl_path:
---------------------------------------------- #tmp
0 id: 3657005019
1 parent_age: 2354335776
g = grands-#7: grands e_path: sys.p.grands[2]
hdl_path:
---------------------------------------------- #tmp
0 id: 3238917208
1 parent_age: 336300761
g = grands-#8: grands e_path: sys.p.grands[3]
hdl_path:
---------------------------------------------- #tmp
0 id: 1416976666
1 parent_age: 2212224392
With IntelliGen constraint engine on Specman 9.20, the above code produces:
Starting the test ...
Running the test ...
p = parent-#0: parent e_path: sys.p
hdl_path:
---------------------------------------------- #tmp
0 sons: (4 items)
1 grands: (4 items)
s = sons-#1: sons e_path: sys.p.sons[0]
hdl_path:
---------------------------------------------- #tmp
0 grands: (4 items)
s = sons-#2: sons e_path: sys.p.sons[1]
hdl_path:
---------------------------------------------- #tmp
0 grands: (4 items)
s = sons-#3: sons e_path: sys.p.sons[2]
hdl_path:
---------------------------------------------- #tmp
0 grands: (4 items)
s = sons-#4: sons e_path: sys.p.sons[3]
hdl_path:
---------------------------------------------- #tmp
0 grands: (4 items)
g = grands-#5: grands e_path: sys.p.grands[0]
hdl_path:
---------------------------------------------- #tmp
0 id: 619055518
1 parent_age: 4122406610
g = grands-#6: grands e_path: sys.p.grands[1]
hdl_path:
---------------------------------------------- #tmp
0 id: 2908565159
1 parent_age: 1741309063
g = grands-#7: grands e_path: sys.p.grands[2]
hdl_path:
---------------------------------------------- #tmp
0 id: 3091108084
1 parent_age: 1231835435
g = grands-#8: grands e_path: sys.p.grands[3]
hdl_path:
---------------------------------------------- #tmp
0 id: 1717477430
1 parent_age: 937745175
No actual running requested.
Checking the test ...
Checking is complete - 0 DUT errors, 0 DUT warnings.

I think you have generation order conflicts:
The sons.grands list is populated, depending on the grands.id field, which means you have to generate the grands list first.
The grands.parent_age depends on the sons parent_age == 70 constraint, which means you have to generate the sons list first.
The easiest and most straight forward way to solve this code issue ( and I know you're giving a dumbed down example) is :
extend parent {
keep for each (g) in grands {
( g.id > 3 ) => g.parent_age == 70;
};
};
After more testing, I'm pretty certain its a constraint ordering issue in combination with the method invocations. The Specman generator doesn't follow through the constraints on the sons' grand list constraint, unless you set those pointers without doing a method call.
<'
unit parent {
sons: list of sons is instance; // <-- swapped these two lines
grands: list of grands is instance; // <-- to do constraint ordering in IntelliGen
// even though Cadence says you don't need
// to
keep for each (s) in sons {
s.grands == grands; -- .all( .id > 3 ); -- removed the 'all' and 'get_enclosing_unit' invocation
};
keep sons.size() == 4;
keep grands.size() == 4;
};
unit sons {
grands:list of grands is instance;
--keep grands == get_enclosing_unit(parent).grands.all( .id > 3 );
//this is not working
keep for each in grands {
it.parent_age == 70;
};
};
unit grands {
id: uint;
parent_age:uint;
};
extend sys {
p : parent is instance;
run() is also {
print p;
for each (s) in p.sons {
print s;
};
for each (g) in p.grands {
print g;
};
};
};
'>
Using IntelliGen ( doesn't work with PGen ):
Starting the test ...
Running the test ...
p = parent-#0: parent e_path: sys.p
hdl_path:
---------------------------------------------- #tmp
0 sons: (4 items)
1 grands: (26 items)
s = sons-#1: sons e_path: sys.p.sons[0]
hdl_path:
---------------------------------------------- #tmp
0 grands: (26 items)
s = sons-#2: sons e_path: sys.p.sons[1]
hdl_path:
---------------------------------------------- #tmp
0 grands: (26 items)
s = sons-#3: sons e_path: sys.p.sons[2]
hdl_path:
---------------------------------------------- #tmp
0 grands: (26 items)
s = sons-#4: sons e_path: sys.p.sons[3]
hdl_path:
---------------------------------------------- #tmp
0 grands: (26 items)
g = grands-#5: grands e_path: sys.p.sons[3].grands[0]
hdl_path:
---------------------------------------------- #tmp
0 id: 4093923439
1 parent_age: 70
[snip]
You might have to look into using pre_generate()/post_generate() directly. You can also look into Cisco's csco_config package that they open sourced (here). We use that package to do weird constraints and constraint propagation in our environment. However, most constraints are top-down, whereas your example seems to be about peers modifying each other.
One other design note. 5 levels of lists constraining each other is a bit of a maintenance issue. Ideally, each level should only know about its child lists and its parent lists at the most. Providing a field and then rippling that field down into lower levels will insulate each level from having to know about all the other levels. However, I know there are reasons to violate design guidelines :-)

Related

how to fetch data of table after doing full outer join?

i am having a business problem
having a table which have following
|sku_id |solr_status |entry_context | mrp
1 active 10 20
1 inactive 10 30
1 active 10 22.5
2 inactive 10 10
2 inactive 10 31
filter the data into
table1->active
table2->inactive
now i have to do the according to condition
full outer join on 1 and 2 on sku_id.. Three possible cases
case (1 is not null, 2 is not null) => use 1
case (1 is null, 2 is not null) => use 2
case (1 is not null, 2 is null) => use 1
my code is something like that
val activeCatalog=dataframe.filter('solr_status===true).cache()
val inActiveCatalog=dataframe.filter('solr_status===false).cache()
val fullCatalog=activeCatalog.join(inActiveCatalog,Seq("sku"),joinType = "outer")
fullCatalog.show(false)
val resultCatalog = (activeCatalog,inActiveCatalog) match {
case (activeCatalog,inActiveCatalog) if(activeCatalog.count()!=0L && inActiveCatalog.count()!=0L)=>
fullCatalog.filter('solr_status===true).cache()
case (activeCatalog, inActiveCatalog) if (activeCatalog.count() == 0L && inActiveCatalog.count() != 0L) =>
fullCatalog.filter('solr_status === false).cache()
case (activeCatalog, inActiveCatalog) if (activeCatalog.count() != 0L && inActiveCatalog.count() == 0L) =>
fullCatalog.filter('solr_status === true).cache()
}
so using the approach i am getting ambiguous column error. also my
result data set should maintain the schema for active or inactive
table, doing outer join will create duplicate columns
any help ?

How to get what percentage of total users joined what number of meetings in PostgreSQL?

There are two tables, one is called "user_preference" that contains all users:
id | firstname | lastname | email |
And "match" which combines users with meetups they joined:
id | matcher | partner | meetup |
Both matcher and partner are foreign keys that represent user_preference.id, meaning that same user can be both matcher and a partner in the same meetup.
What I need to know is what percentage of total unique users joined what number of meetings.
For example:
17% of users joined 5 meetups
20% of users joined 3 meetups
40% of users joined 1 meetup
23% of users joined 0 meetups
The number of meetups should not be hardcoded but dynamic.
But I want to avoid duplication of users for a single meetup and count them only once. For example this:
id | matcher | partner | meetup |
1 | user1 | user2 | meetup1 |
2 | user1 | user3 | meetup1 |
3 | user5 | user1 | meetup1 |
4 | user6 | user1 | meetup2 |
Should count that user1 visited only 2 meetups.
What I managed to do so far is to display the count of meetups each user visited but that is not what I need:
SELECT distinct up.email users, COUNT(m.user) meetups
FROM user_preference up
LEFT JOIN
(
SELECT matcher AS user FROM match
UNION ALL
SELECT partner AS user FROM match
) m ON m.user = up.id
GROUP BY up.email
ORDER BY meetups desc;
In the end I did this by making simple queries and looping through them in the code, its far from elegant solution but it should work.
If someone posts SQL solution I will accept and upvote it...
export const getDevStats = async () => {
const users = await getRepository(UserPreference).query(
`SELECT * FROM user_preference;`
);
const meetups = await getRepository(Meetup).query(
`SELECT * FROM meetup;`
);
const matches = await getRepository(Match).query(
`SELECT * FROM match;`
);
let userMatches: any = {};
users.forEach((user: any) => {
userMatches[user.id] = []
matches.forEach((match: any) => {
if(user.id == match.matcher || user.id == match.partner) {
if(userMatches[user.id].indexOf(match.meetup) === -1) {
userMatches[user.id].push(match.meetup);
}
}
});
});
let matchStats: any = {};
for (var userId of Object.keys(userMatches)) {
if (typeof matchStats[userMatches[userId].length] === 'undefined') {
matchStats[userMatches[userId].length] = 0;
}
matchStats[userMatches[userId].length]++;
}
return {
users: users,
meetups: meetups,
matches: matches,
userMatches: userMatches,
matchStats: matchStats
};
};

Split SQL Column into Multiple Rows by Regex Match

I'm in the middle of converting an NTEXT column into multiple records. I'm looking to split the original column by new line or json object. It's a unique scenario, to be sure, but outside of a sql environment this regex correctly matches everything I need from the original column:
({(.*)(.*\r\n)*?})|(.+\r\n).
If I have a record with a column that has the value:
Foo bar baz
hello world
{
foo: 'bar',
bar: 'foo'
}
{
foo: 'foo',
bar: 'bar'
}
I want to break it into multiple records:
| ID | Text |
---------------------
| 1 | Foo bar baz |
| 2 | hello world |
| 3 | { |
| | foo: 'bar' |
| | bar: 'foo' |
| | } |
| 4 | { |
| | foo: 'foo' |
| | bar: 'bar' |
| | } |
Any easy way to accomplish this? It's a SQL Express server.
With the help of a split/parse function
Declare #String varchar(max)='Foo bar baz
hello world
{
foo: ''bar'',
bar: ''foo''
}
{
foo: ''foo'',
bar: ''bar''
}'
Select ID=Row_Number() over (Order By (Select NULL))
,Text = B.RetVal
From (Select RetSeq,RetVal = IIF(CharIndex('}',RetVal)>0,'{'+RetVal,RetVal) from [dbo].[udf-Str-Parse](#String,'{')) A
Cross Apply (
Select * from [dbo].[udf-Str-Parse](A.RetVal,IIF(CharIndex('{',A.RetVal)>0,char(1),char(10)))
) B
Where B.RetVal is Not Null
Returns
ID Text
1 Foo bar baz
2 hello world
3 {
foo: 'bar',
bar: 'foo'
}
4 {
foo: 'foo',
bar: 'bar'
}
The UDF if needed
CREATE FUNCTION [dbo].[udf-Str-Parse] (#String varchar(max),#Delimiter varchar(10))
Returns Table
As
Return (
Select RetSeq = Row_Number() over (Order By (Select null))
,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>'+ Replace(#String,#Delimiter,'</x><x>')+'</x>' as xml).query('.')) as A
Cross Apply x.nodes('x') AS B(i)
);
--Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
--Performance On a 5,000 random sample -8K 77.8ms, -1M 79ms (+1.16), -- 91.66ms (+13.8)

filter data with entity framework

I'm trying to get the right data
Rooms table
Id | Name
1 Room1
2 Room2
Resources table
Id | Name
1 Resource1
2 Resource2
3 Resource3
RoomResources table
Id | RoomId | ResourceId
1 1 1
2 1 2
3 1 3
4 2 2
5 2 3
I want select a room with Resource1 and Resource2
I'm using this code
int[] ids = sResources.Split(',').Select(s => int.Parse(s)).ToArray();
rooms = from r in context.Rooms
where r.Area.Office.Id == officeId
&& r.MaximumPeople >= numberOfPeople
&& r.RoomResources.Any(s => ids.Contains(s.ResourceId))
select r;
but it return Room1 and Room2 and the result should be Room1
Maybe this?
int[] ids = sResources.Split(',').Select(s => int.Parse(s)).ToArray();
rooms = from r in context.Rooms
where r.Area.Office.Id == officeId
&& r.MaximumPeople >= numberOfPeople
&& ids.All(i => r.RoomResources.Any(s => s.ResourceId == i)) // try this here
select r;

entity framework distinct by field

i have table kopija that goes like this:
idKopija | idFilm | nije_tu
1 | 1 | 0
2 | 1 | 0
3 | 1 | 1
4 | 2 | 1 and etc.
And i have query that goes like this:
var upit = from f in baza.films
join z in baza.zanrs on f.idZanr equals z.idZanr
join k in baza.kopijas on f.idFilm equals k.idFilm
select new
{
idFilm = f.idFilm,
nazivFilm = f.naziv,
nazivZanr = z.naziv,
idZanr = f.idZanr,
godina = f.godina,
slika = f.slika,
klip = f.klip,
nijeTu = k.nije_tu
};
if (checkBox1.Checked)
upit = upit.Where(k => k.nijeTu == 0).Distinct();
else
{
upit = upit.Where(k => k.nijeTu == 0 || k.nijeTu == 1).Distinct();
}
Now i want to make a distinct list of "idFilm". But prolem is that I get idFilm on two places because one of them has nije_tu=0 and other one has nije_tu=1.
Please someone help me.
Thank you.
What about
upit.Where(k => k.nijeTu == 0 || k.nijeTu == 1).Select(x => x.idFilm).Distinct();
?