Dynamic set operations in compilation phase - gams-math

I am trying to define some sets dynamically with set operations (in my case the "-" operation). However, the "-" operation seems to apply only during the execution phase, and when using this set when defining other sets, gams fails with the following error: 644 set.ident or #.ident has undefined data. I believe the issue is that the set defined with the minus operation does not get created until the execution phase.
I could not find a solution to create the set during the compilation phase. Any help appreciated, with a minimal reproducible example below.
set alphabet /
"a"
"b"
"c"
/;
set a(alphabet) /
"a"
/;
sets bc(alphabet);
bc(alphabet) = alphabet(alphabet) - a(alphabet);
set test1(alphabet)
/
#a
/;
set test2(alphabet)
/
#a
#bc
/;
set test3(alphabet)
/
set.a
/;
set test4(alphabet)
/
set.a
set.bc
/;

You are right, "bc(alphabet) = alphabet(alphabet) - a(alphabet);" is an execution time statement. Doing a set subtraction at compile time in GAMS directly, is not so easy (in contrast to a set addition, which works). But if you use a recent GAMS system (24.9), you could use the new embedded code facility to do this:
set alphabet /
"a"
"b"
"c"
/;
set a(alphabet) /
"a"
/;
sets bc(alphabet);
$onEmbeddedCode Python:
gams.set("bc", list(set(gams.get("alphabet")) - set(gams.get("a"))))
$offEmbeddedCode bc
I hope that helps!
Lutz

Related

Oracle SQL if statement based on parameter passed in via .sh script

I have a shell script which calls some SQL like so
sqlplus system/$password#$instance #./oracle/mysqlfile.sql $var1 $var2 $var3
Then in mysqlfile.sql, I define properties like this:
DEFINE var1=&1
DEFINE var2=&3
DEFINE var3=&3
Later in the file, I call another SQL script:
// i wish to wrap this in a if statement - pseudo-code
if(var3="true") do the following
#./oracle/myOthersqlfile.sql &&varA &&varB
I am not sure how to implement this though, any suggestions appreciated
You could (ab)use substitution variables:
set termout off
column var3_path new_value var3_path
select case
when '&var3' = 'true' then './oracle/myOthersqlfile.sql &&varA &&varB'
else '/dev/null'
end as var3_path
from dual;
set termout on
#&var3_path
The query between the set termout commands - which just hide the output of the query - uses a case expression to pick either your real file path or a dummy file; I've used /dev/null, but you could have a 'no-op' file of your own that does nothing if that's clearer. The query gives the result of that the alias var3_path. The new_value line before it turns that into a substitution variable. The # then expands that variable.
So if var3 is 'true' then that runs:
#./oracle/myOthersqlfile.sql &&varA &&varB
(or, actually, with the varA and varB variables already replaced with their actual values) and if it is false it runs:
#/dev/null
which does nothing, silently.
You can set verify on around that code to see when and where substitution is happening.
You can't implement procedural logic into sqlplus. You have these options :
Implement the IF-THEN-ELSE logic inside the shell script that is running the sqlplus.
Use PL/SQL, but then your SQL Script should be called as a process inside an anonymous block, not like an external script.
In your case the easiest way is to change your shell script.
#/bin/bash
#
# load environment Oracle variables
sqlplus system/$password#$instance #./oracle/mysqlfile.sql $var1 $var2 $var3
# if then
if [ $var3 == "true" ]
then
sqlplus system/$password#$instance #./oracle/myOthersqlfile.sql
fi
You should realise that sqlplus is just a CLI ( Command Line Interface ). So you can't apply procedural logic to it.
I have no idea what you do in those sql scripts ( running DMLs, creating files, etc ), but the best approach would be to convert them to PL/SQL, then you can apply whatever logic you need to.

How to define and set a variable in ARM DS-5 Debug/Command view on Eclipse CDT

I am using ARM DS-5 (v5.29.1) Eclipse CDT to debug an embedded project, trying to use the debugger command line for manipulating memory locations through the Commands view in the Debug perspective.
According to the debugger's own context help (Ctrl+Space on the command line), the following command should define a variable and set its value:
set variable INTR_MASK=4
However, when typing this, I get an error:
ERROR(EXP8): Could not find the symbol "INTR_MASK"
How can I define and set a variable?
For reference, here's the help description:
set variable
------------
Evaluates an expression and assigns the result to a variable, register or
memory.
Syntax
set [variable] <expression>
Where:
<expression>
Specifies an expression and assigns the result to a variable, register, or
memory address.
Example
set variable myVar=10 # Assign 10 to variable myVar
set variable $PC=0x8000 # Assign address 0x8000 to
# PC register
set variable $CPSR.N=0 # Clear N bit
set variable (*(int*)0x8000)=1 # Assign 1 to address 0x8000
set variable *0x8000=1 # Assign 1 to address 0x8000
set variable strcpy((char*)0x8000,"My String") # Assign string to address 0x8000
set variable memcpy(void*)0x8000,{10,20,30,40},4) # Assign array to address 0x8000
After communicating ARM support, it come out that the right way to achieve that is something like:
newvar $INTR_MASK = (unsigned int)(0x00000004)
set variable $INTR_MASK = (unsigned int)($INTR_MASK + 0x00080000)
... and the associated bonus is that those symbols can be used in the Expressions view as well, so no more explicit addresses, but rather meaningful names.

Nesting if statements inside a while loop - C shell

I have to write a tcsh script for unix that pulls values from every other line in a text file, compares them and decides if you should buy(1), sell(-1) or do nothing(0). Basically a simple stock profit calculation. I think I have all the logic right, but when I run the script I get a "while syntax error" and it never executes. I have the full script below, is it not possible to nest statements in a while loop with unix? If so any suggestions how to do this?
#!/bin/tcsh
set lineNum='wc -l testcase.txt'
set i=1
while ($i<$lineNum)
set prices='sed -n '$lineNump' testcase.txt'
set arr=( $price )
set j='echo ${#arr}'
set price=0
set x=0
set y=0
set k=0
while ($k < $j)
set a=arr[$k]
set str=""
if ($a>$price)
then
str="$str 1"
price=$((price-a))
else if($a<$price)
then
str="$str -1"
price=$((price+a))
else if($a==$price)
then
str="$str 0"
fi
str="$str $price"
if ($str=='sed -n'('expr $lineNum+1'p)' testcase.txt')
then
x=$((x+1))
fi
y=$((y+1))
end
lineNum=$((lineNum+2))
end
echo $x/$y
Your script appears to be a mixture of tcsh and bash syntax.
As Mark's answer says, the then keyword has to be on the same line as the if (unless you use a backslash to splice two lines, but there's not much point in doing that).
For a variable assignment, the set keyword is not optional; this:
str="$str 1"
is a syntax error in csh/tcsh (it will probably look for a command whose name starts with "str=".) Write that as:
set str = "$str 1"
Note that you can optionally have spaces around the = in a set. tcsh's syntax is a bit messed up:
set foo=bar # ok
set foo = bar # ok
set foo= bar # ok
set foo =bar # error: "Variable name must begin with a letter."
The x=$((x+1)) syntax is specific to bash and related shells. tcsh uses # for arithmetic assignments:
set x = 42
# x ++ # sets $x to 43
# x = $x * 2 # sets $x to 86
If you have a choice, I suggest writing your script to use bash rather than tcsh (you're about halfway there already). Its syntax is much more regular.
The classic rant about csh/tcsh programming can be found here.
You are missing the end statement correspoding to the first while.
You are also using fi instead of endif.
The "then" keywords need to be on the same line as the "if" they belong to.

Clear Screen in SQL*Plus

I'm running the following report but getting an error
/* Simple table formatting */
clear screen;
accept Report_File char prompt 'Enter a file name for summary report ';
/*Set up column headers*/
col StoreCode format A8 heading 'Store Code';
col DESCRIPTION format A8 heading 'Item Description';
col PRICE format $999999.99 heading 'Price';
col QUANTITY format 999 heading 'Quantity';
col (Price*Quantity) format $999999.99 heading 'Value';
/*Format and title pages */
set Pause off;
set Feedback off;
set Space 6;
set newpage 2;
set pagesize 54;
set linesize 200;
set underline =;
title center 'Current Stock Value by Store' skip 2 left -
'prepared by Jason Kemeys' &Report_Officer right -
&Todays_Date skip4;
btitle center format 999 SQL.PNO;
/* Set breaks and computes */
break on StoreCode skip 2 on SuppCode skip 1 on Report;
compute sum of (Price*Quantity) on StoreCode;
compute sum of (Price*Quantity) on Report;
/*Select data & send to file*/
spool &Report_File;
select StoreCode, Description, Quantity, Price, (Price*Quantity)
from Stocks
order by StoreCode;
spool off;
/* Clear all settings */
clear breaks;
clear columns;
clear computes;
set Pause on;
Just need to know why its showing the error and how to get it running; first time doing a report in SQL.
This is the error I'm getting
clear screen;
* ERROR at line 2: ORA-00900: invalid SQL statement
cl scr is the command used to clear screen in SQL.
I suspect this is dependent on the version of Oracle you are using.
This should work in version 11.2 but to quote from the 10g documentation:
CLEAR SCREEN is not available in SQL*Plus.
The same note isn't there in the 11.1 documentation, which would imply that you're using Oracle 10g or earlier. If this supposition is true then there's little that you can do.
It's possible that you can utilise the host command in SQL*Plus to run cls, if you're using Windows, or clear, if you're using Linux, but I'm not certain that it would have exactly the same effect. If it were possible it would simply be:
host cls
host runs an operating system command from SQL*Plus, and so will appear to clear the screen.
simply use cl scr command to clear the SQL plus.
Very few occurances of ";" in your code is actually needed. For example, the "clear screen" command to start with, doesn't need a semicolon. It will work, when you add one, but I wouldn't be sure about all the subsequent commands in that same file. Commands that need them, are limited to INSERT, UPDATE, DELETE, COMMIT, ROLLBACK, and such.
Secondly, if you get weird feedback from SQL-files, and if you have written them outside of Linux/Unix, this often ends up in SQLPLUS complaining about invisible characters. Look at that file via both VI and CAT commands, and note anything weird.

How can I execute a script in a rule and pass a value back to a rule in booggie 2?

In booggie 2, how can I execute a script (programmed in Python) out of a rule and pass the script's return value to the rule?
Please note: The booggie-project does not exist anymore but led to the development of Soley Studio which covers the same functionality.
exec is the command to execute rules and scripts out of a rule. It is followed by parentheses containing a sequence composed of rules and scripts.
There is a strict order in which the application sequence in a rule is executed, (cf. Is there a fixed order of how the right-hand side of a rule is executed in GrGen.NET?). exec is always the last statements that's executed (before return of course). Hence, we can't pass a variable from exec to eval. Therefore, variables resulting from the execution of scripts in exechave to assigned to node/edge-attributes within the exec statement. To do so, we use curly brackets and write the same code as we would in an eval statement.
In the following example, a script is called that returns the highest value of three given values (a.value, b.value, c.value) and stores it a node's attribute (d.value).
exec ((max_value) = getMaxValue(a.value, b.value, c.value) ;>
{
d.value = max_value;
}
);