All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ingenias.generator.browser.BrowserImp Maven / Gradle / Ivy

There is a newer version: 1.0.11
Show newest version
/** 
 * Copyright (C) 2010  Jorge J. Gomez-Sanz 
 * 
 * This file is part of the INGENME tool. INGENME is an open source meta-editor
 * which produces customized editors for user-defined modeling languages
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation version 3 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see 
 **/

package ingenias.generator.browser;

import ingenias.editor.GUIResources;
import ingenias.editor.IDEState;
import ingenias.editor.ModelJGraph;
import ingenias.editor.entities.Entity;

import java.util.*;
import java.io.*;
import ingenias.exception.*;

/**
 *  Implements a Singleton pattern to gain global access to diagrams contained
 *  in a file
 *
 *@author     Jorge J. Gomez-Sanz
 *@created    30 November 2003
 */
public class BrowserImp implements Browser {

	private static Browser browser = null;
	private ingenias.editor.IDEState ids;
	File currentProject=null;

	/**
	 *  Constructor for the BrowserImp object
	 */
	/*private BrowserImp() {
		ids=ingenias.editor.IDEAbs.ide.ids;

	}*/


	public BrowserImp(IDEState ids) {
		this.ids=ids;
		if (ids==null)
			throw new RuntimeException("The ids parameter cannot be null");
		currentProject=ids.getCurrentFile();
	}


	/**
	 *  Constructor for the BrowserImp object
	 *
	 *@param  file           Description of Parameter
	 *@exception  Exception  Description of Exception
	 */
	private BrowserImp(String file) throws ingenias.exception.UnknowFormat, ingenias.exception.DamagedFormat, ingenias.exception.CannotLoad{
		// This is a method to be run in headless mode

		ingenias.editor.persistence.PersistenceManager p = new ingenias.editor.persistence.PersistenceManager();
		ids=IDEState.emptyIDEState();
		try {
			p.loadWithoutListeners(file, new GUIResources(),new Properties(),ids);
		} catch (Throwable t){
			t.printStackTrace();
		}

		this.currentProject=new File(file);
	}



	/**
	 *  Obtains all existing graphs
	 *
	 *@return    The graphs value
	 */
	public Graph[] getGraphs() {
		ingenias.editor.GraphManager gm = ids.gm;
		Vector models = gm.getUOModels();
		Graph[] gs = new Graph[models.size()];
		Iterator it = models.iterator();
		int k = 0;
		while (it.hasNext()) {
			ingenias.editor.ModelJGraph model = (ingenias.editor.ModelJGraph) it.next();
			gs[k] = new GraphImp(model,ids);
			k++;
		}

		return gs;
	}


	/**
	 *  Gets the allEntities attribute of the BrowserImp object
	 *
	 *@return    The allEntities value
	 */
	public GraphEntity[] getAllEntities() {
		
		Graph[] gs = this.getGraphs();
		HashSet entities = new HashSet();
		for (int k = 0; k < gs.length; k++) {
			Graph current = gs[k];
			GraphEntity[] ges=null;
			try {
				ges = current.getEntities();
			} catch (NullEntity e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			for (int i = 0; i < ges.length; i++) {
				entities.add(ges[i]);
			}
		}
		
		GraphEntity[] temp = entities.toArray(new GraphEntity[entities.size()]);
		/*Object[] temp = ids.om.getAllObjects().toArray();
		Graph[] gs=this.getGraphs();
		Vector entities=new Vector(); 		
		for (Object ent:temp){
			if ( Entity.class.isAssignableFrom(ent.getClass()))
				try {
					entities.add(new GraphEntityImp((Entity)ent,null,(ModelJGraph) gs[0].getGraph(), getState()));
				} catch (NullEntity e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		GraphEntity[] result = new GraphEntity[entities.size()];*/
		//System.arraycopy(entities.toArray(), 0, temp, 0, temp.length);
		return temp;
	}


	// Obtains a graph with a concrete id. If there is no any, it returns null
	/**
	 *  Gets the graph attribute of the BrowserImp object
	 *
	 *@param  id  Description of Parameter
	 *@return     The graph value
	 */
	public Graph getGraph(String id) {
		ingenias.editor.GraphManager gm = ids.gm;
		ingenias.editor.ModelJGraph mg = (ingenias.editor.ModelJGraph) gm.getModel(id);
		if (mg != null) {
			return new GraphImp(mg, ids);
		} else {
			return null;
		}
	}


	/**
	 *  Obtains an instance
	 *
	 *@return    The instance value
	 */
	/*public static Browser getInstance()  throws ingenias.exception.NotInitialised{
		if (browser == null) {
			throw new ingenias.exception.NotInitialised("Browser not initialised. You must call \"initialise\" first");
		} else {
			return browser;
		}
	}*/


	/**
	 *  Initialises the diagram browser with a INGENIAS specification file
	 *
	 *@param  file           Description of Parameter
	 *@exception  Exception  Description of Exception
	 */
	public static Browser initialise(String file) throws
	ingenias.exception.UnknowFormat,
	ingenias.exception.DamagedFormat,
	ingenias.exception.CannotLoad {

		//browser = new BrowserImp(IDEState.emptyIDEState());

		browser = new BrowserImp(file);
		return browser;

	}


	/**
	 *  Performs an empty initialisation. Only works when there is an already existing
	 *  instance of GraphManager
	 *
	 *@exception  Exception  Description of Exception
	 */
	/*public static void initialise() {
		ingenias.editor.GraphManager.getInstance();
		// To check if it is initialised
		browser = new BrowserImp();

	}*/


	/**
	 *  Performs an empty initialisation. Only works when there is an already existing
	 *  instance of GraphManager
	 *
	 *@exception  Exception  Description of Exception
	 */
	public static void initialise(IDEState ids) {

		// To check if it is initialised
		browser = new BrowserImp(ids);

	}

	public GraphEntity findEntity(String id){
		GraphEntity[] ents=this.getAllEntities();
		boolean found=false;
		int k=0;
		for (k=0;k relationships1 = ent1.getAllRelationships();
			Vector relationships2 = ent2.getAllRelationships();
			int j=0;
			allIncluded=allIncluded && relationships1.size()==relationships2.size();
			while (allIncluded && j findAllDifferences(Browser bimp1, Browser bimp2){
		GraphEntity[] entities = bimp1.getAllEntities();
		boolean allIncluded=true;
		Vector differences=new Vector();
		int k=0;
		while (k checkEntity(GraphEntity ent1,
			GraphEntity ent2, Vector alreadyVerified) {
		boolean allIncluded=true;
		Vector differences=new Vector();
		if (!ent1.getClass().equals(ent2.getClass())){
			differences.add("entity "+ent1.getID()+":"+ent1.getType()+" does not exist but there is one with id "+ent1.getID()+":"+ent2.getType());
		} else {
			if (!alreadyVerified.contains(ent1)){
				alreadyVerified.add(ent1);
				Vector fieldDifferences=checkFields(ent1,ent2,alreadyVerified);
				differences.addAll(fieldDifferences);
				if (!fieldDifferences.isEmpty()){
					Vector relationships1 = ent1.getAllRelationships();
					Vector relationships2 = ent2.getAllRelationships();
					int j=0;
					allIncluded=allIncluded && relationships1.size()==relationships2.size();
					while (j checkFields(GraphEntity ent1,
			GraphEntity ent2, Vector alreadyVerified) {
		GraphAttribute[] attsEnt1 = ent1.getAllAttrs();
		GraphAttribute[] attsEnt2 = ent2.getAllAttrs();
		Vector differences=new Vector();

		for (int k=0;k differences,
			GraphAttribute gaE1, GraphAttribute gaE2) {
		boolean found;
		if (gaE1.isEntityValue() && gaE2.isEntityValue()){
			// different types of fields. It sho
			GraphEntity entValue1=null;
			GraphEntity entValue2=null;
			try {
				entValue1=gaE1.getEntityValue();
			} catch (NullEntity ne){};
			try {
				entValue2=gaE2.getEntityValue();
			} catch (NullEntity ne){};
			if (entValue1==null && entValue2==null)
				found=true;
			else
				if (entValue1!=null && entValue2==null){
					differences.add("entity " +ent1.getID()+":"+ent1.getType()+" has not the same values for attribute "+
							gaE1.getName()+":"+entValue1+" instead the second spec has null");
				} else
					if (entValue1==null && entValue2!=null){
						differences.add("entity " +ent1.getID()+":"+ent1.getType()+" has not the same values for attribute "+
								gaE1.getName()+":null instead the second spec has \""+entValue2+"\"");
					} else
						if (entValue1!=null && entValue2!=null){
							Vector cdifferences = checkEntity(entValue1,entValue2,alreadyVerified);
							differences.addAll(cdifferences);
							found=cdifferences.isEmpty();
						} 
		}
	}


	private static void evaluateCollectionValueField(GraphEntity ent1,
			Vector alreadyVerified, Vector differences, boolean found,
			GraphAttribute gaE1, GraphAttribute gaE2)  {
		GraphCollection colValue1=null;
		GraphCollection colValue2=null;		
		try {
			colValue1=gaE1.getCollectionValue();
		} catch (ingenias.exception.NullEntity ne){};
		try {
			colValue2=gaE2.getCollectionValue();
		} catch (ingenias.exception.NullEntity ne){};
		if (colValue1==null && colValue2==null){
			found=true;
		} else
			if (colValue1==null && colValue2!=null){
				differences.add("entity " +ent1.getID()+":"+ent1.getType()+" has a null value  for attribute "+
						gaE1.getName()+" instead the second spec has "+colValue2);
			} else 
				if (colValue1!=null && colValue2==null){
					differences.add("entity " +ent1.getID()+":"+ent1.getType()+" has not the same values for attribute "+
							gaE1.getName()+":"+colValue1+" instead the second spec has null");
				} else
					if (colValue1!=null && colValue2!=null &&
					colValue1.size()==colValue2.size()){
						Vector cdifferences = new Vector();
						for (int l=0;l differences, GraphAttribute gaE1, GraphAttribute gaE2) {
		boolean found;
		String simpleValue1=null;
		String simpleValue2=null;
		simpleValue1=gaE1.getSimpleValue();
		simpleValue2=gaE2.getSimpleValue();
		if (simpleValue1==null && simpleValue1==null){		
			found=true;
		} else
			if (simpleValue1==null && simpleValue2!=null){
				differences.add("entity " +ent1.getID()+":"+ent1.getType()+" has a null value  for attribute "+
						gaE1.getName()+" instead the second spec has "+simpleValue2);
			} else 
				if (simpleValue1!=null && simpleValue2==null){
					differences.add("entity " +ent1.getID()+":"+ent1.getType()+" has not the same values for attribute "+
							gaE1.getName()+":"+simpleValue1+" instead the second spec has null");
				} else
					if (simpleValue1!=null && simpleValue2!=null && !simpleValue1.equals(simpleValue2)){
						differences.add("entity " +ent1.getID()+":"+ent1.getType()+" has a value \""+simpleValue1+"\"  for attribute "+
								gaE1.getName()+" instead the second spec has \""+simpleValue2+"\"");
					}
	}


	public static boolean compare(Browser bimp1, Browser bimp2) {
		return findAllDifferences(bimp1, bimp2).isEmpty() && findAllDifferences(bimp2, bimp1).isEmpty();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy