[547] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using System.Threading;
|
---|
| 28 | using HeuristicLab.CEDMA.DB.Interfaces;
|
---|
| 29 | using System.ServiceModel;
|
---|
| 30 | using HeuristicLab.CEDMA.Core;
|
---|
| 31 | using HeuristicLab.Operators;
|
---|
| 32 | using System.IO;
|
---|
| 33 | using System.Xml;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.CEDMA.Operators {
|
---|
| 36 | public class SelectProperties : OperatorBase {
|
---|
| 37 | private static readonly string cedmaNamespace = "http://www.heuristiclab.com/cedma/";
|
---|
| 38 |
|
---|
| 39 | public override string Description {
|
---|
| 40 | get { return "TASK."; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public SelectProperties()
|
---|
| 44 | : base() {
|
---|
| 45 | AddVariableInfo(new VariableInfo("CedmaServerUri", "Uri of the CEDMA server", typeof(StringData), VariableKind.In));
|
---|
| 46 | AddVariableInfo(new VariableInfo("SubjectGuid", "", typeof(StringData), VariableKind.In));
|
---|
[549] | 47 | AddVariableInfo(new VariableInfo("Predicate", "", typeof(StringData), VariableKind.In));
|
---|
| 48 | AddVariableInfo(new VariableInfo("Property", "", typeof(IItem), VariableKind.New));
|
---|
[547] | 49 | }
|
---|
| 50 |
|
---|
| 51 | public override IOperation Apply(IScope scope) {
|
---|
| 52 | string serverUrl = GetVariableValue<StringData>("CedmaServerUri", scope, true).Data;
|
---|
| 53 | StringData subjectGuid = GetVariableValue<StringData>("SubjectGuid", scope, true);
|
---|
[549] | 54 | StringData predicate = GetVariableValue<StringData>("Predicate", scope, true);
|
---|
[547] | 55 |
|
---|
| 56 | NetTcpBinding binding = new NetTcpBinding();
|
---|
| 57 | binding.MaxReceivedMessageSize = 10000000; // 10Mbytes
|
---|
| 58 | binding.ReaderQuotas.MaxStringContentLength = 10000000; // also 10M chars
|
---|
| 59 | binding.ReaderQuotas.MaxArrayLength = 10000000; // also 10M elements;
|
---|
| 60 | binding.Security.Mode = SecurityMode.None;
|
---|
| 61 | using(ChannelFactory<IStore> factory = new ChannelFactory<IStore>(binding)) {
|
---|
| 62 | IStore store = factory.CreateChannel(new EndpointAddress(serverUrl));
|
---|
| 63 | Statement template = new Statement(
|
---|
| 64 | new Entity(cedmaNamespace+subjectGuid.Data),
|
---|
[549] | 65 | new Entity(cedmaNamespace+predicate.Data),
|
---|
[547] | 66 | new Entity(null));
|
---|
| 67 | IList<Statement> result = store.Select(template);
|
---|
| 68 |
|
---|
[549] | 69 | if(result.Count == 1) {
|
---|
| 70 | Statement s = result[0];
|
---|
| 71 | Variable var = new Variable();
|
---|
| 72 | var.Name = scope.TranslateName("Property");
|
---|
| 73 | scope.AddVariable(var);
|
---|
[547] | 74 | if(s.Property is Literal) {
|
---|
[549] | 75 | var.Value = TranslateLiteral((Literal)s.Property);
|
---|
[547] | 76 | } else if(s.Property is SerializedLiteral) {
|
---|
[549] | 77 | var.Value = TranslateLiteral((SerializedLiteral)s.Property);
|
---|
[547] | 78 | } else {
|
---|
[549] | 79 | var.Value = new StringData(((Entity)s.Property).Uri.Replace(cedmaNamespace, ""));
|
---|
[547] | 80 | }
|
---|
[549] | 81 | } else {
|
---|
| 82 | ItemList<IItem> resultValues = new ItemList<IItem>();
|
---|
| 83 | scope.AddVariable(new Variable(scope.TranslateName("Property"), resultValues));
|
---|
| 84 | foreach(Statement s in result) {
|
---|
| 85 | if(s.Property is Literal) {
|
---|
| 86 | resultValues.Add(TranslateLiteral((Literal)s.Property));
|
---|
| 87 | } else if(s.Property is SerializedLiteral) {
|
---|
| 88 | resultValues.Add(TranslateLiteral((SerializedLiteral)s.Property));
|
---|
| 89 | } else {
|
---|
| 90 | resultValues.Add(new StringData(((Entity)s.Property).Uri.Replace(cedmaNamespace, "")));
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
[547] | 93 | }
|
---|
| 94 | }
|
---|
| 95 | return null;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | private IItem TranslateLiteral(SerializedLiteral serializedLiteral) {
|
---|
| 99 | XmlDocument doc = new XmlDocument();
|
---|
| 100 | doc.LoadXml(serializedLiteral.RawData);
|
---|
| 101 | return (IItem)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | private IItem TranslateLiteral(Literal literal) {
|
---|
| 105 | if(literal.Value is bool) {
|
---|
| 106 | return new BoolData((bool)literal.Value);
|
---|
| 107 | } else if(literal.Value is double) {
|
---|
| 108 | return new DoubleData((double)literal.Value);
|
---|
| 109 | } else if(literal.Value is int) {
|
---|
| 110 | return new IntData((int)literal.Value);
|
---|
| 111 | } else if(literal.Value is string) {
|
---|
| 112 | return new StringData((string)literal.Value);
|
---|
| 113 | } else {
|
---|
| 114 | return new StringData(literal.Value.ToString());
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|