How can I saved my outputs in specific folder in cplex? - output

I have an OPL project in Cplex, and I get all my outputs as csv files. All of them are saved in a folder of my project; however, I want to know how can I give it a specific path? I want to save them where I get all my inputs. Is there any way that I do it?

tuple t
{
string firstname;
int number;
}
{t} s={<"Nicolas",2>,<"Alexander",3>};
execute
{
var f=new IloOplOutputFile("c:\\temp\\export.csv");
for(var i in s)
{
f.writeln(i.firstname,";",i.number,";");
}
f.close();
}
writes the result in the folder "c:/temp".
I changed a bit https://www.ibm.com/developerworks/community/forums/html/topic?id=3fd44d41-210b-4b81-a005-819530d6377b&ps=25

I supposed you use an IloOplOutputFile to create the output? In that case note that you can specify an arbitrary path as a constructor argument (see for example Alex's answer). Moreover, notice that the IloOplModel class has a function resolvePath that resolves the path of a resource. So you can do something like
var path = thisOplModel.resolvePath("inputfile");
to get the path to one of your input files. From that you can construct a path for your output files.

Related

How can I use an Excel file as test data correctly?

How can I best use an Excel file as input for an xUnit test? Note that I do not want to use the data inside the Excel, but the Excel itself.
Let's say I have a UnitTests project, where I want to place some Excel files, that I need to put in my tests:
[Fact]
public void Constructor_ShouldReadExcelFile()
{
var mapping = new ExcelMapping("excelfiles/test1.xlsx");
Assert.True(mapping.Valid);
}
but, when running that, the CurrentWorkingDirectory is set to the bin\Debug\net7.0 dir, and I need to make a relative path:
[Fact]
public void Constructor_ShouldReadExcelFile()
{
var mapping = new ExcelMapping("../../../excelfiles/test1.xlsx");
Assert.True(mapping.Valid);
}
This will work, but is this the "right" way?
Your solution looks fine to me.
I often need to retrieve test data files for unit tests and generally proceed as follows. The test data are also under version control but in a different folder than the unit tests. In my unit test class, I define a relative path for the test data and make a member for the absolute path:
const string testDataRelativePath = #"..\..\..\..\excelfiles\";
string testDataFolderAbsolutePath;
The relative path is relative to the project folder where the unit test dll is output.
In the constructor of the test class I define a value for the absolute path.
using System.IO;
using System.Reflection;
public class MyTestClass
{
public MyTestClass()
{
string projectDir = getProjectDir();
testDataFolderAbsolutePath = Path.GetFullPath(Path.Combine(projectDir, testDataRelativePath));
}
internal static string getProjectDir()
{
Assembly assembly = Assembly.GetExecutingAssembly();
return directoryPathNameFromAssemblyCodeBase(assembly);
}
internal static string directoryPathNameFromAssemblyCodeBase(Assembly assembly)
{
Uri codeBaseUrl = new Uri(assembly.CodeBase);
string codeBasePath = Uri.UnescapeDataString(codeBaseUrl.AbsolutePath);
return Path.GetDirectoryName(codeBasePath);
}
// ... Tests ...
}
In the test itself, I then do something like this:
string excelFilePath = Path.Combine(testDataFolderAbsolutePath, "test1.xlsx");
I find that this gives better results on the plurality of systems on which the tests are running.

How would I go about loading a JSON file using Laravel?

I have a JSON file that I'd like to load using Laravel. I'm learning Laravel and would like to know the right way to do this. I have the JSON files in a folder called json in the public folder.
In my routes.php I have the following:
Route::get('/json/{jsonfile}', array(
'as' => 'load-json',
'uses' => 'JSONController#loadJSON'
));
In JSONController I have:
public function loadJSON($jsonfile) {
// not sure what to do here
return View::make('json.display')
->with('jsonfile', $jsonfile);
}
Also is my naming convention ok or do you have better suggestions?
Always be careful when allowing a user inputed data to decide what files to read and write. Here is simple code that will take in the filename and look in the apps/storage/json folder. I'm less familiar with what Illuminate does to protect against system injections but you might want at the very least to make sure that 'filename' doesn't contain anything but alphanumeric characters with a validator.
Unless the JSON (or any file) needs to be public you shouldn't keep it in the public folder. This way they must go through your app (and permissions) to view it. Also you can have more restrictive permissions outside the public folder.
public function loadJSON($filename) {
$path = storage_path() . "/json/${filename}.json"; // ie: /var/www/laravel/app/storage/json/filename.json
if (!File::exists($path)) {
throw new Exception("Invalid File");
}
$file = File::get($path); // string
// Verify Validate JSON?
// Your other Stuff
}

SSIS package : Store value of for loop variable in an array

I am creating an SSIS package where I have to iterate through some of the selected folder and I want to store the folder names in an array to keep track of folders that I have processed. Can I keep an array in SSIS package and keep appending the value in that array?
You can store the value of a for loop variable in an array. Doing this is a little messy IMO. There's likely a cleaner approach using "out of the box" SSIS functionality as #billinkc suggested. However, here are some pointers ...
Let's go with your scenario where you have a for each loop that iterates over some files (using a Foreach File Enumerator) and you want to store the folder names in an array.
Here are some variables we'll use:
FolderList will be the array and CurrentFile will be the for loop variable. The package in its simplest form might look like this:
In the script task, the code might look like this. I've chosen to use a List<string> as my array type, but you could use something else, such as ArrayList. (Note: you'll need to add using statements for System.Collections.Generic and System.IO for the code below):
public void Main()
{
//get current directory
string directory = Path.GetDirectoryName(Dts.Variables["User::CurrentFile"].Value.ToString());
List<string> lst = new List<string>();
// if the FolderList is already a List<string> then set set it to lst
if ((Dts.Variables["User::FolderList"].Value is List<string>))
{
lst = (List<string>)Dts.Variables["User::FolderList"].Value;
}
// if the directory isn't in the list yet, then add it to the list
if(!lst.Contains(directory))
{
lst.Add(directory);
}
// update our variable with the List<string>
Dts.Variables["User::FolderList"].Value = lst;
Dts.TaskResult = (int)ScriptResults.Success;
}
Each time the Script Task is executed, you'll get a new folder added to the array. Once the for each loop is done, you may want to examine the values of the array. You can do this using a Script Task (similar to what we did above):
List<string> lst = (List<string>)Dts.Variables["User::FolderList"].Value;
// do stuff with lst
You can also iterate over the values in the array using a for each loop (use the Foreach From Variable Enumerator), which I just learned as I was walking through this (thanks!). Just set the variable to enumerate over to your array variable (here FolderList) and specify another variable (e.g. CurrentFolder) as index 0 in Variable Mappings. This worked for a List<string>, but I'm not sure what other collection types it would work with.

asp.net MVC saving relative path in database as not the full path

asp.net MVC saving relative path in database as
~/Content/Uploads/11thMay.jpg
not the full path as
D:/Projects/HRMV2/CubicHRMWeb/Content/Uploads/11thMay.jpg
I wanted to save relative path but in DB full path was being saved.
How can I do it?
thanks.
If your question is "How to save as full path", please try this
string path = Path.Combine(Server.MapPath("~/Content/img/Company/LogoFiles"), filename);
If your question is "How to save as relative path", please try this
[HttpPost]
public ActionResult Save(HttpPostedFileBase myFile)
{
string filename = myFile.FileName;
string relativeFileName = "~/Content/Uploads/" + filename;// relative path
return View();
}
this code doesn't allow to save the relative path, when you try it, the totality of filename is setted and not just the filename.if you insert a breakpoint at this point, you must obtain a thing like
"/content/Uploads/C:Document/Picture/father.jpeg"
and not the relative path which you will want it like
"/content/Uploads/father.jpeg"
in order to save the relative path of a document , you ought to use the getfilename property of system.io as show in the below example.
I suppose that you want to do it in the post method of your controller which contains a model or httppostedfilebase
in my example, i have a model with httppostedfilebase property named imagefichier, so you can get the document name by doing this
var lefilename = System.IO.Path.GetFileName(model.imagefichier.FileName);
//Suppose that you want to save it in your root web file
//like ~/Images/Administrateur/leilename,you can do it like this
string cheminbdd = "~/Images/Administrateur/" + lefilename;
//it is cheminbdd that you can save in your database as your relative path

How to export data from LinqPAD as JSON?

I want to create a JSON file for use as part of a simple web prototyping exercise. LinqPAD is perfect for accessing the data from my DB in just the shape I need, however I cannot get it out as JSON very easily.
I don't really care what the schema is, because I can adapt my JavaScript to work with whatever is returned.
Is this possible?
A more fluent solution is to add the following methods to the "My Extensions" File in Linqpad:
public static String DumpJson<T>(this T obj)
{
return
obj
.ToJson()
.Dump();
}
public static String ToJson<T>(this T obj)
{
return
new System.Web.Script.Serialization.JavaScriptSerializer()
.Serialize(obj);
}
Then you can use them like this in any query you like:
Enumerable.Range(1, 10)
.Select(i =>
new
{
Index = i,
IndexTimesTen = i * 10,
})
.DumpJson();
I added "ToJson" separately so it can be used in with "Expessions".
This is not directly supported, and I have opened a feature request here. Vote for it if you would also find this useful.
A workaround for now is to do the following:
Set the language to C# Statement(s)
Add an assembly reference (press F4) to System.Web.Extensions.dll
In the same dialog, add a namespace import to System.Web.Script.Serialization
Use code like the following to dump out your query as JSON
new JavaScriptSerializer().Serialize(query).Dump();
There's a solution with Json.NET since it does indented formatting, and renders Json dates properly. Add Json.NET from NuGet, and refer to Newtonsoft.Json.dll to your “My Extensions” query and as well the following code :
public static object DumpJson(this object value, string description = null)
{
return GetJson(value).Dump(description);
}
private static object GetJson(object value)
{
object dump = value;
var strValue = value as string;
if (strValue != null)
{
var obj = JsonConvert.DeserializeObject(strValue);
dump = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);
}
else
{
dump = JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.Indented);
}
return dump;
}
Use .DumpJson() as .Dump() to render the result. It's possible to override more .DumpJson() with different signatures if necessary.
As of version 4.47, LINQPad has the ability to export JSON built in. Combined with the new lprun.exe utility, it can also satisfy your needs.
http://www.linqpad.net/lprun.aspx