Search in sources :

Example 16 with Card

use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.

the class LearnQueuingManagerTest method testGetNewCardQueuingWithoutCategory.

@SmallTest
@Test
public void testGetNewCardQueuingWithoutCategory() throws Exception {
    QueueManager queueManager = new LearnQueueManager.Builder(getContext(), TestHelper.SAMPLE_DB_PATH).setLearnQueueSize(10).setFilterCategory(null).setCacheSize(50).build();
    Card cqueue = queueManager.dequeue();
    assertEquals(1, (int) cqueue.getId());
    queueManager.release();
}
Also used : LearnQueueManager(org.liberty.android.fantastischmemo.queue.LearnQueueManager) QueueManager(org.liberty.android.fantastischmemo.queue.QueueManager) Card(org.liberty.android.fantastischmemo.entity.Card) Test(org.junit.Test) SmallTest(android.support.test.filters.SmallTest) AbstractExistingDBTest(org.liberty.android.fantastischmemo.test.AbstractExistingDBTest) SmallTest(android.support.test.filters.SmallTest)

Example 17 with Card

use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.

the class QuizQueuingManagerTest method testUpdateCard.

@SmallTest
@Test
public void testUpdateCard() throws Exception {
    Scheduler mockScheduler = mock(Scheduler.class);
    // Create a quiz start the quiz size 3 from card 5
    QueueManager queueManager = new QuizQueueManager.Builder().setDbOpenHelper(helper).setQuizSize(3).setStartCardOrd(5).setScheduler(mockScheduler).build();
    Card card5 = queueManager.dequeue();
    queueManager.remove(card5);
    assertEquals(5, (int) card5.getOrdinal());
    when(mockScheduler.isCardLearned(card5.getLearningData())).thenReturn(true);
    queueManager.update(card5);
    Card card6 = queueManager.dequeue();
    queueManager.remove(card6);
    assertEquals(6, (int) card6.getOrdinal());
    when(mockScheduler.isCardLearned(card6.getLearningData())).thenReturn(false);
    queueManager.update(card6);
    Card card7 = queueManager.dequeue();
    queueManager.remove(card7);
    assertEquals(7, (int) card7.getOrdinal());
    when(mockScheduler.isCardLearned(card7.getLearningData())).thenReturn(true);
    queueManager.update(card7);
    // Now cards in the queue is the card we failed
    // and we succeed this time
    Card card6Again = queueManager.dequeue();
    queueManager.remove(card6Again);
    assertEquals(6, (int) card6Again.getOrdinal());
    when(mockScheduler.isCardLearned(card6.getLearningData())).thenReturn(true);
    queueManager.update(card6Again);
    // No cards in the queue
    Card nullCard = queueManager.dequeue();
    assertNull(nullCard);
}
Also used : Scheduler(org.liberty.android.fantastischmemo.scheduler.Scheduler) QuizQueueManager(org.liberty.android.fantastischmemo.queue.QuizQueueManager) QuizQueueManager(org.liberty.android.fantastischmemo.queue.QuizQueueManager) QueueManager(org.liberty.android.fantastischmemo.queue.QueueManager) Card(org.liberty.android.fantastischmemo.entity.Card) Test(org.junit.Test) SmallTest(android.support.test.filters.SmallTest) AbstractExistingDBTest(org.liberty.android.fantastischmemo.test.AbstractExistingDBTest) SmallTest(android.support.test.filters.SmallTest)

Example 18 with Card

use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.

the class QuizQueuingManagerTest method testFilterByGroup.

@SmallTest
@Test
public void testFilterByGroup() throws Exception {
    // Create a quiz start the quiz size 3 from card 5
    QueueManager queueManager = new QuizQueueManager.Builder().setDbOpenHelper(helper).setQuizSize(3).setStartCardOrd(5).build();
    Card card5 = queueManager.dequeue();
    queueManager.remove(card5);
    assertEquals(5, (int) card5.getOrdinal());
    Card card6 = queueManager.dequeue();
    queueManager.remove(card6);
    assertEquals(6, (int) card6.getOrdinal());
    Card card7 = queueManager.dequeue();
    queueManager.remove(card7);
    assertEquals(7, (int) card7.getOrdinal());
    // No cards in the queue
    Card nullCard = queueManager.dequeue();
    assertNull(nullCard);
}
Also used : QuizQueueManager(org.liberty.android.fantastischmemo.queue.QuizQueueManager) QueueManager(org.liberty.android.fantastischmemo.queue.QueueManager) Card(org.liberty.android.fantastischmemo.entity.Card) Test(org.junit.Test) SmallTest(android.support.test.filters.SmallTest) AbstractExistingDBTest(org.liberty.android.fantastischmemo.test.AbstractExistingDBTest) SmallTest(android.support.test.filters.SmallTest)

Example 19 with Card

use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.

the class Mnemosyne2CardsExporter method createXMLFile.

private void createXMLFile(String dbPath, File xmlFile) throws IOException {
    AnyMemoDBOpenHelper helper = null;
    PrintWriter outXml = null;
    try {
        helper = AnyMemoDBOpenHelperManager.getHelper(dbPath);
        CardDao cardDao = helper.getCardDao();
        CategoryDao categoryDao = helper.getCategoryDao();
        LearningDataDao learningDataDao = helper.getLearningDataDao();
        int cardCount = (int) cardDao.countOf();
        outXml = new PrintWriter(new BufferedWriter(new FileWriter(xmlFile)));
        outXml.printf("<openSM2sync number_of_entries=\"%d\">\n", cardCount);
        // First card tags (categories)
        Map<String, String> categoryOidMap = new HashMap<String, String>();
        Map<Integer, String> cardIdOidMap = new HashMap<Integer, String>(cardCount * 4 / 3);
        Iterator<Category> categoryIterator = categoryDao.iterator();
        while (categoryIterator.hasNext()) {
            Category category = categoryIterator.next();
            String tagName = "__UNTAGGED__";
            String oId = generateOid();
            if (!Strings.isNullOrEmpty(category.getName())) {
                tagName = category.getName();
            }
            categoryOidMap.put(tagName, oId);
            outXml.printf("<log type=\"10\" o_id=\"%s\"><name>%s</name></log>\n", oId, AMStringUtils.encodeXML(tagName));
        }
        // Then cards
        Iterator<Card> cardIterator = cardDao.iterator();
        while (cardIterator.hasNext()) {
            Card card = cardIterator.next();
            String front = card.getQuestion();
            String back = card.getAnswer();
            String oId = generateOid();
            cardIdOidMap.put(card.getId(), oId);
            outXml.printf("<log type=\"16\" o_id=\"%s\"><b>%s</b><f>%s</f></log>\n", oId, AMStringUtils.encodeXML(back), AMStringUtils.encodeXML(front));
        }
        // Then learningData
        // <log card_t="1" fact_v="1.1" e="2.5" gr="-1" tags="5SfWDFGwqrlnGLDQxHHyG0" rt_rp_l="0" lps="0" l_rp="-1" n_rp="-1" ac_rp_l="0" rt_rp="0" ac_rp="0" type="6" o_id="7IXjCysHuCDtXo8hlFrK55" fact="7xmRCBH0WP0DZaxeFn5NLw"></log>
        Iterator<Card> cardIterator2 = cardDao.iterator();
        while (cardIterator2.hasNext()) {
            Card card = cardIterator2.next();
            categoryDao.refresh(card.getCategory());
            learningDataDao.refresh(card.getLearningData());
            String fact = cardIdOidMap.get(card.getId());
            String category = card.getCategory().getName();
            if (Strings.isNullOrEmpty(category)) {
                category = "__UNTAGGED__";
            }
            String tags = categoryOidMap.get(category);
            String oId = generateOid();
            // Needs to converter to unix time
            LearningData learningData = card.getLearningData();
            long l_rp = learningData.getLastLearnDate().getTime() / 1000;
            long n_rp = learningData.getNextLearnDate().getTime() / 1000;
            outXml.printf("<log card_t=\"1\" fact_v=\"1.1\" e=\"%f\" gr=\"%d\" tags=\"%s\" rt_rp_l=\"%d\" lps=\"%d\" l_rp=\"%d\" n_rp=\"%d\" ac_rp_l=\"%d\" rt_rp=\"%d\" ac_rp=\"%d\" type=\"6\" o_id=\"%s\" fact=\"%s\"></log>\n", learningData.getEasiness(), learningData.getGrade(), tags, learningData.getRetRepsSinceLapse(), learningData.getLapses(), l_rp, n_rp, learningData.getAcqRepsSinceLapse(), learningData.getRetReps(), learningData.getAcqReps(), oId, fact);
        }
        outXml.print("</openSM2sync>\n");
    } finally {
        if (helper != null) {
            AnyMemoDBOpenHelperManager.releaseHelper(helper);
        }
        if (outXml != null) {
            outXml.close();
        }
    }
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) CategoryDao(org.liberty.android.fantastischmemo.dao.CategoryDao) HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) LearningDataDao(org.liberty.android.fantastischmemo.dao.LearningDataDao) BufferedWriter(java.io.BufferedWriter) Card(org.liberty.android.fantastischmemo.entity.Card) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) PrintWriter(java.io.PrintWriter)

Example 20 with Card

use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.

the class Mnemosyne2CardsImporter method convert.

@Override
public void convert(String src, String dest) throws Exception {
    // Make the tmp directory tmp/[src file name]/
    String srcFilename = FilenameUtils.getName(src);
    File tmpDirectory = new File(AMEnv.DEFAULT_TMP_PATH + srcFilename);
    FileUtils.deleteDirectory(tmpDirectory);
    FileUtils.forceMkdir(tmpDirectory);
    AnyMemoDBOpenHelper helper = null;
    try {
        // First unzip the file since cards is just a zip archive
        // Example content of cards
        // $ ls
        // METADATA  cards.xml  musicnotes
        AMZipUtils.unZipFile(new File(src), tmpDirectory);
        // Make sure the XML file exists.
        File xmlFile = new File(tmpDirectory + "/cards.xml");
        if (!xmlFile.exists()) {
            throw new Exception("Could not find the cards.xml after extracting " + src);
        }
        List<Card> cardList = xmlToCards(xmlFile);
        if (!new File(dest).exists()) {
            amFileUtil.createDbFileWithDefaultSettings(new File(dest));
        }
        helper = AnyMemoDBOpenHelperManager.getHelper(dest);
        CardDao cardDao = helper.getCardDao();
        cardDao.createCards(cardList);
        // The last step is to see if there are images to import.
        Collection<File> imageFiles = FileUtils.listFiles(tmpDirectory, new SuffixFileFilter(new String[] { "jpg", "png", "bmp" }, IOCase.INSENSITIVE), DirectoryFileFilter.DIRECTORY);
        if (!imageFiles.isEmpty()) {
            String destDbName = FilenameUtils.getName(dest);
            File imageDir = new File(AMEnv.DEFAULT_IMAGE_PATH + destDbName);
            FileUtils.forceMkdir(imageDir);
            for (File imageFile : imageFiles) {
                FileUtils.copyFileToDirectory(imageFile, imageDir);
            }
        }
    } finally {
        if (helper != null) {
            AnyMemoDBOpenHelperManager.releaseHelper(helper);
        }
        FileUtils.deleteDirectory(tmpDirectory);
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) SuffixFileFilter(org.apache.commons.io.filefilter.SuffixFileFilter) File(java.io.File) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Card(org.liberty.android.fantastischmemo.entity.Card)

Aggregations

Card (org.liberty.android.fantastischmemo.entity.Card)95 CardDao (org.liberty.android.fantastischmemo.dao.CardDao)61 SmallTest (android.support.test.filters.SmallTest)43 Test (org.junit.Test)43 AbstractExistingDBTest (org.liberty.android.fantastischmemo.test.AbstractExistingDBTest)41 Category (org.liberty.android.fantastischmemo.entity.Category)30 AnyMemoDBOpenHelper (org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper)28 LearningData (org.liberty.android.fantastischmemo.entity.LearningData)25 CategoryDao (org.liberty.android.fantastischmemo.dao.CategoryDao)20 LearningDataDao (org.liberty.android.fantastischmemo.dao.LearningDataDao)13 File (java.io.File)12 ArrayList (java.util.ArrayList)11 SQLException (java.sql.SQLException)9 QueueManager (org.liberty.android.fantastischmemo.queue.QueueManager)9 IOException (java.io.IOException)6 Date (java.util.Date)6 LearnQueueManager (org.liberty.android.fantastischmemo.queue.LearnQueueManager)6 URL (java.net.URL)5 FileWriter (java.io.FileWriter)4 Scheduler (org.liberty.android.fantastischmemo.scheduler.Scheduler)4