質問

I am trying to use jpl to load the same swipl file into different modules. The reason I had to do this is because I want to have a module that I can assert new predicates to, while leave the other untouched. Problem is swipl seems forbidding this,

jpl.PrologException: PrologException: error(permission_error(load, source, 'load.pro'), context(/(load_files, 2), 'Non-module file already loaded into module stable; trying to load into to_mess'))
    at jpl.Query.get1(Query.java:336)
    at jpl.Query.hasMoreSolutions(Query.java:258)
    at jpl.Query.oneSolution(Query.java:688)
    at jpl.Query.hasSolution(Query.java:759)

I have tried to set redefine_module(true) for load_files, but still no go

val query = new Query(s"load_files(${m}:'${loader}', [redefine_module(true)])")
query.allSolutions()

I have been blocked by this for hours, but cannot find a solution online. Can anybody please help??

役に立ちましたか?

解決

You can use Logtalk running on SWI-Prolog + JPL to easily accomplish having two encapsulation units (objects instead of modules in this case) sharing a common initial definition (the contents of the file you're trying to load in two or more different modules). For the details of using Logtalk + SWI-Prolog + JPL see for example:

https://github.com/LogtalkDotOrg/logtalk3/wiki/Using-Logtalk-with-JPL

For the code sharing implied in your question, one solution is to put the contents of the file in an object and then derive from it (using inheritance) as many object as needed. For a more specific advice I would need more details on what you're trying to accomplish.

他のヒント

The module name is arbitrary, I think you could append an increasing integer to the name. Just be sure to keep track of it, to be able to reference the asserted predicates.

From what I can tell, jpl seems not to be always consistent with what you'd achieved in swipl console, even though they are coming from the exact same build.

Reading documentation of load_files/2 again, I ended up with this solution to make things work...

load_files(stable:'load.pl', [register(false)]) .
load_files('load.pl', [register(false)]) .

Note I cannot assert new predicates into module (this works in swipl console, but NOT through jpl), so I just loaded files into a module which is supposed to be stable, and loaded the same set of files again into the prolog vm (no module) directly where I can assert new predicates.

Update: if I assert the new predicate in prolog directly through jpl (no module), the predicate will transcend into modules. This behaves differently from swipl console.

Update: This is wrong - although jpl does not complain or throw exception, the file is actually loaded for just once.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top