How to use aggregate functions in CodeEffects Rule Editor? - codeeffects

Can anyone Please guide me how can I create an aggregator like SUM(), AVG() in Codeeffects RuleEditor. And these should be passed with set of integer values from the rule editor.
Like if there is an attribute like score which is of int type.........
Rule will be like:
If score equal to Zero then set score to SUM(10,20,30,40)
After this is should get this score as 100

Define a list of integers in your source class:
public List<int> Integers {get;set;}
Define two action methods as well:
public void Sum ( List<int> list )
{
return list.Sum( );
}
public void Add ( int i )
{
this.Integers.Add( i );
}
public int Modulus (int operandOne, int operandTwo)
{
return operandOne % operandTwo;
}
Now give your class as the source to the rule editor and create your rule:
If Modulus( Score, 2 ) is equal to 3
then
Add(10) and Add(20) and Add(30) and Add(40) and
set Score to Sum ( Integers )

Related

Function that receives a char and returns the square power of it's integer value

I need to create a function that transmits a parameter as a 'char' and returns the value of that 'char' as a integer at its square value.
For example:
char nr[] = "11";
cout << strToNumber(nr);//will return the value 121;
int strToNumber (char c[]) {
int cifra, rezultat, fin;
cifra = c - '0';
return cifra * cifra;
}
int main () {
char sir[9] = "13";
char c;
int n = strlen(sir);
for (int i = 0; i < n; ++i) {
c = sir[i];
strToNumber(c);
}
return 0;
}
I tried to send the whole 'char' as a parameter one at a time. First I sent sir[0] which is '1' and afterwards I sent sir[1] which is '3'. The problem is that I did not manage to save the parameters in one variable so that I can easily multiply it by itself. In this case each parameter is multiplied by itself one at a time. '1' * '1' and '3' * '3'.
I am new to this 'char' and I do not fully understand it yet. If there is another way to send my parameters and to handle the function I would truly appreciate it... Thanks in advance!

LINQ query to order elements between two variables

a complete beginner here.
I am trying to create a JSONResult which I am trying to get to return between two variables.
Here is the action:
public JsonResult GetVoices(int? PageNumber, int? PageSize)
{
var StartRow = ((PageNumber - 1) * PageSize) + 1;
var EndRow = PageNumber * PageSize;
var i = 0;
var data = (from a in db.Content
select new
{
RowNumber = i++,
Id = a.Id,
Title = a.Title,
Description = a.Description,
CreatedAt = a.CreatedAt
}).ToList();
return Json(data, JsonRequestBehavior.AllowGet);
}
As you can see there are two parameters which eventually specify the Row to start from and end from. I want all the RowNumber returned to be between StartRow and LastRow
You're looking for something like this:
public JsonResult GetVoices(int PageNumber, int PageSize) =>
Json((from a in db.Content.ToArray()
let StartRow = ((PageNumber - 1) * PageSize) + 1
let EndRow = PageNumber * PageSize
from RowNumber in Enumerable.Range(StartRow, EndRow - StartRow + 1)
select new
{
RowNumber = RowNumber,
Id = a.Id,
Title = a.Title,
Description = a.Description,
CreatedAt = a.CreatedAt
}).ToList(), JsonRequestBehavior.AllowGet);
So you have a PageSize and a PageNumber, and you want all rows that are on the page with PageNumber.
Page 0 contains rows 0 to PageSize - 1
Page 1 contains rows PageSize to 2 * PageSize - 1
Page 2 contains rows 2 * PageSize to 3 * PageSize - 1
etc.
This seems to be a very common problem, so let's not limit ourselves to a solution for this problem only, let's make a solution suitable for all sequences that needs to be divided into pages.
My advice would be to create an extension method of IQueryable<T>, so you can intertwine the pagination with other LINQ methods. If you are not familiar with extension methods, consider to read Extension Methods Demystified
Input: a sequence of similar items: IQueryable<T>, a PageSize and a PageNumber.
Output: all rows on page PageNumber, according to the definition above. So each page, except the last one has PageSize number of rows. The last Page will have Total number of rows % PageSize
I decided to return IQueryable<T>, not ICollection<T>. This has the advantage that if you don't enumerate the rows on a page, the data is not fetched. The disadvantages is that you have to calculate the number of rows on each page, and the number of pages. If you think you will always enumerate all rows on all pages, consider to return ICollection<T>.
public IQueryable<T>> FetchPage<T>(
this IQueryable<T> source,
int pageNumber,
int pageSize)
{
// TODO: check input parameters: source not null, pageSize > 0, pageNumber >= 0
return source.Skip(pageNumber * pageSize)
.Take(pageSize);
}
Usage: if you have a table of Products, and you want to divide this into Pages of size 25, and you want to fetch Page 3, with the cheapest Products first:
IQueryable<Product> productsOnPage3 = dbContext.Products
.OrderBy(product => product.Price)
.FetchPage(3, 25)
.ToList();
If you don't want the complete row on your pages, you can add a Select.
Consider to create extension methods with parameters selector like in LINQ Select:
public IQueryable<T>> FetchPage<T>(
this IQueryable<T> source,
int pageNumber,
int pageSize,
Expression<Func<TSource,TResult>> selector)
{
// TODO: check input parameter selector
return source.FetchPage(pageNumber, pageSize)
.Select(selector);
}
And maybe also an overload with a sortKey.

Count unique values of consts

In my project I defined following consts:
(declare-const v0_g Int)
(declare-const v1_g Int)
(declare-const v2_g Int)
(declare-const v3_g Int)
(declare-const v4_g Int)
...
In result, I got following values in my model:
...
(define-fun v4_g () Int
2)
(define-fun v3_g () Int
10)
(define-fun v2_g () Int
10)
(define-fun v1_g () Int
8)
(define-fun v0_g () Int
0)
...
Now I want to define new const called cost and assign the number of unique values of vi_g (in the example above cost == 4 (i.e. {0,2,8,10}). How can I achieve it using z3 solver?
The only idea I came up with is:
Knowing the maximum value (MAXVAL) that can be assigned to any of vi_g, define MAXVAL boolean consts (ci),
For each of this consts make an assert that e.g. c0 = (v0_g == 0) v (v1_g == 0) v ... v (vn_g == 0),
Count how many ci const equals True.
However, it requires a lot of additional clauses if MAXVAL is large.
There's no easy way to count the number of models of a general formula. Either your specific formula allows some sort of simplification or it isn't easy. See e.g. literature for #SAT (https://en.wikipedia.org/wiki/Sharp-SAT).
A naïve way is to implement the counting with a linear loop blocking one model at a time (or potentially several ones if the model is partial).

Ask an uint, get an int

I know the use of anonym function have to be done with parsimony but :
private function getAnonymFct() : Function
{
return function () : void
{
var num : uint = -1;
if( num < uint.MIN_VALUE )
trace( "WTF ??" );
trace( getQualifiedClassName( num ) );
trace( num );
}
}
public function Main()
{
getAnonymFct()();
}
Will trace :
int
WTF ??
-1
Any ideas why my var num magically become an int instead of uint as typed ?
Type uint is shorthand for unsigned integer, -1 is not a valid unsigned integer, it is a signed integer and is less than uint.MIN_VALUE. I would assume, to avoid an obvious runtime error actionscript has converted num to type int.
In AS both unsigned and signed are stored as 32-bits and -1 in base10 as int would be 11111111111111111111111111111111 in base2, converting that to uint it would be 4294967295 in base 10, which is uint.MAX_VALUE and 10 orders of magnitude different to the original number

How to calculate total of all values assigned in JTextFields and ignore the empty ones?

Basically, I made lots of JTextFields and I'd like them to be calculated for the total. I want to loop across the the JTextFields calculating the total and ignoring the empty ones. It does not have to be a loop.
I made this simple method assigned to a button.
void groceryDept(){
int a = Integer.parseInt(appleField.getText());
int b = Integer.parseInt(bananaField.getText());
int c = Integer.parseInt(tomatoField.getText());
int e = Integer.parseInt(potatoField.getText());
int f = Integer.parseInt(lettuceField.getText());
int g = Integer.parseInt(cucumberField.getText());
int h = Integer.parseInt(watermelonField.getText());
int i = Integer.parseInt(pineappleField.getText());
int j = Integer.parseInt(celeryField.getText());
int k = Integer.parseInt(carrotField.getText());
int total = a+b+c+e+f+g+h+i+j+k;
totalLabel.setText(String.valueOf(total));
}
It only calculates when all JTextFields are filled. I am parsing them because it is made a string initially. If anyone knows how to make the JTextField accept Integers ONLY through netbeans, please let me know!
Oh and I'd also like to have a price that will multiply into the input of whatever quantity the user will input.
If anyone knows how to make the JTextField accept Integers ONLY
-> You want to use a JSpinner