package paper.reasoning;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.ictclas4j.segment.Segment;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.Rule;
import com.hp.hpl.jena.util.PrintUtil;
public class PaperReasoningUI {
private JFrame frame;
private JTextField paperLocation;
private JButton choosePaperLocation;
private JTextArea text;
private JScrollPane textSP;
private JCheckBox[] boxes;
private JButton run;
private JTextArea docList;
private JScrollPane docListSP;
private JFileChooser paperFc;
private Segment seg;
public PaperReasoningUI() {
// ��ʼ������
initFrame();
// ��ʼ����ʾ����
initContentPane();
// ��ʼ����ť
initActions();
// ��ʼ���ļ�ѡ����
initFc();
seg = new Segment(1);
frame.setVisible(true);
}
// ��ʼ������
private void initFrame() {
frame = new JFrame("PaperReasoning");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);
double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
frame.setLocation((int) (width - frame.getWidth()) / 2,
(int) (height - frame.getHeight()) / 2);
frame.setResizable(false);
}
// ��ʼ����ť
private void initActions() {
choosePaperLocation.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = paperFc.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String location = paperFc.getSelectedFile()
.getAbsolutePath();
paperLocation.setText(location);
text.setText("");
docList.setText("");
String lines = "";
try {
BufferedReader fr = new BufferedReader(new FileReader(
new File(location)));
String line = null;
while (true) {
line = fr.readLine();
if (line == null) {
break;
}
lines += line;
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(frame, ex.getMessage(),
"Error!", JOptionPane.ERROR_MESSAGE);
}
text.setText(seg.split(lines).getFinalResult());
String[] text_tokens =text.getText().split("/\\w+ ");
textSP.getVerticalScrollBar().setValue(0);
OntModel model = ModelFactory
.createOntologyModel(OntModelSpec.OWL_DL_MEM);
try {
model.read(new FileInputStream(new File("qa.owl")),
null);
String prefix = "http://www.example.com/qa.owl#";
OntProperty gjc = model.getOntProperty(prefix + "�ؼ���");
for (int i = 0; i < boxes.length; i++) {
boxes[i].setSelected(false);
Individual yd_i = model.getIndividual(prefix
+ boxes[i].getLabel());
Iterator it = yd_i.listPropertyValues(gjc);
while (it.hasNext()) {
Literal lt = (Literal) it.next();
String[] literal_tokens = seg.split(lt.getString()).getFinalResult().split("/\\w+ ");
boolean contain= false;
for (int j=0;j<text_tokens.length-literal_tokens.length;j++){
int k=0;
for (; k<literal_tokens.length;k++){
if (!text_tokens[j+k].equals(literal_tokens[k])){
break;
}
}
if (k ==literal_tokens.length){
contain = true;
break;
}
}
if (contain) {
boxes[i].setSelected(true);
}
}
}
docList.setText("");
docList.append("��ʼ����\n");
PrintUtil.registerPrefix("qa", prefix);
OntClass hd_c = model.getOntClass(prefix + "�Ծ�����Ļش�");
Individual zshd_i = hd_c.createIndividual(prefix
+ "�����Ļش�");
Individual wt_i = model.getIndividual(prefix
+ "ͨ���������������Լ��Ĺ������");
OntProperty wts_p = model.getOntProperty(prefix
+ "�Ծ�������");
zshd_i.addProperty(wts_p, wt_i);
OntProperty ydy_p = model.getObjectProperty(prefix
+ "Ҫ����");
for (int i = 0; i < boxes.length; i++) {
if (boxes[i].isSelected()) {
Individual yd_i = model.getIndividual(prefix
+ boxes[i].getLabel());
zshd_i.addProperty(ydy_p, yd_i);
}
}
String ruleSrc = "[rule1: (?key rdf:type qa:����Ҫ��) "
+ " (?key qa:��ֵ ?points) "
+ " (?answer rdf:type qa:�Ծ�����Ļش�) "
+ " (?question rdf:type qa:�Ծ�����) "
+ " (?standard rdf:type qa:����) "
+ " (?answer qa:�Ծ������� ?question)"
+ " (?question qa:������ ?standard) "
+ " (?standard qa:Ҫ���� ?key) "
+ " (?answer qa:Ҫ���� ?key) "
+ " noValue(?key qa:�÷�) " + " -> "
+ " (?key qa:�÷� ?points)" + "]";
List rules = Rule.parseRules(ruleSrc);
Reasoner reasoner = new GenericRuleReasoner(rules);
InfModel inf = ModelFactory.createInfModel(reasoner,
model);
StmtIterator it = inf.getDeductionsModel()
.listStatements();
while (it.hasNext()) {
Statement stmt = it.nextStatement();
Literal obj = (Literal) stmt.getObject();
docList.append(stmt.getSubject().getLocalName()
+ "\t" + stmt.getPredicate().getLocalName()
+ "\t " + obj.getString() + "��ȫ�֣�\n");
}
ruleSrc = "[rule2: (?keya rdf:type qa:����Ҫ��) "
+ " (?keyb rdf:type qa:����Ҫ��) "
+ " (?keyb qa:��ֵ ?points) "
+ " (?keya ?p ?keyb)"
+ " (?answer rdf:type qa:�Ծ�����Ļش�) "
+ " (?question rdf:type qa:�Ծ�����) "
+ " (?standard rdf:type qa:����) "
+ " (?answer qa:�Ծ������� ?question)"
+ " (?question qa:������ ?standard) "
+ " (?standard qa:Ҫ���� ?keya) "
+ " (?standard qa:Ҫ���� ?keyb) "
+ " (?answer qa:Ҫ���� ?keya) "
+ " noValue(?answer qa:Ҫ���� ?keyb) "
+ " product(?points, 0.5, ?wpoints ) " + " -> "
+ " (?keyb qa:�÷� ?wpoints) " + " ]";
rules = Rule.parseRules(ruleSrc);
reasoner = new GenericRuleReasoner(rules);
InfModel inf2 = ModelFactory.createInfModel(reasoner,
inf);
it = inf2.getDeductionsModel().listStatements();
while (it.hasNext()) {
Statement stmt = it.nextStatement();
Literal obj = (Literal) stmt.getObject();
docList.append(stmt.getSubject().getLocalName()
+ "\t" + stmt.getPredicate().getLocalName()
+ "\t" + obj.getString() + "����֣�\n");
}
ruleSrc = "[rule3: (?key rdf:type qa:����Ҫ��) noValue(?key qa:�÷�) -> (?key qa:�÷� 0.0) ]";
rules = Rule.parseRules(ruleSrc);
reasoner = new GenericRuleReasoner(rules);
InfModel inf3 = ModelFactory.createInfModel(reasoner,
inf2);
it = inf3.getDeductionsModel().listStatements();
while (it.hasNext()) {
Statement stmt = it.nextStatement();
Literal obj = (Literal) stmt.getObject();
docList.append(stmt.getSubject().getLocalName()
+ "\t" + stmt.getPredicate().getLocalName()
+ "\t" + obj.getString() + "����֣�\n");
}
docList
.append("------------------------------------------------------------\n");
ruleSrc = "[rule4: (qa:�Ӱ� qa:�÷� ?jbpoints) "
+ " (qa:��������λ�� qa:�÷� ?wzpoints) "
+ " (qa:н�� qa:�÷� ?xcpoints) "
+ " (qa:�������� qa:�÷� ?hjpoints ) "
+ " sum(?jbpoints, ?wzpoints, ?temp1) "
+ " sum(?temp1, ?xcpoints, ?temp2) "
+ " sum(?temp2, ?hjpoints, ?score) "
+ " (?answer rdf:type qa:�Ծ�����Ļش�) "
+ " (?question rdf:type qa:�Ծ�����) "
+ " (?standard rdf:type qa:����) "
+ " (?answer qa:�Ծ������� ?question)"
+ " (?question qa:������ ?standard) " + " -> "
+ " (?answer qa:�÷� ?score) " + "]";
rules = Rule.parseRules(ruleSrc);
reasoner = new GenericRuleReasoner(rules);
InfModel inf4 = ModelFactory.createInfModel(reasoner,
inf3);
it = inf4.getDeductionsModel().listStatements();
while (it.hasNext()) {
Statement stmt = it.nextStatement();
Literal obj = (Literal) stmt.getObject();
docList.append(stmt.getSubject().getLocalName()
+ "\t" + stmt.getPredicate().getLocalName()
+ "\t" + obj.getString() + "\n");
}
docList.append("�������");
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(frame,
"�����ر���⣬��ȷ��qa.owl������ڵ�ǰĿ¼�¡�", "Error!",
JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame,
"���������鿴error.log�ļ���", "Error!",
JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
}
});
run.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread() {
public void run() {
docList.setText("");
docList.append("��ʼ����\n");
OntModel model = ModelFactory
.createOntologyModel(OntModelSpec.OWL_DL_MEM);
try {
model.read(new FileInputStream(new File("qa.owl")),
null);
String prefix = "http://www.example.com/qa.owl#";
PrintUtil.registerPrefix("qa", prefix);
OntClass hd_c = model.getOntClass(prefix
+ "�Ծ�����Ļش�");
Individual zshd_i = hd_c.createIndividual(prefix
+ "�����Ļش�");
Individual wt_i = model.getIndividual(prefix
+ "ͨ���������������Լ��Ĺ������");
OntProperty wts_p = model.getOntProperty(prefix
+ "�Ծ�������");
zshd_i.addProperty(wts_p, wt_i);
OntProperty ydy_p = model.getObjectProperty(prefix
+ "Ҫ����");
for (int i = 0; i < boxes.length; i++) {
if (boxes[i].isSelected()) {
Individual yd_i = model
.getIndividual(prefix
+ boxes[i].getLabel());
zshd_i.addProperty(ydy_p, yd_i);
}
}
String ruleSrc = "[rule1: (?key rdf:type qa:����Ҫ��) "
+ " (?key qa:��ֵ ?points) "
+ " (?answer rdf:type qa:�Ծ�����Ļش�) "
+ " (?question rdf:type qa:�Ծ�����) "
+ " (?standard rdf:type qa:����) "
+ " (?answer qa:�Ծ������� ?question)"
+ " (?question qa:������ ?standard) "
+ " (?standard qa:Ҫ���� ?key) "
+ " (?answer qa:Ҫ���� ?key) "
+ " noValue(?key qa:�÷�) "
+ " -> "
+ " (?key qa:�÷� ?points)" + "]";
List rules = Rule.parseRules(ruleSrc);
Reasoner reasoner = new GenericRuleReasoner(rules);
InfModel inf = ModelFactory.createInfModel(
reasoner, model);
StmtIterator it = inf.getDeductionsModel()
.listStatements();
while (it.hasNext()) {
Statement stmt = it.nextStatement();
Literal obj = (Literal) stmt.getObject();
docList.append(stmt.getSubject().getLocalName()
+ "\t"
+ stmt.getPredicate().getLocalName()
+ "\t " + obj.getString() + "��ȫ�֣�\n");
}
ruleSrc = "[rule2: (?keya rdf:type qa:����Ҫ��) "
+ " (?keyb rdf:type qa:����Ҫ��) "
+ " (?keyb qa:��ֵ ?points) "
+ " (?keya ?p ?keyb)"
+ " (?answer rdf:type qa:�Ծ�����Ļش�) "
+ " (?question rdf:type qa:�Ծ�����) "
+ " (?standard rdf:type qa:����) "
+ " (?answer qa:�Ծ������� ?question)"
+ " (?question qa:������ ?standard) "
+ " (?standard qa:Ҫ���� ?keya) "
+ " (?standard qa:Ҫ���� ?keyb) "
+ " (?answer qa:Ҫ���� ?keya) "
+ " noValue(?answer qa:Ҫ���� ?keyb) "
+ " product(?points, 0.5, ?wpoints ) "
+ " -> " + " (?keyb qa:�÷� ?wpoints) "
+ " ]";
rules = Rule.parseRules(ruleSrc);
reasoner = new GenericRuleReasoner(rules);
InfModel inf2 = ModelFactory.createInfModel(
reasoner, inf);
it = inf2.getDeductionsModel().listStatements();
while (it.hasNext()) {
Statement stmt = it.nextStatement();
Literal obj = (Literal) stmt.getObject();
docList.append(stmt.getSubject().getLocalName()
+ "\t"
+ stmt.getPredicate().getLocalName()
+ "\t" + obj.getString() + "����֣�\n");
}
ruleSrc = "[rule3: (?key rdf:type qa:����Ҫ��) noValue(?key qa:�÷�) -> (?key qa:�÷� 0.0) ]";
rules = Rule.parseRules(ruleSrc);
reasoner = new GenericRuleReasoner(rules);
InfModel inf3 = ModelFactory.createInfModel(
reasoner, inf2);
it = inf3.getDeductionsModel().listStatements();
while (it.hasNext()) {
Statement stmt = it.nextStatement();
Literal obj = (Literal) stmt.getObject();
docList.append(stmt.getSubject().getLocalName()
+ "\t"
+ stmt.getPredicate().getLocalName()
+ "\t" + obj.getString() + "����֣�\n");
}
docList
.append("------------------------------------------------------------\n");
ruleSrc = "[rule4: (qa:�Ӱ� qa:�÷� ?jbpoints) "
+ " (qa:��������λ�� qa:�÷� ?wzpoints) "
+ " (qa:н�� qa:�÷� ?xcpoints) "
+ " (qa:�������� qa:�÷� ?hjpoints ) "
+ " sum(?jbpoints, ?wzpoints, ?temp1) "
+ " sum(?temp1, ?xcpoints, ?temp2) "
+ " sum(?temp2, ?hjpoints, ?score) "
+ " (?answer rdf:type qa:�Ծ�����Ļش�) "
+ " (?question rdf:type qa:�Ծ�����) "
+ " (?standard rdf:type qa:����) "
+ " (?answer qa:�Ծ������� ?question)"
+ " (?question qa:������ ?standard) "
+ " -> " + " (?answer qa:�÷� ?score) " + "]";
rules = Rule.parseRules(ruleSrc);
reasoner = new GenericRuleReasoner(rules);
InfModel inf4 = ModelFactory.createInfModel(
reasoner, inf3);
it = inf4.getDeductionsModel().listStatements();
while (it.hasNext()) {
Statement stmt = it.nextStatement();
Literal obj = (Literal) stmt.getObject();
docList.append(stmt.getSubject().getLocalName()
+ "\t"
+ stmt.getPredicate().getLocalName()
+ "\t" + obj.getString() + "\n");
}
docList.append("�������");
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(frame,
"�����ر���⣬��ȷ��qa.owl������ڵ�ǰĿ¼�¡�", "Error!",
JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame,
"���������鿴error.log�ļ���", "Error!",
JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
}.start();
}
});
}
// ��ʼ����ʾ����
private void initContentPane() {
JPanel north = new JPanel();
paperLocation = new JTextField(25);
paperLocation.setEditable(false);
choosePaperLocation = new JButton("ѡ����...");
north.add(new JLabel("����ַ��"));
north.add(paperLocation);
north.add(choosePaperLocation);
JPanel center = new JPanel();
text = new JTextArea(7, 40);
text.setEditable(false);
textSP = new JScrollPane(text);
center.add(textSP);
JPanel n = new JPanel();
run = new JButton("����");
boxes = new JCheckBox[4];
boxes[0] = new JCheckBox("���������");
boxes[1] = new JCheckBox("�");
boxes[2] = new JCheckBox("�Ӱ�");
boxes[3] = new JCheckBox("��������");
n.add(new JLabel("�ش��Ҫ���У�"));
for (int i = 0; i < boxes.length; i++) {
boxes[i].setEnabled(false);
n.add(boxes[i]);
}
// n.add(run);
center.add(n);
JPanel south = new JPanel();
docList = new JTextArea(8, 40);
docList.setEditable(false);
docListSP = new JScrollPane(docList);
south.add(docListSP);
frame.getContentPane().add(north, BorderLayout.NORTH);
frame.getContentPane().add(center, BorderLayout.CENTER);
frame.getContentPane().add(south, BorderLayout.SOUTH);
}
// ��ʼ���ļ�ѡ����
private void initFc() {
paperFc = new JFileChooser();
CustomFileFilter filter = new CustomFileFilter();
filter.addExtension("txt");
filter.setDescription("plain text file");
paperFc.setFileFilter(filter);
}
public static void main(String[] args) {
new PaperReasoningUI();
}
}