⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inferenceengine.java

📁 toocom源代码,主要应用在本体匹配方面!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			try{
				this.clearGraph();
				this.tempCon.clear();
				this.tempRel.clear();
				this.selectedGraph = null;
				this.selectedAxiom = null;
				this.g = CGXMLParser.loadGraph(mf.getOntology(),mf.getOntologyLanguage(),fileChooser.getSelectedFile().getPath(),true);
				if(this.g == null){
					JOptionPane.showMessageDialog(this,Constants.NOT_VALID_GRAPH_FILE);
					return false;
				}				
				else{
					this.repaint();
					return true;
				}
			}
			catch(Exception e){
				JOptionPane.showMessageDialog(this,Constants.NOT_VALID_GRAPH_FILE);
				return false;
			}
		}
		else return false;
	}
	
	public void askQuestion(){
		if(this.g == null) JOptionPane.showMessageDialog(this,Constants.NO_VALID_GRAPH);
		else{
			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){
				try{
					this.setPrimitiveMode(-1);
					selectedGraph = CGXMLParser.loadGraph(mf.getOntology(),mf.getOntologyLanguage(),fileChooser.getSelectedFile().getPath(),true);
					if(selectedGraph == null) JOptionPane.showMessageDialog(this,Constants.NOT_VALID_GRAPH_FILE);
					else{
						this.projections = mf.getCogitantClient().getGraphProjections(mf.getOntology(),selectedGraph,g,mf.getOntologyLanguage());
						currentProj = 0;
						selectedAxiom = null;
						this.changeProjection();
						this.repaint();
						ief.toFront();
					}
				}
				catch(Exception e){
					JOptionPane.showMessageDialog(this,Constants.NOT_VALID_GRAPH_FILE);
				}
			}
		}
	}
	
	public void run(){
		TooCoMProgressBar pb = new TooCoMProgressBar(mf,Constants.APPLYING_AXIOM_MESSAGE);
		this.saturation();
		Graph result = this.checkConstraints();
		this.repaint();
		if(result != null){
			JOptionPane.showMessageDialog(this,Constants.IMPLICIT_CONSTRAINT_BROKEN_MESSAGE + " : " + result.getTerm(mf.getOntologyLanguage()));
		}
		pb.setValue(100);	
	}
	
	/** Saturates the current graph with the implicit rules. */
	public void saturation(){
		try{
			if(g != null){
				for(Iterator i = this.implicitRules.iterator();i.hasNext();){
					Axiom implicitAxiom = (Axiom) i.next();
					LinkedList projList = this.getProductiveProjections(implicitAxiom,g);
					while(projList.size() > 0){
						//this.g = mf.getCogitantClient().applyAxiomAsRule(mf.getOntology(),implicitAxiom,this.g,(Projection) projList.getFirst(),mf.getOntologyLanguage());
						this.applyAxiomOnGraph(mf.getOntology(),implicitAxiom,this.g,(Projection) projList.getFirst());
						System.out.println();
						System.out.println("IMPLICIT RULE APPLIED " + implicitAxiom.getTerm(mf.getOntologyLanguage()) + " : " + implicitAxiom.toString(mf.getOntologyLanguage()));
						projList = this.getProductiveProjections(implicitAxiom,g);
					}
				}
			}
		}
		catch(IOException e){
			System.out.println(e.getMessage());
		}
	}
	
	/** Checks the implicite constraints on the current graph, returns the broken constraint
	 *  or null if any constraint is broken. */
	public Graph checkConstraints(){
		try{
			for(Iterator i = this.implicitConstraints.iterator();i.hasNext();){
				Graph constraint = (Graph) i.next();
				LinkedList projs = mf.getCogitantClient().getGraphProjections(mf.getOntology(),constraint,g,mf.getOntologyLanguage());
				if(projs.size() > 0){
					System.out.println();
					System.out.println("IMPLICIT CONSTRAINT BROKEN " + constraint.getTerm(mf.getOntologyLanguage()) + " : " + constraint.toString(mf.getOntologyLanguage()));
					Projection constraintProj = (Projection) projs.getFirst();
					for(Iterator j = constraint.getConcepts().iterator();j.hasNext();){
						Concept c = (Concept) j.next();
						g.getConcept(constraintProj.getCorrespondance(c.getId())).setHighlighted(true);
					}
					for(Iterator j = constraint.getRelations().iterator();j.hasNext();){
						Relation r = (Relation) j.next();
						g.getRelation(constraintProj.getCorrespondance(r.getId())).setHighlighted(true);
					}
					//if(choice == JOptionPane.CLOSED_OPTION) this.g = safeGraph;
					return constraint;
				}
			}
			this.repaint();
			return null;
		}
		catch(Exception e){
			System.out.println(e.getMessage());
			return null;
		}
	}
	
	public void applyAxiom(){
		try{
			TooCoMProgressBar pb = new TooCoMProgressBar(mf,Constants.APPLYING_AXIOM_MESSAGE);
			Graph safeGraph = this.g.cloneGraph();
			if((selectedAxiom != null) && (currentProj >= 0) && (currentProj < projections.size())){
				// Case of an explicit rule
				//this.g = mf.getCogitantClient().applyAxiomAsRule(mf.getOntology(),selectedAxiom,g,(Projection) projections.get(currentProj),mf.getOntologyLanguage());
				this.applyAxiomOnGraph(mf.getOntology(),selectedAxiom,g,(Projection) projections.get(currentProj));
				tempCon.clear();
				tempRel.clear();
				this.saturation();
				Graph cons = this.checkConstraints();
				this.repaint();
				if(cons != null){
					Object[] tab = new Object[2];
					tab[0] = new String(Constants.CANCEL_AXIOM_APPLICATION_OPTION);
					tab[1] = new String(Constants.INITIALIZATION_OF_ENGINE_OPTION);
					int choice = JOptionPane.showOptionDialog(this,Constants.IMPLICIT_CONSTRAINT_BROKEN_MESSAGE + " : " + cons.getTerm(mf.getOntologyLanguage()),Constants.CONSTRAINT_BROKEN_MESSAGE,0,JOptionPane.INFORMATION_MESSAGE,null,tab,null);
					if(choice == 0) this.g = safeGraph;
					if(choice == 1) ief.init();
					this.repaint();
				}
			}
			if((selectedGraph != null) && (currentProj >= 0) && (currentProj < projections.size())){
				// Case of an explicit constraint
				LinkedList projs = mf.getCogitantClient().getGraphProjections(mf.getOntology(),selectedGraph,g,mf.getOntologyLanguage());
				if(projs.size() > 0) JOptionPane.showMessageDialog(this,Constants.CONSTRAINT_BROKEN_MESSAGE + " : " + selectedGraph.getTerm(mf.getOntologyLanguage()));
				else JOptionPane.showMessageDialog(this,Constants.CONSTRAINT_NOT_BROKEN_MESSAGE + " : " + selectedGraph.getTerm(mf.getOntologyLanguage()));
			}		
			pb.setValue(100);	
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
	}
	
	/** Applies the axiom a on the graph g according to the projection p that must exists from a to g. */
	public void applyAxiomOnGraph(Ontology onto, Axiom a,Graph g,Projection p){
		// Correspondance table : axiom id / graph id
		Hashtable table = new Hashtable();
		for(Iterator i = a.getPureConclusionConcepts().iterator();i.hasNext();){
			Concept ca = (Concept) i.next();
			Concept cg = ca.cloneConcept();
			g.addConcept(cg);
			table.put(new Integer(ca.getId()),new Integer(cg.getId()));
		}
		for(Iterator i = a.getConclusionRelations().iterator();i.hasNext();){
			Relation ra = (Relation) i.next();
			Relation rg = ra.cloneRelation();
			g.addRelation(rg);
			for(int j = 1;j <= ra.getArity();j ++){
				Concept c = ra.getLinkedConcept(j);
				Integer cid = (Integer) table.get(new Integer(c.getId()));
				if(table.get(new Integer(c.getId())) != null){
					rg.setLinkedConcept(g.getConcept(cid.intValue()),j);
				}
				else{
					rg.setLinkedConcept(g.getConcept(p.getCorrespondance(c.getId())),j);
				}
			}
		}
	}
	
	/** Returns the projections of the hypothesis part of the axiom such as the application of
	 *  the axiom produces new knowledge. */
	private LinkedList getProductiveProjections(Axiom a,Graph graph) throws IOException{
		LinkedList result = new LinkedList();
		LinkedList hypProj = mf.getCogitantClient().getGraphProjections(mf.getOntology(),a.getHypothesisPart(),graph,mf.getOntologyLanguage());
		if(hypProj.size() > 0){
			LinkedList concProj = mf.getCogitantClient().getGraphProjections(mf.getOntology(),a.getConclusionPart(),graph,mf.getOntologyLanguage());
			for(Iterator i = hypProj.iterator();i.hasNext();){
				Projection p = (Projection) i.next();
				boolean prod = true;
				for(Iterator j = concProj.iterator();j.hasNext();){
					Projection pBis = (Projection) j.next();
					boolean test = true;
					for(Iterator k = a.getHypothesisConclusionConcepts().iterator();k.hasNext();){
						Concept c = (Concept) k.next();
						if(p.getCorrespondance(c.getId()) != pBis.getCorrespondance(c.getId())) test = false;
					}
					if(test) prod = false;
				}
				if(prod) result.add(p); 
			}
		}
		return result;
	}
	
	public void changeProjection(){
		tempCon.clear();
		tempRel.clear();
		for(Iterator i = g.getConcepts().iterator();i.hasNext();){
			((Concept) i.next()).setSelected(false);
		}
		if((selectedAxiom != null) && (currentProj >= 0) && (currentProj < projections.size())){
			Projection p = (Projection) projections.get(currentProj);
			Hashtable concConcepts = new Hashtable();
			for(Iterator i = selectedAxiom.getPureConclusionConcepts().iterator();i.hasNext();){
				Concept c = (Concept) i.next();
				Concept cBis = c.cloneConcept();
				this.tempCon.add(cBis);
				concConcepts.put(c,cBis);
			}
			for(Iterator i = selectedAxiom.getConclusionRelations().iterator();i.hasNext();){
				Relation r = (Relation) i.next();
				Relation rBis = r.cloneRelation();
				int cpt = 1;
				for(Iterator j = r.getLinkedConcepts().iterator();j.hasNext();cpt ++){
					Concept c = (Concept) j.next();
					if(!selectedAxiom.hasHypothesisConclusionConcept(c)){
						rBis.setLinkedConcept((Concept) concConcepts.get(c),cpt);
					}
					else{
						int conId = p.getCorrespondance(c.getId());
						rBis.setLinkedConcept(g.getConcept(conId),cpt);
					}
				}
				this.tempRel.add(rBis);
			}
			for(Iterator i = selectedAxiom.getHypothesisConclusionConcepts().iterator();i.hasNext();){
				Concept c = (Concept) i.next();
				g.getConcept(p.getCorrespondance(c.getId())).setSelected(true);
			}
		}
		if((selectedGraph != null) && (currentProj >= 0) && (currentProj < projections.size())){
			Projection p = (Projection) projections.get(currentProj);
			for(Iterator i = p.getCorrespondances().values().iterator();i.hasNext();){
			 	int id = ((Integer) i.next()).intValue();
				Relation r = g.getRelation(id);
				if(r == null){
					tempCon.add(g.getConcept(id));
				}
				else tempRel.add(r);
			}
		}
	}
	
	public NamedGraphicalObject selectAxiom(){
		try{
			int index = ief.ruleList.getSelectedIndex();
			if((index != -1) && (index < this.explicitRules.size())){
				ief.setPrimitiveMode(-1);
				int cpt = 0;
				for(Iterator i = this.explicitRules.iterator();i.hasNext() && (cpt <= index);){
					Object temp = i.next();
					if(cpt == index){
						if((selectedAxiom == null) || ((selectedAxiom != null) && (!selectedAxiom.equals((Axiom) temp)))){
							selectedAxiom = (Axiom) temp;
							this.projections = mf.getCogitantClient().getGraphProjections(mf.getOntology(),selectedAxiom.getHypothesisPart(),g,mf.getOntologyLanguage());
							currentProj = 0;
							selectedGraph = null;
							this.changeProjection();
							this.repaint();
							return selectedAxiom;
						}
					}
					cpt ++;
				}
			}
			if((index != -1) && (index >= this.explicitRules.size())){
				this.setPrimitiveMode(-1);
				int cpt = 0;
				for(Iterator i = this.explicitConstraints.iterator();i.hasNext() && (cpt <= index);){
					Object temp = i.next();
					if(cpt == (index - this.explicitRules.size())){
						if((selectedGraph == null) || ((selectedGraph != null) && (!selectedGraph.equals((Graph) temp)))){
							selectedGraph = (Graph) temp;
							System.out.println("graph : " + selectedGraph.toString(mf.getOntologyLanguage()));
							this.projections = mf.getCogitantClient().getGraphProjections(mf.getOntology(),selectedGraph,g,mf.getOntologyLanguage());
							currentProj = 0;
							selectedAxiom = null;
							this.changeProjection();
							this.repaint();
							return selectedGraph;
						}
					}
					cpt ++;
				}
			}
			return null;
		}
		catch(Exception e){
			projections.clear();
			System.out.println(e.getMessage());
			return null;
		}
	}
	
	public void exit(){
		this.mode = 0;
		this.g.destroy(mf.getOntology());
		this.g = new Graph();
		this.temporaryImage = null;
		this.selectedGraph = null;
		this.selectedAxiom = null;
		try{
			mf.getCogitantClient().closeServerOntology();
			mf.getCogitantClient().createServerSupport(mf.getOntology(),mf.getOntologyLanguage());
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		this.repaint();
	}
	
	public InferenceEngine(MainFrame mf,InferenceEngineFrame ief){
		this.mf = mf;
		this.setPreferredSize(new Dimension(ief.getSize().width - Constants.IE_BUTTON_PANEL_WIDTH,ief.getSize().height));
		try{
			mf.getCogitantClient().createServerSupport(mf.getOntology(),mf.getOntologyLanguage());
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
		this.g = new Graph();
		this.implicitRules = new LinkedList();
		this.explicitRules = new LinkedList();
		this.implicitConstraints = new LinkedList();
		this.explicitConstraints = new LinkedList();
		this.tempCon = new LinkedList();
		this.tempRel = new LinkedList();
		this.operationalization(true);
		this.mode = 0;
		this.setBackground(Constants.INFERENCE_ENGINE_FRAME_COLOR);
		this.ief = ief;
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
	}
	
	
}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -