コード例 #1
0
 // snak1: base/theirs, snak2: ours
 private bool SnakValueEquals(Snak snak1, Snak snak2, bool valueOptional)
 {
     if (snak1.SnakType != SnakType.Value)
     {
         return(snak2.SnakType == snak1.SnakType);
     }
     if (snak1.DataType == BuiltInDataTypes.WikibaseItem || snak1.DataType == BuiltInDataTypes.WikibaseProperty)
     {
         var src  = MapEntity((string)snak1.DataValue, valueOptional);
         var dest = (string)snak2.DataValue;
         if (string.IsNullOrEmpty(src))
         {
             return(snak2.SnakType == SnakType.SomeValue);
         }
         return(src == dest);
     }
     if (snak1.DataType == BuiltInDataTypes.Quantity)
     {
         var value1     = (WbQuantity)snak1.DataValue;
         var value2     = (WbQuantity)snak2.DataValue;
         var mappedUnit = MapEntityUri(value1.Unit, true);
         if (mappedUnit == null)
         {
             return(false);
         }
         var mapped1 = new WbQuantity(value1.Amount, value1.LowerBound, value1.UpperBound, mappedUnit);
         return(mapped1.Equals(value2));
     }
     return(JToken.DeepEquals(snak1.RawDataValue, snak2.RawDataValue));
 }
コード例 #2
0
 private void FixValueEntityReference(Snak snak, bool optional)
 {
     if (EntityMapping == null)
     {
         return;
     }
     if (snak.DataType == BuiltInDataTypes.WikibaseItem || snak.DataType == BuiltInDataTypes.WikibaseProperty)
     {
         var src = (string)snak.DataValue;
         if (string.IsNullOrEmpty(src))
         {
             return;
         }
         var mapped = MapEntity(src, optional);
         if (mapped == null)
         {
             snak.SnakType = SnakType.SomeValue;
         }
         snak.DataValue = mapped;
     }
     else if (snak.DataType == BuiltInDataTypes.Quantity)
     {
         // TODO WbQuantity is not precise enough!
         var src        = (WbQuantity)snak.DataValue;
         var mappedUnit = MapEntityUri(src.Unit, optional);
         if (mappedUnit == null)
         {
             snak.SnakType = SnakType.SomeValue;
         }
         else
         {
             snak.DataValue = new WbQuantity(src.Amount, src.LowerBound, src.UpperBound, mappedUnit);
         }
     }
 }
コード例 #3
0
ファイル: Statement.cs プロジェクト: barcexwiki/wikibase.net
        public Statement(string guid, Snak snak, List<Snak> qualifiers = null, List<Snak> references = null, StatementRank rank = StatementRank.Normal)
        {
            if (snak == null)
            {
                throw new ArgumentNullException("The snak must not be null");
            }

            Guid = guid;
            Snak = snak;
            Qualifiers = qualifiers == null ? new List<Snak>() : qualifiers;
            References = references == null ? new List<Snak>() : references;
            Rank = rank;
        }
コード例 #4
0
ファイル: WikiDataBot.cs プロジェクト: zenwalk/tambon
 private void SetSlogan(IEnumerable <Entity> entities, StringBuilder collisionInfo, Boolean overrideData)
 {
     if (entities == null)
     {
         throw new ArgumentNullException("entities");
     }
     ClearRunInfo();
     foreach (var entity in entities)
     {
         var item = _helper.GetWikiDataItemForEntity(entity);
         if (item == null)
         {
             _runInfo[WikiDataState.ItemNotFound]++;
             collisionInfo.AppendFormat("{0}: {1} was deleted!", entity.wiki.wikidata, entity.english);
         }
         else
         {
             var state = _helper.SloganCorrect(item, entity);
             _runInfo[state]++;
             if (state == WikiDataState.WrongValue)
             {
                 collisionInfo.AppendFormat("{0}: {1} has wrong slogan", item.id, entity.english);
                 collisionInfo.AppendLine();
             }
             if (state != WikiDataState.Valid)
             {
                 var statement = _helper.SetSlogan(item, entity);
                 if (statement != null)
                 {
                     statement.save(_helper.GetClaimSaveEditSummary(statement));
                     if ((statement != null) && entity.type.IsCompatibleEntityType(EntityType.Amphoe))
                     {
                         var source = AmphoeComHelper.AmphoeWebsite(entity.geocode);
                         if (source != null)
                         {
                             var snak            = new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdReferenceUrl), new StringValue(source.AbsoluteUri));
                             var sloganReference = statement.CreateReferenceForSnak(snak);
                             statement.AddReference(sloganReference);
                             foreach (var reference in statement.References)
                             {
                                 reference.Save(_helper.GetReferenceSaveEditSummary(reference));
                             }
                         }
                     }
                 }
             }
         }
     }
 }
コード例 #5
0
        private Snak CloneSnak(Snak snak, bool valueOptional)
        {
            var propId = MapEntity(snak.PropertyId, true);

            if (propId == null)
            {
                return(null);
            }
            var newSnak = new Snak(propId)
            {
                SnakType     = snak.SnakType,
                DataType     = snak.DataType,
                RawDataValue = snak.RawDataValue
            };

            FixValueEntityReference(newSnak, valueOptional);
            return(newSnak);
        }
コード例 #6
0
 private IEnumerable <string> FindMissingEntities(Snak snak)
 {
     if (EntityMapping == null)
     {
         yield break;
     }
     if (EntityMapping[snak.PropertyId] == null)
     {
         yield return(snak.PropertyId);
     }
     if (snak.DataType == BuiltInDataTypes.WikibaseItem || snak.DataType == BuiltInDataTypes.WikibaseProperty)
     {
         var src = (string)snak.DataValue;
         if (!string.IsNullOrEmpty(src) && EntityMapping[src] == null)
         {
             yield return(src);
         }
     }
 }
コード例 #7
0
        static void Main(string[] args)
        {
            Walls walls = new Walls(80, 25);

            walls.Draw();

            Snak snake = new Snak(new Point(15, 15, '*'), 5, Direction.RIGHT);

            snake.Draw();

            FoodCreator foodCreator = new FoodCreator(80, 25, '$');
            Point       food        = foodCreator.CreateFood();

            food.Draw();

            while (true)
            {
                if (walls.IsHit(snake) || snake.IsHitTail())
                {
                    break;
                }
                if (snake.Eat(food))
                {
                    food = foodCreator.CreateFood();
                    food.Draw();
                }
                else
                {
                    snake.Move();
                }
                Thread.Sleep(100);

                if (Console.KeyAvailable)
                {
                    var key = Console.ReadKey();
                    snake.HandleKey(key.Key);
                }
            }
        }
コード例 #8
0
ファイル: WikiData.cs プロジェクト: zenwalk/tambon
        private void btnTest_Click(object sender, EventArgs e)
        {
            //WikibaseApi api = new WikibaseApi("https://test.wikidata.org", "TambonBot");
            //// Login with username and password
            //var username = ConfigurationManager.AppSettings["WikiDataUsername"];
            //var password = ConfigurationManager.AppSettings["WikiDataPassword"];
            //api.login(username, password);
            //var provider = new EntityProvider(api);
            //var entity = provider.getEntityFromId(new EntityId("q", 281));
            //var statement = entity.Claims.First() as Statement;
            //var qualifier = new Qualifier(statement, SnakType.Value, new EntityId("P11"), new StringValue("abc"));
            //qualifier.Save("Qualifier save");

            var tis1099thesaban = new List <UInt32>()
            {
                1195,
                1196,
                1197,
                1198,
                1199,
                1295,
                1296,
                1297,
                1298,
                1299,
                1396,
                1397,
                1398,
                1399,
                1496,
                1497,
                1498,
                1499,
                1598,
                1599,
                1697,
                1698,
                1699,
                1799,
                1898,
                1899,
                1996,
                1997,
                1998,
                1999,
                2093,
                2094,
                2095,
                2096,
                2097,
                2098,
                2099,
                2197,
                2198,
                2199,
                2297,
                2298,
                2299,
                2399,
                2498,
                2499,
                2598,
                2599,
                2699,
                2798,
                2799,
                3096,
                3097,
                3098,
                3099,
                3198,
                3199,
                3299,
                3399,
                3497,
                3498,
                3499,
                3599,
                3699,
                3799,
                3999,
                4096,
                4097,
                4098,
                4099,
                4199,
                4299,
                4399,
                4499,
                4599,
                4699,
                4799,
                4899,
                4999,
                5099,
                5199,
                5299,
                5398,
                5399,
                5499,
                5599,
                5699,
                5799,
                5899,
                6097,
                6098,
                6099,
                6199,
                6299,
                6398,
                6399,
                6498,
                6499,
                6599,
                6697,
                6698,
                6699,
                6798,
                6799,
                7097,
                7098,
                7099,
                7198,
                7199,
                7298,
                7299,
                7399,
                7497,
                7498,
                7499,
                7598,
                7599,
                7698,
                7699,
                7798,
                7799,
                8097,
                8098,
                8099,
                8199,
                8298,
                8299,
                8398,
                8399,
                8497,
                8498,
                8499,
                8599,
                8698,
                8699,
                9096,
                9097,
                9098,
                9099,
                9199,
                9297,
                9298,
                9299,
                9399,
                9498,
                9499,
                9598,
                9599,
                9698,
                9699,
            };

            var entites = allEntities.Where(x => tis1099thesaban.Contains(x.geocode));

            foreach (var entity in entites)
            {
                var item = _helper.GetWikiDataItemForEntity(entity);
                if (_helper.GeocodeCorrect(item, entity) == WikiDataState.NotSet)
                {
                    var statement = _helper.SetGeocode(item, entity, false);
                    if (statement != null)
                    {
                        statement.save(_helper.GetClaimSaveEditSummary(statement));
                        var snak      = new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdStatedIn), new EntityIdValue(new EntityId(WikiBase.ItemSourceTIS1099BE2548)));
                        var reference = statement.CreateReferenceForSnak(snak);
                        reference.Save(_helper.GetReferenceSaveEditSummary(reference));
                    }
                }
            }
        }
コード例 #9
0
        /// <inheritdoc />
        protected override async Task ProcessRecordAsync(CancellationToken cancellationToken)
        {
            var options = EntityEditOptions.Bulk;

            if (Bot)
            {
                options |= EntityEditOptions.Bot;
            }
            var entity = new Entity(SourceSite, SourceId);
            await entity.RefreshAsync(EntityQueryOptions.FetchClaims
                                      | EntityQueryOptions.FetchInfo, null, cancellationToken);

            if (!entity.Exists)
            {
                throw new InvalidOperationException($"The source entity {entity} does not exist.");
            }
            var dest = new Entity(DestinationSite, DestinationId);
            await dest.RefreshAsync(EntityQueryOptions.FetchInfo | EntityQueryOptions.FetchClaims);

            if (!dest.Exists)
            {
                throw new InvalidOperationException($"The destination entity {dest} does not exist.");
            }

            sourceSiteWikibaseInfo      = WikibaseSiteInfo.FromSiteInfo(SourceSite.SiteInfo);
            destinationSiteWikibaseInfo = WikibaseSiteInfo.FromSiteInfo(DestinationSite.SiteInfo);

            var refProp = CitationProperty == null ? null : new Entity(DestinationSite, CitationProperty);
            // Lookup for the claims that are previously imported from wikidata
            // Claim ID --> Claim
            Dictionary <string, Claim> existingClaims = null;

            if (refProp != null)
            {
                await refProp.RefreshAsync(EntityQueryOptions.FetchInfo);

                existingClaims = dest.Claims.Select(c => new
                {
                    Id = c.References.Select(r =>
                                             (string)r.Snaks.FirstOrDefault(s => s.PropertyId == refProp.Id)?.DataValue)
                         .FirstOrDefault(s => s != null),
                    Claim = c
                }).Where(t => t.Id != null)
                                 .ToDictionary(t => t.Id, t => t.Claim);
            }
            var newClaims    = new List <Claim>();
            var optionalProp = OptionalProperty == null ? null : new HashSet <string>(OptionalProperty);

            bool IsPropertyValueOptional(string propertyId)
            {
                return(optionalProp?.Contains(propertyId) ?? false);
            }

            IEnumerable <string> props = Property;
            var newClaimsCounter       = 0;
            var updatedClaimsCounter   = 0;

            if (Property.Length == 1 && Property[0] == "*")
            {
                if (EntityMapping == null)
                {
                    throw new ArgumentNullException(nameof(EntityMapping));
                }
                props = EntityMapping.Keys.Cast <string>();
            }
            foreach (var prop in props)
            {
                if (prop == null)
                {
                    throw new ArgumentException("Properties have null item.", nameof(Property));
                }
                var pc = entity.Claims[prop.Trim().ToUpperInvariant()];
                foreach (var claim in pc)
                {
                    if (existingClaims != null && existingClaims.TryGetValue(claim.Id, out var newClaim))
                    {
                        var updated = false;
                        if (!SnakValueEquals(claim.MainSnak, newClaim.MainSnak, IsPropertyValueOptional(claim.MainSnak.PropertyId)))
                        {
                            newClaim.MainSnak.SnakType     = claim.MainSnak.SnakType;
                            newClaim.MainSnak.RawDataValue = claim.MainSnak.RawDataValue;
                            FixValueEntityReference(newClaim.MainSnak, IsPropertyValueOptional(claim.MainSnak.PropertyId));
                            updated = true;
                        }
                        var qualifiers = new List <Snak>();
                        var reusedQs   = 0;
                        var newQs      = 0;
                        foreach (var q in claim.Qualifiers)
                        {
                            var mapped = MapEntity(q.PropertyId, true);
                            if (mapped == null)
                            {
                                continue;
                            }
                            var newQ = newClaim.Qualifiers.FirstOrDefault(ourQ =>
                                                                          ourQ.PropertyId == mapped && SnakValueEquals(q, ourQ, IsPropertyValueOptional(q.PropertyId)));
                            if (newQ == null)
                            {
                                newQs++;
                                newQ = new Snak(mapped)
                                {
                                    DataType = q.DataType, RawDataValue = q.RawDataValue
                                };
                                FixValueEntityReference(newQ, optionalProp?.Contains(q.PropertyId) ?? false);
                            }
                            else
                            {
                                reusedQs++;
                            }
                            qualifiers.Add(newQ);
                        }
                        if (newQs > 0 || reusedQs < newClaim.Qualifiers.Count)
                        {
                            newClaim.Qualifiers.Clear();
                            newClaim.Qualifiers.AddRange(qualifiers);
                            updated = true;
                        }
                        if (!updated)
                        {
                            continue;
                        }
                        updatedClaimsCounter++;
                    }
                    else
                    {
                        newClaimsCounter++;
                        newClaim = new Claim(CloneSnak(claim.MainSnak, IsPropertyValueOptional(claim.MainSnak.PropertyId)));
                        foreach (var qualifier in claim.Qualifiers)
                        {
                            var snak = CloneSnak(qualifier, IsPropertyValueOptional(qualifier.PropertyId));
                            if (snak == null)
                            {
                                continue;
                            }
                            newClaim.Qualifiers.Add(snak);
                        }
                    }
                    if (refProp != null)
                    {
                        if (newClaim.References.All(r => (string)r.Snaks.FirstOrDefault(s => s.PropertyId == refProp.Id)?.DataValue != claim.Id))
                        {
                            var refSnak = new Snak(refProp.Id, claim.Id, refProp.DataType);
                            newClaim.References.Add(new ClaimReference(refSnak));
                        }
                    }
                    newClaims.Add(newClaim);
                }
            }
            var changes = new List <EntityEditEntry>();

            changes.AddRange(newClaims.Select(c => new EntityEditEntry(nameof(dest.Claims), c)));
            if (changes.Count == 0)
            {
                WriteCommandDetail($"No matching claims to copy from \"{entity.Id}\"({SourceSite}) to {dest.Id}({DestinationSite})");
                return;
            }
            if (!ShouldProcess(string.Format("{0}({1}) --> {2}({3}), {4} changes. Claims: +{5}, !{6}",
                                             entity.Id, entity.Site, dest.Id, dest.Site, changes.Count, newClaimsCounter, updatedClaimsCounter)))
            {
                return;
            }
            if (Progressive)
            {
                int counter = 1;
                foreach (var change in changes)
                {
                    var claim = (Claim)change.Value;
                    if (ShouldProcess(string.Format("{0}({1}) --> {2}({3}), Target claim: {4}",
                                                    entity.Id, entity.Site, dest.Id, dest.Site, claim)))
                    {
                        await dest.EditAsync(new[] { change },
                                             $"[{counter}/{changes.Count}] Adding {newClaimsCounter} / updating {updatedClaimsCounter} claims from \"{entity.Id}\" on {SourceSite}.",
                                             options, cancellationToken);
                    }
                }
            }
            else
            {
                await dest.EditAsync(changes,
                                     $"Added {newClaimsCounter} / updated {updatedClaimsCounter} claims from \"{entity.Id}\" on {SourceSite}.",
                                     options, cancellationToken);
            }
            WriteCommandDetail($"Added {newClaimsCounter} / updated {updatedClaimsCounter} claims from \"{entity.Id}\"({SourceSite}) to {dest.Id}({DestinationSite})");
        }
コード例 #10
0
ファイル: Statement.cs プロジェクト: barcexwiki/wikibase.net
 public Statement(Snak snak, List<Snak> qualifiers = null, List<Snak> references = null)
     : this(null, snak, qualifiers, references) { }