Skip to content
Snippets Groups Projects
Select Git revision
  • 3bc988de4f080f5e37d5422c0ef167a57818974d
  • master default protected
2 results

AnalogIn.jl

Blame
  • MergeActors.jrag 3.65 KiB
    /* -*-Java-*- */                                         
    
    /*
     * Copyright (C) 2009  Anders Nilsson <anders.nilsson@cs.lth.se>
     *                                                              
     * This file is part of Actors model compiler.                      
     */                                                             
    
    import java.util.HashSet;
    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.io.PrintStream; 
    
    aspect MergeActors {
    	void Element.mergeActors() {}
    	public void Start.mergeActors() {
    		getSpecification().mergeActors();
    	}
    	void Specification.mergeActors() {
    		for (Element e : getElements()) {
    			e.mergeActors();
    		}
    	}
    	void XDF.mergeActors() {
    		HashSet<Instance> schedule = genStaticSchedule(new HashSet<Instance>());
    		HashSet<Connection> cons = getConnections(new HashSet<Connection>());
    		for (Instance i : schedule) {
    			while (i.next != null) {
     				System.out.println("Searching connections for: "+i.name()+" "+i.next.name());
    				for (Connection c : cons) {
    					System.out.println("  "+c.getSource().name()+" "+c.getDest().name());
    					if (c.getSource() == i && c.getDest() == i.next) {
     						System.out.println("   Found");
    						// Check port names and perform actual actor
    						// merge. Then set remove_me flag so that this
    						// connection will be removed from the actor
    						// network.
    						c.remove_me = true;
    						c.is$Final(false);
    					} else if (c.getSource() == i.next) {
    						c.setSource("\""+i.id()+"\"");
    					}
    				}
    				System.out.println("Merging "+i.name()+":"+i.next.name());
    				i.isMerged(true);
    				i.merge(i.next);
    				i.setName(i.name()+"_"+i.next.name());
    				i.next.disabled = true;
    				i.next = i.next.next;
    			}
    		}
    		// Now generate files for the newly merged actor instances.
    		System.out.println("** Merging finished, now generate files **");
    		for (Instance i : getInstances()) {
    			i.genFile();
    		}
    	}
    
    	void Instance.merge(Instance inst) {
    		if (isMerged()) {
    			xlimInstance().merge(inst.xlimInstance());
    		}
    	}
    
    	void Instance.genFile() {
    		if (isMerged()) {
    			System.out.println("generating actor: "+name());
    			try {
    				if (!(xlimInstance() instanceof xlimAST.EmptyStart)) {
    					// 				System.out.println("  Writing");
    					xlimInstance().prettyPrint("",new PrintStream(new File(name()+".xlim")));
    				}
    			} catch (FileNotFoundException e) {
    				System.out.println("Could not print to file: "+name());
    			} 
    		}
    		if (disabled) {
    			// remove from AST
    			is$Final(false);
    		}
    	}
    
    	private boolean Instance.isMerged = false;
    	public void Instance.isMerged(boolean b) {
    		isMerged = b;
    	}
    	public boolean Instance.isMerged() {
    		return isMerged;
    	}
    }
    
    aspect Misc {
    	syn HashSet<Connection> Element.getConnections(HashSet<Connection> set) = set;
    	eq XDF.getConnections(HashSet<Connection> set) {		
    		for (Element e : getElements()) {
    			set = e.getConnections(set);
    		}
    		return set;
    	}
    	eq Connection.getConnections(HashSet<Connection> set) {
    		set.add(this);
    		return set;
    	}
    
    	public HashSet<Instance> XDF.getInstances() {
    		return getInstances(new HashSet<Instance>());
    	}
    	syn HashSet<Instance> Element.getInstances(HashSet<Instance> inst) = inst;
    	eq XDF.getInstances(HashSet<Instance> inst) {
    		for (Element e : getElements()) {
    			inst = e.getInstances(inst);
    		}
    		return inst;
    	}
    	eq Instance.getInstances(HashSet<Instance> inst) {
    		inst.add(this);
    		return inst;
    	}
    }
    
    aspect Rewrites {
    	boolean Connection.remove_me = false;
    	boolean Instance.disabled = false;
    	
    	rewrite Connection {
    		when (remove_me) to ComplexElement {
    			return new ComplexElement();
    		}
    	}
    	rewrite Instance {
    		when (disabled) to ComplexElement {
    			System.out.println("Removing instance: "+name());
    			return new ComplexElement();
    		}
    	}
    }