C# WindowHandles works better than Java GetWindowHandles - java

Ok so i continue to convert my C# framework to Java
In C# , Driver.WindowHandles returns a count of 2, which is correct, so i tell it to select 'last' and it works great.
In Java the Driver.GetWindowHandles returns a count of only 1, which messes up my script as i need to access the 2nd window
Can anyone do a method in Java that mimics the way the c# method does it (as it seems to work better than whatever the Java equivalent does)
Thanks!

Related

Call Minizinc model from Java

How to call a Minizinc model from a Java program with arrays as passed-on parameters?
Is there any special command for doing this?
I frequently do the same but in python. There is probably not any module or extension that can integrate the call in any convenient way but it is quite easy to just call another program.
Since I have not tried it in Java, I will let another stack overflow post guide you: Execute external program in java.
You can pass the parameters either as -D "var_int_name=10;var_int_array=[1,2,3];" or you can supply a data file as the last argument in the call to MiniZinc.
A general tip is to make the output from your MiniZinc model very easy to recognise and parse since many solvers print extra stuff and not just the solution. For example does MiniZinc itself print ---------- between solution. Surround the answer with & or any other sign that is easy to find and parse by a computer. You might also want to verify that you indeed got a solution back.

Java interpreter code at runtime

We have discussed how java is first compiled into Java bytecode and then interpreted by the JVM. Build into the program we are using (Dr Java), there is a panel called Interactions where you can type code in real time and have it be interpreted and ran (I believe that is how it works). I was wondering if it was possible to have a compiled program in java be ran, and then allow a user to input java code to be interpreted to modify the things that happen. I can't really think of any practical uses of this, but here is an example to clarify:
User runs a program and an integer in initialized with the value of 2 and the name of changeNumber. A pop-up comes up allowing the user to input some java code. They can input something like - "changeNumber = changeNumber + 2;" and have the code execute in real time where if you ended up printing out changeNumber, you would get 4.
This is possible using the Reflection API.
As a side note, I do not understand the downvotes. This is a good and well-written question for a beginner.

Populate and Pass an Array of Structures to Java

I have spent a large portion of the morning trying to populate and pass an array of populated structs to C++ from Java using JNA. I have significant portions of JNA based code running and I feel like this should be simple, but I can not figure out or find an example that does not use #deprecated functions. My suspicion is that this is trivial and I'm going to feel dumb when someone shows me, but I would really appreciate some help.
A little background on what I have tried:
I somehow doubt it matters, but after learning how to write the interface files by hand (and getting them to work), I switched over to JNAerator. JNAerator translates
MyStruct* mine
to
MyStruct mine
in Java. This confuses me a bit because in Java this could only be used to point to a single object. At one point I looked at using
MyStruct** mine
which translates to
PointerByReference mine
But this seems like overkill because I don't need to modify the structs, or ever access them again for that matter. I have tried everything I can think of from this point on but I have never managed to successfully send more than the first struct.
After re-reading your question (pass structures from Java to C++), here's what you can do:
// Assuming a native signature like this:
// call_native_function(MyStruct** struct_list, int count)
MyStruct.ByReference[] list = new MyStruct.ByReference[SIZE];
for (int i=0;i < list.length;i++) {
list[i] = new MyStruct.ByReference();
// Initialize the struct as needed
}
// Call whatever native method...
nativeLibrary.call_native_function(list, list.length);
See also the JNA FAQ.

How to script input for a Java program

I'm writing a Java program that requires its (technical) users to write scripts that it uses as input; it interprets these scripts into a series of actions and executes them. I am currently looking for the cleanest way to implement the script/configuration language. I was originally thinking of heading down the XML route, but the nature of the required input really is a procedural, linear flow of actions that need to be executed:
function move(Block b, Position p) {
// user-defined algorithm for moving block "b" to position "p"
}
Block a = getBlockA();
Position p = getPositionP();
move(a, p);
Etc. Please note: the above is an example only and does not constitute the exact syntax I am looking to achieve. I am still in the "30,000 ft view"-design phase, and don't know what my concreted scripting language will ultimately look like. I only provide this example to show that it is a flow/procedural script that the users must write, and that XML is probably not the best candidate for its implementation.
XML, perfect for hierarchial data, just doesn't feel like the best choice for such an implementation (although I could force it to work if need-be).
Not knowing a lick about DSLs, I've begun to read up on Groovy DSLs and they feel like a perfect match for what I need.
My uderstanding is that I could write, say, a Groovy (I'm stronger in Groovy than Scala, JRuby, etc.) DSL that would allow users to write scripts (.groovy files) that my program could then execute as input at runtime.
Is this correct, or am I misunderstanding the intent of DSLs altogether? If I am mistaken, does anybody have any suggestions for me? And if I am correct then how would a Java program read and execute a .groovy file (in other words, how would my program "consume" their script)?
Edit: I'm beginning to like ANTLR. Although I would love to roll up my sleeves and write a Groovy DSL, I don't want my users to be able to write any old Groovy program they want. I want my own "micro-language" and if users step outside of it I want the interpreter to invalidate the script. It's beginning to seem like Groovy/DSLs aren't the right choice, and maybe ANTLR could be the solution I need...?
I think you are on a really good path. Your users can write their files using your simple DSL and them you can run them by Evaling them at runtime. Your biggest challenge will be helping them to use the API of your DSL correctly. Unless they use an IDE this will be pretty tough.
Equivalent of eval() in Groovy
Yes, you can write a Groovy program that will accept a script as input and execute it. I recently wrote a BASIC DSL/interpreter in this way using groovy :
http://cartesianproduct.wordpress.com/binsic-is-not-sinclair-instruction-code/
(In the end it was more interpreter than DSL but that was to do with a peculiarity of Groovy that likely won't affect you - BASIC insists on UPPER CASE keywords which Groovy finds hard to parse - hence they have to be converted to lower case).
Groovy allows you to extend the script environment in various ways (eg injecting variables into the binding and transferring execution from the current script to a different, dynamically loaded script) which make this relatively simple.

Is there an equivalent of Vbscript execute statement in Java

To start off, I know that Vbscript is interpreted and Java is compiled. But is there a way to do the tasks of vbscript 'execute' or 'eval' statements in Java? What I am trying to do is to save a piece of code in a variable, and try to execute the saved code during run time.
For eg, in vbscript,
a = "b = 10"
execute(a)
will assign the value 10 to a variable 'b'. If this is possible in java, I can handle a situation,I have got myself in to without redesigning the whole code. Request your help.
No, Java has no equivalent to VBScript's execute.
You could do what you're outlining using scripting for Java, however, where you have a reasonably wide range of scripting languages to choose from.

Categories

Resources