Tuesday, December 27, 2011

Introduction to Artificial Intelligence

Introduction to AI
Some time ago I have joined to Introduction to Artificial Intelligence  it is online course that is based on Stanford CS221, Introduction to Artificial Intelligence.
This class introduced students to the basics of Artificial Intelligence, which included machine learning, probabilistic reasoning, robotics, and natural language processing.
My reason to join this class was simple I wanted to know more. I took 82% it isn't so bad for not native speaker.
If you want to get more you can join a lot of next online courses - http://www.class-central.com/

Tuesday, August 16, 2011

Trouble with resources in WP7

I started a few projects on WP7. It was done just for fun and for improving my skills (Fun was profitable - I won in Ukrainian WP7 competition). So I had completed my projects and tried to localize it.
I met some issues:
  1. If you have experience in developing for web silverlight - first isn't new for you. When I edit/add/remove a item in resources's file I need to open resources designer's file and change access modifiers of parameterless constructor from "internal" to "public".
    It is know issue and know fix - proof.
  2. Second was new for me. It is strange issue because it appeared only in one of my projects. I created resources file and added key in Resources section of app.xaml (for binding to resources in my xaml files). My project started to crash after this.
    I had spent some time and found source of the issue. I noticed that Resource class (auto generated) is internal. I checked "Custom tool" field in properties of resource file. It was filled "ResXFileCodeGenerator" value. In my other projects this field contained "PublicResXFileCodeGenerator" value. I didn't found detailed description of "ResXFileCodeGenerator" but looks like that it is obsolete tool. I no idea how to it was set in my project.
One more note: it is for WP7 without "Mango" update. I hope I will not meet such issues after this update.

Monday, February 7, 2011

Trimmed TextBlock for Windows Phone 7.

Unfortunately WP7 is based on Silverlight 3. So we can't use some nice features of Silverlight 4 i.e. dynamic key word or TextBlock's trimming. But I've solved issue with trimming for Silverlight 3. Solution is here. Let me explain which code I use in WP7 project:
  • Generic.xaml: File contains style and template for custom control. This file is located in Themes folder. WP7 tools doesn't contains Resources.xaml file template. So I created xml file and name it as "Generic.xaml".
  • TrimmedTextBlock.cs: It is implementation of custom control. I just copied this file. One more note: I decrement c_POINTS_COUNT to "1" because screen resolution of desktop and mobile versions are different.
Easy coding.

Wednesday, December 15, 2010

"using" directive in aspx's pages.

It is funny but I don't know about this. I used full name of type (namespace+type name) in aspx pages. It is unnecessary. ASP.NET has directive for this.
<%@ Import namespace="value" %>
Sometime we omit in haste important things.

UPD: And more easy for Razor view engine:
@using MyNamespace

Monday, November 15, 2010

ASP.NET MVC aplication inside in an iframe (fix issue with empty session).

I developed application using ASP.NET MVC framework. It worked well but when I put my app inside of iframe I founded that in some case in Internet Explorer application missed session.
I started to investigate for the source of the issue.
I founded that application worked well if I decrease security level in the IE. But this fix weren’t suitable for me (I can`t request something in the customers). So I carried on with investigation.
Well description what happen I founded on the follow page (unfortunately, I didn’t found English version of this page, so I used google translate for figure out this article).
So for fix this I should add “PSP” header to response. How can I do it for the ASP.NET MVC application? In classic ASP.NET it is easy to do in code behind or in aspx file. I overrode ExecuteResult method in all classes inherited from ActionResult that used in my application. ViewResult for example:
public class CustomViewResult : ViewResult
{
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.AddHeader("P3P", "CP=\"CAO PSA OUR\"");
base.ExecuteResult(context);
}
}

After it I used my customized classes in base controller of all controllers in application (It was very nice idea to implement one base controller for all other controllers, wasn't it?). For example:
public class BaseController : Controller
{
protected override ViewResult View(string viewName, string masterName, object model)
{
if (model != null)
{
base.ViewData.Model = model;
}

return new CustomViewResult
{
ViewName = viewName,
MasterName = masterName,
ViewData = base.ViewData,
TempData = base.TempData
};
}
}

For figure out which methods I need to override I used cool tools - .NET Reflector.

Thursday, September 16, 2010

Unit Tests for ASP.NET MVC application that uses resources in code behind.

Download source files - here.

Some time ago I start to play with ASP.NET MVC. It is very well pattern and Microsoft made nice choose that implemented it for ASP.NET in my opinion.
I started a project that uses ASP.NET MVC. This project requests multilingual support. I use resources for this. It works well when I use resource's properties in the View's layer but when I call resource's properties from code behind. It's something likes to next:
public ActionResult Index()
{
ViewData["Message"] = CommonResource.WELCOME;
return View();
}
It works but I pay attention that this code breaks my Unit Tests. My Unit Tests looks like:
[TestMethod]
public void Index_Test()
{
//// Arrange
var controller = new HomeController();

// Act
var result = controller.Index();

// Assert
Assert.IsInstanceOfType(result, typeof(ViewResult));
}
It happens because I run my code in other project and my ResourceManager is not filled (it equals to null). How can I fix it? A first idea that came to mind is "extract interface".. but it is very annoying because resource's class is auto-generate and I should update this code after each updating of resources.. I made some research and found additional way for extracting resources - HttpContext contains GetGlobalResourceObject method! It is well way because I can set mock for HttpContext and for any its methods or properties.

Note: GetGlobalResourceObject returns object but I use only strings for localization. So I create linq method for HttpContext that returns string if it presents:
public static string GetGlobalResource(
this HttpContextBase httpContext
, string classKey, string resourceKey)
{
var result = httpContext.GetGlobalResourceObject
(classKey, resourceKey);
return null != result
? result.ToString() : string.Empty;
}
Pay attention, MissingManifestResourceException is possible in this code if you pass incorrect key/keys!

So I change pieces of code in Controller's classes in the next manner:
public ActionResult Index()
{
ViewData[CommonStrings.MESSAGE] = HttpContext
.GetGlobalResource(CommonStrings
.COMMON_RESOURCE, CommonStrings.WELCOME);
return View();
}
After it I should change my Unit Test's methods in next manner:
[TestMethod]
public void Index_Test()
{
//// Arrange
var httpContextMock = MockRepository
.GenerateMock<HttpContextBase>();
httpContextMock.Stub
(x => x.GetGlobalResource(
CommonStrings.COMMON_RESOURCE, CommonStrings.WELCOME))
.Return(CommonStrings.WELCOME);

var controller = new HomeController();
var context = new ControllerContext
(httpContextMock, new RouteData(), controller);
controller.ControllerContext = context;

// Act
var result = controller.Index();

// Assert
Assert.IsInstanceOfType(result, typeof(ViewResult));
ViewDataDictionary viewData = ((ViewResult)result).ViewData;
Assert.AreEqual(CommonStrings.WELCOME
, viewData[CommonStrings.MESSAGE]);
}
Moreover, you can see that I added additional checking related to localization.

Wednesday, March 24, 2010

Trimmed TextBlock for Silverlight.

Download source files - here

Not always Silverlight TextBlock that contains long text has enough space for shows all this text. TextBlock clips text in this case - this behaviour isn't best way for this. Because I wait that TextBlock not only trims but also adds "..." to clipped text. WPF has brilliant property that helps to resolve this trouble - TextTrimming. I implemented this possibility for Silverlight and now want to present it for you. Furthermore I implemented ToolTip that shows trimmed text (only if text is trimmed!)
Enjoy!

UPD: I have just updated performance and behaviour when width defined zero.