2017-08-24 9 views

答えて

1

を/filename.csv。添付ファイルが通過しない場合は、以下のコードもコピーしました。 ModelInitializerを有効にするには、あなたは、シナリオファイルに以下を追加する必要があります:あなたは、クラスのパッケージ名を変更する必要がありますので

<model.initializer class="PredatorPrey.MyInitializer" /> 

私はプレデターPreyのデモでこれをテストしました。 ModelInitializerの例では、context.xmlファイル内のコンテキストIDと同じルートコンテキストIDを指定する必要があります。変数出力フォルダ名を指定する必要があります。この例では、通常どおりファイルシンクにファイル名を指定する必要があり、変数パスが挿入されます。シナリオがGUIに保存されている場合、変数フォルダのパスはシナリオに保存されますが、このコードは既存のパスを確認してパスをoutputFolder文字列に置き換えることが注意点です。したがって、パス全体をoutputFolder文字列に入れて、その一部だけでなく、必要に応じてコードの動作を変更する必要があります。

package PredatorPrey; 

import java.io.File; 

import repast.simphony.data2.engine.FileSinkComponentControllerAction; 
import repast.simphony.data2.engine.FileSinkDescriptor; 
import repast.simphony.engine.controller.NullAbstractControllerAction; 
import repast.simphony.engine.environment.ControllerAction; 
import repast.simphony.engine.environment.RunEnvironmentBuilder; 
import repast.simphony.engine.environment.RunState; 
import repast.simphony.scenario.ModelInitializer; 
import repast.simphony.scenario.Scenario; 
import repast.simphony.util.collections.Tree; 

public class MyInitializer implements ModelInitializer { 

     @Override 
     public void initialize(final Scenario scen, RunEnvironmentBuilder builder) { 

      scen.addMasterControllerAction(new NullAbstractControllerAction() { 

      String rootContextID = "Predator Prey"; 
      String outputFolder = "testoutfolder"; 

        @Override 
        public void batchInitialize(RunState runState, Object contextId) { 

         Tree<ControllerAction> scenarioTree = scen.getControllerRegistry().getActionTree(rootContextID); 

         findFileSinkTreeChildren(scenarioTree, scenarioTree.getRoot(), outputFolder); 

         // Reset the scenario dirty flag so the changes made to the file sink 
         // descriptors don't prompt a scenario save in the GUI 
         scen.setDirty(false); 

        } 

      }); 
     } 

     public static void findFileSinkTreeChildren(Tree<ControllerAction> tree, 
        ControllerAction parent, String outputFolder){ 

      // Check each ControllerAction in the scenario and if it is a FileSink, 
      // modify the output path to include the folder 
      for (ControllerAction act : tree.getChildren(parent)){ 
        if (act instanceof FileSinkComponentControllerAction){ 
         FileSinkDescriptor descriptor = ((FileSinkComponentControllerAction)act).getDescriptor(); 
         String fileName = descriptor.getFileName(); 

         // remove any prefix directories from the file name 
         int lastSeparatorIndex = fileName.lastIndexOf(File.separator); 

         // Check for backslash separator 
         if (fileName.lastIndexOf('\\') > lastSeparatorIndex) 
           lastSeparatorIndex = fileName.lastIndexOf('\\'); 

         // Check for forward slash operator 
         if (fileName.lastIndexOf('/') > lastSeparatorIndex) 
           lastSeparatorIndex = fileName.lastIndexOf('/'); 

         if (lastSeparatorIndex > 0){ 
           fileName = fileName.substring(lastSeparatorIndex+1, fileName.length()); 
         } 

         descriptor.setFileName(outputFolder + File.separator + fileName); 
        } 
        else findFileSinkTreeChildren(tree, act, outputFolder); 
      } 
     } 
} 
関連する問題