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.
Related
I am trying to get the hang of jsonnet files. So far all I have is hard-coded values but what if I wanted to get the hostname for a Java application. For example in Java I would just do:
String hostName = System.getenv("HOSTNAME");
But obviously I can't just have a key-value pair like the following JSON in a jsonnet file.
{name: "hostname", value:System.getenv("HOSTNAME")}
I need a bit of help in understanding how I can do this.
I have looked up std.extvar(x) but the examples I look at just arent clear to me for whatever reason. Is this method relevant? Otherwise, I'm not really sure.
Jsonnet requires all parameters to be passed explicitly. To use a hostname in your Jsonnet code, you need to pass it to the interpreter. For example you can run it as follows:
❯ jsonnet --ext-str "HOSTNAME=$HOST" foo.jsonnet
foo.jsonnet:
std.extVar('HOSTNAME')
You can also use top-level-arguments mechanism to a similar effect (top-level-arguments are passed as function arguments to the evaluated script.
Please see: https://jsonnet.org/learning/tutorial.html#parameterize-entire-config for more in-depth explanation of these features.
FYI not being able to just grab any environment variable or access the system directly is very much by design. The result of Jsonnet evaluation depends only on the code and explicitly passed parameters. This has a lot of benefits, such as the following:
You can easily evaluate on another machine, even on a completely different platform and get exactly the same result.
You are never locked in to configuration on any particular machine – you can always pass any parameters on any machine (very useful for development and debugging).
Avoiding surprises – the evaluation won't break one day, because some random aspect of local configuration changed and some deep part of the code happens to depend on it – all parameters are accounted for.
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.
I have two javascript files:
http://www.dublinbikes.ie/design/marseille/javascript/gmaps_common.js
http://www.dublinbikes.ie/design/marseille/javascript/gmaps_search_station.js
Which for http://www.dublinbikes.ie/All-Stations/Station-map# website
What I want to do is to simulate the search a station status action that on the left hand side by set variable 'value' a station number and pass it to gmaps_common.js for execute, then read the result from gmaps_search_station.js that executed variable called html from LeftBlock(id) function.
Because I need run whole process automatically several times, so please suggest me could I use Java to do this and how.
Thank you very much for the help.
You should never try to emulate a JavaScript interpreter just to get js variables out of it - just do the same thing the script would do in your language. Java is mighty enough.
The script just gets a list of stations from http://www.dublinbikes.ie/service/carto, and then queries details from http://www.dublinbikes.ie/service/stationdetails/dublin/{number} (example). Java has lots of HTTP and XML libraries, it will be no problem for you to do that, too.
I have to use a java program . I need to understand it and then modify it. The program has a source folder. It has a lot of java files. the program has a GUI interface. i have imported the program in eclipse and i can run the program. i want to learn and understand the code but since its too big i want to only read the relevant portions of the code which i need to understand to modify / add features to the program. This program requires input data . this data can be in a CSV file or a MySql database. the immediate problem i am facing with the program is that i need to use float data as input data in one of the columns. when i put data in decimal form in the CSV file . the program has no problem in accepting it and processing it. but when i create a MYSQL database table with floating datatype as one of the column . the program while importing data gives error - "unknown data type float . add this entry is xxx.props file " even after i add the float entry in xxx.props file in the proper way just as other entries are there for other data types in xxx.props file, i still get the same error . what should i do next ? which part of the code should i study to find out the problem?
When you say it "gives error" - does it throw an exception? If so, find out where that exception is thrown from, and work backwards from there.
You haven't explained what the xxx.props file is, how you created the mysql table, or what you're then doing with it, which makes it kinda hard to give any more specific help...
Without a direct question, I can't help you much. However, I can advise you on how to solve the problem.
Learn the basics before you try to make a leap to do everything at once. Try to learn these before coming everything:
Get a working knowledge of Java
Figure out how to develop a GUI that isn't binded to your logic. (Take a look at the MVC design pattern)
Break up the big program into manageable parts
Implement your logic separately from your data access (M C part of MVC)
Use Unit Testing to verify that your components work
Implement the CSV reading, and then move it to the database
This way you have seperable components that will make your job easier to deal with, and you will learn quite a bit along the way.
You've got a lot of small questions but I think your main question is "why am I getting this 'unknown data type float' error". Without looking at any code my first suggestion would be to validate how you're inserting/updating the MySQL table. Hopefully the developer extrapolated the data tier from the UI tier meaning there should only be one or two java classes to look at to verify how the data is inserted.
Overall, it sounds like you're new to Java and application design. Try to follow how the program works from point A to point B. Hopefully the code was broken out into tiers to make it easier for you; IE the GUI is in it's own package/project and the business logic is in it's own package/project, so on and so on.
Ok, so coming from a background of mostly perl, and mostly writing dirty little apps to automate my tasks, I've read the pages about the evils of eval(), and I always use a hash (in perl). I'm currently working on a little project (mostly for me and a couple of other technical people at work), for creating "canned response" e-mails. To allow for additions, subtractions, edits, etc., I'd like to essentially describe the response form(s) in XML, and have my app parse the XML and create the response forms at runtime. I want to use Java (to integrate it into an existing Java tool that I created), and boiled down to a trivial example, what I'm trying to do is take some XML like:
<Form Name="first" Title="Title!">
<Label Name="before">Your Request:</Label>
<Textbox Name="input"/>
<Label Name="after">has been completed.</Label>
<Output>%before%%input%%after%</Output>
</Form>
<Form Name="second">
...
and from parsing that, I want to create a JFrame named first, which contains a JLabel named before with the obvious text, then a textbox, then another JLabel... you get the idea (I eventually want to use the output tag to control exactly how the response is formatted).
I can parse the XML, and get the element name and such, but I don't know how to instantiate the Objects with a name that is the value of a variable, effectively:
JFrame $(thisNode.getAttributes().getNamedItem("Name").getNodeValue()) = new JFrame(thisNode.getAttributes().getNamedItem("Title").getNodeValue());
I've read basically the whole first page of google results on java reflection, but I haven't come across anyone doing quite what I'm looking for (at least not that I could tell). Having basically zero experience with reflection, I'm curious if this is something that can be accomplished using it, or if I should take the same approach as I would in Perl, and create a HashMap or HashTable of Objects, and tie them to a entry in a Hash of JFrames. Or, I'm open to ideas that don't fall into those two categories. The Hash is sort of my stand-by answer, because I've done it in Perl plenty of times, and I'm sure I can make it work in Java, but if there's a feature (like reflection) that's made to do this task, then why not do it the way it was intended to be done?
What you're asking isn't possible in Java. It doesn't work that way and these sorts of tricks, which are common in dynamic languages, aren't the Java way. You can certainly do:
JFrame frame = JFrameBuilder.buildFromTemplate("frame.xml");
where you create a JFrameBuilder class that reads the XML and creates an object from it but the variable name can't be dynamic. You have to remember that there are two steps in Java.
Java source files are compiled into bytecode;
The bytecode is read by a Java interpreter (JVM) and executed.
What you want is essentially asking to execute code in step (1). Now annotations can do things in a compile step (like adding interfaces, implementing methods and so on) but local variable naming is not one of those things.
You could (not necessarily that you should) generate Java source based on your XML, compile the generated code, and finally, execute the compiled code. This could be more efficient if you saved the generated .class files and reused them instead of parsing the XML every time the program is run (it can check the timestamp on the XML and only generate and compile if it's been modified since the last code generation).