/************************************************************************
* Copyright (C) 19aa Claudio Reggiani alias Nophiq *
* Questo programma è software libero; è lecito ridistribuirlo e/o *
* modificarlo secondo i termini della Licenza Pubblica Generica GNU *
* come pubblicata dalla Free Software Foundation; o la versione 2 *
* della licenza o (a scelta) una versione successiva. *
* *
* Questo programma è distribuito nella speranza che sia utile, ma *
* SENZA ALCUNA GARANZIA; senza neppure la garanzia implicita di *
* COMMERCIABILITÀ o di APPLICABILITÀ PER UN PARTICOLARE SCOPO. Si *
* veda la Licenza Pubblica Generica GNU per avere maggiori dettagli. *
* *
* Ognuno dovrebbe avere ricevuto una copia della Licenza Pubblica *
* Generica GNU insieme a questo programma; in caso contrario, la si *
* può ottenere dalla Free Software Foundation, Inc., 675 Mass Ave, *
* Cambridge, MA 02139, Stati Uniti. *
* *
* Per contattarmi attraverso posta elettronica: nophiq@virgilio.it *
************************************************************************/
package albero;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import alice.tuprolog.*;
/**
*
* @author nophiq
*/
private Prolog engine;
private Theory t = null;
private SolveInfo info = null;
private Term resultTerm = null;
private Graph gr;
/** Creates a new instance of ObjectId */
public ObjectPanel(Graph gr) {
this.gr = gr;
engine = new Prolog();
try {
engine.setTheory(t);
}
catch(InvalidTheoryException e) {
System.
out.
println("Teoria non valida: "+e
);
}
System.
out.
println("Errore nella lettura della teoria");
}
}
public void getHierarchy() {
gr.clearAll();
this.drawPanel();
}
private void setObjectSetting
(GridBagConstraints c,
int gridx,
int gridy,
int gridheight,
int gridwidth
) {
c.gridx = gridx; // Posizionamento Orizzontale
c.gridy = gridy; // Posizionamento Verticale
c.gridheight = gridheight;
c.gridwidth = gridwidth;
c.
insets = new Insets (20, 20, 20, 20
);
}
public void drawPanel() {
// TODO: da spostare in getHierarchy()
this.getMember();
this.getRelation();
this.removeAll();
if (gr.getLengthVectorArcs() > 0 && gr.getLengthVectorNodes() > 0) {
// Utilizzo un vettore per ricordarmi le generazioni e settare coordX
// Disegno i nodi
for (int i=0; i<gr.getLengthVectorNodes(); i++) {
if (gr.getNode(i).getPointX() != -1 && gr.getNode(i).getPointY() != -1) {} // Coordinate già settate
else { // Assegno le coordinate se non sono già state calcolate
this.assegnaCoordinate(this, i, tmpGener);
// Vado alla ricerca della possibile moglie, marito
for (int j=0; j<gr.getLengthVectorRelation(); j++) {
Vector v
= gr.
giveMeRelation(gr.
getRelation(j
).
toString());
if (v.elementAt(0).toString().equals(gr.getNode(i).getNodeName()) && v.elementAt(1).equals("=_") &&
gr.getNodeAtThisName(v.elementAt(2).toString()).getPointX() == -1 && gr.getNodeAtThisName(v.elementAt(2).toString()).getPointY() == -1) {
int pos = gr.getNodeAtThisName(v.elementAt(2).toString()).getNumNode();
this.assegnaCoordinate(this, pos, tmpGener);
}
else if (v.elementAt(2).toString().equals(gr.getNode(i).getNodeName()) && v.elementAt(1).equals("=_") &&
gr.getNodeAtThisName(v.elementAt(0).toString()).getPointX() == -1 && gr.getNodeAtThisName(v.elementAt(0).toString()).getPointY() == -1) {
int pos = gr.getNodeAtThisName(v.elementAt(0).toString()).getNumNode();
this.assegnaCoordinate(this, pos, tmpGener);
}
}
}
}
}
this.repaint();
this.revalidate();
}
public void paintComponent
(Graphics g
) {
super.paintComponent(g);
for (int k=0; k<gr.getLengthVectorArcs(); k++) {
int a = gr.getArc(k).getPosStart();
int b = gr.getArc(k).getPosEnd();
int x1 = -1;
int y1 = -1;
for (int m=0; m<comp.length; m++) {
if (((JLabel) comp
[m
]).
getText().
equals(gr.
getNodeAtThisId(a
).
getNodeName())) {
x1
= (int) ((JLabel) comp
[m
]).
getLocation().
getX();
y1
= (int) ((JLabel) comp
[m
]).
getLocation().
getY();
}
}
int x2 = -1;
int y2 = -1;
for (int m=0; m<comp.length; m++) {
if (((JLabel) comp
[m
]).
getText().
equals(gr.
getNodeAtThisId(b
).
getNodeName())) {
x2
= (int) ((JLabel) comp
[m
]).
getLocation().
getX();
y2
= (int) ((JLabel) comp
[m
]).
getLocation().
getY();
}
}
g.drawLine(x1+10, y1+10, x2+10, y2+10);
}
}
private void assegnaCoordinate
(JPanel jp,
int i,
Vector tmpGener
) {
int gener = (gr.getNode(i).getGeneration()-1000)/10;
int coordX = 10;
int coordY = 10;
coordX
= coordX
+70
*this.
countSameGeneration(tmpGener,
new Integer(gener
).
toString());
if (gener < 0) coordY = coordY+(gener*2);
else if (gener > 0) coordY = coordY+(gener*2);
if (gr.
getNode(i
).
getGender().
equals("male")) l.
setForeground(Color.
blue);
else if (gr.
getNode(i
).
getGender().
equals("female")) l.
setForeground(Color.
magenta);
this.setObjectSetting(c, coordX, coordY, 1, 1);
jp.add(l, c);
gr.getNode(i).setPointX(coordX);
gr.getNode(i).setPointY(coordY);
tmpGener.
addElement(new Integer(gener
).
toString());
}
/*
* Funzione non utilizzata
*/
public boolean controlMemberAndRelation() {
// True => Tutti i membri hanno una relazione
// False => Alcuni membri non hanno una relazione
boolean b = true;
for (int i=0; i<gr.getLengthVectorNodes(); i++) {
b = false;
for (int j=0; j<gr.getLengthVectorRelation(); j++) {
Vector v
= gr.
giveMeRelation(gr.
getRelation(j
).
toString());
if (v.elementAt(0).toString().equals(gr.getNode(i).getNodeName()) ||
v.elementAt(2).toString().equals(gr.getNode(i).getNodeName())) b = true;
}
if (!b) return false;
}
return b;
}
int total = v.size();
int count = 0;
for (int i=0; i<total; i++) {
if (v.elementAt(i).equals(gen)) count++;
}
return count;
}
public void getMember() {
try {
query
= new Struct("getMember",
new Var
("Result"),
new Var
("Gender"));
info = engine.solve(query);
while(info.isSuccess()) {
// Operazione riuscita, riporto i risultati.
gr.addNode(info.getTerm("Result").toString(), info.getTerm("Gender").toString());
info = engine.solveNext();
}
}
catch(InvalidVarNameException e) {
System.
err.
println("L'espressione non è valida in Prolog");
}
catch(NoMoreSolutionException e) {
System.
err.
println("# NoMoreSolutionException");
//System.exit(1);
}
catch(NoSolutionException e) {
System.
err.
println("NoSolutionException");
//System.exit(1);
}
catch(UnknownVarException e) {
System.
err.
println("UnknownVarException");
}
}
public void getRelation() {
try {
query
= new Struct("getRelation",
new Var
("Result1"),
new Var
("Result2"),
new Var
("Result3"));
info = engine.solve(query);
while(info.isSuccess()) {
// Operazione riuscita, riporto i risultati.
String s1
= info.
getTerm("Result1").
toString();
String s2
= info.
getTerm("Result2").
toString();
String s3
= info.
getTerm("Result3").
toString();
// Costruisco un vettore di relazioni
gr.getVectorRelation().addElement(s1+s2+s3);
info = engine.solveNext();
}
}
catch(InvalidVarNameException e) {
System.
err.
println("L'espressione non è valida in Prolog");
}
catch(NoMoreSolutionException e) {
System.
err.
println("NoMoreSolutionException");
//System.exit(1);
}
catch(NoSolutionException e) {
System.
err.
println("NoSolutionException");
//System.exit(1);
}
catch(UnknownVarException e) {
System.
err.
println("UnknownVarException");
}
gr.setRelation();
}
public void addTheory
(String s
) {
try {
this.engine.addTheory(new Theory(s));
}
catch(InvalidTheoryException e) {
System.
out.
println("Teoria non valida");
}
}
try {
query
= new Struct("bestTrack", Term.
parse(atom1
), Term.
parse(atom2
),
new Var
("L"), Term.
parse("[]"));
info = engine.solve(query);
if (info.isSuccess()) {
// Operazione riuscita, riporto i risultati.
query
= new Struct("makeRelation", info.
getTerm("L"),
new Var
("L1"));
info = engine.solve(query);
if (info.isSuccess()) {
// Elaboro la nuova Teoria
String elab
= info.
getTerm("L1").
toString().
substring(1,
(info.
getTerm("L1").
toString().
length()-1
));
elab = elab.replace("'", "");
elab = elab.replace("[", "");
elab = elab.replace("]", "");
elab = elab.replace(",", "");
elab = elab.replace("-", ",-");
elab = elab.replace("))", ")),");
elab = elab.replace(")person", "),person");
elab = elab.replace(") person", "),person");
elab = elab.replace("-", "_");
elab = elab.replace(" ", "");
elab = elab.replace(atom1, "X");
elab = elab.replace(atom2, "Y");
System.
out.
println("## "+elab
);
this.getMember();
for (int i=0; i<gr.getLengthVectorNodes(); i++) {
elab = elab.replace(gr.getNode(i).getNodeName(), ("Q"+i));
}
this.addTheory("relation('"+name+"', 'X, Y', '"+elab.substring(0, elab.length()-1)+"').");
elab = elab + " L = [X,Y,";
for (int i=0; i<gr.getLengthVectorNodes(); i++) {
if (elab.contains(s)) elab = elab + "Q" + i + ",";
}
elab = elab.substring(0, elab.length()-1);
elab = elab + "], diseguali(L).";
String newStringTheory
= name
+ "(person(X, _), person(Y, _)) :- " + elab
;
System.
out.
println("## "+newStringTheory
);
this.addTheory(newStringTheory);
}
}
}
catch(InvalidVarNameException e) {
System.
err.
println("L'espressione non è valida in Prolog");
}
catch(NoSolutionException e) {
System.
err.
println("NoSolutionException");
//System.exit(1);
}
catch(UnknownVarException e) {
System.
err.
println("UnknownVarException");
}
catch (InvalidTermException e) {
System.
err.
println("L'espressione non è valida in Prolog");
}
}
public void setTheory
(File f
) {
try {
}
catch(InvalidTheoryException e) {
System.
out.
println("Teoria non valida");
}
}
public String getTheory
() {return this.
engine.
getTheory().
toString();}
public void newFile() {
try {
engine.setTheory(t);
}
catch(InvalidTheoryException e) {
System.
out.
println("Teoria non valida");
}
System.
out.
println("Errore nella lettura della teoria");
}
gr.clearAll();
this.repaint();
}
try {
// Controllo se a e b sono variabili o dati
String c
= name
+"(person("+a
+", _), person("+b
+", _)).";
info = engine.solve(c);
console.append("?- "+name+"("+a+", "+b+").\n");
boolean flag = false;
while(info.isSuccess()) {
// Operazione riuscita, riporto i risultati.
flag = true;
// Controllo se a e b è una variabile oppure un dato
if (Term.parse(a).isVar()) s1 = info.getTerm(a).toString();
if (Term.parse(b).isVar()) s2 = info.getTerm(b).toString();
console.append(s1+" è "+name+" di "+s2+"\n");
info = engine.solveNext();
}
if (!flag) console.append("L'operazione non è riuscita\n");
}
catch(InvalidTermException e) {
System.
err.
println("L'espressione non è valida in Prolog");
}
catch(NoSolutionException e) {
System.
err.
println("NoSolutionException");
//System.exit(1);
}
catch(NoMoreSolutionException e) {
System.
err.
println("NoSolutionException");
//System.exit(1);
}
catch(UnknownVarException e) {
System.
err.
println("UnknownVarException");
}
catch (MalformedGoalException e) {
System.
err.
println("L'espressione non è valida in Prolog");
}
console.append("-----------------\n");
}
public String viewRelation
() {
try {
query
= new Struct("sendMeAllRelation",
new Var
("A"),
new Var
("B"),
new Var
("C"));
info = engine.solve(query);
while(info.isSuccess()) {
// Operazione riuscita, riporto i risultati.
String name
= info.
getTerm("A").
toString();
String var
= info.
getTerm("B").
toString();
String desc
= info.
getTerm("C").
toString();
allRelation = allRelation+name+"("+var+")|"+desc+"{}";
allRelation = allRelation.replace("'", "");
info = engine.solveNext();
}
}
catch(InvalidVarNameException e) {
System.
err.
println("L'espressione non è valida in Prolog");
}
catch(NoMoreSolutionException e) {
System.
err.
println("NoMoreSolutionException");
//System.exit(1);
}
catch(NoSolutionException e) {
System.
err.
println("NoSolutionException");
//System.exit(1);
}
catch(UnknownVarException e) {
System.
err.
println("UnknownVarException");
}
return allRelation;
}
public boolean isMemberPresent
(String s
) {
try {
info = engine.solve("person("+s+", _).");
if (info.isSuccess()) return true;
}
catch(MalformedGoalException e) {
System.
err.
println("L'espressione non è valida in Prolog");
}
return false;
}
public boolean isRulePresent
(String s
) {
try {
info = engine.solve("relation("+s+", _, _).");
if (info.isSuccess()) return true;
}
catch(MalformedGoalException e) {
System.
err.
println("L'espressione non è valida in Prolog");
}
return false;
}
}