mercredi 23 septembre 2015

How to transform heterogenous flat data to data structure

I am looking for a way to transform flat data to a data structure. The input for this transformation is not homogenous. Some data contain too much info, other data contain info that needs processing.

Let me explain with an example. Suppose I have some Excel files with car data. The files contain info about cars and their engines.

File 1:

Name | Type | EngineId | Manufacturer | Power (hp) | Torque
Opel | Adam | I4       | Opel         | 69         | 115

File 2:

Brand | Type  | Engine | Power (kW) | Manufacturer
Fiat  | Punto | 1.2-L  | 44         | Chrysler    

As you can see, the files differ slightly: Name and Brand for the first column, different units of measure for Power, Manufacturer is at different positions and Torque is missing in File 2.

I'd like to transform this to something like:

public class Car {
    string Name;
    string Type;
    Engine Engine;
}

public class Engine {
    string Id;
    string Manufacturer;
    double Power; 
    Dictionary<string,string> OtherAttributes;
}

I think the transform needs classes for transformation rules as well:

public class MappingRules {
    string FileType; // File 1 vs File 2
    List<MappingRule> MappingRules;
}

public class MappingRule<T> {
    string SourceColumnName;
    string Target;
    ITranslate<T> Translator;
}

interface ITranslate<T> {
    T Convert(T sourceValue);
}

My problem is: how can I achieve this, but even more: how do I research this?

Best way to modularize features in user interface

I am designing a user interface to allow interaction with a force network diagram but am struggling with deciding on the optimal way of implementing features. A feature is a way for the user to interact with the diagram (ie. hide specific nodes, show neighboring nodes, hide edges of a certain type, etc)

My current thought process is that I want features packaged into modules that each include the manipulation on the diagram and the associated UI component (checkbox, slider, etc). This way a feature can be inserted and removed independently of others.

I couldn't find a specific pattern or design practice that follows this logic. Is this a good approach? Thanks

Conditional to polymorphism - a more complicated case

Say I have this well known example:

   double getSpeed() {
       switch (_type) {
           case EUROPEAN:
              return getBaseSpeed();
           case AFRICAN:
              return getBaseSpeed() - getLoadFactor() * _numberOfCoconuts;
           case NORWEGIAN_BLUE:
              return (_isNailed) ? 0 : getBaseSpeed(_voltage);
       }
       throw new RuntimeException ("Should be unreachable");
   }

Obviously, I'd refactor into subclasses and everything will be made right in the world again. But what if I have:

   double getSpeed() {
       switch (_type) {
           case EUROPEAN:
              inform_gary(_count);  
              return getBaseSpeed();
           case AFRICAN:
              increment_package_counter();
              transmit_coordinates(_coordinates);
              return getBaseSpeed() - getLoadFactor() * _numberOfCoconuts;
           case NORWEGIAN_BLUE:              
              return (_isNailed) ? 0 : getBaseSpeed(_voltage);
       }
       throw new RuntimeException ("Should be unreachable");
   }

Now it would be useless to subclass because then I'd have to tightly couple the subclasses to code they shouldn't know about. Is there a solution to this problem?

design pattern from UML

I would like to know if someone sees a design pattern I could applicate in the class diagram present on this link

Thank you very much, adrien

Does this follow abstract factory pattern

As per defination "Design Patterns: Abstract Factory". informIT. Archived from the original on 2009-10-23. Retrieved 2012-05-16. Object Creational: Abstract Factory: Intent: Provide an interface for creating families of related or dependent objects without specifying their concrete classes."

Below is my try at abstract design pattern. This is my first hands on factory method. Can anyone please help me on this.

enum ProductType
{
    BeautySoap = 1,
    DetergentSoap = 2,
    HairWax = 3,
    BodyWax = 4,
}
interface ISoap
{
    string Create(string name);

}
interface IWax
{
    string Create(string name);
}

public class HarWax : IWax
{
    public string Create(string name)
    {
        return string.Format("Hair Wax {0} created", name);
    }
}

public class BodyWax : IWax
{

    public string Create(string name)
    {
        return string.Format("Body wax {0} created", name);
    }
}
public class BeautySoapFactory : ISoap
{
    public string Create(string name)
    {
         return string.Format("Toilet soap {0} created", name);
    }

}
public class DetergentSoapFactory : ISoap
{
    public string Create(string name)
    {
         return string.Format("Detergent bar {0} created", name);
    }
}



//factory of factories(Bike Factory, Scooter Factory)
/// <summary>
/// The 'AbstractFactory' interface. 
/// </summary>
interface IBeautyProduct
{
    ISoap CreateSoap(ProductType type);
    IWax CreateWax(ProductType type);
}

class HULFactory : IBeautyProduct
{
    public ISoap CreateSoap(ProductType type)
    {
        switch (type)
        { 
            case ProductType.BeautySoap:
                return new BeautySoapFactory();
            case ProductType.DetergentSoap:
                return new DetergentSoapFactory();
            default:
                throw new ApplicationException(string.Format("Soap '{0}' cannot be created", type));
                //Console.WriteLine(string.Format("Soap '{0}' cannot be created", name));
                //break;
        }
    }
    public IWax CreateWax(ProductType type)
    {
        switch (type)
        {
            case ProductType.HairWax:
                return new HarWax();
            case ProductType.BodyWax:
                return new BodyWax();
            default:
                throw new ApplicationException(string.Format("Wax '{0}' cannot be created", type));
        }
    }
}

class LotusherbalsFactory : IBeautyProduct
{
    public ISoap CreateSoap(ProductType type)
    {
        switch (type)
        {
            case ProductType.BeautySoap:
                return new BeautySoapFactory();
            case ProductType.DetergentSoap:
                return new DetergentSoapFactory();
            default:
                throw new ApplicationException(string.Format("Soap '{0}' cannot be created", type));
            //Console.WriteLine(string.Format("Soap '{0}' cannot be created", name));
            //break;
        }
    }
    public IWax CreateWax(ProductType type)
    {
        switch (type)
        {
            case ProductType.HairWax:
                return new HarWax();
            case ProductType.BodyWax:
                return new BodyWax();
            default:
                throw new ApplicationException(string.Format("Wax '{0}' cannot be created", type));
        }
    }
}

for-loop + Arrow Anti Pattern VS for-loop + continue

Do you think the following fragment of code (for-loop + continue)

for (Identity fileIdentity : fileIdentities) {
      totalFileCount++;
      if (!forceTheStatus(params, fileIdentity))
        continue;
      updatedFileCount++;
      if (!params.shouldUpdateLinkedEntity())
        continue;
      Optional<Identity> batchIdentity = getLinkedBatchIdentity(fileIdentity);
      if (!batchIdentity.isPresent())
        continue;
      totalBatchCount++;
      if (!getRemittanceProcessor().forceTheStatus(params, batchIdentity.get()))
        continue;
      updatedBatchCount++;
}

is better than this other (for-loop + Arrow Anti Pattern)? And why?

for (Identity fileIdentity : fileIdentities) {
      totalFileCount++;
      if (forceTheStatus(params, fileIdentity)) {
        updatedFileCount++;
        if (params.shouldUpdateLinkedEntity()) {
          Optional<Identity> batchIdentity = getLinkedBatchIdentity(fileIdentity);
          if (batchIdentity.isPresent()) {
            totalBatchCount++;
            if (getRemittanceProcessor().forceTheStatus(params, batchIdentity.get()))
              updatedBatchCount++;
          }
        }
      }
}

To me the solution with continue looks harder to understand, but on the other hand we have an anti-pattern :(