View Javadoc
1   package eu.simuline.relana.sys;
2   
3   import eu.simuline.relana.model.CClassLoader;
4   import eu.simuline.relana.model.CClass;
5   import eu.simuline.relana.model.FlatCInstance;
6   import eu.simuline.relana.model.SInstance;
7   import eu.simuline.relana.model.InstanceLocator;
8   //import eu.simuline.relana.model.Deficiency;
9   
10  import eu.simuline.util.sgml.SGMLParser;
11  
12  import org.xml.sax.SAXException;
13  
14  import org.antlr.v4.runtime.RecognitionException;
15  
16  import java.net.URL;
17  import java.net.MalformedURLException;
18  
19  import java.io.InputStreamReader;
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  
23  //import java.util.Iterator;
24  import java.util.Set;
25  import java.util.Map;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Arrays;
29  
30  /**
31   * Relana's main class containing the main method only. 
32   * The main method is intended to be invoked with a single argument 
33   * which is interpreted as the url of a project file <code>*.rml</code> 
34   * which is read by an instance of {@link Project}. 
35   * A project describes the classes of a whole model and highlights 
36   * a single component class as the "base class" 
37   * which holds the overall model. 
38   * <p>
39   * The base class may not specify input 
40   * as the whole model shall be closed in itself. 
41   * **** all variables must be declared as output. **** 
42   * Then an instance is created and flattened, eliminating all structures 
43   * not required for computations. 
44   * Finally the observables each of which are instances of a service classes, 
45   * are extracted and their probability is computed. 
46   * This is the output. 
47   *
48   * Created: Thu Apr 28 21:46:41 2005
49   *
50   * @author <a href="mailto:ernst.reissner@simuline.eu">Ernst Reissner</a>
51   * @version 1.0
52   */
53  public abstract class Relana { // NOPMD 
54      public Relana() {
55  	// is empty. 
56      } // Relana constructor
57  
58      public static void main(String[] args) 
59  	throws MalformedURLException, IOException, SAXException, 
60  	       RecognitionException {
61  	if (args.length != 1) {
62  	    throw new IllegalArgumentException
63  		("Expected a single argument: the project file " + 
64  		 "but found " + Arrays.asList(args) + ". ");
65  	}
66  	// Here, args[0] ist the only existing entry. 
67  
68  	// read project file 
69  	URL proj = new URL(args[0]);
70  	InputStreamReader projectStr = 
71  	    new InputStreamReader(proj.openStream(), "UTF-8");
72  	SGMLParser projectParser = new SGMLParser();
73  	projectParser.parseXML(true);
74  	Project project = new Project();
75  	projectParser.setContentHandler(project);
76  	projectParser.setExceptionHandler(project);
77  	projectParser.parse(new BufferedReader(projectStr));
78  System.out.println("project: " + project);
79  
80  	// load class and make sure that no input-effects occur. 
81  	CClassLoader loader = new CClassLoader(project.getLibrary());
82  	CClass cClass = loader.loadCClass(project.getBaseClass());
83  System.out.println("cClass: " + cClass);
84  	Set<CClass.SClassDecl> decls = cClass.getEffectsRec();
85  	//InstanceLocator loc;
86  	for (CClass.SClassDecl decl : decls) {
87  	    if (decl.isInput()) {
88  		throw new IllegalArgumentException
89  		    ("Found declaration of input variable " + decl + ". ");
90  	    }
91  	}
92  
93  	// instantiate and get all output-variables under observation. 
94  	FlatCInstance flatCInstance = cClass.getInstance().flatten();
95  System.out.println("cInstance: " + flatCInstance);
96  
97  	// verify whether all output effects are indeed declared as output 
98  	Set<InstanceLocator> outServ = project.getOutputEffects();
99  System.out.println("outServ: " + outServ);
100 	Map<List<String>, SInstance> observables = 
101 	    new HashMap<List<String>, SInstance>();
102 	for (InstanceLocator loc : outServ) {
103 	    SInstance serv = flatCInstance.getEffect(loc);
104 	    CClass.SClassDecl decl = cClass.getEffectDecl(loc.getPath());
105 	    if (!decl.isOutput()) {
106 		throw new IllegalArgumentException
107 		    ("Found non-output variable " + loc + ". ");
108 	    }
109 	    // this works with HashMap observables, 
110 	    // because loc.getPath() yields an unmodifyable list. 
111 	    observables.put(loc.getPath(), serv);
112 	}
113 	
114 	// Here, observables contains all stuff under consideration. 
115 
116 System.out.println("observables: " + observables);
117 System.out.println("\nprobabilities: ");
118 
119 	for (List<String> obs : observables.keySet()) {
120 	    System.out.println("\nobs : " + obs + 
121 			       " has prob " + flatCInstance.getProb(obs));
122 
123 	}
124     }
125 } // Relana