text:
package org.motechproject.mds.it.service;

import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.motechproject.mds.domain.Entity;
import org.motechproject.mds.domain.EntityDraft;
import org.motechproject.mds.domain.Field;
import org.motechproject.mds.domain.FieldMetadata;
import org.motechproject.mds.domain.FieldSetting;
import org.motechproject.mds.domain.Lookup;
import org.motechproject.mds.domain.Type;
import org.motechproject.mds.domain.TypeSetting;
import org.motechproject.mds.dto.AdvancedSettingsDto;
import org.motechproject.mds.dto.DraftData;
import org.motechproject.mds.dto.EntityDto;
import org.motechproject.mds.dto.FieldBasicDto;
import org.motechproject.mds.dto.FieldDto;
import org.motechproject.mds.dto.LookupDto;
import org.motechproject.mds.dto.LookupFieldDto;
import org.motechproject.mds.dto.MetadataDto;
import org.motechproject.mds.dto.RestOptionsDto;
import org.motechproject.mds.dto.TypeDto;
import org.motechproject.mds.ex.entity.EntityAlreadyExistException;
import org.motechproject.mds.ex.entity.EntityNotFoundException;
import org.motechproject.mds.it.BaseIT;
import org.motechproject.mds.osgi.EntitiesBundleMonitor;
import org.motechproject.mds.repository.AllEntityDrafts;
import org.motechproject.mds.repository.AllTypes;
import org.motechproject.mds.repository.MetadataHolder;
import org.motechproject.mds.service.EntityService;
import org.motechproject.mds.service.JarGeneratorService;
import org.motechproject.mds.service.TypeService;
import org.motechproject.mds.testutil.DraftBuilder;
import org.motechproject.mds.testutil.FieldTestHelper;
import org.motechproject.mds.util.Constants;
import org.motechproject.mds.util.MDSClassLoader;
import org.motechproject.mds.util.SecurityMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.User;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static ch.lambdaj.Lambda.extract;
import static ch.lambdaj.Lambda.having;
import static ch.lambdaj.Lambda.on;
import static ch.lambdaj.Lambda.selectFirst;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.motechproject.mds.dto.LookupFieldType.RANGE;
import static org.motechproject.mds.dto.LookupFieldType.SET;
import static org.motechproject.mds.dto.LookupFieldType.VALUE;
import static org.motechproject.mds.testutil.LookupTestHelper.lookupFieldsFromNames;
import static org.motechproject.mds.util.Constants.MetadataKeys.RELATED_CLASS;
import static org.motechproject.mds.util.Constants.MetadataKeys.RELATED_FIELD;
import static org.motechproject.mds.util.Constants.MetadataKeys.RELATIONSHIP_COLLECTION_TYPE;
import static org.motechproject.mds.util.Constants.MetadataKeys.OWNING_SIDE;

public class EntityServiceContextIT extends BaseIT {
    private static final String SIMPLE_NAME = "Test";
    private static final String SIMPLE_NAME_2 = "Test2";
    private static final String SIMPLE_NAME_3 = "Test3";

    @Autowired
    private EntityService entityService;

    @Autowired
    private JarGeneratorService jarGeneratorService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private MetadataHolder metadataHolder;

    @Autowired
    private EntitiesBundleMonitor monitor;

    @Autowired
    private AllTypes allTypes;

    @Autowired
    private AllEntityDrafts allEntityDrafts;

    @Before
    public void setUp() throws Exception {
        super.setUp();

        setUpSecurityContext();
        metadataHolder.reloadMetadata();

        Path path = Paths.get(monitor.bundleLocation());
        Files.deleteIfExists(path);
    }

    @Test
    public void shouldCreateEntity() throws Exception {
        // given
        EntityDto entityDto = new EntityDto();
        entityDto.setName(SIMPLE_NAME);

        // when
        entityService.createEntity(entityDto);
        setProperty(monitor, "bundleStarted", true);
        setProperty(monitor, "bundleInstalled", true);
        setProperty(monitor, "contextInitialized", true);
        jarGeneratorService.regenerateMdsDataBundle();

        // then
        // 1. new entry in db should be added
        String className = String.format("%s.%s", Constants.PackagesGenerated.ENTITY, "Test");
        assertTrue(String.format("Not found %s in database", className), containsEntity(className));

        // 2. there should be ability to create a new instance of created entity
        Class<?> clazz = MDSClassLoader.getInstance().loadClass(className);
        Object instance = clazz.newInstance();

        assertNotNull(instance);

        ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            // We want to use the current factory for persisting so this hack is required
            // Normally these classes live in a separate bundle
            Thread.currentThread().setContextClassLoader(MDSClassLoader.getInstance());
            getPersistenceManagerFactory().registerMetadata(metadataHolder.getJdoMetadata());

            getPersistenceManager().makePersistent(instance);

            List<?> list = cast(clazz, (Collection) getPersistenceManager().newQuery(clazz).execute());

            assertNotNull(list);
            assertFalse("The instance of entity should be saved in database", list.isEmpty());
        } finally {
            Thread.currentThread().setContextClassLoader(oldClassLoader);
        }
    }

    @Test(expected = EntityNotFoundException.class)
    public void shouldNotSaveDraftWhenEntityIsUnavailable() throws IOException {
        Map<String, Object> values = new HashMap<>();
        values.put("path", DraftData.ADD_NEW_INDEX);
        values.put("advanced", true);

        DraftData draftData = new DraftData();
        draftData.setEdit(true);
        draftData.setValues(values);

        entityService.saveDraftEntityChanges(123L, draftData);
    }

    @Test(expected = EntityAlreadyExistException.class)
    public void shouldNotSaveEntityOfTheSameName() throws IOException {
        String testEntityName1 = "TestEntity";
        String testEntityName2 = "TeStEnTiTy";

        EntityDto entityDto1 = new EntityDto();
        entityDto1.setName(testEntityName1);

        EntityDto entityDto2 = new EntityDto();
        entityDto2.setName(testEntityName2);

        entityService.createEntity(entityDto1);
        entityService.createEntity(entityDto2);
    }

    @Test
    public void shouldSaveEntityWithGivenLookups() throws IOException {
        EntityDto entityDto = new EntityDto();
        entityDto.setName("myEntity");

        Map<String, Object> values = new HashMap<>();
        values.put("path", DraftData.ADD_NEW_INDEX);
        values.put("advanced", true);
        values.put("value", asList("Lookup 1"));

        DraftData draftData = new DraftData();
        draftData.setEdit(true);
        draftData.setValues(values);
        entityDto = entityService.createEntity(entityDto);

        entityService.saveDraftEntityChanges(entityDto.getId(), draftData);
        entityService.commitChanges(entityDto.getId());

        assertTrue(containsLookup("Lookup 1"));
    }

    @Test
    public void shouldRetrieveAllEntities() throws IOException {
        entityService.createEntity(new EntityDto(null, null, SIMPLE_NAME_2, null, null, SecurityMode.EVERYONE, null));
        entityService.createEntity(new EntityDto(null, null, SIMPLE_NAME_3, null, null, SecurityMode.EVERYONE, null));

        List<EntityDto> result = entityService.listEntities();

        List<String> expected = asList(SIMPLE_NAME_2, SIMPLE_NAME_3);
        List<String> actual = extract(result, on(EntityDto.class).getName());

        Collections.sort(expected);
        Collections.sort(actual);

        assertEquals(expected, actual);
    }

    @Test
    public void shouldRetrieveOnlyEntitiesUserHasAccessTo() throws IOException {
        entityService.createEntity(new EntityDto(null, null, SIMPLE_NAME, null, null, SecurityMode.EVERYONE, null));
        entityService.createEntity(new EntityDto(null, null, SIMPLE_NAME_2, null, null,
                SecurityMode.USERS, new HashSet<>(Arrays.asList("motech", "motech2"))));
        entityService.createEntity(new EntityDto(null, null, SIMPLE_NAME_3, null, null,
                SecurityMode.USERS, new HashSet<>(Arrays.asList("no_motech", "definitely_no_motech"))));

        List<EntityDto> result = entityService.listEntities(true);

        List<String> expected = asList(SIMPLE_NAME, SIMPLE_NAME_2);
        List<String> actual = extract(result, on(EntityDto.class).getName());

        Collections.sort(expected);
        Collections.sort(actual);

        assertEquals(expected, actual);
    }

    @Test
    public void shouldRetrieveAllWorkInProgress() throws IOException {
        EntityDto entityDto = new EntityDto();

        entityDto.setName("WIP1");
        EntityDto result1 = entityService.createEntity(entityDto);

        entityDto.setClassName(null);
        entityDto.setName("WIP2");
        EntityDto result2 = entityService.createEntity(entityDto);

        entityService.saveDraftEntityChanges(result1.getId(),
                DraftBuilder.forNewField("disp", "name", Long.class.getName()));
        entityService.saveDraftEntityChanges(result2.getId(),
                DraftBuilder.forNewField("disp", "name", Long.class.getName()));

        List<EntityDto> wip = entityService.listWorkInProgress();

        assertNotNull(wip);

        List<String> result = extract(wip, on(EntityDto.class).getName());
        Collections.sort(result);
        assertEquals(asList("WIP1", "WIP2"), result);
    }

    @Test
    public void shouldFindEntitiesByPackage() {
        entityService.createEntity(new EntityDto(
                null, "org.motechproject.ivr.Test", SIMPLE_NAME, null, null, SecurityMode.EVERYONE, null));

        entityService.createEntity(new EntityDto(
                null, "org.motechproject.csd.domain.Address", "Address", null, null, SecurityMode.EVERYONE, null));

        entityService.createEntity(new EntityDto(
                null, "org.motechproject.csd.another.Provider", "Provider", null, null, SecurityMode.EVERYONE, null));

        List<EntityDto> entities = entityService.findEntitiesByPackage("org.motechproject.csd");

        assertNotNull(entities);

        List<String> result = extract(entities, on(EntityDto.class).getName());
        Collections.sort(result);
        assertEquals(asList("Address", "Provider"), result);
    }

    @Test
    public void testDraftWorkflow() throws IOException {
        EntityDto entityDto = new EntityDto();
        entityDto.setName("DraftTest");
        entityDto = entityService.createEntity(entityDto);

        final Long entityId = entityDto.getId();

        entityService.saveDraftEntityChanges(entityId,
                DraftBuilder.forNewField("disp", "f1name", Long.class.getName()));

        List<FieldDto> fields = entityService.getFields(entityId);
        assertNotNull(fields);
        assertEquals(asList("id", "creator", "owner", "modifiedBy", "creationDate", "modificationDate", "f1name"),
                extract(fields, on(FieldDto.class).getBasic().getName()));

        // check if abandoning works
        entityService.abandonChanges(entityId);
        fields = entityService.getFields(entityId);

        assertNotNull(fields);
        assertEquals(asList("id", "creator", "owner", "modifiedBy", "creationDate", "modificationDate"),
                extract(fields, on(FieldDto.class).getBasic().getName()));

        // check add-edit-commit
        entityService.saveDraftEntityChanges(entityId,
                DraftBuilder.forNewField("disp", "f1name", Long.class.getName()));

        fields = entityService.getFields(entityId);

        assertNotNull(fields);
        assertEquals(7, fields.size());
        FieldDto field = selectFirst(fields, having(on(FieldDto.class).getBasic().getName(), equalTo("f1name")));

        entityService.saveDraftEntityChanges(entityDto.getId(),
                DraftBuilder.forFieldEdit(field.getId(), "basic.displayName", "newDisp"));

        fields = entityService.getFields(entityId);

        assertNotNull(fields);
        assertEquals(asList("Id", "Created By", "Owner", "Modified By", "Creation Date", "Modification Date", "newDisp"),
                extract(fields, on(FieldDto.class).getBasic().getDisplayName()));
        entityService.commitChanges(entityId);

        // check if changes were persisted in db
        Entity entityFromDb = getEntities().get(0);
        assertEquals(7, entityFromDb.getFields().size());

        Field fieldFromDb = entityFromDb.getField("f1name");
        assertNotNull(fieldFromDb);
        assertEquals("newDisp", fieldFromDb.getDisplayName());

        // no drafts in db
        assertTrue(entityService.listWorkInProgress().isEmpty());
    }

    @Test
    public void shouldNotAddFieldsWithTheSameNameToAnEntity() throws IOException {
        EntityDto entityDto = new EntityDto();
        entityDto.setName("SameFieldTest");
        entityDto = entityService.createEntity(entityDto);

        FieldDto field1 = new FieldDto();
        field1.setBasic(new FieldBasicDto("disp", "name"));
        field1.setType(typeService.findType(Boolean.class));
        FieldDto field2 = new FieldDto();
        field2.setBasic(new FieldBasicDto("dispName2", "name"));
        field2.setType(typeService.findType(Integer.class));

        entityService.addFields(entityDto, asList(field1, field2));

        List<FieldDto> fieldsFromDb = entityService.getFields(entityDto.getId());

        assertNotNull(fieldsFromDb);
        assertEquals(7, fieldsFromDb.size());
        assertEquals(asList("Id", "Created By", "Owner", "Modified By", "Creation Date", "Modification Date", "dispName2"),
                extract(fieldsFromDb, on(FieldDto.class).getBasic().getDisplayName()));
    }

    @Test
    public void shouldAddLookups() throws IOException {
        EntityDto entityDto = new EntityDto();
        entityDto.setName("LookupTestEnt");
        entityDto = entityService.createEntity(entityDto);

        FieldDto boolField = FieldTestHelper.fieldDto("boolField", Boolean.class);
        FieldDto dtField = FieldTestHelper.fieldDto("dtField", DateTime.class);
        FieldDto strField = FieldTestHelper.fieldDto("strField", String.class);

        entityService.addFields(entityDto, asList(boolField, dtField, strField));

        List<LookupFieldDto> lookupFieldDtos = lookupFieldsFromNames("boolField", "dtField", "strField");
        lookupFieldDtos.get(1).setType(RANGE);
        lookupFieldDtos.get(2).setType(SET);

        LookupDto lookup = new LookupDto("lookup", false, false, lookupFieldDtos, true);

        entityService.addLookups(entityDto.getId(), asList(lookup));

        LookupDto lookupFromDb = entityService.getLookupByName(entityDto.getId(), "lookup");

        assertNotNull(lookupFromDb);
        assertEquals("lookup", lookupFromDb.getLookupName());
        assertEquals("lookup", lookupFromDb.getMethodName());
        assertEquals(asList("boolField", "dtField", "strField"),
                extract(lookupFromDb.getLookupFields(), on(LookupFieldDto.class).getName()));
        assertEquals(asList(VALUE, RANGE, SET),
                extract(lookupFromDb.getLookupFields(), on(LookupFieldDto.class).getType()));
        assertEquals(asList("boolField", "dtField", "strField"), lookupFromDb.getFieldsOrder());
    }

    @Test
    public void shouldUpdateRestOptions() throws IOException {
        // given
        EntityDto entityDto = new EntityDto();
        entityDto.setName("RestTestEnt");
        entityDto = entityService.createEntity(entityDto);

        FieldDto boolField = FieldTestHelper.fieldDto("boolField", Boolean.class);
        FieldDto dtField = FieldTestHelper.fieldDto("dtField", DateTime.class);
        FieldDto strField = FieldTestHelper.fieldDto("strField", String.class);

        entityService.addFields(entityDto, asList(boolField, dtField, strField));

        List<LookupFieldDto> lookupFieldDtos = lookupFieldsFromNames("boolField", "dtField", "strField");
        LookupDto lookup = new LookupDto("lookup", false, false, lookupFieldDtos, true);

        List<LookupFieldDto> strLookupFieldDtos = lookupFieldsFromNames("strField");
        LookupDto strLookup = new LookupDto("strLookup", false, false, strLookupFieldDtos, true);

        entityService.addLookups(entityDto.getId(), asList(lookup, strLookup));

        RestOptionsDto restOptionsDto = new RestOptionsDto(true, false, true, false, false);

        restOptionsDto.addField("boolField");
        restOptionsDto.addField("strField");
        restOptionsDto.addLookup("strLookup");

        // when
        entityService.updateRestOptions(entityDto.getId(), restOptionsDto);

        // then
        AdvancedSettingsDto advancedSettingsDto = entityService.getAdvancedSettings(entityDto.getId(), true);
        RestOptionsDto fromDb = advancedSettingsDto.getRestOptions();

        assertTrue(fromDb.isCreate());
        assertFalse(fromDb.isRead());
        assertTrue(fromDb.isUpdate());
        assertFalse(fromDb.isDelete());

        assertEquals(asList("strLookup"), fromDb.getLookupNames());
        assertEquals(asList("boolField", "strField"), fromDb.getFieldNames());
    }

    @Test
    public void testRelatedFieldGenerationForManyToManyRelationship() {
        EntityDto entityDto1 = new EntityDto();
        entityDto1.setName("RelationTestEnt1");
        entityDto1 = entityService.createEntity(entityDto1);
        EntityDto entityDto2 = new EntityDto();
        entityDto2.setName("RelationTestEnt2");
        entityDto2 = entityService.createEntity(entityDto2);
        EntityDto entityDto3 = new EntityDto();
        entityDto3.setName("RelationTestEnt3");
        entityDto3 = entityService.createEntity(entityDto3);

        EntityDraft entityDraft1 = entityService.getEntityDraft(entityDto1.getId());
        Set<Lookup> fieldLookups = new HashSet<>();
        Field field = new Field(entityDraft1, "newField", "Display Name", fieldLookups);
        Type type = allTypes.retrieveByClassName(TypeDto.MANY_TO_MANY_RELATIONSHIP.getTypeClass());
        field.setType(type);
        if (type.hasSettings()) {
            for (TypeSetting setting : type.getSettings()) {
                field.addSetting(new FieldSetting(field, setting));
            }
        }

        FieldMetadata metadata = new FieldMetadata(field, RELATED_CLASS);
        metadata.setValue(entityDto2.getClassName());
        field.addMetadata(metadata);
        metadata = new FieldMetadata(field, RELATIONSHIP_COLLECTION_TYPE);
        metadata.setValue("java.util.Set");
        field.addMetadata(metadata);
        metadata = new FieldMetadata(field, RELATED_FIELD);
        metadata.setValue("relatedField");
        field.addMetadata(metadata);
        metadata = new FieldMetadata(field, OWNING_SIDE);
        metadata.setValue("true");
        entityDraft1.addField(field);
        allEntityDrafts.update(entityDraft1);

        entityService.commitChanges(entityDto1.getId());

        FieldDto relatedField = getField(entityDto2.getId(), "relatedField");

        //Changing related class
        entityDraft1 = entityService.getEntityDraft(entityDto1.getId());
        DraftData draftData = DraftBuilder.forFieldEdit(entityDraft1.getField("newField").getId(), "metadata.0.value", entityDto3.getClassName());
        entityService.saveDraftEntityChanges(entityDto1.getId(), draftData);
        entityService.commitChanges(entityDto1.getId());
        //We changed related entity, so the old related entity field must be removed
        relatedField = getField(entityDto2.getId(), "relatedField");
        assertNull(relatedField);

        relatedField = getField(entityDto3.getId(), "relatedField");
        assertRelatedField(entityDto1, relatedField, "java.util.Set");

        entityDraft1 = entityService.getEntityDraft(entityDto1.getId());
        draftData = DraftBuilder.forFieldEdit(entityDraft1.getField("newField").getId(), "metadata.1.value", "java.util.List");
        entityService.saveDraftEntityChanges(entityDto1.getId(), draftData);
        draftData = DraftBuilder.forFieldEdit(entityDraft1.getField("newField").getId(), "metadata.2.value", "newNameForRelatedField");
        entityService.saveDraftEntityChanges(entityDto1.getId(), draftData);
        entityService.commitChanges(entityDto1.getId());
        relatedField = getField(entityDto3.getId(), "newNameForRelatedField");
        assertRelatedField(entityDto1, relatedField, "java.util.List");

        entityDraft1 = entityService.getEntityDraft(entityDto1.getId());
        draftData = DraftBuilder.forFieldRemoval(entityDraft1.getField("newField").getId());
        entityService.saveDraftEntityChanges(entityDto1.getId(), draftData);
        entityService.commitChanges(entityDto1.getId());
        assertNull(getField(entityDto1.getId(), "newField"));
        assertNull(getField(entityDto3.getId(), "newNameForRelatedField"));
    }

    @Test
    public void shouldAddLookupWithRelatedField() {
        EntityDto entityWithLookup = new EntityDto();
        entityWithLookup.setName("entityWithLookup");
        entityWithLookup = entityService.createEntity(entityWithLookup);
        EntityDto relatedEntity = new EntityDto();
        relatedEntity.setName("relatedEntity");
        relatedEntity = entityService.createEntity(relatedEntity);

        FieldDto stringField = FieldTestHelper.fieldDto("stringField", String.class);
        FieldDto nameField = FieldTestHelper.fieldDto("nameField", String.class);
        FieldDto lengthField = FieldTestHelper.fieldDto("lengthField", Long.class);
        FieldDto relationField = FieldTestHelper.fieldDto("relatedField", TypeDto.ONE_TO_MANY_RELATIONSHIP.getTypeClass());
        relationField.addMetadata(new MetadataDto(RELATED_CLASS, relatedEntity.getClassName()));

        entityService.addFields(entityWithLookup,  asList(stringField, relationField));
        entityService.addFields(relatedEntity, asList(nameField, lengthField));
        List<LookupFieldDto> lookupFieldDtos = lookupFieldsFromNames("relatedField.nameField", "relatedField.lengthField", "stringField");
        LookupDto lookup = new LookupDto("lookup", false, false, lookupFieldDtos, true);

        entityService.addLookups(entityWithLookup.getId(), lookup);

        LookupDto lookupFromDb = entityService.getLookupByName(entityWithLookup.getId(), "lookup");
        assertNotNull(lookupFromDb);
        assertEquals(asList("relatedField.nameField", "relatedField.lengthField", "stringField"), lookupFromDb.getFieldsOrder());
        assertEquals(asList("relatedField", "relatedField", "stringField"),
                extract(lookupFromDb.getLookupFields(), on(LookupFieldDto.class).getName()));
    }

    @Test
    public void shouldUpdateFetchDepth() {
        EntityDto entityDto = new EntityDto();
        entityDto.setName("FetchDepthTest");

        entityDto = entityService.createEntity(entityDto);
        assertNull(entityDto.getMaxFetchDepth());

        entityService.updateMaxFetchDepth(entityDto.getId(), 3);
        entityDto = entityService.getEntity(entityDto.getId());
        assertEquals(Integer.valueOf(3), entityDto.getMaxFetchDepth());
    }

    private void assertRelatedField(EntityDto relatedEntity, FieldDto relatedField, String collection) {
        assertNotNull(relatedField);
        assertEquals(relatedField.getMetadata(RELATED_FIELD).getValue(), "newField");
        assertEquals(relatedField.getMetadata(RELATIONSHIP_COLLECTION_TYPE).getValue(), collection);
        assertEquals(relatedField.getMetadata(RELATED_CLASS).getValue(), relatedEntity.getClassName());
        assertNull(relatedField.getMetadata(OWNING_SIDE));
    }

    private FieldDto getField(Long entityId, String fieldName) {
        List<FieldDto> fields = entityService.getEntityFields(entityId);
        FieldDto field = null;
        for (FieldDto f : fields) {
            if (f.getBasic().getName().equals(fieldName)) {
                field = f;
                break;
            }
        }
        return field;
    }

    private void setUpSecurityContext() {
        SimpleGrantedAuthority authority = new SimpleGrantedAuthority("mdsSchemaAccess");
        List<SimpleGrantedAuthority> authorities = asList(authority);

        User principal = new User("motech", "motech", authorities);

        Authentication authentication = new UsernamePasswordAuthenticationToken(principal, "motech", authorities);

        SecurityContext securityContext = new SecurityContextImpl();
        securityContext.setAuthentication(authentication);

        SecurityContextHolder.setContext(securityContext);
    }
}


text:
package dpla.ingestion3.mappers.utils

case class Document[T](value: T) {
  def get: T = value
}

redpajama_set_name: RedPajamaGithub


text:
//-----------------------------------------------------------------------
// <copyright file="TrackWithDetailsWrapper.cs" company="IxoneCz">
//  Copyright (c) 2011 Tomas Pastorek, www.Ixone.Cz. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotLastFm.Models.Wrappers
{
    using RestSharp.Deserializers;

    /// <summary>
    /// Track with details wrapper
    /// </summary>
    public class TrackWithDetailsWrapper
    {
        /// <summary>
        /// Gets or sets the track.
        /// </summary>
        /// <value>
        /// The track.
        /// </value>
        [DeserializeAs(Name = "track")]
        public TrackWithDetails Track
        {
            get;
            set;
        }
    }
}


text:
require "slim_fast/engine"

module SlimFast
end


text:
package com.bxtel.security5.auth.exceiption;

/**
 * Thrown if an {@link UserDetailsService} implementation cannot locate a {@link User} by its username.
 *
 * @author Ben Alex
 */
public class UserNotLogin extends AuthenticationException {
    //~ Constructors ===================================================================================================
    /**
     * Constructs a UsernameNotFoundException with the specified
     * message.
     *
     * @param msg the detail message.
     */
    public UserNotLogin(String msg) {
        super(msg);
    }
    /**
     * Constructs a {@code UsernameNotFoundException} with the specified message and root cause.
     *
     * @param msg the detail message.
     * @param t root cause
     */
    public UserNotLogin(String msg, Throwable t) {
        super(msg, t);
    }
}


text:
@import Foundation;

@interface ClassWithHandlerMethods
+ (void)firstBoolFlagSuccess:(NSString *_Nonnull)str
                  completion:(void (^_Nonnull)(NSString *_Nullable, BOOL, BOOL,
                                               NSError *_Nullable))handler
    __attribute__((swift_async_error(zero_argument, 2)));

+ (void)secondBoolFlagFailure:(NSString *_Nonnull)str
                   completion:(void (^_Nonnull)(NSString *_Nullable, BOOL, BOOL,
                                                NSError *_Nullable))handler
    __attribute__((swift_async_error(nonzero_argument, 3)));
@end


text:
<?php

namespace Sbtmapp\UserBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class SbtmappUserBundle extends Bundle {

    public function getParent() {
        return 'FOSUserBundle';
    }

}


text:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <display-name>Lognition Web Application</display-name>
    <filter>
        <filter-name>filter</filter-name>
        <filter-class>com.redsaz.lognition.view.StaticContentFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.redsaz.lognition.view.LognitionApplication</param-value>
        </init-param>
    </servlet>
    <context-param>
        <param-name>resteasy.servlet.mapping.prefix</param-name>
        <param-value>/</param-value>
    </context-param>
    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>


text:
{% extends "base.html" %}

{% block title %}{{ page_title }} | VPR{% endblock %}

{% block content %}

    {% include "_above_fold.html" %}

    {% include "_nav.html" %}

    {% include "_books.html" %}
    {% include "_about.html" %}
    {% include "_scrapbook.html" %}

{% endblock %}

{% block scripts %}

{% endblock %}


text:
import Ember from 'ember'

export default Ember.Route.extend({

  model: function () {
    return this.store.createRecord('payment-method', {
      billingAddress: this.store.createRecord('address')
    })
  }

})


text:
export var a = 1;
class c5 { public foo() { } }
module c5 { } // should be ok everywhere

//// [augmentedTypesExternalModule1.js]
define(["require", "exports"], function(require, exports) {
    exports.a = 1;
    var c5 = (function () {
        function c5() {
        }
        c5.prototype.foo = function () {
        };
        return c5;
    })();
});


text: find_average = lambda nums: float(sum(nums)) / len(nums) if nums else 0


text:
package org.apache.bookkeeper.mledger.offload.jcloud.impl;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.mledger.Entry;
import org.apache.pulsar.common.allocator.PulsarByteBufAllocator;

@Slf4j
public class BufferedOffloadStream extends InputStream {
    static final int[] BLOCK_END_PADDING = BlockAwareSegmentInputStreamImpl.BLOCK_END_PADDING;

    private final long ledgerId;
    private final long beginEntryId;

    public BufferedOffloadStream(int blockSize, List<Entry> entries, long ledgerId, long beginEntryId) {
        this.ledgerId = ledgerId;
        this.beginEntryId = beginEntryId;
        this.endEntryId = beginEntryId;
        this.blockSize = blockSize;
        this.entryBuffer = entries;
        this.blockHead = StreamingDataBlockHeaderImpl.of(blockSize, ledgerId, beginEntryId)
                .toStream();
    }

    public long getEndEntryId() {
        return endEntryId;
    }

    private volatile long endEntryId;
    static final int ENTRY_HEADER_SIZE = 4 /* entry size */ + 8 /* entry id */;
    private final long blockSize;
    private final List<Entry> entryBuffer;
    private final InputStream blockHead;
    int offset = 0;
    static final int NOT_INITIALIZED = -1;
    int validDataOffset = NOT_INITIALIZED;
    CompositeByteBuf currentEntry;

    public long getLedgerId() {
        return ledgerId;
    }

    public long getBeginEntryId() {
        return beginEntryId;
    }

    public long getBlockSize() {
        return blockSize;
    }

    @Override
    public int read() throws IOException {
        if (blockHead.available() > 0) {
            offset++;
            return blockHead.read();
        }
        //if current exists, use current first
        if (currentEntry != null) {
            if (currentEntry.readableBytes() > 0) {
                offset += 1;
                return currentEntry.readUnsignedByte();
            } else {
                currentEntry.release();
                currentEntry = null;
            }
        }

        if (blockSize <= offset) {
            return -1;
        } else if (validDataOffset != NOT_INITIALIZED) {
            return BLOCK_END_PADDING[(offset++ - validDataOffset) % BLOCK_END_PADDING.length];
        }

        if (entryBuffer.isEmpty()) {
            validDataOffset = offset;
            return read();
        }

        Entry headEntry = entryBuffer.remove(0);

        //create new block when a ledger end
        if (headEntry.getLedgerId() != this.ledgerId) {
            throw new RuntimeException(
                    String.format("there should not be multi ledger in a block %s %s", headEntry.getLedgerId(),
                            this.ledgerId));
        }

        final int entryLength = headEntry.getLength();
        final long entryId = headEntry.getEntryId();
        CompositeByteBuf entryBuf = PulsarByteBufAllocator.DEFAULT.compositeBuffer(2);
        ByteBuf entryHeaderBuf = PulsarByteBufAllocator.DEFAULT.buffer(ENTRY_HEADER_SIZE, ENTRY_HEADER_SIZE);
        entryHeaderBuf.writeInt(entryLength).writeLong(entryId);
        entryBuf.addComponents(true, entryHeaderBuf, headEntry.getDataBuffer().retain());
        endEntryId = headEntry.getEntryId();
        headEntry.release();
        currentEntry = entryBuf;
        return read();

    }

    @Override
    public void close() throws IOException {
        blockHead.close();
    }

    public static int calculateBlockSize(int streamingBlockSize, int entryCount, int entrySize) {
        int validDataSize = (entryCount * ENTRY_HEADER_SIZE
                + entrySize
                + StreamingDataBlockHeaderImpl.getDataStartOffset());
        return Math.max(streamingBlockSize, validDataSize);
    }
}


text:
START_ATF_NAMESPACE
    namespace Info
    {

        using _personal_automine_stop_zoclctor__personal_automine_stop_zocl2_ptr = void (WINAPIV*)(struct _personal_automine_stop_zocl*);
        using _personal_automine_stop_zoclctor__personal_automine_stop_zocl2_clbk = void (WINAPIV*)(struct _personal_automine_stop_zocl*, _personal_automine_stop_zoclctor__personal_automine_stop_zocl2_ptr);
        using _personal_automine_stop_zoclsize4_ptr = int (WINAPIV*)(struct _personal_automine_stop_zocl*);
        using _personal_automine_stop_zoclsize4_clbk = int (WINAPIV*)(struct _personal_automine_stop_zocl*, _personal_automine_stop_zoclsize4_ptr);
    }; // end namespace Info
END_ATF_NAMESPACE


text:
/* Left will inherit from right (so we don't need to duplicate code) */
.github-fork-ribbon {
    /* The right and left classes determine the side we attach our banner to */
    position: absolute;

    /* Add a bit of padding to give some substance outside the "stitching" */
    padding: 2px 0;

    /* Set the base colour */
    background-color: #111;

    /* Set a gradient: transparent black at the top to almost-transparent black at the bottom */
    background-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0, 0, 0, 0)), to(rgba(0, 0, 0, 0.15)));
    background-image: -webkit-linear-gradient(top, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.15));
    background-image: -moz-linear-gradient(top, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.15));
    background-image: -ms-linear-gradient(top, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.15));
    background-image: -o-linear-gradient(top, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.15));
    background-image: linear-gradient(to bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.15));

    /* Add a drop shadow */
    -webkit-box-shadow: 0 2px 3px 0 rgba(0, 0, 0, 0.5);
    -moz-box-shadow: 0 2px 3px 0 rgba(0, 0, 0, 0.5);
    box-shadow: 0 2px 3px 0 rgba(0, 0, 0, 0.5);

    /* Set the font */
    /*font: 700 13px "Helvetica Neue", Helvetica, Arial, sans-serif;*/
    font: 18px Visitor;

    z-index: 9999;
    pointer-events: auto;
}

.github-fork-ribbon a,
.github-fork-ribbon a:hover {
    /* Set the text properties */
    color: #888;
    text-decoration: none;
    text-shadow: 0 -1px rgba(0, 0, 0, 0.5);
    text-align: center;

    /* Set the geometry. If you fiddle with these you'll also need
       to tweak the top and right values in .github-fork-ribbon. */
    width: 200px;
    line-height: 20px;

    /* Set the layout properties */
    display: inline-block;
    padding: 2px 0;

    /* Add "stitching" effect */
    border-width: 1px 0;
    border-style: dotted;
    border-color: #666;
    border-color: rgba(96, 96, 96, 0.7);
}

.github-fork-ribbon a {
    color: #666;
}

.github-fork-ribbon-wrapper {
    width: 150px;
    height: 150px;
    position: absolute;
    overflow: hidden;
    top: 0;
    z-index: 9999;
    pointer-events: none;
}

.github-fork-ribbon-wrapper.fixed {
    position: fixed;
}

.github-fork-ribbon-wrapper.left {
    left: 0;
}

.github-fork-ribbon-wrapper.right {
    right: 0;
}

.github-fork-ribbon-wrapper.left-bottom {
    position: fixed;
    top: inherit;
    bottom: 0;
    left: 0;
}

.github-fork-ribbon-wrapper.right-bottom {
    position: fixed;
    top: inherit;
    bottom: 0;
    right: 0;
}

.github-fork-ribbon-wrapper.right .github-fork-ribbon {
    top: 42px;
    right: -43px;

    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    transform: rotate(45deg);
}

.github-fork-ribbon-wrapper.left .github-fork-ribbon {
    top: 42px;
    left: -43px;

    -webkit-transform: rotate(-45deg);
    -moz-transform: rotate(-45deg);
    -ms-transform: rotate(-45deg);
    -o-transform: rotate(-45deg);
    transform: rotate(-45deg);
}

.github-fork-ribbon-wrapper.left-bottom .github-fork-ribbon {
    top: 80px;
    left: -43px;

    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    transform: rotate(45deg);
}

.github-fork-ribbon-wrapper.right-bottom .github-fork-ribbon {
    top: 80px;
    right: -43px;

    -webkit-transform: rotate(-45deg);
    -moz-transform: rotate(-45deg);
    -ms-transform: rotate(-45deg);
    -o-transform: rotate(-45deg);
    transform: rotate(-45deg);
}


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.9.1"/>
<title>RefCnt: Member List | Piles</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="/assets/css/tabs.css" rel="stylesheet" type="text/css"/>

<link href="/assets/css/navtree.css" rel="stylesheet" type="text/css"/>

<link href="/assets/css/styleSheetFile.css" rel="stylesheet" type="text/css" />

<link rel="icon"
  type="image/png"
  href="/assets/images/pile-16.png">
<link rel="shortcut icon"
  type="image/png"
  href="/assets/images/pile-16.png">
<link rel="stylesheet" href="/assets/css/main.css">

Piles

    home
    docs
    piles
    GitHub

RefCnt Pile
     version 0.0.1

C++ class implementing a simple reference counting mechanism.

Main Page

Classes

Files

Class List

Class Index

Class Members

RefCnt Member List

This is the complete list of members for RefCnt, including all inherited members.

changeOwner(void *, void *)	RefCnt	inline

dereference(void *owner)	RefCnt

forceRelease()	RefCnt

isOwner(void *)	RefCnt	inline

RefCnt()	RefCnt	protected

reference(void *owner)	RefCnt

referenceCount()	RefCnt	inline

release(void *owner)	RefCnt

~RefCnt()	RefCnt	protectedvirtual

<address class="footer"></address>


text:
// g2o - General Graph Optimization
// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "g2o/core/estimate_propagator.h"

#include <queue>
#include <vector>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <fstream>

//#define DEBUG_ESTIMATE_PROPAGATOR

using namespace std;

namespace g2o {

# ifdef DEBUG_ESTIMATE_PROPAGATOR
  struct FrontierLevelCmp {
    bool operator()(EstimatePropagator::AdjacencyMapEntry* e1, EstimatePropagator::AdjacencyMapEntry* e2) const
    {
      return e1->frontierLevel() < e2->frontierLevel();
    }
  };
# endif

  EstimatePropagator::AdjacencyMapEntry::AdjacencyMapEntry()
  {
    reset();
  }

  void EstimatePropagator::AdjacencyMapEntry::reset()
  {
    _child = 0;
    _parent.clear();
    _edge = 0;
    _distance = numeric_limits<double>::max();
    _frontierLevel = -1;
    inQueue = false;
  }

  EstimatePropagator::EstimatePropagator(OptimizableGraph* g): _graph(g)
  {
    for (OptimizableGraph::VertexIDMap::const_iterator it=_graph->vertices().begin(); it!=_graph->vertices().end(); ++it){
      AdjacencyMapEntry entry;
      entry._child = static_cast<OptimizableGraph::Vertex*>(it->second);
      _adjacencyMap.insert(make_pair(entry.child(), entry));
    }
  }

  void EstimatePropagator::reset()
  {
    for (OptimizableGraph::VertexSet::iterator it=_visited.begin(); it!=_visited.end(); ++it){
      OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(*it);
      AdjacencyMap::iterator at = _adjacencyMap.find(v);
      assert(at != _adjacencyMap.end());
      at->second.reset();
    }
    _visited.clear();
  }

  void EstimatePropagator::propagate(OptimizableGraph::Vertex* v,
      const EstimatePropagator::PropagateCost& cost,
       const EstimatePropagator::PropagateAction& action,
       double maxDistance,
       double maxEdgeCost)
  {
    OptimizableGraph::VertexSet vset;
    vset.insert(v);
    propagate(vset, cost, action, maxDistance, maxEdgeCost);
  }

  void EstimatePropagator::propagate(OptimizableGraph::VertexSet& vset,
      const EstimatePropagator::PropagateCost& cost,
       const EstimatePropagator::PropagateAction& action,
       double maxDistance,
       double maxEdgeCost)
  {
    reset();

    PriorityQueue frontier;
    for (OptimizableGraph::VertexSet::iterator vit=vset.begin(); vit!=vset.end(); ++vit){
      OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(*vit);
      AdjacencyMap::iterator it = _adjacencyMap.find(v);
      assert(it != _adjacencyMap.end());
      it->second._distance = 0.;
      it->second._parent.clear();
      it->second._frontierLevel = 0;
      frontier.push(&it->second);
    }

    while(! frontier.empty()){
      AdjacencyMapEntry* entry = frontier.pop();
      OptimizableGraph::Vertex* u = entry->child();
      double uDistance = entry->distance();
      //cerr << "uDistance " << uDistance << endl;

      // initialize the vertex
      if (entry->_frontierLevel > 0) {
        action(entry->edge(), entry->parent(), u);
      }

      /* std::pair< OptimizableGraph::VertexSet::iterator, bool> insertResult = */ _visited.insert(u);
      OptimizableGraph::EdgeSet::iterator et = u->edges().begin();
      while (et != u->edges().end()){
        OptimizableGraph::Edge* edge = static_cast<OptimizableGraph::Edge*>(*et);
        ++et;

        int maxFrontier = -1;
        OptimizableGraph::VertexSet initializedVertices;
        for (size_t i = 0; i < edge->vertices().size(); ++i) {
          OptimizableGraph::Vertex* z = static_cast<OptimizableGraph::Vertex*>(edge->vertex(i));
          AdjacencyMap::iterator ot = _adjacencyMap.find(z);
          if (ot->second._distance != numeric_limits<double>::max()) {
            initializedVertices.insert(z);
            maxFrontier = (max)(maxFrontier, ot->second._frontierLevel);
          }
        }
        assert(maxFrontier >= 0);

        for (size_t i = 0; i < edge->vertices().size(); ++i) {
          OptimizableGraph::Vertex* z = static_cast<OptimizableGraph::Vertex*>(edge->vertex(i));
          if (z == u)
            continue;

          size_t wasInitialized = initializedVertices.erase(z);

          double edgeDistance = cost(edge, initializedVertices, z);
          if (edgeDistance > 0. && edgeDistance != std::numeric_limits<double>::max() && edgeDistance < maxEdgeCost) {
            double zDistance = uDistance + edgeDistance;
            //cerr << z->id() << " " << zDistance << endl;

            AdjacencyMap::iterator ot = _adjacencyMap.find(z);
            assert(ot!=_adjacencyMap.end());

            if (zDistance < ot->second.distance() && zDistance < maxDistance){
              //if (ot->second.inQueue)
                //cerr << "Updating" << endl;
              ot->second._distance = zDistance;
              ot->second._parent = initializedVertices;
              ot->second._edge = edge;
              ot->second._frontierLevel = maxFrontier + 1;
              frontier.push(&ot->second);
            }
          }

          if (wasInitialized > 0)
            initializedVertices.insert(z);

        }
      }
    }

    // writing debug information like cost for reaching each vertex and the parent used to initialize
#ifdef DEBUG_ESTIMATE_PROPAGATOR
    cerr << "Writing cost.dat" << endl;
    ofstream costStream("cost.dat");
    for (AdjacencyMap::const_iterator it = _adjacencyMap.begin(); it != _adjacencyMap.end(); ++it) {
      HyperGraph::Vertex* u = it->second.child();
      costStream << "vertex " << u->id() << "  cost " << it->second._distance << endl;
    }
    cerr << "Writing init.dat" << endl;
    ofstream initStream("init.dat");
    vector<AdjacencyMapEntry*> frontierLevels;
    for (AdjacencyMap::iterator it = _adjacencyMap.begin(); it != _adjacencyMap.end(); ++it) {
      if (it->second._frontierLevel > 0)
        frontierLevels.push_back(&it->second);
    }
    sort(frontierLevels.begin(), frontierLevels.end(), FrontierLevelCmp());
    for (vector<AdjacencyMapEntry*>::const_iterator it = frontierLevels.begin(); it != frontierLevels.end(); ++it) {
      AdjacencyMapEntry* entry       = *it;
      OptimizableGraph::Vertex* to   = entry->child();

      initStream << "calling init level = " << entry->_frontierLevel << "\t (";
      for (OptimizableGraph::VertexSet::iterator pit = entry->parent().begin(); pit != entry->parent().end(); ++pit) {
        initStream << " " << (*pit)->id();
      }
      initStream << " ) -> " << to->id() << endl;
    }
#endif

  }

  void EstimatePropagator::PriorityQueue::push(AdjacencyMapEntry* entry)
  {
    assert(entry != NULL);
    if (entry->inQueue) {
      assert(entry->queueIt->second == entry);
      erase(entry->queueIt);
    }

    entry->queueIt = insert(std::make_pair(entry->distance(), entry));
    assert(entry->queueIt != end());
    entry->inQueue = true;
  }

  EstimatePropagator::AdjacencyMapEntry* EstimatePropagator::PriorityQueue::pop()
  {
    assert(!empty());
    iterator it = begin();
    AdjacencyMapEntry* entry = it->second;
    erase(it);

    assert(entry != NULL);
    entry->queueIt = end();
    entry->inQueue = false;
    return entry;
  }

  EstimatePropagatorCost::EstimatePropagatorCost (SparseOptimizer* graph) :
    _graph(graph)
  {
  }

  double EstimatePropagatorCost::operator()(OptimizableGraph::Edge* edge, const OptimizableGraph::VertexSet& from, OptimizableGraph::Vertex* to_) const
  {
    OptimizableGraph::Edge* e = dynamic_cast<OptimizableGraph::Edge*>(edge);
    OptimizableGraph::Vertex* to = dynamic_cast<OptimizableGraph::Vertex*>(to_);
    SparseOptimizer::EdgeContainer::const_iterator it = _graph->findActiveEdge(e);
    if (it == _graph->activeEdges().end()) // it has to be an active edge
      return std::numeric_limits<double>::max();
    return e->initialEstimatePossible(from, to);
  }

  EstimatePropagatorCostOdometry::EstimatePropagatorCostOdometry(SparseOptimizer* graph) :
    EstimatePropagatorCost(graph)
  {
  }

  double EstimatePropagatorCostOdometry::operator()(OptimizableGraph::Edge* edge, const OptimizableGraph::VertexSet& from_, OptimizableGraph::Vertex* to_) const
  {
    OptimizableGraph::Edge* e = dynamic_cast<OptimizableGraph::Edge*>(edge);
    OptimizableGraph::Vertex* from = dynamic_cast<OptimizableGraph::Vertex*>(*from_.begin());
    OptimizableGraph::Vertex* to = dynamic_cast<OptimizableGraph::Vertex*>(to_);
    if (std::abs(from->id() - to->id()) != 1) // simple method to identify odometry edges in a pose graph
      return std::numeric_limits<double>::max();
    SparseOptimizer::EdgeContainer::const_iterator it = _graph->findActiveEdge(e);
    if (it == _graph->activeEdges().end()) // it has to be an active edge
      return std::numeric_limits<double>::max();
    return e->initialEstimatePossible(from_, to);
  }

} // end namespace


text:
#######################DISA INFORMATION###############################
#Group ID (Vulid): V-762
#Group Title: Accounts have been assigned the same User Id
#Rule ID: SV-27068r1_rule
#Severity: CAT II
#Rule Version (STIG-ID): GEN000320
#Rule Title: All accounts must be assigned unique User Identification
#Numbers (UIDs).
#
#Vulnerability Discussion: Accounts that share a UID have full access
#to each others' files. This has the same effect as sharing a login.
#There is no way to assure identification, authentication, and
#accountability because the system sees them as the same user.
#If the duplicate UID is 0, this gives potential intruders
#another privileged account to attack.
#
#Responsibility: System Administrator
#IAControls: IAIA-1, IAIA-2
#
#Check Content:
#Perform the following to ensure that there are no duplicate UIDs:
# pwck -r
#If any duplicate UIDs are found, this is a finding.
#
#Fix Text: Edit user accounts to provide unique UIDs for each account.
#######################DISA INFORMATION###############################

#Global Variables#
PDI=GEN000320

#Start-Lockdown
DUPUID=`awk -F ':' '{print $3}' /etc/passwd | sort | uniq -d | tr "\n" " "`
if [ "$DUPUID" != "" ]
then
  echo "------------------------------" > $PDI-error.log
  date >> $PDI-error.log
  echo " " >> $PDI-error.log
  echo "The following uids are duplicates: $DUPUID" >> $PDI-error.log
  echo " " >> $PDI-error.log
  echo "Please review these accounts" >> $PDI-error.log
  echo "------------------------------" >> $PDI-error.log
fi


text:
require "spec_helper"
require 'uri'

module Outreach
  describe Auth do
    before(:all) do
      Outreach.configure do |config|
        config.client_id     = "client_id"
        config.client_secret = "client_secret"
        config.redirect_uri  = "http://localhost"
        config.permissions   = [
          Outreach::READ_SEQUENCES_PERMISSION,
          Outreach::CREATE_PROSPECTS_PERMISSION,
          Outreach::UPDATE_PROSPECTS_PERMISSION,
        ]
      end
    end

    describe "authorize_url" do
      it "creates a url with config permissions" do
        url = Outreach::Auth.authorize_url

        expect(url).to match(/https:\/\/api.outreach.io\/oauth\/authorize/)
        expect(url).to match(/client_id=client_id/)
        expect(url).to match(/redirect_uri=#{URI.encode_www_form_component('http://localhost')}/)
        expect(url).to match(/scope=#{Regexp.quote(URI.encode_www_form_component(Outreach.configuration.permissions.join(" ")))}/)
      end

      it "creates a url with specified permissions" do
        url = Outreach::Auth.authorize_url(Outreach::READ_SEQUENCES_PERMISSION, Outreach::READ_TAGS_PERMISSION)

        expect(url).to match(/https:\/\/api.outreach.io\/oauth\/authorize/)
        expect(url).to match(/client_id=client_id/)
        expect(url).to match(/redirect_uri=#{URI.encode_www_form_component('http://localhost')}/)
        expect(url).to match(/scope=#{Regexp.quote(URI.encode_www_form_component([Outreach::READ_SEQUENCES_PERMISSION, Outreach::READ_TAGS_PERMISSION].join(" ")))}/)
      end
    end

    describe "initialize" do
      it "returns an Auth instance" do
        auth = Outreach::Auth.new "ACCESS_TOKEN", "REFRESH_TOKEN", (Time.now + 3600).to_s
        expect(auth.class).to be Outreach::Auth
      end

      it "sets tokens and expiration time" do
        auth = Outreach::Auth.new "ACCESS_TOKEN", "REFRESH_TOKEN", (Time.now + 3600).to_s

        expect(auth.access_token).to eq "ACCESS_TOKEN"
        expect(auth.refresh_token).to eq "REFRESH_TOKEN"
        expect(auth.expires).to_not be nil
      end

      it "sets the headers variable" do
        auth = Outreach::Auth.new "ACCESS_TOKEN", "REFRESH_TOKEN", (Time.now + 3600).to_s

        expect(auth.instance_variable_get(:@headers)['Authorization']).to eq "Bearer ACCESS_TOKEN"
      end
    end

    describe "authorize" do
      describe "successful response" do
        before do
          resp = double("response")
          allow(resp).to receive(:code).and_return(200)
          allow(resp).to receive(:parsed_response).and_return( {
            'access_token' => 'NEW_ACCESS_TOKEN',
            'refresh_token' => 'NEW_REFRESH_TOKEN',
            'created_at' => Time.now,
            'expires_in' => 7200
          } )
          expect(Outreach::Auth).to receive(:post).and_return(resp)
        end

        it "returns a Auth object" do
          auth = Outreach::Auth.authorize('CODE')
          expect(auth).to be_a_kind_of(Outreach::Auth)
        end
      end

      describe "failure response" do
        before do
          resp = double("response")
          allow(resp).to receive(:code).and_return(401)
          allow(resp).to receive(:message).and_return("test error")
          expect(Outreach::Auth).to receive(:post).and_return(resp)
        end

        it "raises an error" do
          expect {
            Outreach::Auth.authorize('CODE')
          }.to raise_error "Unexpected response for Outreach authorization (401): test error"
        end
      end
    end
  end
end


text:
import {NgModule} from "@angular/core";
import {RouterModule} from "@angular/router";
import {TrustedHtmlFullModule} from "./full/demo.module";

import {TrustedHtmlFullComponent} from "./full/demo.component";

export const routerConfig = [
    {
        path: 'full', component: TrustedHtmlFullComponent
    },
];

@NgModule({
    imports: [
        RouterModule.forChild(routerConfig),
        TrustedHtmlFullModule,
    ],
    exports: [
    ],
    providers: []
})
export class TrustedHtmlDemoModule { }


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>GCancellable</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="index.html" title="GIO Reference Manual">
<link rel="up" href="async.html" title="Asynchronous I/O">
<link rel="prev" href="async.html" title="Asynchronous I/O">
<link rel="next" href="GAsyncResult.html" title="GAsyncResult">
<meta name="generator" content="GTK-Doc V1.19 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">

GIO Reference Manual

Top
                   |
                  Description
                   |
                  Object Hierarchy
                   |
                  Signals

GCancellable

GCancellable — Thread-safe Operation Cancellation Stack

Synopsis

#include <gio/gio.h>

                    GCancellable;
GCancellable *      g_cancellable_new                   (void);
gboolean            g_cancellable_is_cancelled          (GCancellable *cancellable);
gboolean            g_cancellable_set_error_if_cancelled
                                                        (GCancellable *cancellable,
                                                         GError **error);
int                 g_cancellable_get_fd                (GCancellable *cancellable);
gboolean            g_cancellable_make_pollfd           (GCancellable *cancellable,
                                                         GPollFD *pollfd);
void                g_cancellable_release_fd            (GCancellable *cancellable);
GSource *           g_cancellable_source_new            (GCancellable *cancellable);
gboolean            (*GCancellableSourceFunc)           (GCancellable *cancellable,
                                                         gpointer user_data);
GCancellable *      g_cancellable_get_current           (void);
void                g_cancellable_pop_current           (GCancellable *cancellable);
void                g_cancellable_push_current          (GCancellable *cancellable);
void                g_cancellable_reset                 (GCancellable *cancellable);
gulong              g_cancellable_connect               (GCancellable *cancellable,
                                                         GCallback callback,
                                                         gpointer data,
                                                         GDestroyNotify data_destroy_func);
void                g_cancellable_disconnect            (GCancellable *cancellable,
                                                         gulong handler_id);
void                g_cancellable_cancel                (GCancellable *cancellable);

Object Hierarchy

  GObject
   +----GCancellable

Signals

  "cancelled"                                      : Run Last

Description

GCancellable is a thread-safe operation cancellation stack used
throughout GIO to allow for cancellation of synchronous and
asynchronous operations.

Details

GCancellable

typedef struct _GCancellable GCancellable;

Allows actions to be cancelled.

g_cancellable_new ()

GCancellable *      g_cancellable_new                   (void);

Creates a new GCancellable object.

Applications that want to start one or more operations
that should be cancellable should create a GCancellable
and pass it to the operations.

One GCancellable can be used in multiple consecutive
operations or in multiple concurrent operations.

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

Returns :

a GCancellable.

g_cancellable_is_cancelled ()

gboolean            g_cancellable_is_cancelled          (GCancellable *cancellable);

Checks if a cancellable job has been cancelled.

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

cancellable :

a GCancellable or NULL. [<acronym title="NULL is ok, both for passing and for returning.">allow-none</acronym>]

Returns :

TRUE if cancellable is cancelled,
FALSE if called with NULL or if item is not cancelled.

g_cancellable_set_error_if_cancelled ()

gboolean            g_cancellable_set_error_if_cancelled
                                                        (GCancellable *cancellable,
                                                         GError **error);

If the cancellable is cancelled, sets the error to notify
that the operation was cancelled.

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

cancellable :

a GCancellable or NULL. [<acronym title="NULL is ok, both for passing and for returning.">allow-none</acronym>]

error :

GError to append error state to

Returns :

TRUE if cancellable was cancelled, FALSE if it was not

g_cancellable_get_fd ()

int                 g_cancellable_get_fd                (GCancellable *cancellable);

Gets the file descriptor for a cancellable job. This can be used to
implement cancellable operations on Unix systems. The returned fd will
turn readable when cancellable is cancelled.

You are not supposed to read from the fd yourself, just check for
readable status. Reading to unset the readable status is done
with g_cancellable_reset().

After a successful return from this function, you should use
g_cancellable_release_fd() to free up resources allocated for
the returned file descriptor.

See also g_cancellable_make_pollfd().

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

cancellable :

a GCancellable.

Returns :

A valid file descriptor. -1 if the file descriptor
is not supported, or on errors.

g_cancellable_make_pollfd ()

gboolean            g_cancellable_make_pollfd           (GCancellable *cancellable,
                                                         GPollFD *pollfd);

Creates a GPollFD corresponding to cancellable; this can be passed
to g_poll() and used to poll for cancellation. This is useful both
for unix systems without a native poll and for portability to
windows.

When this function returns TRUE, you should use
g_cancellable_release_fd() to free up resources allocated for the
pollfd. After a FALSE return, do not call g_cancellable_release_fd().

If this function returns FALSE, either no cancellable was given or
resource limits prevent this function from allocating the necessary
structures for polling. (On Linux, you will likely have reached
the maximum number of file descriptors.) The suggested way to handle
these cases is to ignore the cancellable.

You are not supposed to read from the fd yourself, just check for
readable status. Reading to unset the readable status is done
with g_cancellable_reset().

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

cancellable :

a GCancellable or NULL. [<acronym title="NULL is ok, both for passing and for returning.">allow-none</acronym>]

pollfd :

a pointer to a GPollFD

Returns :

TRUE if pollfd was successfully initialized, FALSE on
failure to prepare the cancellable.

Since 2.22

g_cancellable_release_fd ()

void                g_cancellable_release_fd            (GCancellable *cancellable);

Releases a resources previously allocated by g_cancellable_get_fd()
or g_cancellable_make_pollfd().

For compatibility reasons with older releases, calling this function
is not strictly required, the resources will be automatically freed
when the cancellable is finalized. However, the cancellable will
block scarce file descriptors until it is finalized if this function
is not called. This can cause the application to run out of file
descriptors when many GCancellables are used at the same time.

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

cancellable :

a GCancellable

Since 2.22

g_cancellable_source_new ()

GSource *           g_cancellable_source_new            (GCancellable *cancellable);

Creates a source that triggers if cancellable is cancelled and
calls its callback of type GCancellableSourceFunc. This is
primarily useful for attaching to another (non-cancellable) source
with g_source_add_child_source() to add cancellability to it.

For convenience, you can call this with a NULL GCancellable,
in which case the source will never trigger.

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

cancellable :

a GCancellable, or NULL. [<acronym title="NULL is ok, both for passing and for returning.">allow-none</acronym>]

Returns :

the new GSource. [<acronym title="Free data after the code is done.">transfer full</acronym>]

Since 2.28

GCancellableSourceFunc ()

gboolean            (*GCancellableSourceFunc)           (GCancellable *cancellable,
                                                         gpointer user_data);

This is the function type of the callback used for the GSource
returned by g_cancellable_source_new().

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

cancellable :

the GCancellable

user_data :

data passed in by the user.

Returns :

it should return FALSE if the source should be removed.

Since 2.28

g_cancellable_get_current ()

GCancellable *      g_cancellable_get_current           (void);

Gets the top cancellable from the stack.

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

Returns :

a GCancellable from the top of the stack, or NULL
if the stack is empty. [<acronym title="Don't free data after the code is done.">transfer none</acronym>]

g_cancellable_pop_current ()

void                g_cancellable_pop_current           (GCancellable *cancellable);

Pops cancellable off the cancellable stack (verifying that cancellable
is on the top of the stack).

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

cancellable :

a GCancellable object

g_cancellable_push_current ()

void                g_cancellable_push_current          (GCancellable *cancellable);

Pushes cancellable onto the cancellable stack. The current
cancellable can then be received using g_cancellable_get_current().

This is useful when implementing cancellable operations in
code that does not allow you to pass down the cancellable object.

This is typically called automatically by e.g. GFile operations,
so you rarely have to call this yourself.

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

cancellable :

a GCancellable object

g_cancellable_reset ()

void                g_cancellable_reset                 (GCancellable *cancellable);

Resets cancellable to its uncancelled state.

If cancellable is currently in use by any cancellable operation
then the behavior of this function is undefined.

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

cancellable :

a GCancellable object.

g_cancellable_connect ()

gulong              g_cancellable_connect               (GCancellable *cancellable,
                                                         GCallback callback,
                                                         gpointer data,
                                                         GDestroyNotify data_destroy_func);

Convenience function to connect to the "cancelled"
signal. Also handles the race condition that may happen
if the cancellable is cancelled right before connecting.

callback is called at most once, either directly at the
time of the connect if cancellable is already cancelled,
or when cancellable is cancelled in some thread.

data_destroy_func will be called when the handler is
disconnected, or immediately if the cancellable is already
cancelled.

See "cancelled" for details on how to use this.

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

cancellable :

A GCancellable.

callback :

The GCallback to connect.

data :

Data to pass to callback.

data_destroy_func :

Free function for data or NULL. [<acronym title="NULL is ok, both for passing and for returning.">allow-none</acronym>]

Returns :

The id of the signal handler or 0 if cancellable has already
been cancelled.

Since 2.22

g_cancellable_disconnect ()

void                g_cancellable_disconnect            (GCancellable *cancellable,
                                                         gulong handler_id);

Disconnects a handler from a cancellable instance similar to
g_signal_handler_disconnect().  Additionally, in the event that a
signal handler is currently running, this call will block until the
handler has finished.  Calling this function from a
"cancelled" signal handler will therefore result in a
deadlock.

This avoids a race condition where a thread cancels at the
same time as the cancellable operation is finished and the
signal handler is removed. See "cancelled" for
details on how to use this.

If cancellable is NULL or handler_id is 0 this function does
nothing.

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

cancellable :

A GCancellable or NULL. [<acronym title="NULL is ok, both for passing and for returning.">allow-none</acronym>]

handler_id :

Handler id of the handler to be disconnected, or 0.

Since 2.22

g_cancellable_cancel ()

void                g_cancellable_cancel                (GCancellable *cancellable);

Will set cancellable to cancelled, and will emit the
"cancelled" signal. (However, see the warning about
race conditions in the documentation for that signal if you are
planning to connect to it.)

This function is thread-safe. In other words, you can safely call
it from a thread other than the one running the operation that was
passed the cancellable.

The convention within gio is that cancelling an asynchronous
operation causes it to complete asynchronously. That is, if you
cancel the operation from the same thread in which it is running,
then the operation's GAsyncReadyCallback will not be invoked until
the application returns to the main loop.

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

cancellable :

a GCancellable object.

Signal Details

The "cancelled" signal

void                user_function                      (GCancellable *cancellable,
                                                        gpointer      user_data)        : Run Last

Emitted when the operation has been cancelled.

Can be used by implementations of cancellable operations. If the
operation is cancelled from another thread, the signal will be
emitted in the thread that cancelled the operation, not the
thread that is running the operation.

Note that disconnecting from this signal (or any signal) in a
multi-threaded program is prone to race conditions. For instance
it is possible that a signal handler may be invoked even
after a call to
g_signal_handler_disconnect() for that handler has already
returned.

There is also a problem when cancellation happen
right before connecting to the signal. If this happens the
signal will unexpectedly not be emitted, and checking before
connecting to the signal leaves a race condition where this is
still happening.

In order to make it safe and easy to connect handlers there
are two helper functions: g_cancellable_connect() and
g_cancellable_disconnect() which protect against problems
like this.

An example of how to us this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

/* Make sure we don't do any unnecessary work if already cancelled */
if (g_cancellable_set_error_if_cancelled (cancellable))
  return;

/* Set up all the data needed to be able to
 * handle cancellation of the operation */
my_data = my_data_new (...);

id = 0;
if (cancellable)
  id = g_cancellable_connect (cancellable,
                  G_CALLBACK (cancelled_handler)
                  data, NULL);

/* cancellable operation here... */

g_cancellable_disconnect (cancellable, id);

/* cancelled_handler is never called after this, it
 * is now safe to free the data */
my_data_free (my_data);

Note that the cancelled signal is emitted in the thread that
the user cancelled from, which may be the main thread. So, the
cancellable signal should not do something that can block.

<colgroup>
<col align="left" valign="top">
<col>
</colgroup>

cancellable :

a GCancellable.

user_data :

user data set when the signal handler was connected.

          Generated by GTK-Doc V1.19


text:
/* @flow */

import Type from '../types/Type';
import ObjectType from '../types/ObjectType';
import getErrorMessage from '../getErrorMessage';
import compareTypes from '../compareTypes';

import invariant from '../invariant';
import type Validation, {ErrorTuple, IdentifierPath} from '../Validation';

// An object of type $Shape<T> does not have to have all of the properties
// that type T defines. But the types of the properties that it does have
// must accepts the types of the same properties in T.

export default class $ShapeType<T> extends Type<$Shape<T>> {
  typeName: string = '$ShapeType';

  type: Type<T>;

  *errors (validation: Validation<any>, path: IdentifierPath, input: any): Generator<ErrorTuple, void, void> {
    let {type} = this;

    if (input === null || (typeof input !== 'object' && typeof input !== 'function')) {
      yield [path, getErrorMessage('ERR_EXPECT_OBJECT'), this];
      return;
    }

    type = type.unwrap();
    invariant(typeof type.getProperty === 'function', 'Can only $Shape<T> object types.');

    for (const key in input) { // eslint-disable-line guard-for-in
      const property = type.getProperty(key);
      if (!property) {
        continue;
      }
      yield* property.errors(validation, path, input);
    }
  }

  accepts (input: any): boolean {
    let {type} = this;
    if (input === null || (typeof input !== 'object' && typeof input !== 'function')) {
      return false;
    }
    type = type.unwrap();
    invariant(typeof type.getProperty === 'function', 'Can only $Shape<T> object types.');
    for (const key in input) { // eslint-disable-line guard-for-in
      const property = type.getProperty(key);
      if (!property || !property.accepts(input)) {
        return false;
      }
    }
    return true;
  }

  compareWith (input: Type<any>): -1 | 0 | 1 {
    return compareTypes(this.unwrap(), input);
  }

  unwrap (): Type<$Shape<T>> {
    let {type} = this;
    type = type.unwrap();
    const context = this.context;
    invariant(type instanceof ObjectType, 'Can only $Shape<T> object types.');
    const properties = type.properties;
    const args = new Array(properties.length);
    for (let i = 0; i < properties.length; i++) {
      const property = properties[i];
      args[i] = context.property(property.key, property.value, true);
    }
    return this.context.object(...args);
  }

  toString (): string {
    return `$Shape<${this.type.toString()}>`;
  }

  toJSON () {
    return {
      typeName: this.typeName,
      type: this.type
    };
  }
}


text:
  PyProbML VAE zoo 🐘

Author:
  Ming Liang Ang. Summer 2021.

Compare results of different VAEs :

VAE tricks and what the different VAE try to address :

A collection of Variational AutoEncoders (VAEs) implemented in **pytorch** with focus on reproducibility and creating reusable blocks that can be used in any project. The aim of this project is to provide
a quick and simple working example for many of the cool VAE idea in the textbook. All the models are trained on the [CelebA dataset](http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html)
for consistency and comparison.

## Requirements
- Python >= 3.7
- PyTorch >= 1.8
- Pytorch Lightning  == 1.4.0
- CUDA enabled computing device

## To download this subdirectory only

Copy the url of the subdirectory and past it to this [webstie](https://download-directory.github.io) and then download this subdirectory as a zipfile

## Instruction For Training The Model

### Download celeba data

**Important :** Make sure to get your kaggle.json from [these instructions](https://github.com/Kaggle/kaggle-api#api-credentials) then run
```
mkdir /root/.kaggle
cp kaggle.json /root/.kaggle/kaggle.json
chmod 600 /root/.kaggle/kaggle.json
rm kaggle.json
```

to copy kaggle.json into a folder first. Then to download the data first donwload the following [script](https://github.com/probml/pyprobml/blob/master/scripts/download_celeba.py)
```
wget -q https://raw.githubusercontent.com/probml/pyprobml/master/scripts/download_celeba.py
```
and run the following script
```
python download_celeba.py
```

### To Train Model

```
python run.py -config ./configs/vanilla_vae.yaml
```

## Results

| Model                                                                  | Paper                                            |Reconstruction | Samples |
|------------------------------------------------------------------------|--------------------------------------------------|---------------|---------|
| Original Images (for reconstruction)                                   |**N/A**                                           |    ![][1]     | **N/A** |
| AE ([Code][ae_code], [Config][ae_config])                              |**N/A**                                           |    ![][18]     | ![][19] |
| VAE ([Code][vae_code], [Config][vae_config])                           |[Link](https://arxiv.org/abs/1312.6114)           |    ![][2]     | ![][10] |
| beta-VAE ([Code][beta_vae_code], [Config][beta_vae_config])            |[Link](https://openreview.net/pdf?id=Sy2fzU9gl)    |    ![][20]     | ![][21] |
| Hinge VAE ([Code][hingevae_code], [Config][hingevae_config])           |[Link](https://arxiv.org/abs/1606.04934)          |    ![][3]     | ![][11] |
| MMD VAE ([Code][mmdvae_code], [Config][mmdvae_config])                 |[Link](https://arxiv.org/abs/1706.02262)          |    ![][4]     | ![][12] |
| Info VAE   ([Code][infovae_code], [Config][infovae_config])            |[Link](https://arxiv.org/abs/1706.02262)          |    ![][5]     | ![][13] |
| LogCosh VAE   ([Code][logcoshvae_code], [Config][logcoshvae_config])   |[Link](https://openreview.net/forum?id=rkglvsC9Ym)|    ![][6]     | ![][14] |
| Two-stage VAE   ([Code][twostage_code], [Config][twostage_config])     |[Link](https://arxiv.org/abs/1903.05789)          |    ![][7]     | ![][15] |
| Sigma VAE   ([Code][sigma_code], [Config][sigma_config])               |[Link](https://arxiv.org/abs/2006.13202)          |    ![][8]     | ![][16] |
| VQ-VAE (*K = 512, D = 64*) ([Code][vqvae_code], [Config][vqvae_config]) + PixelCNN([Code][pixelCNN_code]) |[Link](https://arxiv.org/abs/1711.00937)          |    ![][9]     | ![][17] |

## Acknowledgement

The idea of this zoo and some of the scripts were based on Anand Krishnamoorthy [Pytorch-VAE library](https://github.com/AntixK/PyTorch-VAE), we also used the script from [sayantanauddy](https://github.com/sayantanauddy/vae_lightning) to transform and download the celeba from kaggle.

-----------

[ae_code]: https://github.com/probml/pyprobml/blob/master/vae/models/vanilla_ae.py
[vae_code]: https://github.com/probml/pyprobml/blob/master/vae/models/vanilla_vae.py
[mmdvae_code]: https://github.com/probml/pyprobml/blob/master/vae/models/mmd_vae.py
[hingevae_code]: https://github.com/probml/pyprobml/blob/master/vae/models/hinge_vae.py
[logcoshvae_code]: https://github.com/probml/pyprobml/blob/master/vae/models/logcosh_vae.py
[infovae_code]: https://github.com/probml/pyprobml/blob/master/vae/models/info_vae.py
[vqvae_code]: https://github.com/probml/pyprobml/blob/master/vae/models/vq_vae.py
[twostage_code]: https://github.com/probml/pyprobml/blob/master/vae/models/two_stage_vae.py
[sigma_code]: https://github.com/probml/pyprobml/blob/master/vae/models/sigma_vae.py
[pixelCNN_code]: https://github.com/probml/pyprobml/blob/master/vae/models/sigma_vae.py
[beta_vae_code]: https://github.com/probml/pyprobml/blob/master/vae/models/beta_vae.py

[ae_config]: https://github.com/probml/pyprobml/blob/master/vae/configs/vanilla_ae.yaml
[vae_config]: https://github.com/probml/pyprobml/blob/master/vae/configs/vanilla_vae.yaml
[logcoshvae_config]: https://github.com/probml/pyprobml/blob/master/vae/configs/logcosh_vae.yaml
[infovae_config]: https://github.com/probml/pyprobml/blob/master/vae/configs/info_vae.yaml
[vqvae_config]: https://github.com/probml/pyprobml/blob/master/vae/configs/vq_vae.yaml
[mmdvae_config]: https://github.com/probml/pyprobml/blob/master/vae/configs/mmd_vae.yaml
[hingevae_config]: https://github.com/probml/pyprobml/blob/master/vae/configs/hinge_vae.yaml
[twostage_config]: https://github.com/probml/pyprobml/blob/master/vae/configs/two_stage_vae.yaml
[sigma_config]: https://github.com/probml/pyprobml/blob/master/vae/configs/sigma_vae.yaml
[beta_vae_config]: https://github.com/probml/pyprobml/blob/master/vae/configs/beta_vae.yaml

[1]: https://github.com/probml/pyprobml/blob/master/vae/assets/original.png
[2]: https://github.com/probml/pyprobml/blob/master/vae/assets/vanilla_vae_recon.png
[3]: https://github.com/probml/pyprobml/blob/master/vae/assets/hinge_vae_recon.png
[4]: https://github.com/probml/pyprobml/blob/master/vae/assets/mmd_vae_recon.png
[5]: https://github.com/probml/pyprobml/blob/master/vae/assets/info_vae_recon.png
[6]: https://github.com/probml/pyprobml/blob/master/vae/assets/logcosh_vae_recon.png
[7]: https://github.com/probml/pyprobml/blob/master/vae/assets/two_stage_vae_recon.png
[8]: https://github.com/probml/pyprobml/blob/master/vae/assets/sigma_vae_recon.png
[9]: https://github.com/probml/pyprobml/blob/master/vae/assets/vq_vae_recon.png
[10]: https://github.com/probml/pyprobml/blob/master/vae/assets/vanilla_vae_samples.png
[11]: https://github.com/probml/pyprobml/blob/master/vae/assets/hinge_vae_samples.png
[12]: https://github.com/probml/pyprobml/blob/master/vae/assets/mmd_vae_samples.png
[13]: https://github.com/probml/pyprobml/blob/master/vae/assets/info_vae_samples.png
[14]: https://github.com/probml/pyprobml/blob/master/vae/assets/logcosh_vae_samples.png
[15]: https://github.com/probml/pyprobml/blob/master/vae/assets/two_stage_vae_samples.png
[16]: https://github.com/probml/pyprobml/blob/master/vae/assets/sigma_vae_samples.png
[17]: https://github.com/probml/pyprobml/blob/master/vae/assets/vq_vae_samples.png
[18]: https://github.com/probml/pyprobml/blob/master/vae/assets/vanilla_ae_recon.png
[19]: https://github.com/probml/pyprobml/blob/master/vae/assets/vanilla_ae_samples.png
[20]: https://github.com/probml/pyprobml/blob/master/vae/assets/beta_vae_recon.png
[21]: https://github.com/probml/pyprobml/blob/master/vae/assets/beta_vae_samples.png


text:
    {%- if question_number -%}
      {{question_number}}{{current_location.group_instance|format_number_to_alphabetic_letter}}.
    {% endif %}
    {{question_title}}

  {{question_description}}
  {{question_definition}}
  <fieldset>
    <legend class="field__legend u-vh">
      {{question_title}}
    </legend>

        {% set answer = question.answers[0] %}
        {% for field in form.fields[answer['id']] %}
          {% include 'partials/widgets/relationship_widget.html' %}
        {%- endfor -%}

  </fieldset>

    {% set schema_item = question.answers[-1] %}
    {% if schema_item.guidance %}
      {% with answer_guidance = {
        'id': schema_item.id,
        'label': schema_item.label,
        'schema_item': {
            'content': schema_item.guidance.content,
            'show_guidance': schema_item.guidance.show_guidance,
            'hide_guidance': schema_item.guidance.hide_guidance
        }
      } %}
        {% include 'partials/answer-guidance.html' %}
      {% endwith %}
    {% endif %}


text:
package org.livingdoc.converters.number;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import utils.EnglishDefaultLocale;

@EnglishDefaultLocale
class LongConverterJavaTest {

    LongConverter cut = new LongConverter();

    @Test
    void converterCanConvertedToJavaLong() {
        assertThat(cut.canConvertTo(Long.class)).isTrue();
    }

    @Test
    void javaInteroperabilityIsWorking() {
        Long value = cut.convert("42", null, null);
        assertThat(value).isEqualTo(42L);
    }

}


text:
```bash
terraform destroy -force
```


text:
#ifndef _SYS_PTRACE_H
#define _SYS_PTRACE_H	1

#include <features.h>
#include <sgidefs.h>

__BEGIN_DECLS

/* Type of the REQUEST argument to `ptrace.'  */
enum __ptrace_request
{
  /* Indicate that the process making this request should be traced.
     All signals received by this process can be intercepted by its
     parent, and its parent can use the other `ptrace' requests.  */
  PTRACE_TRACEME = 0,
#define PT_TRACE_ME PTRACE_TRACEME

  /* Return the word in the process's text space at address ADDR.  */
  PTRACE_PEEKTEXT = 1,
#define PT_READ_I PTRACE_PEEKTEXT

  /* Return the word in the process's data space at address ADDR.  */
  PTRACE_PEEKDATA = 2,
#define PT_READ_D PTRACE_PEEKDATA

  /* Return the word in the process's user area at offset ADDR.  */
  PTRACE_PEEKUSER = 3,
#define PT_READ_U PTRACE_PEEKUSER

  /* Write the word DATA into the process's text space at address ADDR.  */
  PTRACE_POKETEXT = 4,
#define PT_WRITE_I PTRACE_POKETEXT

  /* Write the word DATA into the process's data space at address ADDR.  */
  PTRACE_POKEDATA = 5,
#define PT_WRITE_D PTRACE_POKEDATA

  /* Write the word DATA into the process's user area at offset ADDR.  */
  PTRACE_POKEUSER = 6,
#define PT_WRITE_U PTRACE_POKEUSER

  /* Continue the process.  */
  PTRACE_CONT = 7,
#define PT_CONTINUE PTRACE_CONT

  /* Kill the process.  */
  PTRACE_KILL = 8,
#define PT_KILL PTRACE_KILL

  /* Single step the process.
     This is not supported on all machines.  */
  PTRACE_SINGLESTEP = 9,
#define PT_STEP PTRACE_SINGLESTEP

  /* Get all general purpose registers used by a processes.
     This is not supported on all machines.  */
   PTRACE_GETREGS = 12,
#define PT_GETREGS PTRACE_GETREGS

  /* Set all general purpose registers used by a processes.
     This is not supported on all machines.  */
   PTRACE_SETREGS = 13,
#define PT_SETREGS PTRACE_SETREGS

  /* Get all floating point registers used by a processes.
     This is not supported on all machines.  */
   PTRACE_GETFPREGS = 14,
#define PT_GETFPREGS PTRACE_GETFPREGS

  /* Set all floating point registers used by a processes.
     This is not supported on all machines.  */
   PTRACE_SETFPREGS = 15,
#define PT_SETFPREGS PTRACE_SETFPREGS

  /* Attach to a process that is already running. */
  PTRACE_ATTACH = 16,
#define PT_ATTACH PTRACE_ATTACH

  /* Detach from a process attached to with PTRACE_ATTACH.  */
  PTRACE_DETACH = 17,
#define PT_DETACH PTRACE_DETACH

  /* Get all extended floating point registers used by a processes.
     This is not supported on all machines.  */
   PTRACE_GETFPXREGS = 18,
#define PT_GETFPXREGS PTRACE_GETFPXREGS

  /* Set all extended floating point registers used by a processes.
     This is not supported on all machines.  */
   PTRACE_SETFPXREGS = 19,
#define PT_SETFPXREGS PTRACE_SETFPXREGS

  /* Continue and stop at the next (return from) syscall.  */
  PTRACE_SYSCALL = 24
#define PT_SYSCALL PTRACE_SYSCALL
};

/* Perform process tracing functions.  REQUEST is one of the values
   above, and determines the action to be taken.
   For all requests except PTRACE_TRACEME, PID specifies the process to be
   traced.

   PID and the other arguments described above for the various requests should
   appear (those that are used for the particular request) as:
     pid_t PID, void *ADDR, int DATA, void *ADDR2
   after REQUEST.  */
#if _MIPS_SIM == _ABIN32
__extension__ extern long long int ptrace
  (enum __ptrace_request __request, ...) __THROW;
#else
extern long int ptrace (enum __ptrace_request __request, ...) __THROW;
#endif

__END_DECLS

#endif /* _SYS_PTRACE_H */


text:
typedef struct {
	void **data;
	int cnt;
	int size;
} xc_stack_t;

#define S xc_stack_t*
void xc_stack_init_ex(S stack, int initsize);
#define xc_stack_init(stack) xc_stack_init_ex(stack, 8)
void xc_stack_destroy(S stack);
void xc_stack_push(S stack, void *item);
void *xc_stack_pop(S stack);
void *xc_stack_top(S stack);
void *xc_stack_get(S stack, int n);
int xc_stack_count(S stack);
void xc_stack_reverse(S stack);
#undef S


text:
<?xml version="1.0" encoding="UTF-8"?>

<rootTag>
  <Award>
    <AwardTitle>Workshop: International Conference on Software Engineering 2009: Student Travel Support</AwardTitle>
    <AwardEffectiveDate>09/01/2008</AwardEffectiveDate>
    <AwardExpirationDate>08/31/2009</AwardExpirationDate>
    <AwardAmount>10000</AwardAmount>
    <AwardInstrument>
      <Value>Standard Grant</Value>
    </AwardInstrument>
    <Organization>
      05010000
      <Directorate>
        <LongName>Direct For Computer & Info Scie & Enginr</LongName>
      </Directorate>
      <Division>
        <LongName>Division of Computing and Communication Foundations</LongName>
      </Division>
    </Organization>
    <ProgramOfficer>
      <SignBlockName>Sol J. Greenspan</SignBlockName>
    </ProgramOfficer>
    <AbstractNarration>This award supports travel for students participating in the Doctoral Symposium at the International Conference on Software Engineering in Vancouver, Canada, in May 2009. The Symposium provides a forum for Ph.D. students to discuss their research goals before a panel of experts in order to receive guidance on their research and research careers.</AbstractNarration>
    <MinAmdLetterDate>08/15/2008</MinAmdLetterDate>
    <MaxAmdLetterDate>08/15/2008</MaxAmdLetterDate>
    <ARRAAmount/>
    <AwardID>0840392</AwardID>
    <Investigator>
      <FirstName>Stephen</FirstName>
      <LastName>Fickas</LastName>
      <EmailAddress>fickas@cs.uoregon.edu</EmailAddress>
      <StartDate>08/15/2008</StartDate>
      <EndDate/>
      <RoleCode>Principal Investigator</RoleCode>
    </Investigator>
    <Institution>
      <Name>University of Oregon Eugene</Name>
      <CityName>Eugene</CityName>
      <ZipCode>974035219</ZipCode>
      <PhoneNumber>5413465131</PhoneNumber>
      <StreetAddress>5219 UNIVERSITY OF OREGON</StreetAddress>
      <CountryName>United States</CountryName>
      <StateName>Oregon</StateName>
      <StateCode>OR</StateCode>
    </Institution>
    <FoaInformation>
      0000912
      <Name>Computer Science</Name>
    </FoaInformation>
    <ProgramElement>
      7352
      <Text>COMPUTING PROCESSES & ARTIFACT</Text>
    </ProgramElement>
    <ProgramReference>
      9218
      <Text>BASIC RESEARCH & HUMAN RESORCS</Text>
    </ProgramReference>
    <ProgramReference>
      HPCC
      <Text>HIGH PERFORMANCE COMPUTING & COMM</Text>
    </ProgramReference>
  </Award>
</rootTag>


text:
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/

#include <gtest/gtest.h>

#include <gmock/gmock.h>

#include <stout/bytes.hpp>
#include <stout/gtest.hpp>
#include <stout/stringify.hpp>
#include <stout/try.hpp>

TEST(BytesTest, Parse)
{
  EXPECT_SOME_EQ(Terabytes(1), Bytes::parse("1TB"));
  EXPECT_SOME_EQ(Gigabytes(1), Bytes::parse("1GB"));
  EXPECT_SOME_EQ(Megabytes(1), Bytes::parse("1MB"));
  EXPECT_SOME_EQ(Kilobytes(1), Bytes::parse("1KB"));
  EXPECT_SOME_EQ(Bytes(1), Bytes::parse("1B"));

  // Cannot have fractional bytes.
  EXPECT_ERROR(Bytes::parse("1.5B"));

  // Parsing fractions is unsupported.
  EXPECT_ERROR(Bytes::parse("1.5GB"));

  // Unknown unit.
  EXPECT_ERROR(Bytes::parse("1PB"));
}

TEST(BytesTest, Arithmetic)
{
  EXPECT_EQ(Terabytes(1), Gigabytes(512) + Gigabytes(512));
  EXPECT_EQ(Terabytes(1), Terabytes(2) - Terabytes(1));

  EXPECT_EQ(Terabytes(1), Gigabytes(1) * 1024);

  EXPECT_EQ(Gigabytes(1), Terabytes(1) / 1024);
}

TEST(BytesTest, Comparison)
{
  EXPECT_GT(Terabytes(1), Gigabytes(1));
  EXPECT_GT(Gigabytes(1), Megabytes(1));
  EXPECT_GT(Megabytes(1), Kilobytes(1));
  EXPECT_GT(Kilobytes(1), Bytes(1));

  EXPECT_EQ(Bytes(1024), Kilobytes(1));
  EXPECT_LT(Bytes(1023), Kilobytes(1));
  EXPECT_GT(Bytes(1025), Kilobytes(1));
}

TEST(BytesTest, Stringify)
{
  EXPECT_NE(Megabytes(1023), Gigabytes(1));

  EXPECT_EQ("0B", stringify(Bytes()));

  EXPECT_EQ("1KB", stringify(Kilobytes(1)));
  EXPECT_EQ("1MB", stringify(Megabytes(1)));
  EXPECT_EQ("1GB", stringify(Gigabytes(1)));
  EXPECT_EQ("1TB", stringify(Terabytes(1)));

  EXPECT_EQ("1023B", stringify(Bytes(1023)));
  EXPECT_EQ("1023KB", stringify(Kilobytes(1023)));
  EXPECT_EQ("1023MB", stringify(Megabytes(1023)));
  EXPECT_EQ("1023GB", stringify(Gigabytes(1023)));
}


text:
/*import expect from 'expect';
import posts from '../../client/reducers/posts';
import defaultPosts from '../../client/data/posts';

describe('Posts Reducer', function () {

  it('should add a like to an existing post', function () {
    const index = 2;
    const action = { type: 'INCREMENT_LIKES', index };
    const actual = posts(defaultPosts, action);
    expect(actual[index].likes).toEqual(defaultPosts[index].likes + 1);
  });

})*/


text:
module IntegrationTestHelper
  include Capybara::DSL

  def mail_should_be_sent(count = nil)
    size = ActionMailer::Base.deliveries.size
    if count
      size.should == count
    else
      size.should > 0
    end
  end

  def page_should_have_title(title)
    within('title') do
      page.should have_content(title)
    end
  end

  def page_should_not_have_title(title)
    within('title') do
      page.should_not have_content(title)
    end
  end

  def page_should_have_subtitle(subtitle)
    within('.common_container h2') do
      page.should have_content(subtitle)
    end
  end

  def page_should_not_have_subtitle(subtitle)
    within('.common_container h2') do
      page.should_not have_content(subtitle)
    end
  end

  def setup_controller_request(env = {})
    ActiveSupport::Deprecation.warn("remove the code")
  end

  %w(user).each do |resource_name|
    resource_table_name = resource_name.tableize
    class_eval <<-EOS
      def #{resource_name}_sign_in_with_post(#{resource_name}, password, remember_me = false)
        params = {
          '#{resource_name}[email]' => #{resource_name}.email,
          '#{resource_name}[password]' => password
        }
        if remember_me
          params['#{resource_name}[remember_me]'] = '1'
        else
          params['#{resource_name}[remember_me]'] = '0'
        end
        post '/#{resource_table_name}/sign_in', params
      end

      def #{resource_name}_sign_in_with_visit(user, password, remember_me = false)
        visit new_#{resource_name}_session_path
        within('form#user_sign_in') do
          fill_in '#{resource_name}[email]', with: user.email
          fill_in '#{resource_name}[password]', with: password
          check '#{resource_name}[remember_me]' if remember_me
          find('input.btn.btn-primary').click
        end
      end
    EOS
  end
end


text:
#ifndef METAENUM_P_H
#define METAENUM_P_H

#include "metaitem_p.h"

#include <rtti/metaenum.h>

namespace rtti {

class RTTI_PRIVATE MetaEnumPrivate: public MetaItemPrivate
{
public:
    MetaEnumPrivate(std::string_view name, MetaContainer const &owner, MetaType_ID typeId)
        : MetaItemPrivate{name, owner}
        , m_typeId{typeId}
    {}

private:
    MetaType_ID m_typeId;
    internal::NamedVariantList m_elements;

    friend class rtti::MetaEnum;
};

} // namespace rtti

#endif // METAENUM_P_H


text:
require 'rubygems'
require 'minitest/autorun'
require 'minitest/spec'

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'test-explorer'
include TestExplorer


text:
//
//  GTLBigqueryJobStatistics.m
//

// ----------------------------------------------------------------------------
// NOTE: This file is generated from Google APIs Discovery Service.
// Service:
//   BigQuery API (bigquery/v2)
// Description:
//   A data platform for customers to create, manage, share and query data.
// Documentation:
//   https://developers.google.com/bigquery/docs/overview
// Classes:
//   GTLBigqueryJobStatistics (0 custom class methods, 7 custom properties)

#import "GTLBigqueryJobStatistics.h"

#import "GTLBigqueryJobStatistics2.h"
#import "GTLBigqueryJobStatistics3.h"
#import "GTLBigqueryJobStatistics4.h"

// ----------------------------------------------------------------------------
//
//   GTLBigqueryJobStatistics
//

@implementation GTLBigqueryJobStatistics
@dynamic creationTime, endTime, extract, load, query, startTime,
         totalBytesProcessed;
@end


text:
package sharedaction

// Actor handles all shared actions
type Actor struct{}

// NewActor returns an Actor with default settings
func NewActor() Actor {
	return Actor{}
}