How to add element to array with Drools (mvel) - drools

I need insert a new value in a exist array with Drools. My example:
rule "insert new address"
dialect "java"
when
$data : Data( source.address != null)
then
Address address = (Address) $data.source.address
System.out.println("Element: "+address );
$data.target.addressList.add(address);
end
The error that happend is this:
Exception executing consequence for rule "insert new address" in rules: [Error: $data.target.addressList.add(address): null]
EDIT: Added the model
public class Data {
private Source source;
private Client target;
}
public class Source {
...
private Address address;
}
public class Client {
...
private List<Address> addressList;
}

In answer to the question in your title, which is how to add an element to array -- the answer is basically "the same way you would in Java."
To answer the question you actually asked, which has no arrays, your error is effectively a NullPointerException, or another indicator that the field cannot be modified (eg an immutable list.)
This:
Error: $data.target.addressList.add(address): null]
Means that either $data.target or $data.target.addressList is null, or possibly $data.target.addressList is an immutable list.
Make sure that whatever "target" is has been initialized, and that its "addressList" is also initialized as a mutable list type.

Related

Use 2 objects of same class

I am new to Drools and have to compare value of 2 objects of same class.
i.e. let's say the class is
public class Person() {
public string Name;
}
So I want to compare person1's name and person2's name and throw an exception if they are equal.
It would be great if someone can give an example of the same.
That can easily be done with a rule similar to this:
rule 'Unique Names'
when
$p1: Person()
Person(this != $p1, name == $p1.name)
then
//error
end
I would discourage you to simply throw an exception in the then part of the rule though. It is a better idea to collect your results somewhere and then validate those results outside Drools.

Drools get object from map mvel cast exception

I am using jboss EAP 7.2 and Red Hat Decision Central 7.5.0
I have a custom objects like that
public class Model{
private String id;
private Map<String, Object> map;
// ... getters and setters
}
public class ParameterModel{
private String parameterName;
private BigDecimal maxValue;
private BigDecimal minValue;
private Object value;
// ... getters and setters
}
I have created new "Model" object that has custom "id" attribute and "map" attribute contains <parameterName, ParameterModel> pairs.
I sent it to decision manager and drools side ı want to achieve ParameterModel attributes but I could not.
My rule is below.
package com.rule.test;
import com.test.Model;
import com.test.ParameterModel;
rule "drools1"
when
Model(getId().equals("1"), Integer.parseInt(((ParameterModel)getMap().get("param1")).getValue().toString())>10)
then
System.out.println("Error on " + drools.getRule().getName());
end
The exception is below.
Caused by: [Error: null pointer:
Integer.parseInt(((ParameterModel)getMap().get("param1")).getValue().toString())]
[Near : {... Integer.parseInt(((ParameterMo ....}] In [Rule "drools1"
in com/rule/test/test.drl]
Caused by: java.lang.NullPointerException
at org.mvel2.DataConversion.convert(DataConversion.java:129)
at org.mvel2.ast.TypeCast.getReducedValueAccelerated(TypeCast.java:74)
at org.mvel2.compiler.ExecutableAccessor.getValue(ExecutableAccessor.java:38)
at org.mvel2.ast.Substatement.getReducedValueAccelerated(Substatement.java:44)
at org.mvel2.ast.Union.getReducedValueAccelerated(Union.java:44)
at org.mvel2.compiler.ExecutableAccessor.getValue(ExecutableAccessor.java:38)
at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:970)
at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:396)
Thank you all.
Drools has a lot of built-in null checking, but if you insist on bypassing it you're going to end up with a lot of errors.
The other kind of interesting Drools has is a special syntax for maps -- get("foo") can be written as this["foo"].
rule "drools1"
when
Model( id == "1", // equivalent to getId().equals("1")
$map: map != null )
// Special syntax for getting stuff from maps:
Map( $param1: this["param1"] != null ) from $map
ParameterModel( $value: value != null ) from $param1
Integer( this > 10 ) from Integer.parseInt($value.toString())
then
System.out.println("Error on " + drools.getRule().getName());
end
Why was your version giving a NPE? Not a clue, but it was also just about unreadable in its original form. All that really can be gleaned from the stack trace was that it was occurring at some implicit conversion step.
Of course this version will also fail if value isn't something whose toString() turns into a String representation of an integer.
Note that if value is actually an Integer type, then you don't need to waste time with the parse int and can just do:
Integer(this > 10) from $value

Play Framework 2.3 How to add unique constraint to sample application

Given the Play Framework 2.3 Computer Database sample application, I would like to practice adding a unique constraint on an attribute. Let's say I want the name attribute of the Computer class to be unique. I've tried to do this by adding a validate() function (and a getter) to Computer.java:
public List<ValidationError> validate() {
List<ValidationError> errors = new ArrayList<ValidationError>();
if(Computer.find.where().eq("name", getName()).findRowCount() != 0){
errors.add(new ValidationError("name", "Name must be unique. That value is already taken."));
}
return errors;
}
public String getName() {
return name;
}
This check works when creating new records in the database, however, this now causes a validation error when you update a Computer object but don't change the name. Is there a way to add a uniqueness constraint, similar to Rails? How can I validate uniqueness in Play?
Thanks!
UPDATE: see the answer by davide.
I ended up using the #Column(unique = true) constraint from the javax.persistence API. This doesn't generate an error in Play forms; instead, it throws a PersistenceException. Therefore I had to add change my controller to achieve the behavior I wanted. Both the create() and update() actions need a try/catch like this:
try {
computerForm.get().save();
} catch (PersistenceException pe) {
flash("error", "Please correct errors below.");
formData.reject("name", "Name conflict. Please choose a different name.");
return badRequest(createForm.render(computerForm));
}
UPDATE 2: each of the answers below is a possible solution
You need to exclude current entity from unique checking, i.e. like that:
if(Computer.find.where().eq("name", getName()).ne("id", getId()).findRowCount() != 0){
errors.add(new ValidationError("name", "Name must be unique."));
}
It will give you SQL query during update:
select count(*) from computer t0 where t0.name = 'Foo' and t0.id <> 123
And this during create:
select count(*) from computer t0 where t0.name = 'Foo' and t0.id is not null
P.S. ne() expression stands for Not Equal To and of course this approach assumes that your name field is Required
Edit: I sent you pull request with working solution, all you need is to add hidden field in your editForm like:
<input name="id" type="hidden" value='#computerForm("id").value'/>
Other thing is that you can simplify your model, i.e. don't need for getters for public fields.
I not sure if this answer your question, because I'm not familiar with Ruby syntax.
To "create a uniqueness constraint in the database" you can use the javax persistence API. Ebean will also recognize this.
To have a plain uniqueness constraint which involves a single field, you can use the #Column annotation:
#Entity
public class Computer extends Model {
...
#Column(unique = true)
public String name;
...
}
If you need some combination of fields to be unique, instead use the
#Table annotation
#Table(
uniqueConstraints=
#UniqueConstraint(columnNames={"name", "brand"})
)
#Entity
public class Computer extends Model {
...
public String name;
public String brand;
...
}
I hope it helps!

JPA Criteria api using IN expression with join

i have entity Request that have #ManyToMany Set<Region> regions, and Region entity have field region of type RegionEnum of enum type with constants of regions.
I need to create criteria to get requests, where its regions are in collection of RegionEnum;
In my choice:
List<RegionEnum> regs=...; // from method parameter
CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery<Request> cq=cb.createQuery(Request.class);
Root<Request> from=cq.from(Request.class);
cq.where(cb.isTrue(from.join("regions").get("region").in(regs)));
return em.createQuery(cq).getResultList();
I have an java.lang.IllegalArgumentException: PREDICATE_PASSED_TO_EVALUATION (There is no English translation for this message.)
enum:
public enum RegionEnum {
CENTRAL("Центральный"),
SOUTH("Южный"),
NWEST("Северо-Западный"),
FEAST("Дальневосточный"),
SIB("Сибирский"),
URFO("Уральский"),
VOLGA("Волжский"),
NCAU("Северо-Кавказский");
private final String value;
private Region(String value) {
this.value=value;
}
public String value() {
return this.value;
}
}
is my criteria right and problem with enum? or criteria is bad?
I have faced with the same exception today so I tried to solve the problem.
Let's assume we have the following instances already initialized (as in your example):
CriteriaBuilder cb;
List<String> values;
Root<Entity> r; // where Entity has String name attribute
Now we can create a Predicate instance this way which is working using EclipseLink 2.6.2 library:
Predicate good = r.get("name").in(values);
But if we try to wrap the predicate into the cb.isTrue() method (as you did it) which require an "Expression<Boolean>" parameter like this:
Predicate wrong = cb.isTrue(r.get("name").in(values));
the mentioned exception PREDICATE_PASSED_TO_EVALUATION will be raised as you have received it even the expression has right syntax.
I think because Expression is an Ancestor of Predicate class but this just an idea. At least the exception message says something like this.
So just remove the cb.isTrue() wrapping and probably it will work for you as it works for me.

Ormlite and PostgreSQL - Error inserting text array with custom persister

I have been working to setup Ormlite as the primary data access layer between a PostgreSQL database and Java application. Everything has been fairly straightforward, until I started messing with PostgreSQL's array types. In my case, I have two tables that make use of text[] array type. Following the documentation, I created a custom data persister as below:
public class StringArrayPersister extends StringType {
private static final StringArrayPersister singleTon = new StringArrayPersister();
private StringArrayPersister() {
super(SqlType.STRING, new Class<?>[]{String[].class});
}
public static StringArrayPersister getSingleton() {
return singleTon;
}
#Override
public Object javaToSqlArg(FieldType fieldType, Object javaObject) {
String[] array = (String[]) javaObject;
if (array == null) {
return null;
} else {
String join = "";
for (String str : array) {
join += str +",";
}
return "'{" + join.substring(0,join.length() - 1) + "}'";
}
}
#Override
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) {
String string = (String) sqlArg;
if (string == null) {
return null;
} else {
return string.replaceAll("[{}]","").split(",");
}
}
}
And then in my business object implementation, I set up the persister class on the column likeso:
#DatabaseField(columnName = TAGS_FIELD, persisterClass = StringArrayPersister.class)
private String[] tags;
When ever I try inserting a new record with the Dao.create statement, I get an error message saying tags is of type text[], but got character varying... However, when querying existing records from the database, the business object (and text array) load just fine.
Any ideas?
UPDATE:
PostGresSQL 9.2. The exact error message:
Caused by: org.postgresql.util.PSQLException: ERROR: column "tags" is
of type text[] but expression is of type character varying Hint: You
will need to rewrite or cast the expression.
I've not used ormlite before (I generally use MyBatis), however, I believe the proximal issue is this code:
private StringArrayPersister() {
super(SqlType.STRING, new Class<?>[]{String[].class});
}
SqlType.String is mapped to varchar in SQL in the ormlite code, and so therefore I believe is the proximal cause of the error you're getting. See ormlite SQL Data Types info for more detail on that.
Try changing it to this:
private StringArrayPersister() {
super(SqlType.OTHER, new Class<?>[]{String[].class});
}
There may be other tweaks necessary as well to get it fully up and running, but that should get you passed this particular error with the varchar type mismatch.