📄 inferenceengine.java
字号:
package toocom.ui;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.lang.*;
import toocom.ocgl.*;
import toocom.iotools.*;
import java.io.*;
import javax.swing.filechooser.*;
/**
* This class represents the graphical window into which the inference engine is running.
*
* @author Fr閐閞ic F黵st
*/
public class InferenceEngine extends JPanel implements MouseListener, MouseMotionListener{
private MainFrame mf;
private InferenceEngineFrame ief;
// The mode of interaction : 0=concept creation, 1=relation creation, 2=destruction, 3=linking
private int mode;
// localObject/serverID (the local object of a rule is those of the axiom that induces the
// rule, the local object of a constraint is those of the graph that represent a
// constraint induced by an axiom.
private LinkedList implicitRules;
public LinkedList explicitRules;
private LinkedList implicitConstraints;
public LinkedList explicitConstraints;
private Graph g;
private LinkedList tempCon;
private LinkedList tempRel;
private int graphServerID;
private NamedGraphicalObject temporaryImage;
private Concept tempCI;
private Relation tempRI;
private Point pointerPosition;
private Axiom selectedAxiom;
private Graph selectedGraph;
private LinkedList projections;
private int currentProj;
public Graph getGraph(){
return this.g;
}
public void clearGraph(){
this.temporaryImage = null;
this.g = new Graph();
this.tempCI = null;
this.tempRI = null;
this.repaint();
}
public void setPrimitiveMode(int mode){
this.mode = mode;
if(temporaryImage != null){
temporaryImage.setSelected(false);
}
temporaryImage = null;
if(tempRI != null){
tempRI.setSelected(false);
tempRI = null;
}
if(tempCI != null){
tempCI.setSelected(false);
tempCI = null;
}
this.selectedAxiom = null;
this.selectedGraph = null;
this.repaint();
}
public void nextProjection(){
if((mode == -1) && (projections.size() > 0)){
if(currentProj < (projections.size() -1)) currentProj ++;
else currentProj = 0;
System.out.println("next " + currentProj);
}
this.changeProjection();
this.repaint();
}
public void previousProjection(){
if((mode == -1) && (projections.size() > 0)){
if(currentProj > 0) currentProj--;
else currentProj = projections.size() - 1;
}
this.changeProjection();
this.repaint();
}
public void mouseClicked(MouseEvent e){
if(g != null){
if(e.getButton() == MouseEvent.BUTTON3){
Concept ci = this.getPointedConcept(e.getX(),e.getY());
if(ci == null){
Relation ri = this.getPointedRelation(e.getX(),e.getY());
if(ri != null) new RelationPropertyFrame(ri,mf,false);
else{
if(tempRI != null){
tempRI.setSelected(false);
tempRI = null;
}
if(tempCI != null){
tempCI.setSelected(false);
tempCI = null;
}
if(temporaryImage != null){
temporaryImage.setSelected(false);
temporaryImage = null;
}
}
}
else new ConceptPropertyFrame(ci,mf,false);
}
if(e.getButton() == MouseEvent.BUTTON1){
if(mode == 0){
Concept c = new Concept(e.getX(),e.getY());
g.addConcept(c);
this.repaint();
}
if(mode == 1){
Relation r = new Relation(e.getX(),e.getY());
g.addRelation(r);
}
if(mode == 2){
temporaryImage = this.getPointedComponent(e.getX(),e.getY());
if(temporaryImage != null){
g.removeObject(temporaryImage);
}
}
if(mode == 3){
if(tempCI == null){
if(tempRI == null){
tempCI = this.getPointedConcept(e.getX(),e.getY());
if(tempCI == null){
tempRI = this.getPointedRelation(e.getX(),e.getY());
if(tempRI != null) tempRI.setSelected(true);
}
else tempCI.setSelected(true);
}
else{
tempCI = this.getPointedConcept(e.getX(),e.getY());
if(tempCI != null){
tempRI.addLinkedConcept(tempCI);
tempRI.setSelected(false);
tempRI = null;
tempCI = null;
}
}
}
else{
if(tempRI == null){
tempRI = this.getPointedRelation(e.getX(),e.getY());
if(tempRI != null){
tempRI.addLinkedConcept(tempCI);
tempCI.setSelected(false);
tempRI = null;
tempCI = null;
}
}
}
}
}
this.repaint();
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){
if(e.getButton() == MouseEvent.BUTTON1){
if((mode == 0) || (mode == 1) || (mode == 3) || (mode == -1)){
temporaryImage = this.getPointedComponent(e.getX(),e.getY());
pointerPosition = new Point(e.getX(),e.getY());
}
}
}
public void mouseReleased(MouseEvent e){
if((mode == 0) || (mode == 1) || (mode == 3) || (mode == -1)){
temporaryImage = null;
this.repaint();
}
}
public void mouseDragged(MouseEvent e){
if((mode == 0) || (mode == 1) || (mode == 3) || (mode == -1)){
if((temporaryImage != null) && (e.getX() > 0) && (e.getX() < this.getWidth()) &&
(e.getY() > 0) && (e.getY() < this.getHeight())){
Graphics g = this.getGraphics();
temporaryImage.erase(g,Constants.INFERENCE_ENGINE_FRAME_COLOR,mf.getOntologyLanguage());
temporaryImage.translate(e.getX() - pointerPosition.x,e.getY() - pointerPosition.y);
this.repaint();
pointerPosition.setLocation(e.getX(),e.getY());
}
}
}
public void mouseMoved(MouseEvent e){}
/** Returns the concept image which contains the point (x,y) with the closiest center to the point (x,y).
* Returns null if any concept is pointed. */
public Concept getPointedConcept(int x, int y){
if(g != null){
Concept result = null;
double distance = Double.MAX_VALUE;
for(Iterator i = g.getConcepts().iterator();i.hasNext();){
Concept ci = (Concept) i.next();
if(ci.containsPoint(x,y,this.getGraphics(),mf.getOntologyLanguage())){
double temp = Math.sqrt(((ci.x - x) * (ci.x - x))
+ ((ci.y - y) * (ci.y - y)));
if(temp <= distance){
distance = temp;
result = ci;
}
}
}
for(Iterator i = tempCon.iterator();i.hasNext();){
Concept ci = (Concept) i.next();
if(ci.containsPoint(x,y,this.getGraphics(),mf.getOntologyLanguage())){
double temp = Math.sqrt(((ci.x - x) * (ci.x - x))
+ ((ci.y - y) * (ci.y - y)));
if(temp <= distance){
distance = temp;
result = ci;
}
}
}
return result;
}
else return null;
}
/** Returns the relation image which contains the point (x,y) with the closiest center to the point (x,y).
* Returns null if any relation is pointed. */
public Relation getPointedRelation(int x, int y){
if(g != null){
Relation result = null;
double distance = Double.MAX_VALUE;
for(Iterator i = g.getRelations().iterator();i.hasNext();){
Relation ri = (Relation) i.next();
if(ri.containsPoint(x,y,this.getGraphics(),mf.getOntologyLanguage())){
double temp = Math.sqrt(((ri.x - x) * (ri.x - x))
+ ((ri.y - y) * (ri.y - y)));
if(temp <= distance){
distance = temp;
result = ri;
}
}
}
for(Iterator i = tempRel.iterator();i.hasNext();){
Relation ri = (Relation) i.next();
if(ri.containsPoint(x,y,this.getGraphics(),mf.getOntologyLanguage())){
double temp = Math.sqrt(((ri.x - x) * (ri.x - x))
+ ((ri.y - y) * (ri.y - y)));
if(temp <= distance){
distance = temp;
result = ri;
}
}
}
return result;
}else return null;
}
/** Returns the object image which contains the point (x,y) with the closiest center to the point (x,y).
* Returns null if any component is pointed. */
public NamedGraphicalObject getPointedComponent(int x, int y){
NamedGraphicalObject noi = null;
noi = this.getPointedConcept(x,y);
if(noi == null) return this.getPointedRelation(x,y);
else return noi;
}
public void paint(Graphics gr){
gr.setColor(Constants.INFERENCE_ENGINE_FRAME_COLOR);
gr.fillRect(0,0,this.getWidth(),this.getHeight());
if(g != null){
if(selectedAxiom != null){
for(Iterator i = tempRel.iterator();i.hasNext();){
((Relation) i.next()).paintLinks(gr,mf.getOntologyLanguage());
}
for(Iterator i = tempRel.iterator();i.hasNext();){
((Relation) i.next()).paintObjectAsConclusion(gr,mf.getOntologyLanguage());
}
for(Iterator i = tempCon.iterator();i.hasNext();){
((Concept) i.next()).paintObjectAsConclusion(gr,mf.getOntologyLanguage());
}
}
g.paintObject(gr,mf.getOntologyLanguage());
if(selectedGraph != null){
for(Iterator i = tempRel.iterator();i.hasNext();){
((Relation) i.next()).paintObjectAsConclusion(gr,mf.getOntologyLanguage());
}
for(Iterator i = tempCon.iterator();i.hasNext();){
((Concept) i.next()).paintObjectAsConclusion(gr,mf.getOntologyLanguage());
}
}
}
//tb.repaint();
}
public void saveGraph(){
if(this.g != null){
JFileChooser fileChooser = new JFileChooser(Constants.APPLICATION_DIRECTORY);
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.setFileFilter(new CGXMLFileFilter());
if(fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
File f = fileChooser.getSelectedFile();
if(!f.getName().endsWith(CGXMLParser.CGXML_FILE_EXTENSION)) f = new File(f.getPath() + CGXMLParser.CGXML_FILE_EXTENSION);
if(f.exists()){
if(JOptionPane.showConfirmDialog(this,f.getName() + "\n" + Constants.OVERWRITE_FILE_MESSAGE) == JOptionPane.OK_OPTION){
try{
if(CGXMLParser.saveGraph(mf.getOntology(),this.g,mf.getOntologyLanguage(),f.getPath(),true))
JOptionPane.showMessageDialog(this,Constants.SAVING_GRAPH_SUCCESSFULL_MESSAGE);
else JOptionPane.showMessageDialog(this,Constants.SAVING_GRAPH_PROBLEM_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(this,Constants.SAVING_GRAPH_PROBLEM_MESSAGE + "\n\n" + e.getMessage());
}
}
}
else{
try{
if(CGXMLParser.saveGraph(mf.getOntology(),this.g,mf.getOntologyLanguage(),f.getPath(),true))
JOptionPane.showMessageDialog(this,Constants.SAVING_GRAPH_SUCCESSFULL_MESSAGE);
else JOptionPane.showMessageDialog(this,Constants.SAVING_GRAPH_PROBLEM_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(this,Constants.SAVING_GRAPH_PROBLEM_MESSAGE + "\n\n" + e.getMessage());
}
}
}
}
}
public boolean loadGraph(){
JFileChooser fileChooser = new JFileChooser(Constants.APPLICATION_DIRECTORY);
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.setFileFilter(new CGXMLFileFilter());
if(fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -