Drools indecate variable as unexpected - drools

I wan't return from query Company object. But IDE indecate company as unexpected
enter image description here
What is my error:
dialect "java"
query getBalance(World world, Company company)
$company := Company(companyName == "Intel") from world.getCompanies()
end
rule "BMWCarsToPerson"
when
$world : World()
$company : Company()
getBalance($world, $company)
/*$company : Company() from $world.getCompanies()*/
$manager : Manager() from $company.getManagers()
exists Car(name == BrandOfCar.BMW) from $manager.getCars()
$bmwCarsFromManager : Car(
name == BrandOfCar.BMW
) from $manager.getCars()
$person : Person(
name == "Elvis"
) from $world.getPersons()
/*$listMageners : List() from $manager*/
then
end

You shouldn't (and cannot) use a query for this simple task. Replace
$company : Company()
getBalance($world, $company)
by
$company: Company( companyName == "Intel" ) from $world.getCompanies()

Related

PowerShell DataTable delete column containing value "description"

I would like to delete all empty rows from the DataTable below as well as delete the column containing the value "description". I am able to delete all empty rows; however, I can't figure out how to also explicitly delete description from the DataTable.
Original DataTable:
dev_id : 4721
office_id : 355
name : Bobby
ip : 10.10.10.1
ipv6 : 2001:1930:ff28:ff00::1
mac :
serial_num : XX234555
platform : Supercode
:
description : I;need;to;delete;this;
:
:
max_speed : 100000
Code:
$myreader = $mycommand.ExecuteReader()
$DevicesDataTable.Load($myreader)
$myconnection.Close()
$Columns = $DevicesDataTable.Columns.Count
$Rows = $DevicesDataTable.Rows.Count
for ($r = 0; $r -lt $Rows; $r++) {
$Empty = 0
for ($c = 0; $c -lt $Columns; $c++) {
if (($DevicesDataTable.Rows[$r].IsNull($c)) -or ($DevicesDataTable.Rows[$r].Equals("description"))) {
$Empty++
}
}
if ($Empty -eq $Columns) {
$DevicesDataTable.Rows[$r].Delete()
}
}
# Delete
$DevicesDataTable.AcceptChanges()
Write-Output $DevicesDataTable
Result:
Note DataTable below still shows description:
dev_id : 4721
office_id : 355
name : Bobby
ip : 10.10.10.1
ipv6 : 2001:1930:ff28:ff00::1
serial_num : XX234555
platform : Supercode
description : I;need;to;delete;this;
max_speed : 100000
Note: I am able to identify the column containing "description", however, I'm not sure how to delete the respective row via the correct method:
if ($DevicesDataTable.Columns[$c].ColumnName.Equals("description"))
You can drop a column from the data table by calling Remove() on the Columns collection:
$DevicesDataTable.Columns.Remove('description')

Split PSCustomObjects in to strings/array

Hi I have n sum of objects for each Node like this:
NodeName : 11111
System.AreaId : 2375
System.AreaPath : Project
System.TeamProject : Project
System.NodeName : Project
System.AreaLevel1 : Project
Every node can have different objects in it.
How can I split them to an arrays/strings without specifying the object name so I can create foreach separate object loop?
mklement0 beat me to what I was going to post. Since I have the code drafted already I will post it.
Like mklement0 said in comments, you can access object properties through use of .psobject.Properties. Below in the code I am using a switch statement to check if an object contains a specific property.
$objs = #(
[pscustomobject]#{
AreaId = 2375
AreaPath = ''
TeamProject = 'Project2'
NodeName = ''
AreaLevel1 = ''
},
[pscustomobject]#{
AreaId = 342
AreaPath = ''
TeamProject = 'Project2'
Color = 'Red'
}
)
switch ($objs) {
{ $_.psobject.properties.name -contains 'Color' } {
'Object contains Color property'
}
{ $_.psobject.properties.name -contains 'NodeName' } {
'Object contains NodeName property'
}
Default {}
}

How to check for null value in accumulate Drools

Iam first time user of Drools. In accumulate function I have a scenario to check for the (!= null) feature of java. But in Drools I find that != doesn't work .
when
$addr : String(length > 0) from accumulate(
$person : Person(),
action(
$addresses : $person.getAddresses();
// i want to check whether $addresses is not
null.
if($addresses !=null) is what i want to achieve
)
Any help is really appreciated.
Edit the rule as Below :
Person(adresses !=null) && $addr : String(length > 0) from accumulate(
$person : Person(),
action(
$addresses : $person.getAddresses();
)

Update embedded subdocument in array with doctrine and mongodb

I want to update (replace) a subdocument within an array of all the documents, that have this specific embedded subdocument.
My sample content object is:
{
"_id" : ObjectId("51f289e5345f9d10090022ef"),
"title" : "This is a content",
"descriptors" : [
{
"_id" : ObjectId("51f289e5345f9d10090022f4"),
"name" : "This is a descriptor",
"type" : "This is a property"
},
{
"_id" : ObjectId("51f289e5345f9d10090022f0"),
"name" : "This is another descriptor",
"type" : "This is another property"
}
]
}
I want to find the object with a specific descriptor._id.
I want to replace the one subdocument with another one (respectivley with an updated version of the old object, but not necessarily with the same properties).
While this works well in RoboMongo / shell...
db.Content.update(
{
'descriptors._id': ObjectId("51f289e5345f9d10090022f4")
},
{
$set: {
'descriptors.$': {
"_id" : ObjectId("51f289e5345f9d10090022f4"),
"name" : "This is the updated descriptor",
"category" : "This is a new property"
}
}
},
{
'multi': true
})
...and with the plain php methods...
$descriptor = array();
$descriptor['_id'] = new \MongoID('51f289e5345f9d10090022f4');
$descriptor['name'] = 'This is the updated descriptor';
$descriptor['category'] = 'This is a new property';
$mongo = new \Mongo('mongodb://localhost:27017');
$database = $mongo->selectDB('MyDatabase');
$output = $database->selectCollection('Content')->update(
array('descriptors._id' => $descriptor['_id']),
array('$set' => array('descriptors.$' => $descriptor)),
array("multiple" => true)
);
...it doesn't work with Doctrine MongoDB ODM...
$descriptor = array();
$descriptor['_id'] = new \MongoID('51f289e5345f9d10090022f4');
$descriptor['name'] = 'This is the updated descriptor';
$descriptor['category'] = 'This is a new property';
$query = $dm->createQueryBuilder('Content')
->update()->multiple(true)
->field('descriptors._id')->equals($descriptor['_id'])
->field('descriptors.$')->set($descriptor)
->getQuery()->execute();
...because it fails with the following error:
Notice: Undefined offset: 2 in C:\MyProject\vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\Persisters\DocumentPersister.php line 998
So I assume, that Doctrine MongoDB ODM needs three parts in the dot-notation. The not so nice solution would be to iterate over the properties of the subdocument and set them manually.
$descriptor = array();
$descriptor['_id'] = new \MongoID('51f289e5345f9d10090022f4');
$descriptor['name'] = 'This is the updated descriptor';
$descriptor['category'] = 'This is a new property';
$query = $dm->createQueryBuilder('Content')
->update()->multiple(true)
->field('descriptors._id')->equals($descriptor['_id']);
foreach ($descriptor as $key => $value) {
$query->field('descriptors.$.'.$key)->set($value);
}
$query->getQuery()->execute();
But this will only update existing and add new properties, but won't remove old/unnecessary properties from the subdocument.
Any ideas how to solve the problem:
with a simple query
while using Doctrine MongoDB ODM
without looping over the subdocument-array in php
I'm using:
Mongo-Server: 2.4.5
PHP: 5.4.16
PHP-Mongo-Driver: 1.4.1
Composer:
"php": ">=5.3.3",
"symfony/symfony": "2.3.*",
"doctrine/orm": ">=2.2.3,<2.4-dev",
"doctrine/doctrine-bundle": "1.2.*",
"doctrine/mongodb-odm": "1.0.*#dev",
"doctrine/mongodb-odm-bundle": "3.0.*#dev"
This was a bug in ODM and should be fixed in PR #661. Please take a look and ensure that the revised test satisfies your use case.
Until the next beta of ODM is tagged, this will only reside in the master branch; however, that should match your 1.0.*#dev version requirement.
You can do this thing but in a "not easy" way becuse when you use EmbedMany or ReferenceMany in Doctrine becames a Doctrine ArrayCollection objects that implemented the ArrayCollection contains() method with (as explained here) with the PHP in_array() method with the $strict parameter set to true... this way works with ReferenceMany but not with EmbedMany!
The concept is: ArrayCollection -> array -> ArrayCollection
In the definition of Document
...
/**
* #ODM\EmbedMany(targetDocument="EmbeddedElement")
*/
private $elements = array();
...
public function getElements() { return $this->elements; }
public function setElements($elements) { $this->elements = $elements; }
public function addElement(Element $element) { $this->elements[] = $element; }
public function setElement(Element $element, $index=0)
{
$elementsArray = $this->elements->toArray();
$elementsArray[$index] = $element;
$this->elements = new ArrayCollection($elementsArray);
}
public function removeElement(Element $element)
{
$index = array_search($element, $this->elements->toArray(), $strict=false);
if($index !== false)
$this->elements->remove($index);
}
public function removeElementByIndex($index) { $this->elements->remove($index); }

How do I bind DBI parameters at runtime in Perl?

I have the following code :
sub run_query {
my $name = shift || undef;
my $sql = (defined $name ) ? "select * from table where name = ?" :
"select * from table";
my $sth = $dbh->prepare("$sql");
$sth->execute($name);
}
The above subroutine need to work as follows: if $name is provided, then run the first query, else fetch all the data from the table. How can I bind the name field? I'd like it bound dynamically if it is provided.
From the DBI documentation on cpan:
A common issue is to have a code fragment handle a value that could be
either defined or undef (non-NULL or NULL) at runtime. A simple
technique is to prepare the appropriate statement as needed, and
substitute the placeholder for non-NULL cases:
$sql_clause = defined $age ? "age = ?" : "age IS NULL";
$sth = $dbh->prepare(qq{
SELECT fullname FROM people WHERE $sql_clause
});
$sth->execute(defined $age ? $age : ());
It does not exactly apply to your question, which I assume is that your execute fails if you add an argument where one is not expected. So, the last line here would apply:
$sth->execute(defined $name ? $name : ());
You should probably have two different subs, but you could use
sub run_query {
my $sql = #_
? "select * from table where name = ?"
: "select * from table";
my $sth = $dbh->prepare($sql);
$sth->execute(#_);
}
You can conditionally omit parameters if $name is not defined:
$sth->execute(defined $name ? $name : ());