Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package nextrek.minstrek.core;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map.Entry;

import android.content.Context;
import android.util.Log;

public class BookModel {

    private static final String TAG = "BookModel";
    String bookName;
    PageModelList pages;
    FlagList events;
    FlagList objects;

    public BookModel(String bookName, PageModelList pages, FlagList events, FlagList objects) {
        this.bookName = bookName;
        this.pages = pages;
        this.events = events;
        this.objects = objects;
    }

    public PageModel getPage(String pageName) {
        return pages.get(pageName.toLowerCase(Locale.ITALIAN));
    }

    public boolean verifyPrecondition(String expression) {

        if (expression == null) {
            // Log.d(TAG, "verifyPrecondition: empty expression [ NULL ] -> pass");
            return true;
        }

        // " Oggetto(diar, True) " -> "Oggetto(diar,True)"
        expression = expression.replaceAll(" ", "");

        if (expression.length() < 3) {
            // Log.d(TAG, "verifyPrecondition: empty expression [ \"\" ] -> pass");
            return true;
        }

        // "Oggetto(diar,True)" -> "oggetto(diar,true)"
        expression = expression.toLowerCase(Locale.ITALIAN);

        Log.d(TAG, "verifyPrecondition: [" + expression + "]");

        // oggetto(diar,true)&oggetto(biglietto,true)&evento(litigio,true)
        String[] conditions = expression.split("&");

        boolean finalResult = true;
        for (int i = 0; i < conditions.length; i++) {
            String condition = conditions[i];

            // "evento(cell,true)" -> "evento,cell,true"
            String csv = condition.replace("(", ",").replace(")", "");
            // "evento,cell,true" -> ["evento","cell","true"]
            String[] TKV = csv.split(",");
            String flagCategory = TKV[0];
            String flagName = TKV[1];
            boolean flagValue = Boolean.valueOf(TKV[2]);

            FlagList list = null;
            if (flagCategory.equalsIgnoreCase("evento")) {
                list = events;
            }
            if (flagCategory.equalsIgnoreCase("oggetto")) {
                list = objects;
            }
            if (list == null) {
                Log.e(TAG, flagCategory + " * DOES NOT EXISTS *");
                return false;
            }

            boolean oldValue = list.isTrue(flagName);
            boolean partialResult = (oldValue == flagValue);

            Log.d(TAG, "partialResult: [" + flagCategory + "." + flagName + "=" + flagValue + "] == " + oldValue + " -> " + (partialResult ? "pass" : "FAIL"));

            finalResult &= partialResult;
            /*
             * // for performance reason we can quit here.. but for debug is better to dump all expressions
             * if (!finalResult) {
             * break;
             * }
             */
        }

        Log.d(TAG, "  finalResult: [" + finalResult + "]");

        return finalResult;
    }

    public void performPostCondition(String expression) {

        if (expression == null) {
            // Log.e(TAG, "performPostCondition: [ NULL ]");
            return;
        }

        // " Oggetto(diar, True) " -> "Oggetto(diar,True)"
        expression = expression.replaceAll(" ", "");

        if (expression.length() < 3) {
            // Log.e(TAG, "performPostCondition: inconsistent expression [ " + expression + " ]");
            return;
        }

        // "Oggetto(diar,True)" -> "oggetto(diar,true)"
        expression = expression.toLowerCase(Locale.getDefault());

        Log.d(TAG, "performPostCondition: [" + expression + "]");

        // evento(cell,false)
        String[] TKV = expression.replace("(", ",").replace(")", "").split(",");
        String flagCategory = TKV[0];
        String flagName = TKV[1];
        boolean newValue = Boolean.valueOf(TKV[2]);

        FlagList list = null;
        if (flagCategory.equalsIgnoreCase("evento")) {
            list = events;
        }
        if (flagCategory.equalsIgnoreCase("oggetto")) {
            list = objects;
        }
        if (list == null) {
            Log.e(TAG, flagCategory + " * DOES NOT EXISTS *");
            return;
        }

        Boolean oldValue = list.change(flagName, newValue);
        if (oldValue == null) {
            Log.e(TAG, "performPostCondition: [" + flagCategory + "." + flagName + "] is UNDEFINED !!");
        }
        Log.d(TAG, "performPostCondition: [" + flagCategory + "." + flagName + "] " + oldValue + " -> " + newValue);
    }

    public String getName() {
        return bookName;
    }

    public FlagList getEvents() {
        return this.events;
    }

    public FlagList getObjects() {
        return this.objects;
    }

    public void verifyImages(Context context) {
        String bookName = null;
        try {
            String[] fileList = context.getAssets().list("book");
            if (fileList.length > 0) {
                bookName = "book" + File.separatorChar + fileList[0];
                Log.d(TAG, "book -> " + bookName);
            }

            String[] imagesList = context.getAssets().list(bookName + File.separatorChar + "images");
            Log.d(TAG, "imagesList -> " + imagesList);
            HashMap<String, String> imagesFS = new HashMap<String, String>(1024);
            for (String imagefilename : imagesList) {
                String oldValue = imagesFS.put(imagefilename.toLowerCase(Locale.ENGLISH), imagefilename);
                if (oldValue != null) {
                    // immagine con problemi di case
                    Log.e(TAG, "mv \"" + oldValue + "\" \"check/" + oldValue + "\"");
                    Log.e(TAG, "mv \"" + imagefilename + "\" \"check/" + imagefilename + "\"");
                }
            }

            HashSet<String> imagesNeededByBook = new HashSet<String>(1024);
            for (Entry<String, PageModel> entry : pages.entrySet()) {
                PageModel page = entry.getValue();
                imagesNeededByBook.add(page.getTopImage());
            }

            for (String img : imagesNeededByBook) {
                // find read name
                String realName = imagesFS.get(img);
                if (realName == null) {
                    // immagine non trovata!!
                    Log.e(TAG, "echo \"" + img + "\"");
                } else {
                    Log.e(TAG, "mv \"" + realName + "\" \"good/" + img + "\"");
                }
            }
        } catch (IOException e1) {
            e1.printStackTrace();
            return;
        }
    }

    public void shutdown() {
        if (pages != null) {
            pages.clear();
            pages = null;
        }
        
        if (events != null) {
            events.clear();
            events = null;
        }
        
        if (objects != null) {
            objects.clear();
            events = null;
        }
    }
}

Commits for Nextrek/Android/Minstrek/MinstrekLib/src/nextrek/minstrek/core/BookModel.java

Diff revisions: vs.
Revision Author Commited Message
11 Diff Diff DRuega picture DRuega Mon 03 Jun, 2013 19:58:21 +0000

Allineamento repository con ultime modifiche

4 FMMortaroli picture FMMortaroli Fri 19 Apr, 2013 16:54:38 +0000