<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns="ID@NCSU"
	targetNamespace="ID@NCSU"
	elementFormDefault="qualified">

<!-- Big Picture of KnowledgeBase -->

<xsd:element name="KnowledgeBase">
  <xsd:complexType>
   <xsd:sequence>
     <xsd:element ref="Predicates"/>
     <xsd:element ref="Implications"/>
     <xsd:element ref="HyperAlertTypes"/>
   </xsd:sequence>
  </xsd:complexType>

  <xsd:unique name="unique1">
   <xsd:selector xpath="HyperAlertTypes/HyperAlertType"/>
   <xsd:field xpath="@Name"/>
  </xsd:unique>

<!-- set the name of the predicate as the key -->

  <xsd:key name="PredicateNameKey">
   <xsd:selector xpath="Predicates/Predicate"/>
   <xsd:field xpath="@Name"/>
  </xsd:key>

  <xsd:keyref name="keyref1" refer="PredicateNameKey">
    <xsd:selector xpath="Implications/Implication/Implying"/>
    <xsd:field xpath="ImplyingName"/>
  </xsd:keyref>

  <xsd:keyref name="keyref2" refer="PredicateNameKey">
    <xsd:selector xpath="Implications/Implication/Implied"/>
    <xsd:field xpath="ImpliedName"/>
  </xsd:keyref>

  <xsd:keyref name="keyref3" refer="PredicateNameKey">
    <xsd:selector xpath="HyperAlertTypes/HyperAlertType/Prerequisite/Predicate"/>
    <xsd:field xpath="@Name"/>
  </xsd:keyref>

  <xsd:keyref name="keyref4" refer="PredicateNameKey">
    <xsd:selector xpath="HyperAlertTypes/HyperAlertType/Consequence/Predicate"/>
    <xsd:field xpath="@Name"/>
  </xsd:keyref>

<!-- set the argument ID as the key -->

  <xsd:unique name="ArgumentKey">
   <xsd:selector xpath="Predicates/Predicate/Arg"/>
      <xsd:field xpath="@id"/>
  </xsd:unique>

  <xsd:keyref name="keyref5" refer="ArgumentKey">
    <xsd:selector xpath="Implications/Implication/ArgMap/ImplyingArg"/>
    <xsd:field xpath="@id"/>
  </xsd:keyref>

  <xsd:keyref name="keyref6" refer="ArgumentKey">
    <xsd:selector xpath="Implications/Implication/ArgMap/ImpliedArg"/>
    <xsd:field xpath="@id"/>
  </xsd:keyref>

  <xsd:keyref name="keyref7" refer="ArgumentKey">
    <xsd:selector xpath="HyperAlertTypes/HyperAlertType/Prerequisite/Predicate/Arg"/>
    <xsd:field xpath="@id"/>
  </xsd:keyref>

  <xsd:keyref name="keyref8" refer="ArgumentKey">
    <xsd:selector xpath="HyperAlertTypes/HyperAlertType/Consequence/Predicate/Arg"/>
    <xsd:field xpath="@id"/>
  </xsd:keyref>

</xsd:element>

<!-- ========================================= -->
<!-- Predicates schema -->

<xsd:element name="Predicates">
  <xsd:complexType>
   <xsd:sequence>
     <xsd:element name="Predicate" minOccurs="0" maxOccurs="unbounded">
       <xsd:complexType>
         <xsd:sequence>
           <xsd:element name="Arg" minOccurs="0" maxOccurs="unbounded">
             <xsd:complexType>
               <xsd:attribute name="id" type="xsd:positiveInteger">
               </xsd:attribute>
               <xsd:attribute name="Pos" type="xsd:positiveInteger">
               </xsd:attribute>
               <xsd:attribute name="Attr">
                 <xsd:simpleType>
	           <xsd:restriction base="xsd:string">
	             <xsd:enumeration value="int"/>
	             <xsd:enumeration value="varchar(15)"/>
	           </xsd:restriction>
	         </xsd:simpleType>
               </xsd:attribute>
             </xsd:complexType>
           </xsd:element>
<!-- end of element Arg -->
         </xsd:sequence>
         <xsd:attribute name="Name"  type="xsd:string" use="required">
         </xsd:attribute>
       </xsd:complexType>
     </xsd:element>
   </xsd:sequence>
  </xsd:complexType>
</xsd:element>

<!-- ========================================= -->
<!-- Implication schema -->
<xsd:element name="Implications">
  <xsd:complexType>
   <xsd:sequence>
     <xsd:element ref="Implication" minOccurs='0' maxOccurs='unbounded'/>
   </xsd:sequence>
  </xsd:complexType>
</xsd:element>

<xsd:element name="Implication">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="ImplyingName" type="xsd:string"/>
      <xsd:element name="ImpliedName" type="xsd:string"/>
      <xsd:element name="ArgMap" minOccurs='0' maxOccurs='unbounded'>
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="ImplyingArg">
              <xsd:complexType>
                <xsd:attribute name="id" type="xsd:positiveInteger">
                </xsd:attribute>
              </xsd:complexType>
            </xsd:element>
            <xsd:element name="ImpliedArg">
              <xsd:complexType>
                <xsd:attribute name="id" type="xsd:positiveInteger">
                </xsd:attribute>
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>

    <xsd:attribute name="Phantom" default="No">
      <xsd:simpleType>
        <xsd:restriction base="xsd:string">
          <xsd:enumeration value="Yes"/>
          <xsd:enumeration value="No"/>
        </xsd:restriction>
      </xsd:simpleType>
    </xsd:attribute>

  </xsd:complexType>
</xsd:element>

<!-- ========================================= -->
<!-- HyperAlertTypes schema -->

<xsd:element name="HyperAlertTypes">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element ref="HyperAlertType" minOccurs='1' maxOccurs='unbounded'/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

<xsd:element name="HyperAlertType">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element ref="Fact" minOccurs='1' maxOccurs='unbounded'/>
      <xsd:element ref="Prerequisite" minOccurs='0' maxOccurs='unbounded'/>
      <xsd:element ref="Consequence" minOccurs='0' maxOccurs='unbounded'/>
    </xsd:sequence>
    <xsd:attribute name="Name" type="xsd:string" use="required">
    </xsd:attribute>
  </xsd:complexType>

<!-- set the Fact as the key for each hyper alert type -->
  <xsd:key name="FactKey">
    <xsd:selector xpath="Fact"/>
    <xsd:field xpath="@FactName"/>
  </xsd:key>

  <xsd:keyref name="keyref9" refer="FactKey">
    <xsd:selector xpath="Prerequisite/Predicate/Arg"/>
    <xsd:field xpath="@ArgName"/>
  </xsd:keyref>

  <xsd:keyref name="keyref10" refer="FactKey">
    <xsd:selector xpath="Consequence/Predicate/Arg"/>
    <xsd:field xpath="@ArgName"/>
  </xsd:keyref>

</xsd:element>

<xsd:element name="Fact">
  <xsd:complexType>
    <xsd:attribute name="FactName" type="xsd:string">
    </xsd:attribute>
    <xsd:attribute name="FactType">
      <xsd:simpleType>
        <xsd:restriction base="xsd:string">
          <xsd:enumeration value="int"/>
          <xsd:enumeration value="varchar(15)"/>
        </xsd:restriction>
      </xsd:simpleType>
    </xsd:attribute>
  </xsd:complexType>
</xsd:element>

<xsd:element name="Prerequisite">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element ref="Predicate" minOccurs='0' maxOccurs='unbounded'/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

<xsd:element name="Consequence">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element ref="Predicate" minOccurs='0' maxOccurs='unbounded'/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

<xsd:element name="Predicate">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="Arg" minOccurs='0' maxOccurs='unbounded'>
        <xsd:complexType>
          <xsd:attribute name="id" type="xsd:positiveInteger" use="required">
          </xsd:attribute>
          <xsd:attribute name="ArgName" type="xsd:string" use="required">
          </xsd:attribute>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
    <xsd:attribute name="Name" type="xsd:string" use='required'>
    </xsd:attribute>
  </xsd:complexType>
</xsd:element>

</xsd:schema>