If you have a look at the [entity relationship diagram (ERD)][1] which illustrates relationships between the Salesforce Knowledge objects, you will see that the [Vote][2] object is related with the KnowledgeArticle. So to create a rating with Apex, you will simply have to create a new record of VOTE sObject and set the parentId as the KnowledgeArticleId and the Type as the rating score i.e. 1,2,3,4 or 5. 

  [1]: https://www.salesforce.com/developer/docs/api/Content/sforce_api_guidelines_knowledge.htm
  [2]: https://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_vote.htm