I am exporting data from my AnyLogic model by writing/apending each run a text file from data that is being saved to the internal database. I use the below code in the Experiment's After simulation run field:
// create header
file.println("id"+","+"replicate"+","+"diagnostic_pathway_duration"+","+"total_cost_patient"+","+
"referred_gh"+","+"referred_th"+","+"tx_gh"+","+"tx_th"+","+"tx_ah"+","+"arrival_gh"+","+
"complete_gh"+","+"arrival_th"+","+"complete_th"+","+"arrival_ah"+","+"complete_ah"+","+
"pathway_concluded");
// Write data from dbase table
List<Tuple> rows = selectFrom(patient_export).list();
for (Tuple row : rows) {
file.println( row.get( patient_export.id ) + "," +
row.get( patient_export.replicate ) + "," +
row.get( patient_export.diagnostic_pathway_duration ) + "," +
row.get( patient_export.total_cost_patient ) + "," +
row.get( patient_export.referred_gh ) + "," +
row.get( patient_export.referred_th ) + "," +
row.get( patient_export.tx_gh ) + "," +
row.get( patient_export.tx_th ) + "," +
row.get( patient_export.tx_ah ) + "," +
row.get( patient_export.arrival_gh ) + "," +
row.get( patient_export.complete_gh ) + "," +
row.get( patient_export.arrival_th ) + "," +
row.get( patient_export.complete_th ) + "," +
row.get( patient_export.arrival_ah ) + "," +
row.get( patient_export.complete_ah ) + "," +
row.get( patient_export.pathway_concluded ));
}
file.close();
deleteFrom(patient_export).execute();
In the initial experiment setup field I included this code:
deleteFrom(patient_export).execute();
However, after a while no more simulation runs are being executed. The model just does not progress any more, and the CPU is not being taxed. In one instance, the experiment stopped simulating after 45 iterations, which resulted in a text file of 210 mb with approximately 1.1million rows.
I am saving the data to the internal database by triggering a function in Patient using an event. This is the code:
insertInto(patient_export)
.columns(patient_export.iteration, patient_export.replicate, patient_export.id, patient_export.diagnostic_pathway_duration,
patient_export.total_cost_patient, patient_export.referred_gh, patient_export.referred_th, patient_export.tx_gh,
patient_export.tx_th, patient_export.tx_ah, patient_export.arrival_gh, patient_export.complete_gh, patient_export.arrival_th,
patient_export.complete_th, patient_export.arrival_ah, patient_export.complete_ah, patient_export.pathway_concluded
) //database tables
.values(main.v_iteration, main.v_replicate, p_patientId, p_diagnostic_pathway_duration, p_totalCost, p_referred_GH, p_referred_TH,
p_Tx_GH, p_Tx_TH, p_Tx_AH, p_timeArrivedDxGH, p_timeCompletedDxGH, p_timeArrivedDxTH, p_timeCompletedDxTH, p_timeArrivedDxAH,
p_timeCompletedDxAH, p_diagnosticPathwayConcluded
.execute();
What am I doing wrong here? Is the internal database playing up? All help is much appreciated.
Compact the base on close:
See help article under Properties: https://help.anylogic.com/index.jsp?topic=%2Fcom.anylogic.help%2Fhtml%2Fconnectivity%2Fdatabase.html&resultof=%22%64%61%74%61%62%61%73%65%22%20%22%64%61%74%61%62%61%73%22%20
Related
the error is:
Compilation error. Line 13: 'range' cannot be used as a variable or
function name.
//#version=5
indicator('My Script')
// 计算振幅
range = high - low
// 定义信息框显示的内容
info = "开盘价: " + tostring(open) + "\n"
+ "最高价: " + tostring(high) + "\n"
+ "最低价: " + tostring(low) + "\n"
+ "振幅: " + tostring(range) + "\n"
+ "成交量: " + tostring(volume)
// 定义当鼠标悬停在当前k线时显示信息框
bgcolor(not nz(bgcolor[1]) and barstate.ishovered, color.new(color.red, 50))
// 当鼠标悬停在当前k线时,显示信息框
label.new(bar_index, high, text=info, xloc=xloc.bar_time, yloc=yloc.abovebar
Your code is full of error.
For the range problem, just change :
range = high - low
To :
myrange = high - low
I have an object constructor that sets some properties, and later uses them to concatenate a string to write to the DOM. I can see that this works in some cases, but not in others.
function Fighter(name, level, attackPts, defencePts, imgSource) {
// TRUNCATED PROPERTY ASSIGNMENTS AREA:
this.attackPts = attackPts;
this.defencePts = defencePts;
// DOM ELEMENT CONSTRUCTORS:
this.img = "style='background: #444 url(" + imgSource + ") no-repeat center'";
this.char_card_name = "<h3>" + this.name + "</h3>";
this.char_card_hitdef = "<h4>" + this.attackPts + "/" + this.defencePts + "</h4>";
this.char_card = "<div class='fighter " + faction + "' " + "id='" + this.name + "'>" + this.img + this.char_card_name + this.char_card_hitdef + "</div>";
In a game function later on I modify the attack and defense points for this "Fighter" object, and that works as expected, and my console.log() tests verify that the concatenated properties also update . . . . up until the final string to pull it all together and display:
this.char_card = "<div class='fighter " + faction + "' " + "id='" + this.name + "'>" + this.img + this.char_card_name + this.char_card_hitdef + "</div>";
When I log this property, those attack and defense numbers don't budge, even though they update successfully in the previous property, this.char_card_hitdef
What could I be overlooking here? I crawled all over the web looking for scope or variable reference issues, but my log statements bring me right back to this one pinching point.
Because you are still in the constructor, you need to refer to the variables without this. that are a parameter AND property of the Object Fighter.
So change
this.char_card = "<div class='fighter " + faction + "' " + "id='" + this.name + "'>" + this.img + this.char_card_name + this.char_card_hitdef + "</div>";
to
this.char_card = "<div class='fighter " + faction + "' " + "id='" + name + "'>" + this.img + this.char_card_name + this.char_card_hitdef + "</div>"
And all other properties that refer to properties above them.
You can read more about constructors here
The purpose is to have 2 lines of text on the X axis for each bar.
e.g
JUN
2018
and style each line in a different way ( like color the year label in green ) .
Can we do it?
This is how I populate the bars in a custom way.
str += '<set label="' + months[i].toUpperCase() + ' ' + currentyear + '" value="' + amount + '" color="08cf77" toolText="' + cur_symbol + amount + '" />\n';
Simply use
'
'
So the line should be updated as;
str += '<set label="' + months[i].toUpperCase() + '
' + currentyear + '" value="' + amount + '" color="08cf77" toolText="' + cur_symbol + amount + '" />\n';
I have integrated SQLite.swift framework in one of my swift project and everything was working fine until i added the below query. After adding the below query project is taking too long to build. I waited for 30 mins but still project is n't complied.
do
{
let stmt = try DB!.prepare ("SELECT e." + ENDPOINT_ID + " as _id, lk." + HUB_ID + ", e." + X_ENDPOINT_ID + ", e." + ENDPOINT_DESC + ", e." + ENDPOINT_TYPE_ID +", et." + ENDPOINT_STATUS_MIN + ", et." + ENDPOINT_STATUS_MAX + ", e." + ENDPOINT_STATUS + " FROM " + TABLE_ENDPOINT + " as e INNER JOIN " + TABLE_ENDPOINT_TYPE + " as et " +
" ON e." + ENDPOINT_TYPE_ID + " = et." + ENDPOINT_TYPE_ID +
" INNER JOIN " + TABLE_LINKING + " as lk " +
" ON e." + ENDPOINT_ID + " = lk." + ENDPOINT_ID +
" INNER JOIN " + TABLE_NODE + " as n " +
" ON lk." + NODE_ID + " = n." + NODE_ID +
" INNER JOIN " + TABLE_NODE_TYPE + " as nt " +
" ON n." + NODE_TYPE_ID + " = nt." + NODE_TYPE_ID +
" WHERE lk." + SECTION_ID + "=" + section_Id +
" AND nt." + NODE_CATEGORY + " = "S" " +
" ORDER BY e." + ENDPOINT_ID + " ASC")
let arr = Array(try stmt.run)
print("\(arr)")
return arr
} catch {
print("failed: \(error)")
return []
}
If i comment the above code and try to run the project it takes hardly a minute to run the project but after adding this code, it's taking hell lot of time. I have waited for almost 30 mins but still the project is not compiled nor it's throwing any error.
Thanks in advance for help
The Swift compiler has difficulties to deal with big string literals. See also this question for further hints.
I would recommend to split up the sql and to build a string variable in small steps:
var sql = "SELECT e." + ENDPOINT_ID
sql = sql + HUB_ID + ", e."
sql = sql + X_ENDPOINT_ID + ", e."
...
let stmt = try DB!.prepare(sql)
let query = (TABLE_ENDPOINT.select(TABLE_ENDPOINT[ENDPOINT_ID], TABLE_LINKING[HUB_ID], TABLE_ENDPOINT[ETCT_ENDPOINT_ID], TABLE_ENDPOINT[ENDPOINT_DESC], TABLE_ENDPOINT[ENDPOINT_TYPE_ID], TABLE_ENDPOINT_TYPE[ENDPOINT_STATUS_MIN], TABLE_ENDPOINT_TYPE[ENDPOINT_STATUS_MAX], TABLE_ENDPOINT[ENDPOINT_STATUS]).join(TABLE_ENDPOINT_TYPE, on: TABLE_ENDPOINT[ENDPOINT_TYPE_ID] == TABLE_ENDPOINT_TYPE[ENDPOINT_TYPE_ID]).join(TABLE_LINKING, on: TABLE_ENDPOINT[ENDPOINT_ID] == TABLE_LINKING[ENDPOINT_ID]).join(TABLE_NODE, on: TABLE_LINKING[NODE_ID] == TABLE_NODE[NODE_ID]).join(TABLE_NODE_TYPE, on: TABLE_NODE[NODE_TYPE_ID] == TABLE_NODE_TYPE[NODE_TYPE_ID])
.filter(TABLE_LINKING[SECTION_ID] == section_Id && TABLE_NODE_TYPE[NODE_CATEGORY] == "S")
.order(TABLE_ENDPOINT[ENDPOINT_ID].asc))
let arr = Array(try DB!.prepare(query))
I have an Array of dictionary like this :
The dictionaries are created :
let dateOfSurvey = String(NSDate())
let idOfSurvey = General.codeClasse + "-" + String(General.codePersonnel)
let dictionaryResults = ["dateOfSurvey": dateOfSurvey,
"ID": idOfSurvey,
"themaQuestionStep": questionResultThema,
"actionQuestionStep": questionResultAction,
"F01D1a": questionResult01,
"F02D2c": questionResult02,
"F03D3a": questionResult03,
"F04D4a": questionResult04,
"F05D1b": questionResult05,
"F06D2b": questionResult06,
"F07D3b": questionResult07,
"F08D4b": questionResult08,
"F09D1c": questionResult09,
"F10D2a": questionResult10,
"F11D3c": questionResult11,
"F12D4c": questionResult12]
then added to an array :
arrayOfResults.addObject(dictionaryResults)
I would like to send/export the result by mail and think a CSV file would be a good way.
Is there a simple way to create this csv from the array of dictionaries in swift ?
After no solution for array of dict I have to change the data in a list of arrays
dictionaryResults become arrayOfSurvey
let dateOfSurvey = String(NSDate())
let idOfSurvey = General.codeClasse + "-" + String(General.codePersonnel)
let arrayOfSurvey = [dateOfSurvey , idOfSurvey , questionResultThema , questionResultAction, questionResult01, questionResult02, questionResult03, questionResult04, questionResult05, questionResult06, questionResult07, questionResult08, questionResult09, questionResult10, questionResult11, questionResult12]
I lose the key-value but now be able to put the data in cvs
arrayOfResults.addObject(arrayOfSurvey)
let count = arrayOfResults.count
General.cvs = "dateOfSurvey" + "," + "ID" + "," + "themaQuestionStep" + "," + "actionQuestionStep" + "," + "F01D1a" + "," + "F02D2c" + "," + "F03D3a" + "," + "F04D4a" + "," + "F05D1b" + "," + "F06D2b" + "," + "F07D3b" + "," + "F08D4b" + "," + "F09D1c" + "," + "F10D2a" + "," + "F11D3c" + "," + "F12D4c"
for i in 0...count {
let arrayAtIndex = arrayOfResults.objectAtIndex(i)
let stringRepresentation = arrayAtIndex.componentsJoinedByString(",")
General.cvs += "\n" + stringRepresentation
}
let fileData = General.cvs.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)