@Before
  public void init() {

    List<OclReal> realSequence = new ArrayList<OclReal>();
    realSequence.add(oclReal0_5);
    oclSequence =
        myStandardLibraryFactory.createOclSequence(
            realSequence, EssentialOclPlugin.getOclLibraryProvider().getOclLibrary().getOclReal());

    realSequence.add(oclReal1_5);
    realSequence.add(undefined);
    oclSequence2 =
        myStandardLibraryFactory.createOclSequence(
            realSequence, EssentialOclPlugin.getOclLibraryProvider().getOclLibrary().getOclReal());

    List<OclReal> realSequence2 = new ArrayList<OclReal>();
    realSequence2.add(oclReal0_5);
    realSequence2.add(oclReal0_5);
    realSequence2.add(oclReal1_5);
    realSequence2.add(undefined);

    oclSequence3 =
        myStandardLibraryFactory.createOclSequence(
            realSequence2, EssentialOclPlugin.getOclLibraryProvider().getOclLibrary().getOclReal());
  }
Beispiel #2
0
 /* (non-Javadoc)
  * @see tudresden.ocl20.pivot.pivotmodel.base.AbstractProperty#getType()
  */
 @Override
 public Type getType() {
   Type elementType = this.factory.createType(dslColumn.getType());
   if (dslColumn.getType().getName().contains(" ARRAY")
       || dslColumn.getType().getName().contains("[]")
       || dslColumn.getType().getName().contains("[ ]")) {
     elementType =
         EssentialOclPlugin.getOclLibraryProvider().getOclLibrary().getBagType(elementType);
   }
   return elementType;
 }
/**
 * Tests for sequences in OCL.
 *
 * @author Michael Thiele
 */
public class JavaOclSequenceTest {

  private final IStandardLibraryFactory myStandardLibraryFactory =
      TestPerformer.getInstance().getSLFactory();

  private final OclSequence<OclReal> emptySequence =
      myStandardLibraryFactory.createOclSequence(
          new ArrayList<OclReal>(),
          EssentialOclPlugin.getOclLibraryProvider().getOclLibrary().getOclAny());

  /** Sequence { 0.5 } */
  private OclSequence<OclReal> oclSequence;

  /** Sequence { 0.5, 1.5, null } */
  private OclSequence<OclReal> oclSequence2;

  /** Sequence { 0.5, 0.5, 1.5, null } */
  private OclSequence<OclReal> oclSequence3;

  private final OclReal oclReal0_5 = myStandardLibraryFactory.createOclReal(0.5);
  private final OclReal oclReal1_5 = myStandardLibraryFactory.createOclReal(1.5);

  private final OclInteger integer0 = myStandardLibraryFactory.createOclInteger(0L);
  private final OclInteger integer1 = myStandardLibraryFactory.createOclInteger(1L);
  private final OclInteger integer2 = myStandardLibraryFactory.createOclInteger(2L);
  private final OclInteger integer3 = myStandardLibraryFactory.createOclInteger(3L);
  private final OclInteger integer4 = myStandardLibraryFactory.createOclInteger(4L);

  private final OclReal undefined =
      (OclReal)
          myStandardLibraryFactory.createOclUndefined(
              oclReal0_5.getModelInstanceReal().getType(), "undefined value");

  @Before
  public void init() {

    List<OclReal> realSequence = new ArrayList<OclReal>();
    realSequence.add(oclReal0_5);
    oclSequence =
        myStandardLibraryFactory.createOclSequence(
            realSequence, EssentialOclPlugin.getOclLibraryProvider().getOclLibrary().getOclReal());

    realSequence.add(oclReal1_5);
    realSequence.add(undefined);
    oclSequence2 =
        myStandardLibraryFactory.createOclSequence(
            realSequence, EssentialOclPlugin.getOclLibraryProvider().getOclLibrary().getOclReal());

    List<OclReal> realSequence2 = new ArrayList<OclReal>();
    realSequence2.add(oclReal0_5);
    realSequence2.add(oclReal0_5);
    realSequence2.add(oclReal1_5);
    realSequence2.add(undefined);

    oclSequence3 =
        myStandardLibraryFactory.createOclSequence(
            realSequence2, EssentialOclPlugin.getOclLibraryProvider().getOclLibrary().getOclReal());
  }

  @Test
  public void testAppend() {

    assertTrue(emptySequence.append(oclReal0_5).isEqualTo(oclSequence).isTrue());
    assertTrue(emptySequence.isEmpty().isTrue());

    assertTrue(oclSequence.append(oclReal1_5).append(undefined).isEqualTo(oclSequence2).isTrue());
    assertTrue(oclSequence.size().isEqualTo(integer1).isTrue());
  }

  @Test
  public void testExcluding() {

    assertTrue(oclSequence.excluding(oclReal0_5).isEqualTo(emptySequence).isTrue());
    assertTrue(oclSequence.size().isEqualTo(integer1).isTrue());

    assertTrue(oclSequence.excluding(oclReal1_5).isEqualTo(oclSequence).isTrue());

    assertTrue(
        oclSequence3
            .excluding(oclReal0_5)
            .excluding(undefined)
            .excluding(oclReal1_5)
            .isEqualTo(emptySequence)
            .isTrue());
    assertTrue(oclSequence3.size().isEqualTo(integer4).isTrue());
  }

  @Test
  public void testIncluding() {

    assertTrue(emptySequence.including(oclReal0_5).isEqualTo(oclSequence).isTrue());
    assertTrue(emptySequence.isEmpty().isTrue());

    assertTrue(
        oclSequence.including(oclReal1_5).including(undefined).isEqualTo(oclSequence2).isTrue());
    assertTrue(oclSequence.size().isEqualTo(integer1).isTrue());
  }

  @Test
  public void testInsertAt() {

    assertTrue(emptySequence.insertAt(integer1, oclReal0_5).isEqualTo(oclSequence).isTrue());
    assertTrue(emptySequence.isEmpty().isTrue());
    assertTrue(emptySequence.insertAt(integer0, oclReal0_5).oclIsInvalid().isTrue());
    assertTrue(emptySequence.isEmpty().isTrue());
    assertTrue(
        oclSequence
            .insertAt(integer2, undefined)
            .insertAt(integer2, oclReal1_5)
            .insertAt(integer2, oclReal0_5)
            .isEqualTo(oclSequence3)
            .isTrue());
    assertTrue(oclSequence.size().isEqualTo(integer1).isTrue());
    assertTrue(
        oclSequence
            .insertAt(integer1, oclReal0_5)
            .insertAt(integer4, oclReal1_5)
            .oclIsInvalid()
            .isTrue());
  }

  @Test
  public void testPrepend() {

    assertTrue(emptySequence.prepend(oclReal0_5).isEqualTo(oclSequence).isTrue());
    assertTrue(emptySequence.isEmpty().isTrue());
    assertTrue(oclSequence2.prepend(oclReal0_5).isEqualTo(oclSequence3).isTrue());
    assertTrue(oclSequence2.size().isEqualTo(integer3).isTrue());
  }

  @Test
  public void testSubSequence() {

    assertTrue(emptySequence.subSequence(integer1, integer1).oclIsInvalid().isTrue());
    assertTrue(oclSequence.subSequence(integer1, integer1).isEqualTo(oclSequence).isTrue());
    assertTrue(oclSequence.subSequence(integer0, integer1).oclIsInvalid().isTrue());
    assertTrue(oclSequence3.subSequence(integer2, integer4).isEqualTo(oclSequence2).isTrue());
    assertTrue(oclSequence3.subSequence(integer2, integer1).oclIsInvalid().isTrue());
  }

  @Test
  public void testUnion() {

    assertTrue(oclSequence.union(oclSequence2).isEqualTo(oclSequence3).isTrue());
    assertTrue(oclSequence.size().isEqualTo(integer1).isTrue());
    assertTrue(oclSequence2.size().isEqualTo(integer3).isTrue());

    assertTrue(emptySequence.union(emptySequence).isEqualTo(emptySequence).isTrue());
    assertTrue(emptySequence.union(oclSequence).isEqualTo(oclSequence).isTrue());
    assertTrue(emptySequence.isEmpty().isTrue());
    assertTrue(oclSequence.size().isEqualTo(integer1).isTrue());

    assertTrue(oclSequence.union(emptySequence).isEqualTo(oclSequence).isTrue());
    assertTrue(emptySequence.isEmpty().isTrue());
    assertTrue(oclSequence.size().isEqualTo(integer1).isTrue());
  }
}
/**
 * An {@link ITypeResolver} helps in finding {@link Type}s. These can either be located in the
 * {@link OclLibrary} or in an associated {@link IModel}.
 *
 * @author Matthias Braeuer
 */
public class TypeResolver {

  /** The {@link Logger} for this class. */
  private static final Logger LOGGER = EssentialOclPlugin.getLogger(TypeResolver.class);

  /** The associated {@link OclLibrary}. */
  private OclLibrary oclLibrary;

  /** Cache for the predefined {@link Type}s from the OCL Standard library. */
  private Map<String, Type> predefinedTypes;

  /**
   * Creates a new default {@link TypeResolver}.
   *
   * @param oclLibrary The associated {@link OclLibrary}, must not be <code>null</code>.
   */
  public TypeResolver(OclLibrary oclLibrary) {

    /*
     * Should not happen in the default implementation, but clients may create
     * own type resolvers.
     */
    if (oclLibrary == null) {
      throw new IllegalArgumentException(
          "The reference to the associated OCL standard library must not be null."); //$NON-NLS-1$
    }
    // no else.

    this.oclLibrary = oclLibrary;
  }

  /**
   * Looks up a {@link Type} in the OCL library or in the associated {@link IModel}.
   *
   * @param pathName The path name of the {@link Type} to look for.
   * @return A {@link Type} instance.
   * @throws IllegalArgumentException If the given pathname is either <code>null</code> or empty.
   * @throws ModelAccessException If something goes wrong when accessing the {@link IModel}.
   * @throws TypeNotFoundException If a {@link Type} with the given path name cannot be found.
   * @throws ModelBusException If a general configuration error occurs.
   */
  public Type findType(List<String> pathName, IModel model)
      throws TypeNotFoundException, ModelAccessException {

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("findType(pathName=" + pathName + ") - enter"); // $NON-NLS-1$ //$NON-NLS-2$
    }
    // no else.

    Type type = null;

    if (pathName == null || pathName.isEmpty()) {
      throw new IllegalArgumentException("The path name must not be null or empty."); // $NON-NLS-1$
    }
    // no else.

    /* Try to look up a primitive type. */
    if (pathName.size() == 1) {
      type = this.getPredefinedTypes().get(pathName.get(0));
    }
    // no else.

    /* If no primitive type found, try to look in the oclLibrary. */
    if (type == null) {
      type = model.findType(pathName);
    }
    // no else.

    if (type == null) {
      throw new TypeNotFoundException(
          "Unable to find type '" + pathName + "'."); // $NON-NLS-1$//$NON-NLS-2$
    }
    // no else.

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("findType() - exit - return value=" + type); // $NON-NLS-1$
    }
    // no else.

    return type;
  }

  /** Helper method to initialize the cache with the primitive types. */
  protected void initializePredefinedTypes(OclLibrary oclLibrary) {

    this.predefinedTypes.put(oclLibrary.getOclAny().getName(), oclLibrary.getOclAny());
    this.predefinedTypes.put(oclLibrary.getOclType().getName(), oclLibrary.getOclType());
    this.predefinedTypes.put(oclLibrary.getOclVoid().getName(), oclLibrary.getOclVoid());
    this.predefinedTypes.put(oclLibrary.getOclInvalid().getName(), oclLibrary.getOclInvalid());

    this.predefinedTypes.put(oclLibrary.getOclBoolean().getName(), oclLibrary.getOclBoolean());
    this.predefinedTypes.put(oclLibrary.getOclInteger().getName(), oclLibrary.getOclInteger());
    this.predefinedTypes.put(oclLibrary.getOclReal().getName(), oclLibrary.getOclReal());
    this.predefinedTypes.put(oclLibrary.getOclString().getName(), oclLibrary.getOclString());

    this.predefinedTypes.put(
        oclLibrary.getOclCollection().getName(), oclLibrary.getOclCollection());
    this.predefinedTypes.put(oclLibrary.getOclSequence().getName(), oclLibrary.getOclSequence());
    this.predefinedTypes.put(oclLibrary.getOclBag().getName(), oclLibrary.getOclBag());
    this.predefinedTypes.put(oclLibrary.getOclSet().getName(), oclLibrary.getOclSet());
    this.predefinedTypes.put(
        oclLibrary.getOclOrderedSet().getName(), oclLibrary.getOclOrderedSet());
  }

  /** A helper method to lazily get the OCL standard {@link Type}s. */
  private Map<String, Type> getPredefinedTypes() {

    if (this.predefinedTypes == null) {

      /* Instantiate the map for the primitive types and initialize it. */
      this.predefinedTypes = new HashMap<String, Type>();
      this.initializePredefinedTypes(oclLibrary);
    }

    return this.predefinedTypes;
  }
}