Skip to content

Creating AML

Eric Domke edited this page Jun 30, 2017 · 1 revision

There are many different ways to craft an AML query using Innovator.Client. The method you use will depend on your situation and your coding style. For inspiration, consider the following examples:

Parameter Substitution

Arguably the easiest way to craft AML is to embed the AML string in code and use @0-style parameters to substitute values into the query. Just remember to use single quotes with your AML attributes.

// Get preliminary parts which have existed for a little bit of time
private IEnumerable<IReadOnlyItem> GetParts(IConnection conn
  , string classification)
{
  return conn.Apply(
    @"<Item type='Part' action='get'>
        <classification>@0</classification>
        <created_on condition='lt'>@1</created_on>
        <state>Preliminary</state>
      </Item>", classification, DateTime.Now.AddMinutes(-20)).Items();
}

If you like this approach, but want to modify the query a bit, consider the following approach

private IEnumerable<IReadOnlyItem> GetParts(IConnection conn
  , string classification)
{
  var aml = conn.AmlContext;
  var query = aml.FromXml(
    @"<Item type='Part' action='get'>
        <classification>@0</classification>
        <created_on condition='lt'>@1</created_on>
      </Item>", classification, DateTime.Now.AddMinutes(-20)).AssertItem();
  query.State().Set("Preliminary");
  return conn.Apply(query.ToAml()).Items();
}

Create AML Objects

Using a syntax similar to LINQ to XML, AML objects can be constructed by nesting calls to create the various elements.

private IEnumerable<IReadOnlyItem> GetParts(IConnection conn
  , string classification)
{
  var aml = conn.AmlContext;
  var query = aml.Item(aml.Type("Part"), aml.Action("get")
    , aml.Classification(classification)
    , aml.CreatedOn(aml.Condition(Condition.LessThan), DateTime.Now.AddMinutes(-20))
    , aml.Property("state", "Preliminary")
  );
  return conn.Apply(query.ToAml()).Items();
}

VB XML Literals

The multi-line syntax is not as convenient as it is in C#. However, Visual Basic supports XML literals. Therefore, just embed your query directly

Private Function GetParts(conn As IConnection,
  classification as String) As IEnumerable(Of IReadOnlyItem)
  Return conn.Apply(<Item type="Part" action="get">
        <classification>@0</classification>
        <created_on condition="lt">@1</created_on>
        <state>Preliminary</state>
      </Item>, classification, DateTime.Now.AddMinutes(-20)).Items()
End Function

String Templates

If you are writing code in .Net 4.6 or later, you can Innovator.Client with Interpolated Strings.

// Get preliminary parts which have existed for a little bit of time
private IEnumerable<IReadOnlyItem> GetParts(IConnection conn
  , string classification)
{
  var startDate = DateTime.Now.AddMinutes(-20);
  return conn.Apply(
    (FormattableString)$@"<Item type='Part' action='get'>
        <classification>{classification}</classification>
        <created_on condition='lt'>{startDate}</created_on>
        <state>Preliminary</state>
      </Item>").Items();
}

LINQ with IQueryable

Innovator.Client currently only supports creating the most basic queries using LINQ. For example:

private IEnumerable<IReadOnlyItem> GetParts(IConnection conn
  , string classification)
{
  return conn.Queryable("Part")
    .Where(i => i.Classification().Value == classification
      && i.CreatedOn().AsDateTime() < DateTime.Now.AddMinutes(-20)
      && i.State().Value == "Preliminary").ToArray();
}
Clone this wiki locally