-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuery.java
77 lines (66 loc) · 2.6 KB
/
Query.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import com.ugos.jiprolog.engine.JIPEngine;
import com.ugos.jiprolog.engine.JIPQuery;
import com.ugos.jiprolog.engine.JIPSyntaxErrorException;
import com.ugos.jiprolog.engine.JIPTerm;
import com.ugos.jiprolog.engine.JIPVariable;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author ruankotovich
* @github https://github.com/ruankotovich
*/
public class Query {
public void main2(String args[]) {
// New instance of prolog engine
JIPEngine jip = new JIPEngine();
JIPTerm queryTerm = null;
// parse query
try {
// consult file
//jip.consultStream(in, string);
BufferedReader bufferedReader;
bufferedReader = new BufferedReader(new FileReader("./relacoes.pl"));
while (bufferedReader.ready()) {
jip.assertz(jip.getTermParser().parseTerm(bufferedReader.readLine()));
}
queryTerm = jip.getTermParser().parseTerm("canHaveAWedding(Father, Child)");
} catch (JIPSyntaxErrorException ex) {
System.exit(0); // needed to close threads by AWT if shareware
} catch (FileNotFoundException ex) {
Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
}
// needed to close threads by AWT if shareware
// open Query
JIPQuery jipQuery = jip.openSynchronousQuery(queryTerm);
JIPTerm solution;
// Loop while there is another solution
while (jipQuery.hasMoreChoicePoints()) {
solution = jipQuery.nextSolution();
if (solution != null) {
if (solution.getVariables().length > 0) {
JIPVariable[] vars = solution.getVariables();
StringBuilder stringB = new StringBuilder();
for (JIPVariable var : vars) {
if (!var.isAnonymous()) {
stringB.append(var.getName()).append(" = ").append(var.toString(jip)).append("\n");
}
}
JOptionPane.showMessageDialog(null, stringB);
}
}
}
}
}