Drools with eval function rule won't fire - eval

I have the following rule:
rule "AddSource"
when
$model : MFMModel ()
Node( type == "source", funName : name )
$ffs : Structure( ffsName : name )
WholePart( structure == ffsName, ffunction == funName )
eval (Test.checkExsit($model,$ffs))
then
System.out.println( ffsName + ":" + funName);
Source s = new Source( funName );
insert (s);
$ffs.addToStructure( s );
System.out.println(Test.checkExsit($model,$ffs));
end
The rule fires when I delete the eval function in the when part, and the printout says Test.checkExsit($model,$ffs) return true.
But with the eval function, the rule will never fire.
Anyone knows what the problem is?

Related

Antlr4 parsing templateLiteral in jsx

I have tried to use grammar defined in grammars-v4 project(https://github.com/antlr/grammars-v4/tree/master/javascript/jsx) to parse jsx file.
When I parse the code snippet below,
let str =
`${dsName}${parameterStr ? `( ${parameterStr} )` : ""}${returns ? `{
${returns}}` : ""}`;
it shows the following error
line 2:32 at [#8,42:42='(',<8>,2:32]:no viable alternative at input '('
https://astexplorer.net/ shows it is a TemplateLiteral with conditionalExpression inside, any thoughts for parsing such grammar?
Thanks in advance.
Edit:
Thanks #Bart, it works.
It works fine on the following code.
const href1 = `https://example.com/lib/downloads_${page}.htm?joinSource=${joinSource}${inviter?`&inviter=${inviter}`: ''}`;
const href2 = `https://example.com/act/kol/detail_${this.props.values.list.res.rows && this.props.values.list.res.rows[0].id}_${page}.htm?joinSource=${joinSource}${inviter?`&inviter=${inviter}`: ''}`;
Given Mike's observation that it's the template string that is messing things up, here's a quick solution:
Changes:
JavaScriptLexerBase.java
Add the instance variable:
// Keeps track of the the current depth of nested template string backticks.
// E.g. after the X in:
//
// `${a ? `${X
//
// templateDepth will be 2. This variable is needed to determine if a `}` is a
// plain CloseBrace, or one that closes an expression inside a template string.
protected int templateDepth = 0;
JavaScriptLexer.g4
// Place TemplateCloseBrace above CloseBrace!
TemplateCloseBrace: {this.templateDepth > 0}? '}' -> popMode;
...
// Remove (or comment) TemplateStringLiteral
// TemplateStringLiteral: '`' ('\\`' | ~'`')* '`';
BackTick
: '`' {this.templateDepth++;} -> pushMode(TEMPLATE)
;
...
// Place at the end of the file:
mode TEMPLATE;
BackTickInside
: '`' {this.templateDepth--;} -> type(BackTick), popMode
;
TemplateStringStartExpression
: '${' -> pushMode(DEFAULT_MODE)
;
TemplateStringAtom
: ~[`]
;
JavaScriptParser.g4
singleExpression
: ...
| singleExpression templateStringLiteral # TemplateStringExpression // ECMAScript 6
| ...
;
literal
: ...
| templateStringLiteral
| ...
;
templateStringLiteral
: BackTick templateStringAtom* BackTick
;
templateStringAtom
: TemplateStringAtom
| TemplateStringStartExpression singleExpression TemplateCloseBrace
;
I did not fully test my solution, but it parses your example input successfully:
A quick look at the ANTLR grammar shows:
// TODO: `${`tmp`}`
TemplateStringLiteral: '`' ('\\`' | ~'`')* '`'
So, apparently, it’s on the “TODO List”. Allowing nested “`” string interpolation is surprising (to say the least), and will be “non-trivial” for the ANTLR Lexer (So, I would not "hold my breather" waiting on a fix for this).
Have you tried: (replacing the nested “`”s with “‘s?).
let str =
`${dsName}${parameterStr ? “( ${parameterStr} )” : ""}${returns ? “{
${returns}}” : ""}`;

Lua : timer.cancel --> 'Attempt to index a nil value'

I'm fairly new to Lua, and one particular command in my code has been causing me some major problems.
I've been trying to cancel the timer:
currentPuddleDelay = timer.performWithDelay(puddleDelay, createPuddle);
The error that I am shown is:
Attempt to index a nil value
File: ?
stack traceback:
?: in function 'cancel'
game.lua:534: in function '?'
?: in function 'dispatchEvent'
?: in function '_saveSceneAndHide'
?: in function 'gotoScene'
game.lua:452: in function '?'
?: in function <?:182>
From what I've researched already, this problem can occur when the timer is within a function and is local, however, the timer in my code is global, so I don't think that that is the problem.
Below is the bit of code with the issue:
local function createPuddle()
local function constantDelay()
local puddle = display.newImage( sceneGroup, "images/puddle.png" )
puddle.x = puddleLane
puddle.y = -200
physics.addBody( puddle, "dynamic", {density=0, filter=puddleCollisionFilter} )
puddle:applyForce( 0, puddleSpeed, puddle.x, puddle.y )
sceneGroup:insert(3,puddle)
local function onPuddleCollision( self, event )
if ( event.phase == "began" ) then
print('puddle collision')
puddle:removeSelf( )
puddle = nil
composer.gotoScene( "menu" )
end
end
puddle.collision = onPuddleCollision
puddle:addEventListener( "collision" )
end
local constantDelayTimer = timer.performWithDelay(puddleDelay/2,constantDelay,1)
currentPuddleDelayHold = timer.performWithDelay(puddleDelay, createPuddle);
end
currentPuddleDelay = timer.performWithDelay(puddleDelay, createPuddle);
And then later on in the program:
timer.cancel(currentPuddleDelay)
Any help would be greatly appreciated.
I can only guess as you most likely did not provide all relevant code.
It obviously doesn't make sense to cancel a non-existing timer so for the start just do
if currentPuddleDelay then timer.cancel(currentPuddleDelay) end
If there is any reason why currentPuddleDelay should still exist you should find out why it is nil.

Use of uninitialized value $end_time1 in string eq .......error in perl

i want to retrive some data from my database,if $end_time1 is null then will be update table ,otherwise will do nothing ,but when i run the code,i found the $end_time1 is null,then will be update the table ,but if not null ,it's will be return the error :
Use of uninitialized value $end_time1 in string eq ........
part of my code :
my $select_sth = $dbh->prepare("SELECT id ,H1, H2, addr1, addr2, time_1, time_2,
end_time_1,end_time_2,count1,count2 FROM service") or die "$dbh->errstr";
$select_sth->execute() or die "$dbh->errstr";
while (my #row_ref = $select_sth->fetchrow_array)
{
my $Rid = $row_ref[0];
my $addr1 = $row_ref[3];
my $addr2 = $row_ref[4];
my $end_time1 = "NULL" unless $row_ref[7];
my $end_time2 = "NULL" unless $row_ref[8];
my $count1 = $row_ref[9];
my $count2 = $row_ref[10];
if($end_time1 eq "NULL")
{
print "$end_time1 is null\n";
$dbh->do ("update service set end_time_1 = '$datetime_now' where id = $Rid");
}
}
please someone what's wrong with my code ?how to fix ?
Your current code only sets $endtime1 if $row_ref[7] is not defined. This means that $endtime1 is undefined if $row_ref[7] does have a value, so you get that Use of uninitialized value $end_time1 in string eq... error when you test it.
Change your code so that $endtime1 will either be set to $row_ref[7] (if it is defined) or to NULL:
my $end_time1 = $row_ref[7] || 'NULL';
Then you can use your existing code:
if ($end_time1 eq "NULL")
{
print "end_time1 is null\n";
$dbh->do ("update service set end_time_1 = '$datetime_now' where id = $Rid");
}
The same issue exists for $endtime2, so you may want to make a similar alteration.
Use defined:
if ( !defined $end_time1 ) {
print "end_time1 is null\n";
}

Lua For/do loop

I am trying to create a mod for a game server that i am playing, the idea is to give flag permissions to people based on being a mod or admin. The flags can be assigned by the server owner in a config file.
What i am trying to create, is a for loop that reads a section of the config file and passes the index and value to a function that then sends the information to the server.
function PLUGIN:cmdgiveflags ( netuser , cmd, args )
if (netuser:CanAdmin()) then
local a, targetuser = rust.FindNetUsersByName( args[1] )
if (not a) then
if (targetuser == 0) then
rust.Notice( netuser , "No Players by that name!" )
else
rust.Notice( netuser , "Multiple Players by the name!" )
end
return
end
if ( args[2] == "admin" ) then
**for key,value in pairs(self.Config.admin) do self.addflag(targetuser,key,value) end**
elseif ( args[2] == "mod") then
**for key,value in pairs(self.Config.mod) do self.addflag(targetuser,key,value) end**
else
rust.Notice( netuser , "Invalid Option, must be mod or admin" )
end
else
rust.Notice( netuser , "Only and Admin can use this command" )
end
end
function PLUGIN:addflag (targetuser, key, value)
print ("targetuser is "..targetuser)
**print ("key is "..key)**
**print ("value is "..value)**
if (value == true) then
if (key == "cangod" ) or (key == "canban") or (key == "cankick") or (key == "all") then
rust.RunServerCommand( "oxmin.giveflag " .. targetuser .. " " .. key )
print (targetuser .. " has been given the flag: " .. key )
else
rust.RunServerCommand( "flags.addflag " .. targetuser .. " " .. key )
print (targetuser .. " has been given the flag: " .. key )
end
end
end
The problem that i am having is when the variables key and value are sent to the addflags functions, key shows up as true and value is nil. but if i just do print(key,value) in the for loop they come out as expected. I only just started coding in Lua so I'm not too sure what the rules all are just yet. Any help would be appreciated.
Oh and sorry if the code isnt formatted correctly on the site, not sure how to get it to paste with the correct formatting.
You want self:addflag instead of self.addflag.
The definition
function PLUGIN:addflag (targetuser, key, value)
is sugar for
function PLUGIN.addflag (self, targetuser, key, value)
The call
self:addflag(targetuser,key,value)
is sugar for
self.addflag(self,targetuser,key,value)
So you have to make a call that is consistent with the definition of the function.

String Arrays in Guvnor rule

I am having some difficulty in understanding how a String[] can be represented in a Guvnor rule. How can an array of strings be passed to a Java method that uses String[] as an argument from a rule in Guvnor?
I keep getting mismatched input errors, Error Code 102 when I attempt to validate the rule in Guvnor.
Any pointers/tips welcome
In the following rule, comm is a global object with a function sendMail with the function signature (String[] recipientlist, String alertType, String message)
rule "list-email"
dialect "java"
when
$result : Grade( subject == "Math" , $marks : mark >= 99.0 )
$emailList : "{xyz#abc.com, fgh#def.com}"
then
comm.sendMail($emailList, "High Grade Alert", "Scored: " + " Marks:" + Double.toString($marks));
It's not a good idea to try and introduce a String[] on the LHS - you aren't matching with it, and I doubt the syntax is correct. Use this - on the RHS it's Java:
rule "list-email"
dialect "java"
when
$result : Grade( subject == "Math" , $marks : mark >= 99.0 )
then
String[] addrs = new String[]{"xyz#abc.com", "fgh#def.com"};
comm.sendMail(addrs, "High Grade Alert", "Scored: " + " Marks:" + $marks );
end

Resources