private static ApplicationLoadResult loadApplication(
			File projectDirectory,
			ApplicationTemplateDescriptor appDescriptor,
			ApplicationLoadResult result ) {

		ApplicationTemplate app = result.applicationTemplate;
		result.applicationTemplate.setDirectory( projectDirectory );


		// Load the graph
		File graphDirectory = new File( projectDirectory, Constants.PROJECT_DIR_GRAPH );
		GRAPH: if( ! graphDirectory.exists()) {
			RoboconfError error = new RoboconfError( ErrorCode.PROJ_NO_GRAPH_DIR, directory( projectDirectory ));
			result.loadErrors.add( error );

		} else if( appDescriptor != null
				&& ! Utils.isEmptyOrWhitespaces( appDescriptor.getGraphEntryPoint())) {

			File mainGraphFile = new File( graphDirectory, appDescriptor.getGraphEntryPoint());
			if( ! mainGraphFile.exists()) {
				RoboconfError error = new RoboconfError( ErrorCode.PROJ_MISSING_GRAPH_EP, expected( mainGraphFile.getAbsolutePath()));
				result.loadErrors.add( error );
				break GRAPH;
			}

			Graphs graphs = loadGraph( mainGraphFile, graphDirectory, result );
			app.setGraphs( graphs );
		}


		// Load the instances
		File instDirectory = new File( projectDirectory, Constants.PROJECT_DIR_INSTANCES );
		INST: if( appDescriptor != null && instDirectory.exists()) {

			if( app.getGraphs() == null ) {
				result.loadErrors.add( new RoboconfError( ErrorCode.CO_GRAPH_COULD_NOT_BE_BUILT ));
				break INST;
			}

			if( Utils.isEmptyOrWhitespaces( appDescriptor.getInstanceEntryPoint()))
				break INST;

			File mainInstFile = new File( instDirectory, appDescriptor.getInstanceEntryPoint());
			InstancesLoadResult ilr = loadInstances( mainInstFile, instDirectory, app.getGraphs(), app.getName());

			result.getParsedFiles().addAll( ilr.getParsedFiles());
			result.objectToSource.putAll( ilr.getObjectToSource());
			result.loadErrors.addAll( ilr.getLoadErrors());
			app.getRootInstances().addAll( ilr.getRootInstances());
		}


		// Commands
		File commandsDirectory = new File( projectDirectory, Constants.PROJECT_DIR_COMMANDS );
		List<String> commandNames = new ArrayList<> ();
		if( app.getGraphs() != null && commandsDirectory.exists()) {

			for( File f : Utils.listAllFiles( commandsDirectory )) {

				if( ! f.getName().endsWith( Constants.FILE_EXT_COMMANDS )) {
					result.loadErrors.add( new RoboconfError( ErrorCode.PROJ_INVALID_COMMAND_EXT ));

				} else {
					CommandsParser parser = new CommandsParser( app, f );
					result.loadErrors.addAll( parser.getParsingErrors());
					commandNames.add( f.getName().replace( Constants.FILE_EXT_COMMANDS, "" ));
				}
			}
		}


		// Autonomic
		File autonomicRulesDirectory = new File( projectDirectory, Constants.PROJECT_DIR_RULES_AUTONOMIC );
		if( app.getGraphs() != null && autonomicRulesDirectory.exists()) {

			for( File f : Utils.listAllFiles( autonomicRulesDirectory )) {

				if( ! f.getName().endsWith( Constants.FILE_EXT_RULE )) {
					result.loadErrors.add( new RoboconfError( ErrorCode.PROJ_INVALID_RULE_EXT ));

				} else {
					// Parsing errors
					RuleParser parser = new RuleParser( f );
					result.loadErrors.addAll( parser.getParsingErrors());

					// Invalid references to commands?
					List<String> coll = new ArrayList<>( parser.getRule().getCommandsToInvoke());
					coll.removeAll( commandNames );
					for( String commandName : coll )
						result.loadErrors.add( new RoboconfError( ErrorCode.RULE_UNKNOWN_COMMAND, name( commandName )));
				}
			}
		}


		// Check for files that are not reachable or not in the right directories
		if( projectDirectory.isDirectory()) {
			String[] exts = { Constants.FILE_EXT_GRAPH, Constants.FILE_EXT_INSTANCES };
			File[] directories = { graphDirectory, instDirectory };

			for( int i=0; i<exts.length; i++ ) {
				List<File> files = Utils.listAllFiles( projectDirectory, exts[ i ]);
				List<File> filesWithInvalidLocation = new ArrayList<> ();
				for( File f : files ) {
					if( ! Utils.isAncestor( directories[ i ], f )) {
						result.loadErrors.add( new ParsingError( ErrorCode.PROJ_INVALID_FILE_LOCATION, f, 1 ));
						filesWithInvalidLocation.add( f );
					}
				}

				files.removeAll( result.getParsedFiles());
				files.removeAll( filesWithInvalidLocation );
				for( File f : files )
					result.loadErrors.add( new ParsingError( ErrorCode.PROJ_UNREACHABLE_FILE, f, 1 ));
			}
		}


		// Validate the entire application
		if( ! RoboconfErrorHelpers.containsCriticalErrors( result.loadErrors )) {
			Collection<ModelError> errors = RuntimeModelValidator.validate( app );
			result.loadErrors.addAll( errors );
		}

		return result;
	}
private static ApplicationLoadResult loadApplication ( File projectDirectory , ApplicationTemplateDescriptor appDescriptor , ApplicationLoadResult result ) { ApplicationTemplate app = result . applicationTemplate ; result . applicationTemplate . setDirectory ( projectDirectory ) ; File graphDirectory = new File ( projectDirectory , Constants . PROJECT_DIR_GRAPH ) ; GRAPH : if ( ! graphDirectory . exists ( ) ) { RoboconfError error = new RoboconfError ( ErrorCode . PROJ_NO_GRAPH_DIR , directory ( projectDirectory ) ) ; result . loadErrors . add ( error ) ; } else if ( appDescriptor != null && ! Utils . isEmptyOrWhitespaces ( appDescriptor . getGraphEntryPoint ( ) ) ) { File mainGraphFile = new File ( graphDirectory , appDescriptor . getGraphEntryPoint ( ) ) ; if ( ! mainGraphFile . exists ( ) ) { RoboconfError error = new RoboconfError ( ErrorCode . PROJ_MISSING_GRAPH_EP , expected ( mainGraphFile . getAbsolutePath ( ) ) ) ; result . loadErrors . add ( error ) ; break GRAPH ; } Graphs graphs = loadGraph ( mainGraphFile , graphDirectory , result ) ; app . setGraphs ( graphs ) ; } File instDirectory = new File ( projectDirectory , Constants . PROJECT_DIR_INSTANCES ) ; INST : if ( appDescriptor != null && instDirectory . exists ( ) ) { if ( app . getGraphs ( ) == null ) { result . loadErrors . add ( new RoboconfError ( ErrorCode . CO_GRAPH_COULD_NOT_BE_BUILT ) ) ; break INST ; } if ( Utils . isEmptyOrWhitespaces ( appDescriptor . getInstanceEntryPoint ( ) ) ) break INST ; File mainInstFile = new File ( instDirectory , appDescriptor . getInstanceEntryPoint ( ) ) ; InstancesLoadResult ilr = loadInstances ( mainInstFile , instDirectory , app . getGraphs ( ) , app . getName ( ) ) ; result . getParsedFiles ( ) . addAll ( ilr . getParsedFiles ( ) ) ; result . objectToSource . putAll ( ilr . getObjectToSource ( ) ) ; result . loadErrors . addAll ( ilr . getLoadErrors ( ) ) ; app . getRootInstances ( ) . addAll ( ilr . getRootInstances ( ) ) ; } File commandsDirectory = new File ( projectDirectory , Constants . PROJECT_DIR_COMMANDS ) ; List < String > commandNames = new ArrayList < > ( ) ; if ( app . getGraphs ( ) != null && commandsDirectory . exists ( ) ) { for ( File f : Utils . listAllFiles ( commandsDirectory ) ) { if ( ! f . getName ( ) . endsWith ( Constants . FILE_EXT_COMMANDS ) ) { result . loadErrors . add ( new RoboconfError ( ErrorCode . PROJ_INVALID_COMMAND_EXT ) ) ; } else { CommandsParser parser = new CommandsParser ( app , f ) ; result . loadErrors . addAll ( parser . getParsingErrors ( ) ) ; commandNames . add ( f . getName ( ) . replace ( Constants . FILE_EXT_COMMANDS , "" ) ) ; } } } File autonomicRulesDirectory = new File ( projectDirectory , Constants . PROJECT_DIR_RULES_AUTONOMIC ) ; if ( app . getGraphs ( ) != null && autonomicRulesDirectory . exists ( ) ) { for ( File f : Utils . listAllFiles ( autonomicRulesDirectory ) ) { if ( ! f . getName ( ) . endsWith ( Constants . FILE_EXT_RULE ) ) { result . loadErrors . add ( new RoboconfError ( ErrorCode . PROJ_INVALID_RULE_EXT ) ) ; } else { RuleParser parser = new RuleParser ( f ) ; result . loadErrors . addAll ( parser . getParsingErrors ( ) ) ; List < String > coll = new ArrayList < > ( parser . getRule ( ) . getCommandsToInvoke ( ) ) ; coll . removeAll ( commandNames ) ; for ( String commandName : coll ) result . loadErrors . add ( new RoboconfError ( ErrorCode . RULE_UNKNOWN_COMMAND , name ( commandName ) ) ) ; } } } if ( projectDirectory . isDirectory ( ) ) { String [ ] exts = { Constants . FILE_EXT_GRAPH , Constants . FILE_EXT_INSTANCES } ; File [ ] directories = { graphDirectory , instDirectory } ; for ( int i = 0 ; i < exts . length ; i ++ ) { List < File > files = Utils . listAllFiles ( projectDirectory , exts [ i ] ) ; List < File > filesWithInvalidLocation = new ArrayList < > ( ) ; for ( File f : files ) { if ( ! Utils . isAncestor ( directories [ i ] , f ) ) { result . loadErrors . add ( new ParsingError ( ErrorCode . PROJ_INVALID_FILE_LOCATION , f , 1 ) ) ; filesWithInvalidLocation . add ( f ) ; } } files . removeAll ( result . getParsedFiles ( ) ) ; files . removeAll ( filesWithInvalidLocation ) ; for ( File f : files ) result . loadErrors . add ( new ParsingError ( ErrorCode . PROJ_UNREACHABLE_FILE , f , 1 ) ) ; } } if ( ! RoboconfErrorHelpers . containsCriticalErrors ( result . loadErrors ) ) { Collection < ModelError > errors = RuntimeModelValidator . validate ( app ) ; result . loadErrors . addAll ( errors ) ; } return result ; }
Loads an application from a directory.
@param projectDirectory the project directory
@param appDescriptor the application's descriptor
@param result the result to populate
@return a load result (never null)