Tuesday, May 31, 2016

Day 19: Old is Gold

So, I'm back to this blog after a long hiatus. During research, you feel like whatever you are doing is a complete waste of time.

I have lines and lines of code that I no longer use. Though it is part of the learning process, it's very easy to feel completely annoyed.

Until today, when I found I could reuse some code I wrote 1.5 years ago :D

Cheers!

Tuesday, June 16, 2015

Day 18: A Day of Frustration and Installation - SWI Prolog and JPL

There are some days when you do absolutely nothing in lab but set up an environment. Because research scholars typically use highly random packages or developing ones, support for some of them is abysmal. So I thought if this post is to help even one other person save time, I'll be happy.

Prolog is a logic machine that I've been trying to connect with Java. SWI-Prolog comes with java support through the package JPL. But, setting it up proved to be a veritable nightmare.

And it was a nightmare even after these two very helpful tutorials here and here :P

So, here is my experience in setting up what at this time are the latest version of Prolog and JPL on Fedora 20.

Step #1 : Remove previous installations of SWI-Prolog

Some version incompatibilities lead to "Fatal Error: Could not find system resources". This is mentioned in one of the tutorials as the last step which I took the liberty of placing right on top! In Fedora, that was done using:

yum erase pl

Step #2 : Install dependencies

For JPL to access SWI Prolog, we need to compile from source. Hence, we need to manually install the dependencies first.

sudo yum install autoconf chrpath libunwind freetype-devel gmp-devel java-1.6.0-openjdk-devel jpackage-utils libICE-devel libjpeg-devel libSM-devel libX11-devel libXaw-devel libXext-devel libXft-devel libXinerama-devel libXmu-devel libXpm-devel libXrender-devel libXt-devel ncurses-devel openssl-devel pkgconfig readline-devel unixODBC-devel zlib-devel uuid-devel libarchive-devel

Step #3 : Download SWI - Prolog

The latest one is here. You need to download the source and extract into a folder.

tar xvf  swipl-7.2.1.tar.gz /usr/local/src/

Step #4 : Configure and Install

While configuring, it is important that it is done exactly as described with sharing enabled and prefix specified.

cd /usr/local/src/swipl-7.2.1
./configure --prefix=/usr/local/src/swipl-7.2.1 --enable-shared
make
make install

Step #5 : Check if SWI-Prolog is installed

Type swipl into the command prompt and see if you get "Welcome to SWI Prolog". I did not not get that message. So what I did was to check if it was working in the "bin" folder.

cd /usr/local/src/swipl-7.2.1/bin
./swipl

This worked fine for me.
Then, I copied it to usr/bin so that it can be accessed from anywhere.

cp swipl /usr/bin/swipl

Step #6 : Configure and Install JPL

This step is very simple. It develops the jpl.jar that is required for the Java Bridge.

cd /usr/local/src/swipl-7.2.1/packages/jpl
./configure
make
make install

Step #7 : Set up environment variables

I did this through Eclipse. Right click on the Java file that you are going to use and choose "Run As" and then "Run Configuration". Under the environment tab, you need to add two variables.

LD_LIBRARY_PATH = /usr/local/src/swipl-7.2.1/lib/x86_64-linux
PATH = ... : /usr/local/src/swipl-7.2.1/packages/jpl

The first path points to libswipl.so and the next one to libjpl.so. You can set these up in command line as well.

Step #8 : Set up JVM arguments

JPL requires a JVM argument. While running, you need to specify this option. In Eclipse, in the same "Run Configuration" window, choose "Arguments" tab and type the following under VM arguments.

-Djava.library.path = /usr/local/src/swipl-7.2.1/packages/jpl
Otherwise, the error would be "java.lang.UnsatisfiedLinkError: no jpl in java.library.path".

Step #9 : The moment of truth :P

Make a file, "foo.pl" which has :

foo(a).
foo(b).

Create a Java file in the same folder with the following code :

import jpl.Compound;
import jpl.Query;
import jpl.Term;
import jpl.Variable;

public class PrologBridge {

public static void main(String[] args) {
Query q1 = new Query("consult('foo.pl')");
System.out.println( "consult " + (q1.hasSolution() ? "succeeded" : "failed"));
Query q2 = new Query("foo(a)");
Boolean resp= q2.hasSolution();
System.out.println("foo(a) is " + resp.toString());
Query q3 = new Query("foo(c)");
System.out.println("foo(c) is " + ( q3.hasSolution() ? "provable" : "not provable" ));
Query q4 = new Query(new Compound("foo", new Term[] { new Variable("X")}));
while (q4.hasMoreElements())
System.out.println(q4.nextElement());
}
}

It took me a lot of time to get the correct program as the ones given in both the documentation and the tutorials were not working. That is when Stack Overflow came to my rescue!

This was my output:

foo(a) is true
foo(c) is not provable
{X=a}
{X=b}

Happy Coding! :)


Tuesday, January 20, 2015

Day 17: Netbeans and ?

I was faced with a rather queer problem today where compilation is successful but I got a compilation error at runtime. This is some weird behaviour of Netbeans and not my code thank goodness.

http://stackoverflow.com/questions/2333285/java-lang-runtimeexception-uncompilable-source-code-what-can-cause-this

On a lighter note, I felt sorry for an over-enthu student who was discussing with my friend his ideas which were progressively proven to be existing much to his dismay. This is a classic feeling everyone has.


Tuesday, January 6, 2015

Day 16: Input/Output

Implementing [Bakman[2007]]

Question 1

Input:
David has 5 apples. He gave apples to Ruth. David has 3 apples. How many apples did David give?

Output:
=================================================
Question:
David has 5 apples. He gave apples to Ruth. David has 3 apples. How many apples did David give?
=================================================
Simplified Question:
David  has 5 apples.
David  gave apples to Ruth.
David  has 3 apples.
How many apples  did David give.

==================================================
Solution:
David forfeit 2 apple

Question 2

Input:
Ruth has 3 dolls. Ann has 4 dolls. How many dolls do Ruth and Ann have altogether?

Output:
===================================================
Question:
Ruth has 3 dolls. Ann has 4 dolls. How many dolls do Ruth and Ann have altogether?
===================================================
Simplified Question:
Ruth  has 3 dolls.
Ann  has 4 dolls.
How many dolls  do Ruth and Ann have altogether.

===================================================
Solution:
altogether Ann and Ruth has 7 doll


Question 3

Input:
Sara has 6 flowers. Clara has 3 flowers more than Sara. How many flowers does Clara have ?

Output:
===================================================
Question:
Sara has 6 flowers. Clara has 3 flowers more than Sara. How many flowers does Clara have ?
===================================================
Simplified Question:
Sara  has 6 flowers.
Clara  has 3 flowers more than Sara.
How many flowers  does Clara have.

===================================================
Solution:
Clara has 9 flower


Question 4

Input:
Ruth had 3 apples. She put 2 apples into a basket. How many apples are there in the basket now, if in the beginning there were 4 apples in the basket?

Output:
===================================================
Question:
Ruth had 3 apples. She put 2 apples into a basket. How many apples are there in the basket now, if in the beginning there were 4 apples in the basket?
===================================================
Simplified Question:
Ruth  had 3 apples.
Ruth  put 2 apples into a basket.
How many apples  are there in the basket now.
There  were 4 apples in the basket.

===================================================
Solution:
6 apple in basket

Question 5

Input:
Ruth had 5 nuts more than Dan had. Ruth gave Dan 3 nuts. Dan gave 2 nuts to David. Now Dan has 4 nuts and David has 6 nuts. How many nuts does Ruth have now?

Output:
===================================================
Question:
Ruth had 5 nuts more than Dan had. Ruth gave Dan 3 nuts. Dan gave 2 nuts to David. Now Dan has 4 nuts and David has 6 nuts. How many nuts does Ruth have now?
===================================================
Simplified Question:
Ruth  had 5 nuts more than Dan had.
Ruth  gave Dan 3 nuts.
Dan  gave 2 nuts to David.
Dan  has 4 nuts.
David  has 6 nuts.
How many nuts  does Ruth have now.

====================================================
Solution:
Dan has 6 nut
Dan had 3 nut
Ruth had 8 nut
Ruth has 5 nut
David had 4 nut

[It provides all possible answers apart from the asked question]

Question 6

Input:
David gave 3 candies to Ruth, and John gave 2 candies to David. Now David has 4 candies more than Ruth has. How many candies does David have now, if Ruth had 7 candies in the beginning?

Output:
==================================================
Question:
David gave 3 candies to Ruth, and John gave 2 candies to David. Now David has 4 candies more than Ruth has. How many candies does David have now, if Ruth had 7 candies in the beginning?
===================================================
Simplified Question:
David  gave 3 candies to Ruth.
John  gave 2 candies to David.
David  has 4 candies more than Ruth has.
How many candies  does David have now.
Ruth  had 7 candies in the beginning.

====================================================
Solution:
Ruth has 10 candy
David had 15 candy
David has 12 candy
David has 14 candy

[This shows the progression of David's gains and losses]

Wednesday, December 24, 2014

Day 15: What a workout!

Today started off very pleasantly for me. I opened my program after a long time and was faced with Greek and Latin as the old saying goes.

I had lunch at 11 30 itself which seems to be a nice idea. I coded solidly till around seven thirty. Idiotic mistakes I made

1. Precedence of logical operators
2. The fact that strings are immutable - copy.replace does nothing to copy unless I put copy = copy.replace

Anyway, I managed to capture events from my word problem and solve it. It's going good.

Tuesday, December 23, 2014

Day 14: Talk after talk after talk

Somewhere in an academic mentality, the urge to absorb information and the fact that you may miss out something urges me to attend a talk every time I am in lab. And no matter how amazing it was, I will always feel, oh my God it's evening and I haven't done anything :P

Apart from that, yesterday somebody asked me a few doubts and I had this sinking feeling - why do you ask me when I myself am so self-doubting. But as the conversation progressed, I felt more and more confident. Then, he asked me about MIT's Smart system and I felt like, oh I don't know what it is and googled it.

Only to find that I have already visited the page and I realized I had read it so fast and it had completely evaporated from my system. This is coming from a person who remembers what was the colour of her chair in kindergarten :P

Which reminded me to get back to blogging, I'm getting old for God's Sake!


Sunday, December 7, 2014

Day 13: How to stop a thread in Java

This is my ultimate guide to killing a thread.

a) Interrupt it
b) In its definition, catch the InterruptedException and interrupt it again :P

Demos are highly confusing.