You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by ke...@apache.org on 2007/07/02 16:29:28 UTC

svn commit: r552503 - in /incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo: ExecuteSamples2.java MedicalScenario.java PrintDataGraph.java

Author: kelvingoodson
Date: Mon Jul  2 07:29:26 2007
New Revision: 552503

URL: http://svn.apache.org/viewvc?view=rev&rev=552503
Log:
improving the medical scenario sample and adding function to the print datagraph code to handle sequences better, and to deal with non-containment references properly

Modified:
    incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java
    incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/MedicalScenario.java
    incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/PrintDataGraph.java

Modified: incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java?view=diff&rev=552503&r1=552502&r2=552503
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java (original)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java Mon Jul  2 07:29:26 2007
@@ -53,7 +53,8 @@
       ObtainingDataGraphFromXml.class,
       org.apache.tuscany.samples.sdo.tuscanyapi.CreateCompany.class,
       CreateDataObjectFromXmlString.class,
-      PrintDataGraph.class
+      PrintDataGraph.class,
+      MedicalScenario.class
      
     };
     
@@ -73,6 +74,7 @@
      *  Edit this to run fewer samples.
      */
     int runSamplesUpToLevel = SampleInfrastructure.SAMPLE_LEVEL_ADVANCED.intValue(); 
+    boolean pauseBetweenSamples = false;
     
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
     
@@ -83,8 +85,10 @@
       if(sample.getSampleComplexityLevel() <= runSamplesUpToLevel) {
         sample.run();
         sampleCount++;
-        System.out.println(">>>Press Enter to continue");
-        in.readLine();
+        if(pauseBetweenSamples) {
+          System.out.println(">>>Press Enter to continue");
+          in.readLine();
+        }
       }
     }
     System.out.println("Ran " + sampleCount + " samples");

Modified: incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/MedicalScenario.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/MedicalScenario.java?view=diff&rev=552503&r1=552502&r2=552503
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/MedicalScenario.java (original)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/MedicalScenario.java Mon Jul  2 07:29:26 2007
@@ -24,8 +24,10 @@
 
 import org.apache.tuscany.sdo.api.SDOUtil;
 
+
 import commonj.sdo.DataObject;
 import commonj.sdo.Property;
+import commonj.sdo.Sequence;
 import commonj.sdo.Type;
 import commonj.sdo.helper.DataFactory;
 import commonj.sdo.helper.HelperContext;
@@ -44,7 +46,11 @@
 
   private static final String medicalURI = "www.example.org/MedicalTest";
 
-  boolean typesViaAPI = false;
+  boolean typesViaAPI = true;
+
+  private static final String usage = " [-api |-xsd]\n"
+      + "   -api : define the types using the SDO API\n"
+      + "   -xsd : define the types using an XML schema file";
 
   public MedicalScenario(String[] args, Integer userLevel) {
 
@@ -65,10 +71,14 @@
       }
     }
   }
+  
+  public MedicalScenario(Integer userLevel) {
+    super(userLevel, SAMPLE_LEVEL_INTERMEDIATE);
+  }
+  
 
   private void printUsage(String[] args) {
-    System.out.println("Usage: " + this.getClass().getCanonicalName()
-        + " [-xsd | -api]");
+    System.out.println("Usage: " + this.getClass().getCanonicalName() + usage);
   }
 
   /**
@@ -87,85 +97,174 @@
     HelperContext scope = SDOUtil.createHelperContext();
 
     if (typesViaAPI) {
+
       commentary("In this execution of the sample we use Types created\n"
           + "using the SDO API");
+
       createTypesViaAPI(scope);
+
     } else {
+
       commentary("In this execution of the sample we use Types created\n"
           + "by loading an XMLSchema");
+
       loadTypesFromXMLSchemaFile(scope, "MedicalTest.xsd");
+
     }
 
-    commentary(COMMENTARY_FOR_NOVICE,
+    commentary(
+        COMMENTARY_FOR_NOVICE,
         "The DataFactory associated with the scope that the types were created within\n"
-          + "can be used to create an instance of the Person Type\n\n"
-          + "DataFactory dataFactory = scope.getDataFactory();\n"
-          + "DataObject person1 = dataFactory.create(\"www.example.org/people\", \"Person\");");
-    
+            + "can be used to create an instance of the Person Type\n\n"
+            + "DataFactory dataFactory = scope.getDataFactory();\n"
+            + "DataObject person1 = dataFactory.create(\"www.example.org/people\", \"Person\");");
+
     DataFactory dataFactory = scope.getDataFactory();
     DataObject person1 = dataFactory.create("www.example.org/people", "Person");
-    
-    commentary(
-        "The setString() of dataObject method is used to set the properties of the\n"
+
+    commentary("The setString() of dataObject method is used to set the properties of the\n"
         + "new Person DataObject, including a unique identifier reference value\n"
-        + "for the Person instance.");
+        + "for the Person instance.\n\n"
+        + "person1.setString(\"id\", \"1\");\n"
+        + "person1.setString(\"name\", \"Joe Johnson Snr.\");\n"
+        + "person1.setString(\"gender\", \"male\"););");
 
     person1.setString("id", "1");
     person1.setString("name", "Joe Johnson Snr.");
     person1.setString("gender", "male");
 
+    commentary("An alternative approach to using the DataFactory directly to create\n"
+        + "all DataObjects is to use a top-down approach,  where we create the\n"
+        + "root object for a data graph,  and then use the createDataObject(String propertyName)\n"
+        + "method to create the contained DataObjects.  Here we create the overall\n"
+        + "medical test DataObject,  and then create the contained \"referrals\" DataObject\n\n"
+        + "DataObject test = dataFactory.create(\"www.example.org/MedicalTest\", \"Test\");\n"
+        + "DataObject referrals = test.createDataObject(\"referrals\");");
+
     DataObject test = dataFactory.create("www.example.org/MedicalTest", "Test");
     DataObject referrals = test.createDataObject("referrals");
+
+    commentary("Now we can add the person we created earlier into the set of people who have\n"
+        + "been referred for this medical test.\n\n"
+        + "test.set(\"referrals\", referrals);\n"
+        + "referrals.getList(\"person\").add(person1);");
+
     test.set("referrals", referrals);
     referrals.getList("person").add(person1);
 
+    commentary("Let's take a look at how the current state of the data"
+        + "graph is rendered in XML ...");
 
     System.out.println(scope.getXMLHelper().save(test,
         "www.example.org/MedicalTest", "test"));
 
+    commentary("The scenario unfolds and the Joe Johnson Snr. becomes a patient\n\n"
+        + "DataObject patients = test.createDataObject(\"patients\");\n"
+        + "patients.getList(\"person\").add(person1);");
+
     DataObject patients = test.createDataObject("patients");
 
     patients.getList("person").add(person1);
 
+    commentary("Having added Joe Johnson Snr. to the set of patients we can see\n"
+        + "the way that SDO preserves a single containment hierarchy within a\n"
+        + "datagraph.  If we look at the XML rendering of the graph again, we will\n"
+        + "see that by adding him to the set of patients he has been removed from the\n"
+        + "containment property associated with the referrals set ...");
+
     System.out.println(scope.getXMLHelper().save(test,
         "www.example.org/MedicalTest", "test"));
 
+    commentary("The 'Person' Type we are making use of here has been designed to be\n"
+        + "multi-purpose,  in that the type has been declared to be 'Open'.\n"
+        + "That means that we can make use of 'Open Content' Properties\n"
+        + "(If the type system has been defined using an XML schema\n"
+        + "then these properties will derive from global elements)\n"
+        + "We can look up open content Properties using the TypeHelper\n\n"
+        + "Property conditionProperty = scope.getTypeHelper().getOpenContentProperty(\n"
+        + "    \"www.example.org/MedicalTest\", \"condition\");");
+
     Property conditionProperty = scope.getTypeHelper().getOpenContentProperty(
         "www.example.org/MedicalTest", "condition");
 
-    // set up an instance of the type of the global property
-    DataObject condition = dataFactory.create("www.example.org/MedicalTest",
-        "Condition");
+    commentary("We can create a value of the appropriate Type for this open\n"
+        + "content Property\n\n"
+        + "DataObject condition = dataFactory.create(conditionProperty.getType());\n"
+        + "condition.setString(\"name\", \"Panar Syndrome\");");
+
+    DataObject condition = dataFactory.create(conditionProperty.getType());
     condition.setString("name", "Panar Syndrome");
 
-    // get the exisiting list of Conditions associated with patient[0] of the
-    // test set
-    // (Note that until we make this call, no such list exists, but since Person
-    // is open
-    // the List will be created on demand.
+    commentary("If you ask a DataObject that has an 'Open' Type for its list of\n"
+        + "values associated with an open content Property, and the DataObject\n"
+        + "doesn't currently have any values for the Property,  it will return\n"
+        + "an empty list. We can use the list to add values for the Property\n\n"
+        + "List conditions = person1.getList(conditionProperty);\n"
+        + "conditions.add(condition);");
 
     List conditions = person1.getList(conditionProperty);
-    // populate the with one condition
     conditions.add(condition);
 
+    commentary("A further look at the data graph in XML form shows\n"
+        + "the presence of the new condition Property's value ...");
+
     System.out.println(scope.getXMLHelper().save(test,
         "www.example.org/MedicalTest", "test"));
 
-    DataObject relatives = test.createDataObject("relatives");
+    commentary("Having looked at the way SDO handles Open content\n"
+        + "We now turn our attention to 'non-containment' relationships.\n"
+        + "To do this we first create the set of people in the test that\n"
+        + "constitute the blood relatives of patients -- 'relatives'\n"
+        + "and define a new person to be Joe Johnson Snr's child.\n\n"
+        + "DataObject relatives = test.createDataObject(\"relatives\");\n"
+        + "DataObject person2 = relatives.createDataObject(\"person\");\n"
+        + "person2.setString(\"id\", \"2\");\n"
+        + "person2.setString(\"name\", \"Joe Johnson Jnr.\");\n"
+        + "person2.setString(\"gender\", \"male\");");
 
+    DataObject relatives = test.createDataObject("relatives");
     DataObject person2 = relatives.createDataObject("person");
 
     person2.setString("id", "2");
     person2.setString("name", "Joe Johnson Jnr.");
     person2.setString("gender", "male");
 
+    commentary("Another quick look at the XML rendering of the graph confirms that\n"
+        + "the set of relatives now includes Joe Johnson Jnr, but we haven't yet\n"
+        + "defined who he is related to, or how.");
+
+    System.out.println(scope.getXMLHelper().save(test,
+        "www.example.org/MedicalTest", "test"));
+
+    commentary("The Person type has a Property 'relative'\n"
+        + "so we create a relative for Joe Johnson Snr.\n\n"
+        + "DataObject relation = person1.createDataObject(\"relative\");\n"
+        + "relation.set(\"target\", person2);\n"
+        + "relation.set(\"relationship\", \"child\");");
+
     DataObject relation = person1.createDataObject("relative");
     relation.set("target", person2);
     relation.set("relationship", "child");
 
+    commentary("Now when we look at the XML rendering of the data graph\n"
+        + "we can see that the action of setting the 'target' of the\n"
+        + "relationship to Joe Johnson Jnr didn't displace him from the\n"
+        + "set of 'relatives',  because the 'target' Property is a\n"
+        + "non-containment Property.  This non-containment relationship\n"
+        + "is reflected in the XML by the reference to the unique identifier\n"
+        + "for Joe Johnson Jnr, \"2\" ...\n\n"
+        + "   ...\n    <relative target=\"2\" relationship=\"child\"/>\n   ...\n");
+
     System.out.println(scope.getXMLHelper().save(test,
         "www.example.org/MedicalTest", "test"));
-
+    
+    commentary("Now that the graph is complete we can use the PrintDataGraph sample utility\n" +
+        "to reveal the full SDO nature of the final data graph\n\n" +
+        "");
+
+    PrintDataGraph printer = new PrintDataGraph(COMMENTARY_FOR_ADVANCED);
+    printer.printDataObject(test);
+    System.out.println(printer.getBuf());
   }
 
   /**
@@ -272,27 +371,6 @@
     p.setBoolean("containment", true); // why is this not the default?
 
     typeHelper.defineOpenContentProperty(medicalURI, p);
-
-    // List medTypes = SDOUtil.getTypes(typeHelper, medicalURI);
-    // String xsd = scope.getXSDHelper().generate(medTypes);
-    // System.out.println("generated XSD \n" + xsd);
-    // List perTypes = SDOUtil.getTypes(typeHelper, medicalURI);
-    // xsd = scope.getXSDHelper().generate(perTypes);
-    // System.out.println("generated XSD \n" + xsd);
-
-    // DataObject root = scope.getDataFactory().create("commonj.sdo",
-    // "DataObject");
-    // Property contp = SDOUtil.createProperty(globalContainer, "typeDesc",
-    // typeHelper.getType("commonj.sdo", "Type"));
-    // SDOUtil.setMany(contp, true);
-    // SDOUtil.setContainment(contp, true);
-    // List typeList = root.getList(contp);
-    // for(Iterator it=typeDeclarations.iterator(); it.hasNext();) {
-    // DataObject dob = (DataObject)it.next();
-    // typeList.add(dob);
-    // }
-    //    
-    // XMLHelper.INSTANCE.save(root, medicalURI, "typeList", System.out);
 
   }
 

Modified: incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/PrintDataGraph.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/PrintDataGraph.java?view=diff&rev=552503&r1=552502&r2=552503
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/PrintDataGraph.java (original)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/PrintDataGraph.java Mon Jul  2 07:29:26 2007
@@ -56,6 +56,48 @@
     PrintDataGraph sample = new PrintDataGraph(COMMENTARY_FOR_NOVICE);
     sample.run();
   }
+  
+  public void runSample() throws Exception {
+    commentary("This sample demonstrates a common pattern of traversing a data graph\n"
+        + "and printing the values of its Properties.  As the sample traverses a couple of\n"
+        + "graphs it provides commentary about what it has found and what actions it\n"
+        + "is taking, whilst building up a text representation of the graph. It then\n"
+        + "shows you the results of its labours.");
+
+    HelperContext scope = createScopeForTypes();
+
+    commentary(
+        COMMENTARY_ALWAYS,
+        "First we look at a data graph of a Purchase Order which has a fairly simple XML schema\n"
+            + "and the graph's containment hierarchy has a couple of levels of depth");
+
+    loadTypesFromXMLSchemaFile(scope, SdoSampleConstants.PO_XSD_RESOURCE);
+
+    XMLDocument purchaseOrder = getXMLDocumentFromFile(scope,
+        SdoSampleConstants.PO_XML_RESOURCE);
+
+    printXMLDocument(purchaseOrder);
+
+    commentary(COMMENTARY_ALWAYS,
+        "And here is the resultant view of the data graph\n\n");
+    System.out.println(getBuf().toString());
+
+    commentary(COMMENTARY_ALWAYS,
+        "Next we look at a graph representing a form letter,  where the Type of the\n"
+            + "root data object is 'Sequenced'");
+
+    loadTypesFromXMLSchemaFile(scope, "letter.xsd");
+    DataObject letter = getDataObjectFromFile(scope, "letter.xml");
+
+    reset();
+    print(letter);
+
+    commentary(COMMENTARY_ALWAYS,
+        "And here is the resultant view of the data graph\n\n");
+
+    System.out.println(getBuf().toString());
+
+  }
 
   public void reset() {
     indent = 0;
@@ -166,7 +208,7 @@
       for (int i = 0; i < dataObject.getInstanceProperties().size(); i++) {
         Property p = (Property) dataObject.getInstanceProperties().get(i);
         indent();
-        printPropertyValue(dataObject, p);
+        printValueOfProperty(dataObject, p);
       }
 
       decrementIndent();
@@ -174,6 +216,8 @@
 
   }
 
+
+
   public void printSequence(Sequence seq) {
 
     commentary(
@@ -189,6 +233,7 @@
     incrementIndent();
     indent();
     buf.append("Sequence: {\n");
+
     incrementIndent();
     for (int i = 0; i < seq.size(); i++) {
       Property p = seq.getProperty(i);
@@ -196,20 +241,38 @@
         indent();
         buf.append("text: ").append(seq.getValue(i));
         lineBreak();
-      } else if (p.getType().isDataType()) {
-        indent();
-        buf.append("Property: ").append(p.getName()).append(": ");
-        printSimpleValue(seq.getValue(i));
-        lineBreak();
-      }
+      } else {
+        printPropertyValuePair(p, seq.getValue(i));
+     }
     }
     decrementIndent();
+
     indent();
-    buf.append('}');
+    buf.append("}\n");
     decrementIndent();
   }
 
-  private void printPropertyValue(DataObject dataObject, Property p) {
+  private void printPropertyValuePair(Property p, Object value) {
+
+    indent();
+    buf.append("Property: ").append(p.getName()).append(": ");
+    if(p.getType().isDataType()) {
+      printSimpleValue(value);
+      lineBreak();
+    } else {
+      if(p.isContainment()) {
+        incrementIndent();
+        printDataObject((DataObject)value);
+        decrementIndent();
+      } else {
+        printReferencedDataObject((DataObject)value);
+      }
+    }
+
+    
+  }
+
+  private void printValueOfProperty(DataObject dataObject, Property p) {
 
     commentary(
         COMMENTARY_FOR_INTERMEDIATE,
@@ -277,20 +340,24 @@
 
         "Recording the fact that we have encountered another non-containment reference");
 
-    incrementIndent();
-    indent();
     List path = new ArrayList();
     DataObject current = dataObject;
     while (current != null) {
-      // TODO sort out indexing
-      path.add(current.getContainmentProperty().getName());
-      current = dataObject.getContainer();
+      Property containmentProperty = current.getContainmentProperty();
+      if(containmentProperty != null) {
+        if(containmentProperty.isMany()) {
+          List pValues = current.getContainer().getList(containmentProperty);
+          int index = pValues.indexOf(current)+1;
+          path.add("["+index+"]");
+        }
+        path.add("/"+current.getContainmentProperty().getName());
+      }
+      current = current.getContainer();
     }
     buf.append("reference to: ");
     for (ListIterator i = path.listIterator(path.size()); i.hasPrevious();) {
-      buf.append("/").append(i.previous());
+      buf.append(i.previous());
     }
-    decrementIndent();
   }
 
   private void printReferencedDataObjects(List list) {
@@ -314,9 +381,10 @@
         COMMENTARY_FOR_NOVICE,
         "Traversing a list of DataObjects which represent the values of a multi-valued containment Property");
 
+
     lineBreak();
     indent();
-    buf.append("[\n");
+    buf.append("[");
     incrementIndent();
     for (Iterator i = list.iterator(); i.hasNext();) {
       printDataObject((DataObject) i.next());
@@ -368,52 +436,6 @@
 
   public void setBuf(StringBuffer b) {
     buf = b;
-  }
-
-  public void runSample() throws Exception {
-    commentary("This sample demonstrates a common pattern of traversing a data graph\n"
-        + "and printing the values of its Properties.  As the sample traverses a couple of\n"
-        + "graphs it provides commentary about what it has found and what actions it\n"
-        + "is taking, whilst building up a text representation of the graph. It then\n"
-        + "shows you the results of its labours.");
-
-    HelperContext scope = createScopeForTypes();
-
-    commentary(
-        COMMENTARY_ALWAYS,
-        "First we look at a data graph of a Purchase Order which has a fairly simple XML schema\n"
-            + "and the graph's containment hierarchy has a couple of levels of depth");
-
-    loadTypesFromXMLSchemaFile(scope, SdoSampleConstants.PO_XSD_RESOURCE);
-
-    XMLDocument purchaseOrder = getXMLDocumentFromFile(scope,
-        SdoSampleConstants.PO_XML_RESOURCE);
-
-    // StringBuffer result = printXMLDocument(purchaseOrder);
-
-    printXMLDocument(purchaseOrder);
-
-    commentary(COMMENTARY_ALWAYS,
-        "And here is the resultant view of the data graph\n\n");
-    System.out.println(getBuf().toString());
-
-    commentary(COMMENTARY_ALWAYS,
-        "Next we look at a graph representing a form letter,  where the Type of the\n"
-            + "root data object is 'Sequenced'");
-
-    loadTypesFromXMLSchemaFile(scope, "letter.xsd");
-    DataObject letter = getDataObjectFromFile(scope, "letter.xml");
-
-    reset();
-    print(letter);
-
-    commentary(COMMENTARY_ALWAYS,
-        "And here is the resultant view of the data graph\n\n");
-
-    System.out.println(getBuf().toString());
-
-    // TODO add an example with non-containment
-
   }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org