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/06/29 18:49:30 UTC

svn commit: r551954 - in /incubator/tuscany/java/sdo/sample/src/main: java/org/apache/tuscany/samples/sdo/MedicalScenario.java java/org/apache/tuscany/samples/sdo/SampleBase.java resources/MedicalTest.xsd resources/People.xsd

Author: kelvingoodson
Date: Fri Jun 29 09:49:29 2007
New Revision: 551954

URL: http://svn.apache.org/viewvc?view=rev&rev=551954
Log:
dding sample from JDJ paper --- transformation to sample layout andaddition of commentary not yet complete

Added:
    incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/MedicalScenario.java
    incubator/tuscany/java/sdo/sample/src/main/resources/MedicalTest.xsd
    incubator/tuscany/java/sdo/sample/src/main/resources/People.xsd
Modified:
    incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/SampleBase.java

Added: 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=auto&rev=551954
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/MedicalScenario.java (added)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/MedicalScenario.java Fri Jun 29 09:49:29 2007
@@ -0,0 +1,322 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.tuscany.samples.sdo;
+
+import java.io.FileNotFoundException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.tuscany.sdo.api.SDOUtil;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.Property;
+import commonj.sdo.Type;
+import commonj.sdo.helper.DataFactory;
+import commonj.sdo.helper.HelperContext;
+import commonj.sdo.helper.TypeHelper;
+
+/**
+ * This sample is based on a 2 part article by Kelvin Goodson and Geoffrey Winn.
+ * See <A href="http://soa.sys-con.com/read/313547.htm">Part1</A> and <A
+ * href="http://soa.sys-con.com/read/358059.htm">Part 2</A> of the article.
+ */
+public class MedicalScenario extends SampleBase {
+
+  private static final String sdoApiUri = "commonj.sdo";
+
+  private static final String peopleURI = "www.example.org/people";
+
+  private static final String medicalURI = "www.example.org/MedicalTest";
+
+  boolean typesViaAPI = false;
+
+  public MedicalScenario(String[] args, Integer userLevel) {
+
+    super(userLevel, SAMPLE_LEVEL_INTERMEDIATE);
+
+    if (args.length > 1) {
+      printUsage(args);
+      System.exit(-1);
+    }
+    if (args.length == 1) {
+      if (args[0].equals("-xsd")) {
+        typesViaAPI = false;
+      } else if (args[0].equals("-api")) {
+        typesViaAPI = true;
+      } else {
+        printUsage(args);
+        System.exit(-1);
+      }
+    }
+  }
+
+  private void printUsage(String[] args) {
+    System.out.println("Usage: " + this.getClass().getCanonicalName()
+        + " [-xsd | -api]");
+  }
+
+  /**
+   * @param args
+   * @throws Exception
+   */
+  public static void main(String[] args) throws Exception {
+
+    MedicalScenario s = new MedicalScenario(args, COMMENTARY_FOR_NOVICE);
+    s.run();
+
+  }
+
+  public void runSample() throws Exception {
+
+    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,
+        "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\");");
+    
+    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"
+        + "new Person DataObject, including a unique identifier reference value\n"
+        + "for the Person instance.");
+
+    person1.setString("id", "1");
+    person1.setString("name", "Joe Johnson Snr.");
+    person1.setString("gender", "male");
+
+    DataObject test = dataFactory.create("www.example.org/MedicalTest", "Test");
+    DataObject referrals = test.createDataObject("referrals");
+    test.set("referrals", referrals);
+    referrals.getList("person").add(person1);
+
+
+    System.out.println(scope.getXMLHelper().save(test,
+        "www.example.org/MedicalTest", "test"));
+
+    DataObject patients = test.createDataObject("patients");
+
+    patients.getList("person").add(person1);
+
+    System.out.println(scope.getXMLHelper().save(test,
+        "www.example.org/MedicalTest", "test"));
+
+    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");
+    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.
+
+    List conditions = person1.getList(conditionProperty);
+    // populate the with one condition
+    conditions.add(condition);
+
+    System.out.println(scope.getXMLHelper().save(test,
+        "www.example.org/MedicalTest", "test"));
+
+    DataObject relatives = test.createDataObject("relatives");
+
+    DataObject person2 = relatives.createDataObject("person");
+
+    person2.setString("id", "2");
+    person2.setString("name", "Joe Johnson Jnr.");
+    person2.setString("gender", "male");
+
+    DataObject relation = person1.createDataObject("relative");
+    relation.set("target", person2);
+    relation.set("relationship", "child");
+
+    System.out.println(scope.getXMLHelper().save(test,
+        "www.example.org/MedicalTest", "test"));
+
+  }
+
+  /**
+   * @throws Exception
+   * @throws FileNotFoundException
+   */
+  private void createTypesViaAPI(HelperContext scope) throws Exception {
+
+    List typeDeclarations = new ArrayList();
+
+    TypeHelper typeHelper = scope.getTypeHelper();
+
+    Type stringType = typeHelper.getType(sdoApiUri, "String");
+    Type dateType = typeHelper.getType(sdoApiUri, "Date");
+    Type booleanType = typeHelper.getType(sdoApiUri, "Boolean");
+
+    // <complexType name="Person">
+    // <sequence>
+    // <element name="dob" type="date"/>
+    // <element name="relative" maxOccurs="unbounded" type="tns:Relative"/>
+    // <any namespace="##other" processContents="lax" maxOccurs="unbounded"/>
+    // </sequence>
+    // <attribute name="id" type="ID"/>
+    // <attribute name="name" type="string"/>
+    // <attribute name="gender" type = "tns:Gender"/>
+    // </complexType>
+
+    DataObject personTypeDesc = createTypeDescription(scope, peopleURI,
+        "Person");
+    typeDeclarations.add(personTypeDesc);
+
+    addPropertyDescription(personTypeDesc, stringType, "name");
+    addPropertyDescription(personTypeDesc, dateType, "dob");
+    addPropertyDescription(personTypeDesc, stringType, "id"); // set to unique
+    // identifier?
+    addPropertyDescription(personTypeDesc, stringType, "gender"); // restrict?
+
+    DataObject relativeType = createTypeDescription(scope, peopleURI,
+        "Relative"); // forward declare the Relative type
+    typeDeclarations.add(relativeType);
+
+    DataObject rp = addPropertyDescription(personTypeDesc, relativeType,
+        "relative");
+    rp.setBoolean("many", true);
+    personTypeDesc.set("open", Boolean.TRUE);
+
+    // <complexType name="Relative">
+    // <attribute name="target" type="IDREF" sdoxml:propertyType="tns:Person"
+    // use="required"/>
+    // <attribute name="relationship" type="string" />
+    // <attribute name="genetic" use="optional" type="boolean"/>
+    // </complexType>
+
+    addPropertyDescription(relativeType, stringType, "relationship");
+    addPropertyDescription(relativeType, booleanType, "genetic");
+    DataObject targetPersonProp = addPropertyDescription(relativeType,
+        personTypeDesc, "target");
+    targetPersonProp.setBoolean("containment", false);
+
+    // <complexType name="PersonSet">
+    // <sequence>
+    // <element name="person" type="tns:Person" maxOccurs="unbounded"/>
+    // </sequence>
+    // </complexType>
+
+    DataObject pSet = createTypeDescription(scope, peopleURI, "PersonSet");
+    typeDeclarations.add(pSet);
+    DataObject pSetProperty = addPropertyDescription(pSet, personTypeDesc,
+        "person");
+    pSetProperty.setBoolean("many", true);
+
+    // <complexType name="Condition">
+    // <sequence>
+    // <element name="diagnosed" type="date" />
+    // </sequence>
+    // <attribute name="name" type="tns:ConditionName" />
+    // </complexType>
+
+    DataObject condition = createTypeDescription(scope, medicalURI, "Condition");
+    typeDeclarations.add(condition);
+    addPropertyDescription(condition, booleanType, "diagnosed");
+    addPropertyDescription(condition, stringType, "name"); // constrain?
+
+    // <complexType name="Test">
+    // <sequence>
+    // <element name="referrals" type="people:PersonSet" />
+    // <element name="patients" type="people:PersonSet" />
+    // <element name="relatives" type="people:PersonSet" />
+    // </sequence>
+    // </complexType>
+
+    DataObject testType = createTypeDescription(scope, medicalURI, "Test");
+    typeDeclarations.add(testType);
+    addPropertyDescription(testType, pSet, "referrals");
+    addPropertyDescription(testType, pSet, "patients");
+    addPropertyDescription(testType, pSet, "relatives");
+
+    List types = typeHelper.define(typeDeclarations);
+
+    DataObject p = scope.getDataFactory().create("commonj.sdo", "Property");
+    p.set("type", typeHelper.getType(medicalURI, "Condition"));
+    p.set("name", "condition");
+    p.setBoolean("many", true);
+    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);
+
+  }
+
+  /**
+   * @param uri
+   * @param name
+   * @return
+   */
+  private DataObject createTypeDescription(HelperContext scope, String uri,
+      String name) {
+    DataObject typeDesc = scope.getDataFactory().create(sdoApiUri, "Type");
+    typeDesc.set("name", name);
+    typeDesc.set("uri", uri);
+    return typeDesc;
+  }
+
+  private DataObject addPropertyDescription(
+      DataObject containerTypeDescription, Object propertyType,
+      String propertyName) {
+    DataObject property = containerTypeDescription.createDataObject("property");
+    property.set("type", propertyType);
+    property.setString("name", propertyName);
+    property.setBoolean("containment", true);
+    return property;
+  }
+
+}

Modified: incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/SampleBase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/SampleBase.java?view=diff&rev=551954&r1=551953&r2=551954
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/SampleBase.java (original)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/SampleBase.java Fri Jun 29 09:49:29 2007
@@ -25,6 +25,8 @@
 import java.io.InputStream;
 import java.io.StringBufferInputStream;
 import java.io.StringReader;
+import java.net.URL;
+import java.util.List;
 
 import org.apache.tuscany.sdo.api.SDOUtil;
 
@@ -115,9 +117,11 @@
     
     InputStream is = null;
     try {
+    	
+        URL url = getClass().getResource("/"+fileName);
+        is = url.openStream();
+        xsdHelper.define(is, url.toString());
 
-        is = ClassLoader.getSystemResourceAsStream(fileName);
-        xsdHelper.define(is, null);
      } catch (Exception e) {
         somethingUnexpectedHasHappened(e);
      } finally {

Added: incubator/tuscany/java/sdo/sample/src/main/resources/MedicalTest.xsd
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/resources/MedicalTest.xsd?view=auto&rev=551954
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/resources/MedicalTest.xsd (added)
+++ incubator/tuscany/java/sdo/sample/src/main/resources/MedicalTest.xsd Fri Jun 29 09:49:29 2007
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<schema xmlns="http://www.w3.org/2001/XMLSchema"
+	xmlns:people="www.example.org/people" xmlns:sdo="commonj.sdo"
+	xmlns:sdoxml="commonj.sdo/xml"
+	xmlns:tns="www.example.org/MedicalTest"
+	targetNamespace="www.example.org/MedicalTest">
+
+	<import namespace="www.example.org/people"
+		schemaLocation="People.xsd" />
+
+	<element name="test" type="tns:Test" />
+	<element name="condition" type="tns:Condition" />
+
+	<complexType name="Test">
+		<sequence>
+			<element name="referrals" type="people:PersonSet" />
+			<element name="patients" type="people:PersonSet" />
+			<element name="relatives" type="people:PersonSet" />
+		</sequence>
+	</complexType>
+
+	<complexType name="Condition">
+		<sequence>
+			<element name="diagnosed" type="date" />
+		</sequence>
+		<attribute name="name" type="tns:ConditionName" />
+	</complexType>
+
+	<simpleType name="ConditionName">
+		<restriction base="string">
+			<enumeration value="Rigellian fever" />
+			<enumeration value="Vegan choriomeningitis" />
+			<enumeration value="Scrofungulus" />
+			<enumeration value="Panar Syndrome" />
+		</restriction>
+	</simpleType>
+
+
+</schema>

Added: incubator/tuscany/java/sdo/sample/src/main/resources/People.xsd
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/resources/People.xsd?view=auto&rev=551954
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/resources/People.xsd (added)
+++ incubator/tuscany/java/sdo/sample/src/main/resources/People.xsd Fri Jun 29 09:49:29 2007
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<schema xmlns="http://www.w3.org/2001/XMLSchema"
+	targetNamespace="www.example.org/people"
+	xmlns:sdo="commonj.sdo"
+    xmlns:sdoxml="commonj.sdo/xml"
+	xmlns:tns="www.example.org/people">
+
+	<!--   <import namespace="commonj.sdo/xml" schemaLocation="sdoXML.xsd" /> -->
+				
+	<complexType name="Person">
+		<sequence>
+			<element name="dob" type="date"/>
+			<element name="relative" maxOccurs="unbounded" type="tns:Relative"/>
+			<any namespace="##other" processContents="lax" maxOccurs="unbounded"/>
+		</sequence>
+		<attribute name="id" type="ID"/>
+		<attribute name="name" type="string"/>
+	    <attribute name="gender" type = "tns:Gender"/>	
+	</complexType>
+	
+	<!-- <complexType name="Parent">
+	    <attribute name="parent" type="IDREF" sdoxml:propertyType="tns:Person" use="required"/>
+	    <attribute name="genetic" use="optional" type="boolean"/>
+	</complexType>  -->
+	
+	<complexType name="Relative">
+	    <attribute name="target" type="IDREF" sdoxml:propertyType="tns:Person" use="required"/>
+	    <attribute name="relationship" type="string" />
+	    <attribute name="genetic" use="optional" type="boolean"/>
+	</complexType>
+	
+	<!-- <complexType name="GeneticParent">
+		<complexContent>
+		<extension base="tns:Parent">
+		</extension>
+		</complexContent>
+	</complexType>  -->
+	
+	<complexType name="PersonSet">
+		<sequence>
+			<element name="person" type="tns:Person" maxOccurs="unbounded"/>
+		</sequence>
+	</complexType>
+
+	<simpleType name="Gender">
+       <restriction base="string">
+		   <enumeration value="male" />
+		   <enumeration value="female" />	
+	   </restriction>
+	</simpleType>
+
+</schema>
\ No newline at end of file



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