001    /**
002     * @author paulk, Jul 21, 2002
003     */
004    
005    package toolbus.process;
006    
007    import toolbus.Functions;
008    import toolbus.TBTermFactory;
009    import toolbus.TBTermVar;
010    import toolbus.exceptions.ToolBusError;
011    import toolbus.parsercup.PositionInformation;
012    import aterm.ATerm;
013    import aterm.ATermList;
014    
015    /**
016     * ProcessDefinition describes the name, formal parameters and body of a defined process.
017     */
018    public class ProcessDefinition{
019            private final String name;
020            private final ATermList formals;
021            private final ProcessExpression PE;
022            private final TBTermFactory tbfactory;
023            private final PositionInformation posInfo;
024            
025            public ProcessDefinition(String name, ATermList formals, ProcessExpression PE, TBTermFactory tbfactory, PositionInformation posInfo){
026                    this.name = name;
027                    this.formals = formals;
028                    this.PE = PE;
029                    this.tbfactory = tbfactory;
030                    this.posInfo = posInfo;
031            }
032            
033            public PositionInformation getPosInfo(){
034                    return posInfo;
035            }
036            
037            public String getName(){
038                    return name;
039            }
040            
041            public ATermList getFormals(){
042                    return formals;
043            }
044            
045            public int getNumberOfFormals(){
046                    return formals.getLength();
047            }
048            
049            public ProcessExpression getOriginalProcessExpression(){
050                    return PE;
051            }
052            
053            public ProcessExpression getProcessExpression(ATermList actuals) throws ToolBusError{
054                    if(actuals.getLength() != formals.getLength()) throw new ToolBusError("process " + name + ": mismatch between formals " + formals + " and actuals " + actuals);
055                    
056                    for(int i = 0; i < actuals.getLength(); i++){
057                            TBTermVar formal = (TBTermVar) formals.getChildAt(i);
058                            ATerm actual = (ATerm) actuals.getChildAt(i);
059                            if(tbfactory.isResultVar(formal) && !tbfactory.isResultVar(actual)) throw new ToolBusError("process " + name + ": mismatch between formal " + formal + " and actual " + actual);
060                            
061                            if(!Functions.compatibleTypes(formal, actual)) throw new ToolBusError("argument #" + (i + 1) + " of process " + name + " should have type " + formal.getVarType() + " instead of " + actual);
062                    }
063                    return PE.copy();
064            }
065            
066            public String toString(){
067                    return "ProcessDefinition(" + name + ", " + formals + ", " + PE + ")";
068            }
069    }