在运行Android序言序言、Android

2023-09-06 16:37:39 作者:发尾也羞涩

我在Android的测试tuProlog。我有一个活动TuProlog,类解析器与序言code互动,data.pl包含序言code。我可以罚款运行它与输出Java项目安慰,但我现在面临无法做到为Android项目。为Android,我得到FileNotFoundException异常,即使我的文件data.pl在项目的根目录复制,里面src和我的包里面。我只是想获取结果作为字符串,并显示我的结果TextView中。这里是我的codeS

 公共类TuProlog扩展活动实现OnClickListener {TextView的电视;按钮B1;/ **当第一次创建活动调用。 * /@覆盖公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.main);    电视=(的TextView)findViewById(R.id.label);    B1 =(按钮)findViewById(R.id.button1);    b1.setOnClickListener(本);}@覆盖公共无效的onClick(视图v){    解析器定制=新的分析器();    字符串结果= custom.parse();    tv.setText(结果);}}公共类解析器{Prolog的发动机;PrintStream的orgStream = System.out的;ByteArrayOutputStream BAOS =新ByteArrayOutputStream();PrintStream的PSOUT =新的PrintStream(BAOS,Boolean.TRUE); //使用自动冲洗串myResult;公共字符串解析(){    发动机=新的Prolog();    尝试{        理论T =新理论(新的FileInputStream(data.pl));        尝试{            engine.setTheory(T);            尝试{                SolveInfo答案= engine.solve(喜欢(约翰,X));                尝试{                    术语衍生物= answer.getTerm(X);                    返回myResult ;;                }                赶上(NoSolutionException E){                    e.printStackTrace();                }                赶上(UnknownVarException E){                    e.printStackTrace();                }            }            赶上(MalformedGoalException E){                e.printStackTrace();            }        }        赶上(InvalidTheoryException E){            e.printStackTrace();        }    }    赶上(FileNotFoundException异常五){        e.printStackTrace();    }    赶上(IOException异常五){        e.printStackTrace();    }    返回null;}    @覆盖公共无效onSpy(SpyEvent E){    // TODO自动生成方法存根    Log.d(TAG,** LG'd onSpy => SpyEvent发生**);    的System.out.println(** onSpy => SpyEvent发生** \\ n);    myResult = e.getMsg();}@覆盖公共无效onOutput(OutputEvent E){    // TODO自动生成方法存根     Log.d(TAG,** LG'd:onOutput => OutputEvent发生**);        的System.out.println(** onOutput => OutputEvent发生** \\ n);        myResult = e.getMsg();}@覆盖公共无效onWarning(WarningEvent E){    // TODO自动生成方法存根    Log.d(TAG,** LG'd:onWarning => WarningEvent发生**);    的System.out.println(** onWarning => WarningEvent发生** \\ n);    myResult = e.getMsg();}} 

Data.pl

 喜欢(约翰,玛丽)。喜欢(玛丽酒)。 

下面是我的logcat输出,我不知道System.err的

  18 04-15:51:25.480:W / System.err的(23813):java.io.FileNotFoundException:/data.pl(没有这样的文件或目录)04-15 18:51:25.484:W / System.err的(23813):在org.apache.harmony.luni.platform.OSFileSystem.open(本机方法)04-15 18:51:25.484:W / System.err的(23813):在dalvik.system.BlockGuard $ WrappedFileSystem.open(BlockGuard.java:232)04-15 18:51:25.484:W / System.err的(23813):在java.io.FileInputStream中的<&初始化GT;(FileInputStream.java:80)04-15 18:51:25.484:W / System.err的(23813):在java.io.FileInputStream中的<&初始化GT;(FileInputStream.java:132)04-15 18:51:25.484:W / System.err的(23813):在com.tuprolog.alicia.Parser.parse(Parser.java:32)04-15 18:51:25.484:W / System.err的(23813):在com.tuprolog.alicia.TuProlog.onClick(TuProlog.java:51)04-15 18:51:25.484:W / System.err的(23813):在android.view.View.performClick(View.java:2485)04-15 18:51:25.484:W / System.err的(23813):在android.view.View $ PerformClick.run(View.java:9080)04-15 18:51:25.484:W / System.err的(23813):在android.os.Handler.handleCallback(Handler.java:587) 
android前言

解决方案

我以前做过,并贴(概念非常公测仅证明)来源$ C ​​$ C审讯,请参见下文。

要下载Eclipse(太阳神)项目来源$ C ​​$ C,后藤:versaggi.biz,下载Adrea,TuProlog开发项目时,Eclipse(太阳神)Java源代码项目,终于到了Android的TuProlog Eclipse中,项目源。这应该让你开始。请记住,这是概念$ C $证明仅C,并且将发布最终版本之前被彻底改写。鉴于此,它的工作不够好,你得到一些见解我怎么做我所做的。如果你想要的任何援助都只是直接与我联系,我会很乐意一起帮你。 : - )

I am testing tuProlog in Android. I have an Activity TuProlog, class Parser to interact with prolog code and data.pl which contains prolog code. I can run it fine as a java project with output to console but I am facing trouble doing so as an Android project. For Android I get FileNotFoundException even though my file data.pl is copied in root of project, inside src and inside my package. I just want to fetch the result as string and display my result in TextView. Here are my codes

public class TuProlog extends Activity implements OnClickListener{

TextView tv;
Button b1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tv = (TextView)findViewById(R.id.label);
    b1 = (Button)findViewById(R.id.button1);
    b1.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    Parser custom = new Parser();
    String result = custom.parse();
    tv.setText(result);
}   
}


public class Parser {

Prolog engine;
PrintStream orgStream   = System.out;

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream psout = new PrintStream(baos, Boolean.TRUE); // Using autoFlush
String myResult ;

public String parse()
{
    engine  = new Prolog();
    try{
        Theory t = new Theory(new FileInputStream("data.pl"));
        try{
            engine.setTheory(t);
            try{
                SolveInfo answer = engine.solve("likes(john,X).");
                try{
                    Term derivative = answer.getTerm("X");
                    return myResult;;
                }
                catch (NoSolutionException e){
                    e.printStackTrace();
                }
                catch (UnknownVarException e){
                    e.printStackTrace();
                }
            }
            catch (MalformedGoalException e){
                e.printStackTrace();
            }
        }
        catch (InvalidTheoryException e){
            e.printStackTrace();
        }
    } 
    catch (FileNotFoundException e){
        e.printStackTrace();
    }
    catch (IOException e){
        e.printStackTrace();
    }
    return null;
}
    @Override
public void onSpy(SpyEvent e) {
    // TODO Auto-generated method stub
    Log.d("TAG", "** LG'd onSpy => SpyEvent Occured ** " );
    System.out.println("** onSpy => SpyEvent Occured ** \n ");
    myResult =  e.getMsg();
}


@Override
public void onOutput(OutputEvent e) {
    // TODO Auto-generated method stub
     Log.d("TAG", "** LG'd: onOutput => OutputEvent Occured ** " );
        System.out.println("** onOutput => OutputEvent Occured ** \n ");
        myResult =  e.getMsg();

}


@Override
public void onWarning(WarningEvent e) {
    // TODO Auto-generated method stub
    Log.d("TAG", "** LG'd: onWarning => WarningEvent Occured ** " );
    System.out.println("** onWarning => WarningEvent Occured ** \n ");
    myResult = e.getMsg();
}
}

Data.pl

likes(john,mary).
likes(mary,wine).

Here is my logcat output, I dont know about System.err

04-15 18:51:25.480: W/System.err(23813): java.io.FileNotFoundException: /data.pl (No such file or directory)
04-15 18:51:25.484: W/System.err(23813): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
04-15 18:51:25.484: W/System.err(23813): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
04-15 18:51:25.484: W/System.err(23813): at java.io.FileInputStream.<init>(FileInputStream.java:80)
04-15 18:51:25.484: W/System.err(23813): at java.io.FileInputStream.<init>(FileInputStream.java:132)
04-15 18:51:25.484: W/System.err(23813): at com.tuprolog.alicia.Parser.parse(Parser.java:32)
04-15 18:51:25.484: W/System.err(23813): at com.tuprolog.alicia.TuProlog.onClick(TuProlog.java:51)
04-15 18:51:25.484: W/System.err(23813): at android.view.View.performClick(View.java:2485)
04-15 18:51:25.484: W/System.err(23813): at android.view.View$PerformClick.run(View.java:9080)
04-15 18:51:25.484: W/System.err(23813): at android.os.Handler.handleCallback(Handler.java:587)

解决方案

I've done it before and have posted the (very, very beta proof of concept only) source code for interrogation, pls see below.

To download the Eclipse (Helios) project Source Code, goto: versaggi.biz, Downloads Adrea, TuProlog Dev Project, Eclipse (Helios) Java Source Project, and finally to TuProlog Android Eclipse Porject Source. That should get you started. Please keep in mind that this is proof of concept code ONLY and is will be completely rewritten before the final version is released. Given that, it does work well enough for you to get some insights into how I did what I did. If you want any assistance at all just contact me directly and I'll be glad to help you along. :-)