001 /*--------------------------------------------------------------------------+ 002 $Id: TargetLinkDataResolver.java 26285 2010-02-18 11:22:54Z juergens $ 003 | | 004 | Copyright 2005-2010 Technische Universitaet Muenchen | 005 | | 006 | Licensed under the Apache License, Version 2.0 (the "License"); | 007 | you may not use this file except in compliance with the License. | 008 | You may obtain a copy of the License at | 009 | | 010 | http://www.apache.org/licenses/LICENSE-2.0 | 011 | | 012 | Unless required by applicable law or agreed to in writing, software | 013 | distributed under the License is distributed on an "AS IS" BASIS, | 014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 015 | See the License for the specific language governing permissions and | 016 | limitations under the License. | 017 +--------------------------------------------------------------------------*/ 018 package edu.tum.cs.simulink.targetlink; 019 020 import java.io.StringReader; 021 import java.util.Map; 022 023 import java_cup.runtime.Symbol; 024 import edu.tum.cs.commons.visitor.IVisitor; 025 import edu.tum.cs.simulink.builder.SimulinkModelBuildingException; 026 import edu.tum.cs.simulink.model.SimulinkBlock; 027 import edu.tum.cs.simulink.model.SimulinkConstants; 028 import edu.tum.cs.simulink.util.SimulinkUtils; 029 030 /** 031 * This visitor identifies Targetlink blocks, parses their data, unfolds it and 032 * stores it as normal parameters at the block. The parameter names of nestes 033 * Targetlink structs are separated by {@value #PARAMETER_SEPARATOR}. 034 * 035 * @author deissenb 036 * @author $Author: juergens $ 037 * @version $Rev: 26285 $ 038 * @levd.rating GREEN Hash: 5B2683922D6EF98C76A91BAD9B4EAEAD 039 */ 040 public class TargetLinkDataResolver implements 041 IVisitor<SimulinkBlock, SimulinkModelBuildingException> { 042 043 /** Separator for Targetlink parameter names. */ 044 public static final String PARAMETER_SEPARATOR = "/"; 045 046 /** 047 * If this is a Targetlink block, parse Targetlink data, resolve the structs 048 * and stores parameters at the block. 049 */ 050 public void visit(SimulinkBlock block) 051 throws SimulinkModelBuildingException { 052 if (SimulinkUtils.isTargetlinkBlock(block)) { 053 unfoldTargetlinkData(block); 054 } 055 } 056 057 /** 058 * Parse Targetlink data, resolve the structs and store parameters at the 059 * block. Currently this only analyzes Targetlink data stored at parameter 060 * {@link SimulinkConstants#PARAM_TARGETLINK_DATA}. 061 */ 062 private void unfoldTargetlinkData(SimulinkBlock block) 063 throws SimulinkModelBuildingException { 064 String data = block 065 .getParameter(SimulinkConstants.PARAM_TARGETLINK_DATA); 066 if (data == null) { 067 return; 068 } 069 070 TargetlinkStruct struct = parseTargetlinkdata(block, data); 071 Map<String, String> values = struct.getParameters(); 072 for (String key : values.keySet()) { 073 block.setParameter(SimulinkConstants.PARAM_TARGETLINK_DATA + key, 074 values.get(key)); 075 } 076 } 077 078 /** Parse Targetlink data. */ 079 private TargetlinkStruct parseTargetlinkdata(SimulinkBlock block, 080 String data) throws SimulinkModelBuildingException { 081 TargetlinkDataScanner scanner = new TargetlinkDataScanner( 082 new StringReader(data)); 083 TargetlinkDataParser parser = new TargetlinkDataParser(scanner); 084 Symbol sym; 085 try { 086 sym = parser.parse(); 087 } catch (Exception ex) { 088 throw new SimulinkModelBuildingException(ex + " in block " 089 + block.getId()); 090 } 091 092 TargetlinkStruct struct = (TargetlinkStruct) sym.value; 093 return struct; 094 } 095 }