import java.sql.*;
import java.io.*;
import uk.ac.ic.doc.automed.*;
import uk.ac.ic.doc.automed.reps.*;

/**************************************************************
 * An application to setup YATTA models for semistructured data 
 * using the Automed API
 **************************************************************/

// There is only one construct for YATTA model.
// Oh! there is a root node also. 

public class DefineYATTAModel {

  /* Define a YATTA model, with constructs i.e. node, edge and pattern */
  public static void createYATTAModel() 
    throws AutoMedException, SQLException, IOException {
    // Zap any existing model called "yatta"
    if (Model.exists("yatta")) {
      System.out.println("Removed old yatta modelling language");
      Model.getModel("yatta").retract();
    }

    // create YATTA model
    Model yatta=Model.createModel("yatta"); 

    // Create the constructs of the YATTA modelling language

    // ROOT ...
    // Node can stand by themselves; hence NODAL
    Construct rootnode=
      yatta.createConstruct("rootnode",Construct.CLASS_NODAL,true);
    rootnode.addNodeNameScheme();    // To hold name
    rootnode.addLabelScheme(false);  // To hold data type

    // YATTANODE - LINK NODAL
    Construct yattanode=
      yatta.createConstruct("yattanode",Construct.CLASS_LINK_NODAL,true); 
    //"uk.ac.ic.doc.automed.editor.drawables.YattaDrawLinkNodalLink");

    Construct rootOrYatta=
      yatta.createConstruct("rootOrYatta",
			    Construct.CLASS_ALTERNATION,
			    false);
    rootOrYatta.addReferenceScheme(rootnode);
    rootOrYatta.addReferenceScheme(yattanode);

    // The scheme of a yatta node contains node's name, data type, cardinality constraints, a sequence of nodes  
    // in the form <<node_name, data_type, constraint, root, a sequence of nodes>>

    yattanode.addReferenceScheme(rootOrYatta);         // To hold a root node
    yattanode.addEdgeNameScheme();                     // To hold name
    yattanode.addLabelScheme(false);                   // To hold data type
    yattanode.addConstraintScheme(false);              // To hold cardinality constraint
    //yattanode.addLabelScheme(false);                   // To hold order (for list) or count (for bag)
    // To make it 0..N the `upper' needs to be lower than the `lower'
    //yattanode.addReferenceScheme(yattanode, 0, -1);    // To hold the nodes from root node to the node itself

    // SEQUENCE - LINK
    Construct sequence=
      yatta.createConstruct("sequence",Construct.CLASS_LINK,true);
    sequence.addEdgeNameScheme();
    sequence.addReferenceScheme(yattanode, 2, 2);

    System.out.println("Created YATTA Model");
  }

  public static void main(String[] args) {
    try {
      System.out.println("AutoMed Repository: Define Models");
      createYATTAModel();
    }
    catch (AutoMedException ae) {
	ae.printStackTrace();
    }
    catch (SQLException se) {
	se.printStackTrace();
    }
    catch (IOException ie) {
	ie.printStackTrace();
    }

  }
}

























