Problem to run a Lua Function from Java

36 views
Skip to first unread message

Joaco

unread,
Jan 11, 2012, 7:56:50 PM1/11/12
to JNLua Discussion
Hi, I'm trying to run the following Lua function from Java but I don't
realize how to do, the function is simple but require LuaXml and other
lua files.
This function (ParseXml) must take a Xml file and get some data.Is
there any way to run it using JnLua?

- ParseXml.lua

require('LuaXml')
require('Node')
require('ReferencesCounter')
require('StringUtils')
require('ReferenceCountingReport')

function parse(filePath)

local xfile = xml.load(filePath)

local lexicos={} --Table that keeps all the lexicon definition.
local nameCurrentState

for i,node in ipairs(xfile) do
-- only process the lexicon elements
if (node:tag() == "lexicon") then
local lexicoNode=Node:new()
lexicoNode.available = true
-- search for a id_lexicon element inside lexicon tag
local id = node:find("id_lexicon")
if id then
lexicoNode.id= id[1] --Set node's id
end
--search for the name of the lexicon inside lexicon tag
local name = node:find("name")
if name then
--print(name[1])
lexicoNode.name = name --Set node's name
end
end
table.insert(lexicos,lexicoNode) --Insert the current node to the
list of nodes.
end
end
end

- Node.lua

-- struct of Node
Node = {
id = nil,
name = nil,
category = nil,
notions = {},
behavioral = {},
referenceFrom = {},
available= true,
classification = nil -- subject, object, verb, etc..
}

-- create an instance of Node
function Node:new(o)
o = o or {}
setmetatable(o,self)
self.__index = self
return o
end

-- add behavioral at the last of the array
function Node:addBehavioral(e)
local index = #self.behavioral
if index == 0 then -- if the array doesn't exist or is empty
index = index + 1
self.behavioral = {}
end
table.insert(self.behavioral,e)
end

-- add an edge at the last of the array
function Node:addNotion(e)
local index = #self.notions
if(index == 0) then -- if the array doesn't exist or is empty
index = index + 1
self.notions = {}
end
table.insert(self.notions,e)
end

-- remove from the edge's list the edge in the parameter position
function Node:removeNotionAt(i)
self.notions[i] = nil
end

-- remove from the edge's list the edge in the parameter position
function Node:removeBehavioralAt(i)
self.behavioral[i] = nil
end

-- add a Node at the reference list
function Node:addReferenceFrom(e)
local index = #self.referenceFrom
if(index == 0) then -- if the array doesn't exist or is empty
index = index + 1
self.referenceFrom = {}
end
table.insert(self.referenceFrom,e)
end




Anyone can help me or tell me a clue to do it?

Tnx

Andre Naef

unread,
Jan 12, 2012, 4:02:45 PM1/12/12
to jnlua-...@googlegroups.com
Running this function involves about the following:

1. Create a Java LuaState.
2. Call load(), passing an input stream with the contents of ParseXml.lua
3. Call call(0, 0) to run the loaded source chunk.
4. Call getGlobal("parse") to push the parse function on the Lua stack.
5. Call pushString(filename) to push the filename on the Lua stack.
6. Call call(1, 0) to invoke the parse function, passing the file name as
the argument.

You must ensure that everything that is loaded by "require" can be found by
a Lua loader, i.e. is in package.path or package.cpath.

Joaco

unread,
Jan 30, 2012, 6:54:09 PM1/30/12
to JNLua Discussion
Thanks Man! your explanation was very helpful. I have one more
question for you if don't mind.
I'm trying to use jnlua-0.9.4 in a web proyect with maven2 and yetty.
Because I didn't find a maven repository with jnlua, I installed
manually with the following sentence

mvn install:install-file -DgroupId=com.naef -DartifactId=jnlua -
Dversion=0.9.4 -Dpackaging=jar -Dfile="e:\jnlua-0.9.4.jar"

and this is part of my pom's proyect file

     </dependencies>
               ...
              <!-- JnLua -->
              <dependency>
                     <groupId>com.naef</groupId>
                     <artifactId>jnlua</artifactId>
                     <version>0.9.1-SNAPSHOT</version>
              </dependency>
       </dependencies>

The problem happens when I want to create a LuaState instance, the
program hung and had to be killed.

 Did you try to use jnlua from a server? Do you got any idea why could
this happen?

Thanks






NOTE: in the way of maven get up the dependencies, only one of the
jars (jnlua-0.9.1 or jnlua-0.9.1-SNAPSHOT) is in the proyect since the
difference (for Maven2) between this two jars is the version.

Andre Naef

unread,
Jan 30, 2012, 7:40:33 PM1/30/12
to jnlua-...@googlegroups.com
Have you been able to pinpoint, using a debugger, where exactly the "hang"
occurs?

I am not aware of any general sever specific issues that would jump to my
mind.

Reply all
Reply to author
Forward
0 new messages