📄 graph.java
字号:
package toocom.ocgl;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
/**
* This class represents the conceptual graphs of the ontology. A conceptual graph is a bipartite
* graph with concept nodes and relation nodes.
*
* @author Fr閐閞ic F黵st
*/
public class Graph extends NamedGraphicalObject{
private LinkedList concepts;
private LinkedList relations;
public Graph(Terms terms){
super(terms);
this.concepts = new LinkedList();
this.relations = new LinkedList();
}
public Graph(){
super(Terms.getUndefinedTerms());
this.concepts = new LinkedList();
this.relations = new LinkedList();
}
public void destroy(Ontology onto){}
/** Returns the cloned graph, with the same terms. */
public Graph cloneGraph(){
Graph g = new Graph(this.getTerms());
// old concept id / new concept id
Hashtable cons = new Hashtable();
for(Iterator i = this.concepts.iterator();i.hasNext();){
Concept cold = (Concept) i.next();
Concept cnew = cold.cloneConcept();
g.addConcept(cnew);
cons.put(new Integer(cold.getId()),new Integer(cnew.getId()));
}
for(Iterator i = this.relations.iterator();i.hasNext();){
Relation rold = (Relation) i.next();
Relation rnew = rold.cloneRelation();
g.addRelation(rnew);
for(int j = 1;j <= rold.getArity();j ++){
Concept c = (Concept) rold.getLinkedConcept(j);
if(c != null){
rnew.setLinkedConcept(g.getConcept(((Integer) cons.get(new Integer(c.getId()))).intValue()),j);
}
}
}
return g;
}
/** Returns the description of the graph as logical formula. */
public String toString(Language l){
String result = "";
if(this.getTerms() != null) result = this.getTerm(l) + " : ";
Hashtable variables = new Hashtable();
int cpt = 1;
String exists = CGConstants.THERE_EXISTS + " ";
for(Iterator i = this.concepts.iterator();i.hasNext();){
Concept c = (Concept) i.next();
if(c.isGenericConcept()){
String var = new String("x" + cpt);
variables.put(c,var);
exists += var + ",";
cpt ++;
result += c.getType().getTerm(l) + "(" + var + ") " + CGConstants.AND + " ";
}
else result += c.getType().getTerm(l) + "(" + c.getIndividual().getTerm(l) + ") " + CGConstants.AND + " ";
}
exists = exists.substring(0,exists.length() - 1);
exists += " " + CGConstants.SUCH_AS + " ";
result = exists + result;
for(Iterator i = this.relations.iterator();i.hasNext();){
Relation r = (Relation) i.next();
result += r.getTerm(l) + "(";
for(Iterator j = r.getLinkedConcepts().iterator();j.hasNext();){
Concept c = (Concept) j.next();
if(c.isGenericConcept()) result += (String) variables.get(c);
else result += c.getIndividual().getTerm(l);
if(j.hasNext()) result += ",";
}
result += ") " + CGConstants.AND + " ";
}
return result.substring(0,result.length() - CGConstants.AND.length() - 1);
}
/** Returns the number of vertex (concepts and relations) of the graph. */
public int getVertexCount(){
return (concepts.size() + relations.size());
}
/** Returns true if this graph contains no concept and no relation. */
public boolean isEmpty(){
return ((concepts.size() == 0) && (relations.size() == 0));
}
public LinkedList getConcepts(){
return concepts;
}
public LinkedList getRelations(){
return relations;
}
/** Returns the concept specified by its id, null if any concept with this id is present
* in the graph. */
public Concept getConcept(int id){
for(Iterator i = concepts.iterator();i.hasNext();){
Concept c = (Concept) i.next();
if(c.getId() == id) return c;
}
return null;
}
/** Returns the concept specified by its label, null if any concept with this label
* is present in the graph. */
public Concept getConcept(String label, Language l){
for(Iterator i = concepts.iterator();i.hasNext();){
Concept c = (Concept) i.next();
if(c.getTerm(l).equals(label)) return c;
}
return null;
}
/** Returns the relation specified by its id, null if any relation with this id is present
* in the graph. */
public Relation getRelation(int id){
for(Iterator i = relations.iterator();i.hasNext();){
Relation r = (Relation) i.next();
if(r.getId() == id) return r;
}
return null;
}
/** Returns the relation specified by its type label, null if any relation with this label
* is present in the graph. */
public Relation getRelation(String typeLabel, Language l){
for(Iterator i = relations.iterator();i.hasNext();){
Relation r = (Relation) i.next();
if(r.getTerm(l).equals(typeLabel)) return r;
}
return null;
}
/** Returns true if a relation with the given label exists between the 2 concepts. */
public boolean existsRelation(String rLabel, Language l, Concept c1, Concept c2){
for(Iterator i = relations.iterator();i.hasNext();){
Relation r = (Relation) i.next();
if(r.getTerm(l).equals(rLabel) && r.hasLinkedConcept(c1) && r.hasLinkedConcept(c2)) return true;
}
return false;
}
/** Returns the concept or relation with the given id, null if any. */
public NamedGraphicalObject getObject(int id){
Concept c = this.getConcept(id);
if(c != null) return c;
else return this.getRelation(id);
}
/** Returns the list of relations linked to the given concept in the graph. If the concept
* is linked to a relation with more than a link, the relation will only appear one time
* in the list. */
public LinkedList getLinkedRelations(Concept c){
LinkedList result = new LinkedList();
for(Iterator i = relations.iterator();i.hasNext();){
Relation r = (Relation) i.next();
if(r.getConceptIndexList(c).size() != 0) result.add(r);
}
return result;
}
/** Add the given concept if it does not already appears in the list and returns true,
* returns false otherwise. */
public boolean addConcept(Concept c){
if(!this.concepts.contains(c)){
this.concepts.add(c);
return true;
}
else return false;
}
/** Add the given relation if it does not already appears in the list and returns true,
* returns false otherwise. */
public boolean addRelation(Relation r){
if(!this.relations.contains(r)){
this.relations.add(r);
return true;
}
else return false;
}
/** Remove the given concept and returns true if the concept is in the list, returns false
* otherwise. */
public boolean removeConcept(Concept c){
if(this.concepts.remove(c)){
for(Iterator i = this.relations.iterator();i.hasNext();){
Relation r = (Relation) i.next();
for(int j = 1;j <= r.getArity();j ++){
Concept linked = r.getLinkedConcept(j);
if((linked != null) && (linked.equals(c))) r.setLinkedConcept(null,j);
}
}
return true;
}
else return false;
}
/** Remove the given relation and returns true if the relation is in the list, returns false
* otherwise. */
public boolean removeRelation(Relation r){
return this.relations.remove(r);
}
/** Returns true if the object (concept or relation) really appears in the graph and
* removes it, returns false otherwise. */
public boolean removeObject(NamedObject no){
if(this.concepts.contains(no)) return this.removeConcept((Concept) no);
else{
if(this.relations.contains(no)) return this.removeRelation((Relation) no);
else return false;
}
}
/** Returns the other concept if the graph contains another concept like c (same type and same individual),
* null otherwise. */
public Concept containsAnotherConceptLike(Concept c){
for(Iterator i = this.getConcepts().iterator();i.hasNext();){
Concept con = (Concept) i.next();
if((!con.equals(c)) && (con.getType() != null) && (c.getType() != null) && con.getType().equals(c.getType()) &&
(((con.getIndividual() == null) && (c.getIndividual() == null)) ||
((con.getIndividual() != null) && (c.getIndividual() != null) && (c.getIndividual().equals(con.getIndividual()))))) return con;
}
return null;
}
/** Returns the different relation if it exists between c1 and c2, null otherwise. */
public Relation containsDifferentRelationBetween(Concept c1,Concept c2, Ontology onto){
for(Iterator i = this.getRelations().iterator();i.hasNext();){
Relation r = (Relation) i.next();
if((r.getType() != null) && (r.getType().equals(onto.getDifferenceRelationType())) &&
r.hasLinkedConcept(c1) && r.hasLinkedConcept(c2)) return r;
}
return null;
}
/** Returns one of the relation of the igraph incompatible (or exclusive) with r, if it exists, null otherwise. */
public Relation getIncompatibleRelation(Relation r){
RelationExclusivity exclu = (RelationExclusivity) r.getType().getAxiomSchemata("toocom.ocgl.RelationExclusivity").getFirst();
LinkedList incomp = r.getType().getAxiomSchemata("toocom.ocgl.RelationIncompatibility");
for(Iterator i = this.getRelations().iterator();i.hasNext();){
Relation rbis = (Relation) i.next();
if((exclu != null) && (exclu.getTheOtherPrimitive(r.getType()).equals(rbis.getType()))) return rbis;
else{
for(Iterator j = incomp.iterator();j.hasNext();){
RelationIncompatibility relin = (RelationIncompatibility) j.next();
if(relin.getTheOtherPrimitive(r.getType()).equals(rbis.getType())) return rbis;
}
}
}
return null;
}
/** Returns the bounds of the image according to the font size of the given Graphics. */
public RectangularShape getBounds(Graphics g,Language l){
int xmin = -1;
int xmax = -1;
int ymin = -1;
int ymax = -1;
for(Iterator i = this.getConcepts().iterator();i.hasNext();){
Concept c = (Concept) i.next();
if(xmin == -1) xmin = (int) c.getBounds(g,l).getX();
else xmin = Math.min(xmin,(int) c.getBounds(g,l).getX());
if(xmax == -1) xmax = (int) (c.getBounds(g,l).getX() + c.getBounds(g,l).getWidth());
else xmax = Math.max(xmax,(int) (c.getBounds(g,l).getX() + c.getBounds(g,l).getWidth()));
if(ymin == -1) ymin = (int) c.getBounds(g,l).getY();
else ymin = Math.min(ymin,(int) c.getBounds(g,l).getY());
if(ymax == -1) ymax = (int) (c.getBounds(g,l).getY() + c.getBounds(g,l).getHeight());
else ymax = Math.max(ymax,(int) (c.getBounds(g,l).getY() + c.getBounds(g,l).getHeight()));
}
for(Iterator i = this.getRelations().iterator();i.hasNext();){
Relation r = (Relation) i.next();
if(xmin == -1) xmin = (int) r.getBounds(g,l).getX();
else xmin = Math.min(xmin,(int) r.getBounds(g,l).getX());
if(xmax == -1) xmax = (int) (r.getBounds(g,l).getX() + r.getBounds(g,l).getWidth());
else xmax = Math.max(xmax,(int) (r.getBounds(g,l).getX() + r.getBounds(g,l).getWidth()));
if(ymin == -1) ymin = (int) r.getBounds(g,l).getY();
else ymin = Math.min(ymin,(int) r.getBounds(g,l).getY());
if(ymax == -1) ymax = (int) (r.getBounds(g,l).getY() + r.getBounds(g,l).getHeight());
else ymax = Math.max(ymax,(int) (r.getBounds(g,l).getY() + r.getBounds(g,l).getHeight()));
}
return new Rectangle(xmin,ymin,xmax - xmin,ymax - ymin);
}
/** Paint the concept type with its label in a rectangular shape. */
public void paintObject(Graphics g,Language l){
for(Iterator i = this.getRelations().iterator();i.hasNext();){
Relation r = (Relation) i.next();
r.paintLinks(g,l);
}
for(Iterator i = this.getRelations().iterator();i.hasNext();){
Relation r = (Relation) i.next();
r.paintObject(g,l);
}
for(Iterator i = this.getConcepts().iterator();i.hasNext();){
Concept c = (Concept) i.next();
c.paintObject(g,l);
}
}
public void translate(int dx, int dy){
for(Iterator i = this.getRelations().iterator();i.hasNext();){
((Relation) i.next()).translate(dx,dy);
}
for(Iterator i = this.getConcepts().iterator();i.hasNext();){
((Concept) i.next()).translate(dx,dy);
}
}
/** Returns the graph that contains the seleted nodes of this one. The relations and
* concepts added to the new graph are those which appear in the original graph. */
public Graph getSelectedPart(){
Graph result = new Graph();
for(Iterator i = this.getRelations().iterator();i.hasNext();){
Relation r = (Relation) i.next();
if(r.isSelected()) result.addRelation(r);
}
for(Iterator i = this.getConcepts().iterator();i.hasNext();){
Concept c = (Concept) i.next();
if(c.isSelected()) result.addConcept(c);
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -