GWT - passing Java arrays to Javascript - java

I am a newcomer to GWT and Javascript.
I am trying to send in a java int[] to my javascript function. I am using a gwt-exporter to handle the processing for me. This is how I have set things up.
static class gwtExplorerTest implements Exportable {
#Export("$wnd.handleAnchorClick")
public static void handleAnchorClick(int param1, int param2 , int[] a1 , int[] a2)
{
//process things here
}
}
Can someone help me with javascript code to pass in the arrays I need here? What I currently have is:
href="javascript:window.handleAnchorClick(" + currentRow + "," + currentColumn + "," + rowVals + "," + colVals + ",") "
as my JS function call, where rowVals and colVals are the two arrays I need to pass in. It does not seem to work. Can someone please help me out?
Thanks

Your function in java is right and gwt-exporter supports this syntax. The call from JS should be like:
window.handleAnchorClick(1, 2, [3,4], [5,6])
Your problem is that you are trying to call the exported method from a href attribute in your html and you are using a bad syntax.
First, it is better to use the onClick attribute instead of href, because you dont need the javascript: tag, and it is better to prevent default. And, I'd rather define a function to do the call to avoid syntax issues.
<script>
var currentRow = 1;
var currentColumn = 2;
var rowVals = [3,4];
var colVals = [5,6];
function mycall() {
window.handleAnchorClick(currentRow, currentColumn, rowVals, colVals);
}
</script>
<!-- I prefer this -->
click
<!-- But this should work as well -->
click

If you are using json string, then i hope you need to change parameter type to string in handleAnchorClick method. Then you need to typecast to json.

Related

Input/output in GLPK for Java

I find a lot of GLPK for Java examples about how to specify the model (problem/constraints) to the solver and read parameters from a data file, but very little about programmatic parameter input/output.
In my case I need to submit values (array of weights and values) to a knapsack problem programmatically and postprocess the solution as well (perform addtional numeric checks on the solution found) in order to decide whether to proceed or not.
Think of the equivalent of reading a param: line from a data file without calling glp_mpl_read_data or printing details of a solution to a file without calling glp_print_mip/sol/itp.
Can you provide example code or point me to the right resource?
This is only a partial answer. I managed to solve the output part using the
GLPK.get_ipt_obj_val
GLPK.get_mip_obj_val
GLPK.get_ipt_col_val
GLPK.get_mip_col_val
functions as in the following example
static void writeMipSolution(glp_prob lp) {
String name = GLPK.glp_get_obj_name(lp);
double val = GLPK.glp_mip_obj_val(lp);
System.out.println(name + " = " + val);
int n = GLPK.glp_get_num_cols(lp);
for (int i = 1; i <= n; i++) {
name = GLPK.glp_get_col_name(lp, i);
val = GLPK.glp_mip_col_val(lp, i);
System.out.println(name + " = " + val);
}
}
Still investigating the input part, though.

setVariableData to assign a Invoke Input Variable Collection from java embedding

I am using the below line in JAVA Embedding to assign value to a BPEL Invoke DB adapter input variable.
setVariableData("S2C_insert_InputVariable","TmpInvStoc3Collection","/ns8:TmpInvStoc3Collection/ns8:TmpInvStoc3/ns8:batchid","12345");
Now i want to put this statement in a while loop within java and want to repeat this for n iterations. I want to place a loop variable in the collection but I dont know how to do this.
I am looking for something like below.
setVariableData("S2C_insert_InputVariable","TmpInvStoc3Collection","/ns8:TmpInvStoc3Collection/ns8:TmpInvStoc3[$loop_variable]/ns8:batchid","12345");
Please let me know how to achieve this
Regards
Murali
This is based on the assumption that value is a function of i and cannot be calculated in the BPEL.
String qry = "";
for (int i = 0; i < n; i++)
{
value = SomeFunctionThatRequiresJavaRatherThanBPEL(i);
qry = "/ns8:TmpInvStoc3Collection/ns8:TmpInvStoc3[" + i + "]/ns8:batchid";
setVariableData("S2C_insert_InputVariable","TmpInvStoc3Collection",qry,value);
}
The code could be more efficient but it should do what you want.

Primefaces Calendar - disabling specific dates using EL

so from my previous question, Disable specific dates on p:calendar, i know that i can disable specific dates using Javascript like this:
var disabledDays = ["5-15-2013", "6-23-2013"];
function disableAllTheseDays(date) {
var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
for (i = 0; i < disabledDays.length; i++) {
if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1) {
return [false];
}
}
return [true];
}
with:
<p:calendar id="pfdate" navigator="true" pattern="MM-dd-yyyy"
value="#{day}" BeforeShowDay="disableAllTheseDays" showOn="button"/>
However, my question is that how can i store dates in disabledDays array using EL expressions? I need to do this because the dates that i need to disable varies. Disabling dates needs to be dynamic. If i can't do this with EL expressions, is there anyways to use an array that will have dynamic data?
Thanks
One possibility is just converting the value in a bean (["5-15-2013", "6-23-2013"]), and put it directly in the Javascript code:
var disabledDays = #{myBean.disabledDays};
It's not the cleanest one, but the easiest. Another possibility is just having the list of strings in the bean and use <ui:repeat> to print it as a comma separated list.

Java - Computation of Derivations with Apache Commons Mathematic Library

I have a problem in using the apache commons math library.
I just want to create functions like f(x) = 4x^2 + 2x and I want to compute the derivative of this function --> f'(x) = 8x + 2
I read the article about Differentiation (http://commons.apache.org/proper/commons-math/userguide/analysis.html, section 4.7).
There is an example which I don't understand:
int params = 1;
int order = 3;
double xRealValue = 2.5;
DerivativeStructure x = new DerivativeStructure(params, order, 0, xRealValue);
DerivativeStructure y = f(x); //COMPILE ERROR
System.out.println("y = " + y.getValue();
System.out.println("y' = " + y.getPartialDerivative(1);
System.out.println("y'' = " + y.getPartialDerivative(2);
System.out.println("y''' = " + y.getPartialDerivative(3);
In Line 5 a compile error occurs of course. The function f(x) is called and not defined. What I am getting wrong?
Has anyone any experience with the differentiation/derivation with the apache commons math library or does anyone know another library/framework which can help me?
Thanks
In the paragraph below that example, the author describes ways to create DerivativeStructures. It isn't magic. In the example you quoted, someone was supposed to write the function f. Well, that wasn't very clear.
There are several ways a user can create an implementation of the UnivariateDifferentiableFunction interface. The first method is to simply write it directly using the appropriate methods from DerivativeStructure to compute addition, subtraction, sine, cosine... This is often quite straigthforward and there is no need to remember the rules for differentiation: the user code only represent the function itself, the differentials will be computed automatically under the hood. The second method is to write a classical UnivariateFunction and to pass it to an existing implementation of the UnivariateFunctionDifferentiator interface to retrieve a differentiated version of the same function. The first method is more suited to small functions for which user already control all the underlying code. The second method is more suited to either large functions that would be cumbersome to write using the DerivativeStructure API, or functions for which user does not have control to the full underlying code (for example functions that call external libraries).
Use the first idea.
// Function of 1 variable, keep track of 3 derivatives with respect to that variable,
// use 2.5 as the current value. Basically, the identity function.
DerivativeStructure x = new DerivativeStructure(1, 3, 0, 2.5);
// Basically, x --> x^2.
DerivativeStructure x2 = x.pow(2);
//Linear combination: y = 4x^2 + 2x
DerivativeStructure y = new DerivativeStructure(4.0, x2, 2.0, x);
System.out.println("y = " + y.getValue());
System.out.println("y' = " + y.getPartialDerivative(1));
System.out.println("y'' = " + y.getPartialDerivative(2));
System.out.println("y''' = " + y.getPartialDerivative(3));
The following thread from the Apache mailing list seems to illustrate the two possible ways of how the derivative of a UnivariateDifferentiableFunction can be defined. I am adding a new answer as I'm unable to comment on the previous one (insufficient reputation).
The used sample specification of the function is f(x) = x^2.
(1) Using a DerivativeStructure:
public DerivativeStructure value(DerivativeStructure t) {
return t.multiply(t);
}
(2) By writing a classical UnivariateFunction:
public UnivariateRealFunction derivative() {
return new UnivariateRealFunction() {
public double value(double x) {
// example derivative
return 2.*x;
}
}
}
If I understand well, the advantage of the first case is that the derivative does not need to be obtained manually, as in the second case. In case the derivative is known, there should thus be no advantage of defining a DerivativeStructure, right? The application I have in mind is that of a Newton-Raphson solver, for which generally the function value and its derivative need to be known.
The full example is provided on the aforementioned web site (authors are Thomas Neidhart and Franz Simons). Any further comments are most welcome!

Java to php, Byte, Type, Array

Here is a small Java Code Snippet.
ConfigData[] cd = new ConfigData[1];
cd[0] = new ConfigData();
byte[] tmpbyte ={1,(byte)0x01};
cd[0].settmpdata(tmpbyte);
"ConfigData" is my custom Type (int, Byte Array).
In my last thread i found the tip how to Build / work with a "ByteArray" in php.
But this seems to be a question of the structure of those objects / array.
So..
How can i depict that in PHP.
I hope that this can make some guidelines for you, in the terms of what you can do in PHP, and in a relation to your code snippet (the code is tested):
<?php
// define class
class ConfigData
{
var $intVal;
var $tmpData;
}
$cData = new ConfigData(); // new ConfigData instance
$array[0] = $cData; // put it in a single element array
$array[0]->intVal = 5; // assign an integer to intVal
$array[0]->tmpData = array(1, 1, 2); // assign an array of whatever to tmpData
foreach($array[0]->tmpData as $val) // iterate through assigned array
echo $val." "; // print array item (and append " " )
?>
Now, you might also want to check how byte manipulation in PHP is achieved. I suggest you to do a little Google search, and maybe check the official manual. You question was not specific enough, so I can't say more.

Categories

Resources