用于定制策略的 API

编写定制策略时,您可以使用若干 API。 您使用的 API 取决于您处理的资产以及您希望对这些资产执行的操作。例如,您用于更新运行策略所在的资产的 API 不同于用于创建资产的 API。

用于当前资产的 API

您可以使用 Java 客户端 API 创建策略。

如果您仅更新正在对其运行策略的资产,那么可以使用策略上下文方法来创建会话和获取资产。如要创建 RAMSession 和检索当前 RAMAsset,请使用以下 API:
RAMSession ramSession = getPolicyContext().getRAMSession();
RAMAsset ramAsset = getPolicyContext().getRAMAsset();

您可以使用多个 Rational® Asset Manager Java™ API 类来更新当前资产。例如,您可以添加关系或设置操作。您还可以使用 API 类来处理资产、资产属性和工件。

您还可以使用 Java API 来更改或创建运行策略所在的资产之外的资产。使用 ram.client API 时要获得最佳性能,可将资产更新限制于单个会话。

请在定制策略代码中遵循以下步骤:
  1. 使用 getRAMSession 方法创建会话。这些会话无需使用许可证。
  2. 使用 getRAMAsset 方法获取资产。
  3. 使用 RAMSession 和 RAMAsset API 执行资产操作。
  4. 使用 session.putAssets 方法将更改作为一个事务提交。
例如:
RAMSession session = getPolicyContext().getRAMSession();  
RAMAsset currentAsset = this.getPolicyContext().getRAMAsset();  
  
// Use the client APIs to make changes to the asset ...

// You can also modify or create other assets:
						RAMAsset asset = session.createAsset("1.0");
						asset.setName("Test Asset");
						asset.setCommunity(session.getCommunity("Sample Application Development"));
						asset.setAssetType(session.getAssetType("Business Solution"));
						asset.setShortDescription("Test asset");
						asset.setOwners(new RAMUser[] { session.getUser("admin") });
		 	
// Commit changes to the asset repository for the current asset:
						session.putAsset(currentAsset);  

// You can use queueAssetForPut and then putAssets to queue and then 
// commit all assets you have changed in the session as one transaction:
						session.queueAssetForPut(currentAsset);  

// Commit changes to all assets you have changed in the session:
						session.putAssets(new NullProgressMonitor());  	

对于 Java 客户端 API,请参阅:客户端软件包公共数据软件包

有关其他示例,请参阅本主题中的“示例”部分和使用 Rational Asset Manager Java API

  • 获取资产:
    RAMAsset myAsset = getPolicyContext().getRAMAsset();
  • 获取资产属性的示例:
    AssetAttribute myAttribute = myAsset.getAssetAttribute("Family Name");
    String[] myAttrValue = myAttribute.getValues();
    
    myAttribute = myAsset.getAssetAttribute("Work Item");
    myAttrValue = myAttribute.getValues();
    
    myAttribute = myAsset.getAssetAttribute("Requirement");
    myAttrValue = myAttribute.getValues();
  • 创建查询以搜索资产:
    SearchQuery query = session.createAssetQuery(queryParam);
    int offset = 0;
    int maxResults = 100;
    query.setResultsStartIndex(offset);
    query.setMaxResults(maxResults);
    SearchResult result = session.getAssets(query);
  • 创建资产:
    RAMAsset newAsset = session.createAsset("1.0");
    newAsset.setName("The new related asset");
    newAsset.setCommunity(session.getCommunity("Rational Asset Manager Development"));
    newAsset.setAssetType(session.getAssetType("Specification"));
    newAsset.setShortDescription("The Specification asset is required.");
  • 为新资产创建 AssetID:
    AssetID id = new AssetID();
    id.setGUID(newAsset.getId());
    id.setVersion(newAsset.getVersion());
  • 创建查询,检查结果集,并修改资产:
    public Result test() {		try {
    						// Create a query 
    						String queryParam = null;
       	RAMSession session = getPolicyContext().getRAMSession();  
        SearchQuery query = session.createAssetQuery(queryParam);
    			int offset = 0;
    			int maxResults = 100;
    			query.setResultsStartIndex(offset);
    			query.setMaxResults(maxResults);
    					 		 SearchResult result = session.getAssets(query);
    			 
    						if (result != null && result.getAssetSearchResults() != null) {
    								AssetSearchResult[] assets = result.getAssetSearchResults();
    	
    								for (AssetSearchResult asset : assets) {
    										AssetInformation assetInformation = asset.getAsset();
    								// Modify an asset here, for example,
    								// create a "DependsOn" relationship from Release and Implementation type assets to a Specification asset of the same name using the client APIs, like this:
    								// Relationship theRelationships = session.getRelationships
    								// getRelationships().add(newItem);
    				}
    			}
    		}
     
    				// Catch exceptions...
    								Result result = new Result();
    				relationshipRange.setHighestVersion("2.0");
    				return result;
    		 
    	}
  • 使用 Rational Asset Manager Java API 来获取文件夹位置,以检索资产工件信息并下载工件:
     				// Get the asset
    				RAMAsset ramAsset = getRAMAsset(); 
                
    		 				 		// Get the location and content of the artifacts
    				RAMFolderArtifact srcFolderArtifact = (RAMFolderArtifact)ramAsset.getArtifactsRoot();
    				Artifact[] srcArtifacts = srcFolderArtifact.computeArtifactsAsFlatList(new NullProgressMonitor());
    		 
    				for (Artifact artifact : srcArtifacts) {
            RAMArtifact ramArtifact = (RAMArtifact) artifact;
    										InputStream is = ramArtifact.downloadContents();
    										f = File.createTempFile("theArtifact", null, new File(ramSession.getLocalStorageLocation()));
    										UtilitiesCommon.copyStreams(is, new FileOutputStream(f), null, true, true);
             }

反馈