MATSIM
RunObjectAttributesExample.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2010 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.utils.objectattributes;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
31 
32  public static void main(String[] args) {
33  // assume we're working with some links, e.g. in a network
34  List<String> linkIds = new ArrayList<String>();
35  linkIds.add("1");
36  linkIds.add("2");
37  linkIds.add("3");
38  linkIds.add("4");
39 
40  // define some attributes
41  ObjectAttributes linkAttributes = new ObjectAttributes();
42  // read from file or create somehow else
43  linkAttributes.putAttribute("1", "roadtype", "motorway");
44  linkAttributes.putAttribute("1", "hasSpeedBumps", Boolean.TRUE);
45  linkAttributes.putAttribute("2", "roadtype", "trunk");
46  linkAttributes.putAttribute("2", "hasSpeedBumps", Boolean.TRUE);
47  // do not define a roadtype for object "3"
48  linkAttributes.putAttribute("3", "hasSpeedBumps", Boolean.FALSE);
49  // do not define any attributes for object "4"
50 
51  // now do something useful, e.g. use the attributes for filtering
52  filterByAttributes(linkIds, linkAttributes, "roadtype", "trunk");
53  filterByAttributes(linkIds, linkAttributes, "hasSpeedBumps", true);
54  filterByAttributes(linkIds, linkAttributes, "hasSpeedBumps", false);
55  }
56 
57  public static void filterByAttributes(List<String> objectIds, ObjectAttributes objectAttributes, String attributeName, Object attributeValue) {
58  System.out.println("filtering for " + attributeName + " = " + attributeValue);
59  for (String id : objectIds) {
60  Object o = objectAttributes.getAttribute(id, attributeName);
61  if (attributeValue.equals(o)) {
62  System.out.println(" " + id);
63  }
64  }
65  }
66 }
Object putAttribute(final String objectId, final String attribute, final Object value)
Object getAttribute(final String objectId, final String attribute)
static void filterByAttributes(List< String > objectIds, ObjectAttributes objectAttributes, String attributeName, Object attributeValue)