Using java in a predicative logic manner

234 views Asked by At

Premises ('till point 7 let's pretend they're all true or reasonable or pertinent, please)

(1) Java is free, has Swing and Javafx etc.

(2) About Java there are a lot of docs, tutorials, etc.

(3) I'd like to use it for kinda ontology - first order logic reasoning

(4) I know about protegé but it's not enough

(5) Let me please use an example:

  1. predicate logic Language.

    • Let's pretend we are talking about Fruits

pseudo-code in declarative Language

Axioms or similar: domains and functions

includedAsSubset(Orange, Fruit);

includedAsSubset(Apple, Fruit);

includedAsSubset(Color, Thing);

includedAsSubset(Fruit, Thing);

isRed: Fruit -> Boolean;

isYellow: Fruit -> Boolean;

hasColor: Fruit -> Color;

    **Facts**                         

isIn(thisOrange, Orange);

not(isRed(thisOrange));

isIn(thisApple, Apple);
  1. Translation in Java code

I'd use interfaces for "A extends B, C"

public interface Fruit {
    Boolean isRed();
    Boolean isYellow();
    Color hasColor();
}

public interface Orange extends Fruit {};

public interface Apple extends Fruit {};

public class ConcreteApple implements Apple {

    Color thisAppleColor;
    static ArrayList<Color> concreteApplesColours = new ArrayList<Color>();

    ConcreteApple(Color color) {
        Color red = new Color();
        concreteApplesColours.add(red);
        Color yellow = new Color();
        concreteApplesColours.add(yellow);

        setThisAppleColor(color); // try catch should be added to check for the color...
    }

    @Override
    Boolean isRed() {
        return new Boolean(this.getThisAppleColor().equals(red));
    }
    @Override
    Boolean isYellow() {
        return new Boolean(this.getThisAppleColor().equals(yellow));
    }
    @Override
    Color hasColor() {
        return this.getThisAppleColor();
    }
}

public class Reasoner {
    static void main() {
        Color orange = new Color();
        Color Yellow = new Color();
        Orange thisOrange = new ConcreteOrange(orange);
        Apple thisApple = new ConcreteApple(yellow);
        if(thisApple.isRed() && thisOrange.isRed()) {
            doSomething();
        } else if (thisApple.isRed()) {
            doSomethingElse();
        }
    }
}

(6) The sketch of the project is as follows:
(6.1) DB or file (maybe xml file or txt. for example F1.txt) in which sentences of the kind "A is a class", "B is a subclass of A" etc. are collected (represented as "includeIn(B, A)" etc.);
(6.2) another file (F2.txt) for function-predicate signatures: "f:Y -> Boolean", "g:X -> Y" etc.
(6.3) another file about facts (F3.txt): "x isIn X", "y isIn Y", "f(g(x)) == true AND not(f(y))"
(6.4) a Java program (J1.jar) which "translates" (by Reflection) F1.txt and F2.txt in a second java program (J2.jar) by which file F3.txt is used to append new facts and delete some of the old ones.

(7) Project's Ratio: it's simpler (I suppose it is) to add facts (file F3.txt) and rules (Domains in F1.txt or Predicates-Functions in F2.txt) instead of modifying java code when needed. And Java as a lot of libraries, ide's, gui tools etc.

(8) Similar topics and questions (clearly not enough for my needs):

Question (two-fold):

Is there any name for such a topic or way of programming using OO Language for another paradigm in a mixed way (it has something to do with ontology http://protegewiki.stanford.edu/wiki/Main_Page, http://www.w3.org/TR/owl-features/, Description Logic and declarative programming)?

Do you think is it the wrong approach (if you need declarative - functional -logic programming use another Language: it does not matter if they are not as good as Java for GUI's, they're not Platform Independent, robust etc.)?

Many thanks

1

There are 1 answers

1
Alexei Kaigorodov On

Actually you are building an in-memory database of facts, and want to make queries against it. As with any other database, you face several problems: accurate modification of indexes when main fact is changed (or a fact is added or removed), planning query evaluation, synchronization with backend storage. This technical aspects should not be mixed with business logic in your program, so they should be concentrated in a framework (library). Either you develop your own framework, or take an existing one.

I recommend to play with some opensource SPARQL java implementation, and then, if you still prefer java programs over textual fact/query representation, hack that implementation and use internals directly, bypassing textual representation.