How to use eval via Jython? - java

I am trying to use the original eval in Python, via Jython.
But for some reason I don't understand, I am getting a NullPointerExec.
public static String Parse(String s)
{
ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
try
{
return engine.eval("eval('%s')".format(s)).toString();
}
catch (ScriptException e)
{
e.printStackTrace();
}
return "--";
}

You can use Pythons eval function through Java (and Jython) like this:
public static boolean XLargerThanY(int x, int y) throws ScriptException {
PyScriptEngineFactory factory = new PyScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine();
PyFunction function = (PyFunction) engine.eval("x > y");
PyBoolean ans = (PyBoolean) function.__call__(new PyInteger(x), new PyInteger(y));
return ans.getBooleanValue();
}

Related

Dynamic formula based on user inputs

I have many objects with different metrics. I am building a formula based on the user input.
class Object{
double metrics1;
double metrics2;
.. double metricsN; //number of metrics is knowned
}
Users can input
formula=metrics1
or
formula=(metrics1+metrics2)/metrics3
I already have a parser to parse the formula, but I do not know how to store this expression for further calculation.
I want to avoid parsing the formula again and again for every single object (I can have up to a few hundred thousands).
Use ScriptEngine and reflection like this.
static class Evaluator {
ScriptEngine engine = new ScriptEngineManager()
.getEngineByExtension("js");
void formula(String formula) {
try {
engine.eval("function foo() { return " + formula + "; }");
} catch (ScriptException e) {
e.printStackTrace();
}
}
Object eval(Object values)
throws ScriptException,
IllegalArgumentException,
IllegalAccessException {
for (Field f : values.getClass().getFields())
engine.put(f.getName(), f.get(values));
return engine.eval("foo()");
}
}
public static class Object1 {
public double metrics1;
public double metrics2;
Object1(double metrics1, double metrics2) {
this.metrics1 = metrics1;
this.metrics2 = metrics2;
}
}
public static void main(String[] args)
throws ScriptException,
IllegalArgumentException,
IllegalAccessException {
Evaluator e = new Evaluator();
e.formula("metrics1 + metrics2");
Object1 object = new Object1(1.0, 2.0);
System.out.println(e.eval(object));
// -> 3.0
}

Why isn't this multithreaded code faster?

This is my java code. Before, it calls BatchGenerateResult sequentially which is a lengthy process, but I want to try some multithreading and have each one of them run at the same time. However when I test it, the new time is the same as the old time. I expected the new time to be faster. Does anyone know whats wrong?
public class PlutoMake {
public static String classDir;
public static void main(String[] args) throws JSONException, IOException,
InterruptedException {
// determine path to the class file, I will use it as current directory
String classDirFile = PlutoMake.class.getResource("PlutoMake.class")
.getPath();
classDir = classDirFile.substring(0, classDirFile.lastIndexOf("/") + 1);
// get the input arguments
final String logoPath;
final String filename;
if (args.length < 2) {
logoPath = classDir + "tests/android.png";
filename = "result.png";
} else {
logoPath = args[0];
filename = args[1];
}
// make sure the logo image exists
File logofile = new File(logoPath);
if (!logofile.exists() || logofile.isDirectory()) {
System.exit(1);
}
// get the master.js file
String text = readFile(classDir + "master.js");
JSONArray files = new JSONArray(text);
ExecutorService es = Executors.newCachedThreadPool();
// loop through all active templates
int len = files.length();
for (int i = 0; i < len; i += 1) {
final JSONObject template = files.getJSONObject(i);
if (template.getBoolean("active")) {
es.execute(new Runnable() {
#Override
public void run() {
try {
BatchGenerateResult(logoPath, template.getString("template"),
template.getString("mapping"),
template.getString("metadata"), template.getString("result")
+ filename, template.getString("filter"),
template.getString("mask"), template.getInt("x"),
template.getInt("y"), template.getInt("w"),
template.getInt("h"));
} catch (IOException | JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
es.shutdown();
boolean finshed = es.awaitTermination(2, TimeUnit.MINUTES);
}
private static void BatchGenerateResult(String logoPath, String templatePath,
String mappingPath, String metadataPath, String resultPath,
String filter, String maskPath, int x, int y, int w, int h)
throws IOException, JSONException {
ColorFilter filterobj = null;
if (filter.equals("none")) {
filterobj = new NoFilter();
} else if (filter.equals("darken")) {
filterobj = new Darken();
} else if (filter.equals("vividlight")) {
filterobj = new VividLight();
} else {
System.exit(1);
}
String text = readFile(classDir + metadataPath);
JSONObject metadata = new JSONObject(text);
Map<Point, Point> mapping = MyJSON.ReadMapping(classDir + mappingPath);
BufferedImage warpedimage = Exporter.GenerateWarpedLogo(logoPath, maskPath,
mapping, metadata.getInt("width"), metadata.getInt("height"));
// ImageIO.write(warpedimage, "png", new FileOutputStream(classDir +
// "warpedlogo.png"));
Exporter.StampLogo(templatePath, resultPath, x, y, w, h, warpedimage,
filterobj);
warpedimage.flush();
}
private static String readFile(String path) throws IOException {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();
String text = new String(data, "UTF-8");
return text;
}
}
It looks like, for all practical purposes the following code should be the only one which can improve performance by using multithreading.
BufferedImage warpedimage = Exporter.GenerateWarpedLogo(logoPath, maskPath,
mapping, metadata.getInt("width"), metadata.getInt("height"));
// ImageIO.write(warpedimage, "png", new FileOutputStream(classDir +
// "warpedlogo.png"));
Exporter.StampLogo(templatePath, resultPath, x, y, w, h, warpedimage,
filterobj);
The rest of it major IO - I doubt how much performance improvement you can achieve there.
Do a profile and check how long each one of the methods is executing. Depending on that you should be able to understand.
Hi sorry not able add to comment part as just joined..
would suggest to first go for dummy method any check whether it works at your end then add your business logic...
if the sample works then you might need to check your "template" class
here's the sample.. check the timestamp
package example;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExecutorStaticExample {
public static void main(String[] args){
ExecutorService ex = Executors.newCachedThreadPool();
for (int i=0;i<10;i++){
ex.execute(new Runnable(){
#Override
public void run() {
helloStatic();
System.out.println(System.currentTimeMillis());
}
});
}
}
static void helloStatic(){
System.out.println("hello form static");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

RubyNoMethodError - call a ruby method from Java

I have this ruby class :
require 'stringio'
require 'hirb'
class Engine
def initialize()
#binding = Kernel.binding
end
def run(code)
# run something
stdout_id = $stdout.to_i
$stdout = StringIO.new
cmd = <<-EOF
$SAFE = 3
$stdout = StringIO.new
begin
#{code}
end
EOF
begin
result = Thread.new { Kernel.eval(cmd, #binding) }.value
rescue SecurityError
return "illegal"
rescue Exception => e
return e
ensure
output = get_stdout
$stdout = IO.new(stdout_id)
end
return output
end
private
def get_stdout
raise TypeError, "$stdout is a #{$stdout.class}" unless $stdout.is_a? StringIO
$stdout.rewind
$stdout.read
end
end
The "run" method should call an IRB's function and to capture the output (string format).
I want to call this function from a Java class but it can't find the IRB methods, even they are loaded (require 'hirb').
My java class looks like this :
public class MyClass {
private final static String jrubyhome = "/usr/lib/jruby/";
private String rubySources;
private String hirbSource;
private String myEngine;
private boolean loaded = false;
private void loadPaths() {
String userDir;
userDir = System.getProperty("user.dir");
rubySources = userDir + "/../ruby";
hirbSource = userDir + "/hirb.rb";
myEngine = rubySources + "/engine.rb";
System.setProperty("jruby.home", jrubyhome);
System.setProperty("org.jruby.embed.class.path", rubySources+":"+hirbSource);
System.setProperty("hbase.ruby.sources", rubySources+":"+hirbSource);
}
private String commandResponse(String command)
throws FileNotFoundException
{
String response;
loadPaths();
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("jruby");
ScriptingContainer container = new ScriptingContainer();
Reader reader = new FileReader(myEngine);
try {
Object receiver = engine.eval(reader);
String method = "run";
Object ob = container.callMethod(receiver,method,command);
response = ob.getClass().toString();
return response;
} catch (ScriptException e) {
System.out.println("exception");
}
return "FAILED";
}
public static void main(String args[])
throws IOException {
MyClass my = new MyClass();
System.out.println(my.commandResponse(args[0]));
}
}
Do you know what could be the problem?
[EDITED] After I extended the Kernel module and added the commands it worked.

using dbpedia spotlight in java or scala

Does anyone know where to find a little how to on using dbpedia spotlight in java or scala? Or could anyone explain how it's done? I can't find any information on this...
The DBpedia Spotlight wiki pages would be a good place to start.
And I believe the installation page has listed the most popular ways (using a jar, or set up a web service) to use the application.
It includes instructions on using the Java/Scala API with your own installation, or calling the Web Service.
There are some additional data needed to be downloaded to run your own server for full service, good time to make a coffee for yourself.
you need download dbpedia spotlight (jar file) after that u can use next two classes ( author pablomendes ) i only make some change .
public class db extends AnnotationClient {
//private final static String API_URL = "http://jodaiber.dyndns.org:2222/";
private static String API_URL = "http://spotlight.dbpedia.org:80/";
private static double CONFIDENCE = 0.0;
private static int SUPPORT = 0;
private static String powered_by ="non";
private static String spotter ="CoOccurrenceBasedSelector";//"LingPipeSpotter"=Annotate all spots
//AtLeastOneNounSelector"=No verbs and adjs.
//"CoOccurrenceBasedSelector" =No 'common words'
//"NESpotter"=Only Per.,Org.,Loc.
private static String disambiguator ="Default";//Default ;Occurrences=Occurrence-centric;Document=Document-centric
private static String showScores ="yes";
#SuppressWarnings("static-access")
public void configiration(double CONFIDENCE,int SUPPORT,
String powered_by,String spotter,String disambiguator,String showScores){
this.CONFIDENCE=CONFIDENCE;
this.SUPPORT=SUPPORT;
this.powered_by=powered_by;
this.spotter=spotter;
this.disambiguator=disambiguator;
this.showScores=showScores;
}
public List<DBpediaResource> extract(Text text) throws AnnotationException {
LOG.info("Querying API.");
String spotlightResponse;
try {
String Query=API_URL + "rest/annotate/?" +
"confidence=" + CONFIDENCE
+ "&support=" + SUPPORT
+ "&spotter=" + spotter
+ "&disambiguator=" + disambiguator
+ "&showScores=" + showScores
+ "&powered_by=" + powered_by
+ "&text=" + URLEncoder.encode(text.text(), "utf-8");
LOG.info(Query);
GetMethod getMethod = new GetMethod(Query);
getMethod.addRequestHeader(new Header("Accept", "application/json"));
spotlightResponse = request(getMethod);
} catch (UnsupportedEncodingException e) {
throw new AnnotationException("Could not encode text.", e);
}
assert spotlightResponse != null;
JSONObject resultJSON = null;
JSONArray entities = null;
try {
resultJSON = new JSONObject(spotlightResponse);
entities = resultJSON.getJSONArray("Resources");
} catch (JSONException e) {
//throw new AnnotationException("Received invalid response from DBpedia Spotlight API.");
}
LinkedList<DBpediaResource> resources = new LinkedList<DBpediaResource>();
if(entities!=null)
for(int i = 0; i < entities.length(); i++) {
try {
JSONObject entity = entities.getJSONObject(i);
resources.add(
new DBpediaResource(entity.getString("#URI"),
Integer.parseInt(entity.getString("#support"))));
} catch (JSONException e) {
LOG.error("JSON exception "+e);
}
}
return resources;
}
}
second class
/**
* #author pablomendes
*/
public abstract class AnnotationClient {
public Logger LOG = Logger.getLogger(this.getClass());
private List<String> RES = new ArrayList<String>();
// Create an instance of HttpClient.
private static HttpClient client = new HttpClient();
public List<String> getResu(){
return RES;
}
public String request(HttpMethod method) throws AnnotationException {
String response = null;
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
LOG.error("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
response = new String(responseBody);
} catch (HttpException e) {
LOG.error("Fatal protocol violation: " + e.getMessage());
throw new AnnotationException("Protocol error executing HTTP request.",e);
} catch (IOException e) {
LOG.error("Fatal transport error: " + e.getMessage());
LOG.error(method.getQueryString());
throw new AnnotationException("Transport error executing HTTP request.",e);
} finally {
// Release the connection.
method.releaseConnection();
}
return response;
}
protected static String readFileAsString(String filePath) throws java.io.IOException{
return readFileAsString(new File(filePath));
}
protected static String readFileAsString(File file) throws IOException {
byte[] buffer = new byte[(int) file.length()];
#SuppressWarnings("resource")
BufferedInputStream f = new BufferedInputStream(new FileInputStream(file));
f.read(buffer);
return new String(buffer);
}
static abstract class LineParser {
public abstract String parse(String s) throws ParseException;
static class ManualDatasetLineParser extends LineParser {
public String parse(String s) throws ParseException {
return s.trim();
}
}
static class OccTSVLineParser extends LineParser {
public String parse(String s) throws ParseException {
String result = s;
try {
result = s.trim().split("\t")[3];
} catch (ArrayIndexOutOfBoundsException e) {
throw new ParseException(e.getMessage(), 3);
}
return result;
}
}
}
public void saveExtractedEntitiesSet(String Question, LineParser parser, int restartFrom) throws Exception {
String text = Question;
int i=0;
//int correct =0 ; int error = 0;int sum = 0;
for (String snippet: text.split("\n")) {
String s = parser.parse(snippet);
if (s!= null && !s.equals("")) {
i++;
if (i<restartFrom) continue;
List<DBpediaResource> entities = new ArrayList<DBpediaResource>();
try {
entities = extract(new Text(snippet.replaceAll("\\s+"," ")));
System.out.println(entities.get(0).getFullUri());
} catch (AnnotationException e) {
// error++;
LOG.error(e);
e.printStackTrace();
}
for (DBpediaResource e: entities) {
RES.add(e.uri());
}
}
}
}
public abstract List<DBpediaResource> extract(Text text) throws AnnotationException;
public void evaluate(String Question) throws Exception {
evaluateManual(Question,0);
}
public void evaluateManual(String Question, int restartFrom) throws Exception {
saveExtractedEntitiesSet(Question,new LineParser.ManualDatasetLineParser(), restartFrom);
}
}
main()
public static void main(String[] args) throws Exception {
String Question ="Is the Amazon river longer than the Nile River?";
db c = new db ();
c.configiration(0.0, 0, "non", "CoOccurrenceBasedSelector", "Default", "yes");
System.out.println("resource : "+c.getResu());
}
I just add one little fix for your answer.
Your code is running, if you add the evaluate method call:
public static void main(String[] args) throws Exception {
String question = "Is the Amazon river longer than the Nile River?";
db c = new db ();
c.configiration(0.0, 0, "non", "CoOccurrenceBasedSelector", "Default", "yes");
c.evaluate(question);
System.out.println("resource : "+c.getResu());
}
Lamine
In the request method of the second class (AnnotationClient) in Adel's answer, the author Pablo Mendes hasn't finished
TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
which is an annoying warning that needs to be removed by replacing
byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
response = new String(responseBody);
with
Reader in = new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8");
StringWriter writer = new StringWriter();
org.apache.commons.io.IOUtils.copy(in, writer);
response = writer.toString();

reasonable handling of ScriptException thrown by JSR223 Rhino

I'm starting to run into the dirty little secrets of what is an otherwise very useful JSR223 scripting environment.
I'm using the builtin version of Rhino shipped with Java 6 SE, accessing it through JSR223's ScriptingEngine et al.
When I get an exception caused by a Java object I've exported into the Javascript environment, it is a ScriptingException that wraps a sun.org.mozilla.javascript.internal.WrappedException that wraps my real exception (e.g. UnsupportedOperationException or whatever)
The ScriptingException returns null for getFileName() and -1 for getLineNumber().
But when I look at the message and at the debugger, the WrappedException has the correct filename and line number, it's just not publishing it via the ScriptingException's getter methods.
Great. Now what do I do? I don't know how I'm going to use sun.org.mozilla.javascript.internal.wrappedException which isn't a public class anyway.
Argh. Java 6's Rhino does the same thing (doesn't publish the file name / line number / etc via ScriptingException's methods) with sun.org.mozilla.javascript.internal.EvaluatorException and who knows how many other exceptions.
The only reasonable way I can think of to handle this is to use reflection. Here's my solution.
void handleScriptingException(ScriptingException se)
{
final Throwable t1 = se.getCause();
String lineSource = null;
String filename = null;
Integer lineNumber = null;
if (hasGetterMethod(t1, "sourceName"))
{
lineNumber = getProperty(t1, "lineNumber", Integer.class);
filename = getProperty(t1, "sourceName", String.class);
lineSource = getProperty(t1, "lineSource", String.class);
}
else
{
filename = se.getFileName();
lineNumber = se.getLineNumber();
}
/* do something with this info */
}
static private Method getGetterMethod(Object object, String propertyName)
{
String methodName = "get"+getBeanSuffix(propertyName);
try {
Class<?> cl = object.getClass();
return cl.getMethod(methodName);
}
catch (NoSuchMethodException e) {
return null;
/* gulp */
}
}
static private String getBeanSuffix(String propertyName) {
return propertyName.substring(0,1).toUpperCase()
+propertyName.substring(1);
}
static private boolean hasGetterMethod(Object object, String propertyName)
{
return getGetterMethod(object, propertyName) != null;
}
static private <T> T getProperty(Object object, String propertyName,
Class<T> cl) {
try {
Object result = getGetterMethod(object, propertyName).invoke(object);
return cl.cast(result);
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}

Categories

Resources